View Javadoc

1   /*
2    * Copyright (c) 1998-2004 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.experiment.gui.views;
20  
21  import java.awt.Color;
22  import java.awt.GridBagConstraints;
23  import java.awt.GridBagLayout;
24  import java.awt.Insets;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  import java.io.File;
28  
29  import javax.swing.BorderFactory;
30  import javax.swing.Box;
31  import javax.swing.BoxLayout;
32  import javax.swing.DefaultListModel;
33  import javax.swing.JButton;
34  import javax.swing.JList;
35  import javax.swing.JOptionPane;
36  import javax.swing.JPanel;
37  import javax.swing.JScrollPane;
38  import javax.swing.JTabbedPane;
39  import javax.swing.ListSelectionModel;
40  import javax.swing.ScrollPaneConstants;
41  import javax.swing.border.EtchedBorder;
42  import javax.swing.event.ListSelectionEvent;
43  import javax.swing.event.ListSelectionListener;
44  
45  import jgroup.core.ConfigurationException;
46  import jgroup.experiment.ShellCommand;
47  import jgroup.experiment.gui.GUIview;
48  import jgroup.experiment.gui.views.helpers.ExperimentTabManager;
49  import jgroup.experiment.gui.views.helpers.FileList;
50  import jgroup.relacs.config.ExperimentConfig;
51  
52  
53  /**
54   * @author Bjarte Svaeren
55   */
56  public class ExperimentView
57    implements GUIview
58  {
59    ////////////////////////////////////////////////////////////////////////////////////////////
60    // Fields for the default settings
61    ////////////////////////////////////////////////////////////////////////////////////////////
62    
63    // The default directory for the experiment config-files
64    public static final String DEFAULT_EXPERIMENT_CONFIG_DIR
65        = new String("config" + File.separator + "experiment");
66  
67  
68    ////////////////////////////////////////////////////////////////////////////////////////////
69    // GUI fields
70    ////////////////////////////////////////////////////////////////////////////////////////////
71  
72    // Button representing the view
73    JButton viewButton;
74    
75    // The main panel - contains the whole view
76    JPanel mainPanel;
77    
78    // The components of the main panel
79    JPanel               centerPanel;
80    JList                expList;
81    DefaultListModel     listModel;
82    JScrollPane          expListPane;
83    JTabbedPane          expTabs;
84    ExperimentTabManager tabManager;
85  
86    Box     buttonBox;
87    JButton newButton;
88    JButton saveButton;
89    JButton runButton;
90    JButton summaryButton;
91        
92  
93    ////////////////////////////////////////////////////////////////////////////////////////////
94    // Data fields
95    ////////////////////////////////////////////////////////////////////////////////////////////
96  
97    // Holds the list of files in the experiment config-dir:
98    FileList fileList;
99    
100   // The directory containing the experiments:
101   File experimentDir;
102 
103 
104   ////////////////////////////////////////////////////////////////////////////////////////////
105   // Constructor
106   ////////////////////////////////////////////////////////////////////////////////////////////
107 
108   public ExperimentView()
109   {
110     viewButton = new JButton("Experiments");
111     tabManager = new ExperimentTabManager(experimentDir);
112     
113     String s = File.separator;
114     String dir = System.getProperty("jgroup.experiment.dir");
115     if (dir == null || dir.length() == 0) {
116       dir = DEFAULT_EXPERIMENT_CONFIG_DIR;
117       System.setProperty("jgroup.log.dir", dir);
118     }
119     
120     experimentDir = new File(dir);
121   }
122   
123   
124   ////////////////////////////////////////////////////////////////////////////////////////////
125   // Methods from interface GUIview
126   ////////////////////////////////////////////////////////////////////////////////////////////
127 
128   /* (non-Javadoc)
129    * @see jgroup.experiment.gui.GUIview#makeMainPanel()
130    */
131   public JPanel makeMainPanel()
132   {
133     makeCenterPanel();
134     makeButtons();
135     
136     mainPanel = new JPanel(new GridBagLayout());
137     GridBagConstraints gbc = new GridBagConstraints();
138     
139     gbc.fill       = GridBagConstraints.BOTH;
140     gbc.anchor     = GridBagConstraints.FIRST_LINE_START;
141     gbc.gridx      = 0;
142     gbc.gridy      = 0;
143     gbc.gridwidth  = GridBagConstraints.REMAINDER;
144     gbc.gridheight = GridBagConstraints.RELATIVE;
145     gbc.weightx    = 0.1;
146     gbc.weighty    = 0.1; 
147     mainPanel.add(centerPanel, gbc);
148     
149     gbc.fill   = GridBagConstraints.VERTICAL;
150     gbc.anchor = GridBagConstraints.LAST_LINE_END;
151     gbc.gridx  = 0;
152     gbc.gridy  = GridBagConstraints.RELATIVE;
153     gbc.gridwidth = GridBagConstraints.REMAINDER;
154     gbc.gridheight = GridBagConstraints.REMAINDER;
155     gbc.weighty    = 0;
156     mainPanel.add(buttonBox, gbc);
157     
158     mainPanel.setBorder(BorderFactory.createCompoundBorder(
159                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
160                         BorderFactory.createCompoundBorder(
161                         BorderFactory.createTitledBorder("Experiment View:"),
162                         BorderFactory.createEmptyBorder(5, 5, 5, 5))));
163    
164     return mainPanel;
165   }
166 
167 
168   /* (non-Javadoc)
169    * @see jgroup.experiment.gui.GUIview#getView()
170    */
171   public JButton getView()
172   {
173     return viewButton;
174   }
175 
176 
177   ////////////////////////////////////////////////////////////////////////////////////////////
178   // Private methods
179   ////////////////////////////////////////////////////////////////////////////////////////////
180 
181   private void makeCenterPanel()
182   {
183     makeList();
184     makeTabs();
185     
186     centerPanel = new JPanel(new GridBagLayout());
187     GridBagConstraints gbc = new GridBagConstraints();
188     
189     gbc.anchor     = GridBagConstraints.LINE_START;
190     gbc.fill       = GridBagConstraints.BOTH;
191     gbc.gridheight = GridBagConstraints.REMAINDER;
192     gbc.gridwidth  = 1;
193     gbc.weightx    = 0.1;
194     gbc.weighty    = 0.1;
195     gbc.gridx      = 0;
196     gbc.gridy      = 0;
197     gbc.insets     = new Insets(2,0,0,5);
198     centerPanel.add(expListPane, gbc);
199     
200     gbc.anchor     = GridBagConstraints.CENTER;
201     gbc.gridheight = GridBagConstraints.REMAINDER;
202     gbc.gridwidth  = GridBagConstraints.REMAINDER;
203     gbc.gridx      = GridBagConstraints.RELATIVE;
204     gbc.gridy      = 0;
205     gbc.weightx    = 0.3;   
206     gbc.insets     = new Insets(0,5,0,0);
207     centerPanel.add(expTabs, gbc);
208   }
209 
210 
211   private void makeButtons()
212   {
213     newButton     = new JButton("NEW");
214     saveButton    = new JButton("SAVE");
215     runButton     = new JButton("RUN");
216     summaryButton = new JButton("SUMMARY");
217         
218     buttonBox = new Box(BoxLayout.X_AXIS);
219     buttonBox.add(Box.createHorizontalGlue());
220     buttonBox.add(newButton);
221     buttonBox.add(Box.createHorizontalStrut(10));
222     buttonBox.add(saveButton);
223     buttonBox.add(Box.createHorizontalStrut(10));
224     buttonBox.add(runButton);
225     buttonBox.add(Box.createHorizontalStrut(10));
226     buttonBox.add(summaryButton);
227     buttonBox.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
228     
229     addButtonListeners();
230   }
231   
232   
233   private void makeList()
234   {
235     // Get a list of files in the experiment dir
236     fileList  = new FileList(experimentDir);
237     listModel = new DefaultListModel();
238 
239     // Add the list to the list model   
240     addList();
241     
242     // Use the list model to create the JList    
243     expList = new JList(listModel);
244     
245     // Set the attributes of the JList
246     expList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
247     expListPane = new JScrollPane(expList);
248     expListPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
249     expListPane.setBorder(BorderFactory.createCompoundBorder(
250                           BorderFactory.createLineBorder(Color.BLACK, 2),
251                           BorderFactory.createEtchedBorder()));
252 
253     // Add a listener to the JList
254     expList.addListSelectionListener(new ListSelectionListener() {
255       public void valueChanged(ListSelectionEvent e) {
256         if(e.getValueIsAdjusting() == false) {
257           JList list = (JList) e.getSource();
258           
259           // If no element is selected, there's no need to update the tabs. 
260           // Most likely the 'NEW' button was pressed.
261           if(list.isSelectionEmpty())
262             return;
263             
264           String fileName = experimentDir + File.separator 
265                             + (String) expList.getSelectedValue();
266                             
267 //          // Notify the user that the file is loading
268 //          JFrame    topFrame = (JFrame) mainPanel.getTopLevelAncestor();
269 //          JWindow   window   = new JWindow(topFrame);
270 //          Container pane     = window.getContentPane();
271 //          JLabel    label    = new JLabel("Loading...");
272 //          label.setBorder(BorderFactory.createCompoundBorder(
273 //                          BorderFactory.createLineBorder(Color.BLACK, 4),
274 //                          BorderFactory.createEmptyBorder(40, 40, 40, 40)));  
275 //          pane.setLayout(new BorderLayout());
276 //          pane.add(label, BorderLayout.CENTER);
277 //          window.pack();
278 //          int xpos = (topFrame.getX() + topFrame.getX() + topFrame.getWidth())  / 2 - 40;
279 //          int ypos = (topFrame.getY() + topFrame.getY() + topFrame.getHeight()) / 2 - 40;
280 //          System.out.println("X:" + xpos + " Y:" + ypos);
281 //          System.out.println("TopX:" + topFrame.getX() + " TopY:" + topFrame.getY());
282 //          System.out.println("Width:" + topFrame.getWidth() + " Height:" + topFrame.getHeight()); 
283 //          window.setLocation(xpos, ypos);
284 //          window.setVisible(true);
285 //          topFrame.setEnabled(false);
286 
287           // Do the loading
288           try {
289             tabManager.updateExperimentTabs(fileName);
290           } catch (ConfigurationException e1) {
291             e1.printStackTrace();
292           }
293           
294 //          window.setVisible(false);
295 //          topFrame.setEnabled(true);
296         }
297       }
298     });    
299   }
300   
301   
302   private void updateList()
303   {
304     fileList = new FileList(experimentDir);
305     listModel.clear();
306     addList();
307   }
308   
309   
310   private void makeTabs()
311   {
312     expTabs = tabManager.makeTabs();
313   }
314   
315   
316   private void addButtonListeners()
317   {
318     newButton.addActionListener(new ActionListener() {
319       public void actionPerformed(ActionEvent e) {
320         tabManager.clear();
321         expList.clearSelection();
322         updateList();
323       }
324     });
325 
326     saveButton.addActionListener(new ActionListener() {
327       public void actionPerformed(ActionEvent e) {
328         tabManager.save();
329         updateList();
330       }
331     });
332     
333     runButton.addActionListener((new ActionListener() {
334       public void actionPerformed(ActionEvent e) {
335         tabManager.save();
336         updateList();
337         try {
338           ShellCommand.exec(new ExperimentConfig().ant("experiment"));
339         } catch (Exception e1) {
340           JOptionPane.showMessageDialog(mainPanel.getTopLevelAncestor(),
341                                         "Exception occured. Run aborted.",
342                                         "Run error",
343                                         JOptionPane.WARNING_MESSAGE);
344           e1.printStackTrace();          
345         }
346       }
347     }));
348   }
349   
350   
351   private void addList()
352   {
353     String[] fileListArray = fileList.getFileList();
354 
355     // Add the list to the list model
356     for (int i = 0; i < fileListArray.length; i++) {
357       listModel.addElement(fileListArray[i]);
358     }      
359   }
360 }
361 
362 
363