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.helpers;
20  
21  import java.awt.GridBagConstraints;
22  import java.awt.GridBagLayout;
23  import java.awt.Insets;
24  import java.io.File;
25  import java.io.IOException;
26  
27  import javax.swing.BorderFactory;
28  import javax.swing.JLabel;
29  import javax.swing.JOptionPane;
30  import javax.swing.JPanel;
31  import javax.swing.JTabbedPane;
32  import javax.swing.JTextField;
33  import javax.swing.SwingConstants;
34  
35  import jgroup.core.ConfigurationException;
36  import jgroup.relacs.config.ConfigParser;
37  import jgroup.relacs.config.ExperimentConfig;
38  
39  import org.w3c.dom.Document;
40  import org.w3c.dom.Element;
41  import org.w3c.dom.NodeList;
42  
43  /**
44   * Utility class creating the tabs in the experiment view.
45   * 
46   * @author Bjarte Svaeren
47   */
48  public class ExperimentTabManager
49  {
50    ////////////////////////////////////////////////////////////////////////////////////////////
51    // GUI fields
52    ////////////////////////////////////////////////////////////////////////////////////////////
53  
54    // The tabs to manage
55    private JTabbedPane tabs;
56    
57    // Tabs in the tabbed pane:
58    private JPanel generalPanel;
59    private JPanel serversPanel;
60    private JPanel clientsPanel;
61    private JPanel propertiesPanel;
62    private JPanel preExperimentPanel;
63    private JPanel experimentPanel;
64    private JPanel postExperimentPanel;
65    private JPanel repeatScriptPanel;
66     
67    // Components of the general panel:
68    private JLabel     nameLabel;
69    private JLabel     fileNameLabel;
70    private JTextField nameField;
71    private JTextField fileNameField;
72    
73    
74  
75    ////////////////////////////////////////////////////////////////////////////////////////////
76    // Data fields
77    ////////////////////////////////////////////////////////////////////////////////////////////
78  
79    // Object describing the current experiment
80    private ExperimentConfig expConfig;
81    
82    // Array containing the PanelManagers managing the various tabs
83    private PanelManager[] panelManagers;
84    
85    // File describing the path to the current experiment
86    private File experimentDir;
87  
88    
89    ////////////////////////////////////////////////////////////////////////////////////////////
90    // Constructor
91    ////////////////////////////////////////////////////////////////////////////////////////////
92  
93    public ExperimentTabManager(File dir)
94    {
95      experimentDir = dir;
96    }
97    
98    
99    ////////////////////////////////////////////////////////////////////////////////////////////
100   // Public methods
101   ////////////////////////////////////////////////////////////////////////////////////////////
102 
103   public JTabbedPane makeTabs()
104   {
105     generalPanel        = new JPanel(new GridBagLayout());    
106     serversPanel        = new JPanel(new GridBagLayout());
107     clientsPanel        = new JPanel(new GridBagLayout());
108     preExperimentPanel  = new JPanel(new GridBagLayout());
109     experimentPanel     = new JPanel(new GridBagLayout());
110     postExperimentPanel = new JPanel(new GridBagLayout());
111     propertiesPanel     = new JPanel(new GridBagLayout());
112     repeatScriptPanel   = new JPanel(new GridBagLayout());
113     
114     tabs = new JTabbedPane(SwingConstants.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
115     tabs.add("General", generalPanel);
116     tabs.add("Servers", serversPanel);
117     tabs.add("Clients", clientsPanel);
118     tabs.add("Properties", propertiesPanel);
119     tabs.add("PreExperiment", preExperimentPanel);
120     tabs.add("Experiment", experimentPanel);
121     tabs.add("PostExperiment", postExperimentPanel);
122     tabs.add("RepeatScript", repeatScriptPanel);
123 
124     // Initialize the tab managers for the panels
125     panelManagers = new PanelManager[] { 
126                     new HostPanelManager(serversPanel),
127                     new HostPanelManager(clientsPanel),
128                     new PropertiesPanelManager(propertiesPanel),
129                     new ScriptPanelManager(repeatScriptPanel), 
130                     new RunListPanelManager(preExperimentPanel),
131                     new RunListPanelManager(postExperimentPanel),
132                     new RunListPanelManager(experimentPanel)
133                    };
134     
135     // Make the panel of every tab
136     makeGeneralPanel();
137     for (int i = 0; i < panelManagers.length; i++) {
138       panelManagers[i].makePanel();
139     }        
140 
141     return tabs;
142   }
143     
144   
145   public void updateExperimentTabs(String fileName)
146    throws ConfigurationException
147   {
148     clear();
149 
150     File xmlFile = new File(fileName);
151 
152     if(!xmlFile.exists())
153       throw new ConfigurationException("Could not find " + xmlFile);
154       
155     try {
156       ConfigParser.parse(xmlFile.getCanonicalPath(), true);
157       expConfig = (ExperimentConfig) ConfigParser.getConfig(ExperimentConfig.class);
158     } catch (ConfigurationException e) {
159       e.printStackTrace();
160     } catch (IOException e) {
161       e.printStackTrace();
162     }
163     
164     updateGeneralPanel(fileName);
165     for (int i = 0; i < panelManagers.length; i++) {
166       panelManagers[i].updatePanel(expConfig);
167     }
168   }
169   
170   
171   public void clear()
172   {
173     if(ChangeTracker.hasComponentChanges()) {
174       int answer 
175         = JOptionPane.showConfirmDialog(tabs.getTopLevelAncestor(),
176                                         "You have unsaved changes!\n"
177                                       + "Would you like to save now?",
178                                         "Would you like to save?",
179                                         JOptionPane.YES_NO_OPTION,
180                                         JOptionPane.WARNING_MESSAGE);
181 
182       // YES: the file is save, NO: the file is not saved
183       if(answer == JOptionPane.YES_OPTION)
184         save();
185     }
186                                     
187     // Reset the ExperimentConfig object
188     expConfig = new ExperimentConfig();
189     
190     // Clear the general panel
191     nameField.setText("");
192     fileNameField.setText("");
193 
194     // Clear the other panels    
195     for (int i = 0; i < panelManagers.length; i++) {
196       panelManagers[i].clear();
197     }
198     
199     ChangeTracker.clearChanges();
200     tabs.revalidate();
201   }
202   
203   
204   /**
205    * Same as <code>save(false)</code>
206    */
207   public void save() { save(false); }
208   
209   
210   public void save(boolean overwrite)
211   {
212     String fileName = fileNameField.getText();
213 
214 //// TEST
215 //    int index = fileName.indexOf("/");
216 //    while(fileName.indexOf("/", index + 1) != -1) {
217 //      index = fileName.indexOf("/", index + 1);
218 //    }
219 //    fileName = fileName.substring(0, index + 1);
220 //    fileName = fileName + "test.xml";
221 //// TEST END    
222 
223     // If there's no filename, do nothing
224     if(fileName == null || fileName.length() == 0) {
225       JOptionPane.showMessageDialog(tabs.getTopLevelAncestor(),
226                                     "You must specify a filename.",
227                                     "Missing parameter",
228                                     JOptionPane.WARNING_MESSAGE);
229       return;
230     } 
231     
232     File experimentFile = new File(fileName);
233 
234     // Overwrite the file if it exists? (and overwrite is not set to true)
235     if(experimentFile.exists() && overwrite == false) {
236       int answer 
237         = JOptionPane.showConfirmDialog(tabs.getTopLevelAncestor(),
238                                         "The file '" + fileNameField.getText() 
239                                       + "'\n" + "already exists. Overwrite?",                                  
240                                         "Overwrite?",
241                                         JOptionPane.YES_NO_OPTION,
242                                         JOptionPane.WARNING_MESSAGE);
243       // If the user does not want to overwrite, do nothing
244       if(answer == JOptionPane.NO_OPTION)
245         return;
246     }
247 
248     // Create XMLcreator object with Experiment settings    
249     XMLcreator xmlcreator = new XMLcreator(experimentFile, 
250                                            "ExperimentConfiguration", "0.1",
251                                            "experiment.dtd", "us-ascii");
252     // Get the resulting document                                           
253     Document document = xmlcreator.getDocument();
254     // Add the Experiment tag
255     Element rootElement = document.getDocumentElement();
256     Element experimentElement = document.createElement("Experiment");
257     rootElement.appendChild(experimentElement);
258     
259     // Add info about the experiment to the document
260     for(int i = 0; i < panelManagers.length; i++) {
261       panelManagers[i].save(experimentElement, document);
262     }
263     
264     // Make sure the experiment has a name
265     NodeList nodeList  = document.getElementsByTagName("Exp");
266     Element expElement = (Element) nodeList.item(0);
267     expElement.setAttribute("name", nameField.getText());
268     
269     // Save the document to a file as XML
270     xmlcreator.save();
271   }
272   
273       
274   ////////////////////////////////////////////////////////////////////////////////////////////
275   // Private methods
276   ////////////////////////////////////////////////////////////////////////////////////////////
277 
278   private void makeGeneralPanel()
279   {   
280     nameLabel     = new JLabel("Experiment name:");
281     fileNameLabel = new JLabel("Filename:");
282     nameField     = new JTextField(20);
283     fileNameField = new JTextField(20);
284     
285     // Add action listeners, so that the labels and tab are marked
286     // when the fields change
287     ChangeTracker.addListener(nameField, nameLabel, generalPanel);
288     ChangeTracker.addListener(fileNameField, fileNameLabel, generalPanel);
289     
290     GridBagConstraints gbc = new GridBagConstraints();
291     
292     gbc.anchor     = GridBagConstraints.LINE_START;
293     gbc.gridx      = 1;
294     gbc.gridy      = 1;
295     gbc.weightx    = 0;
296     gbc.insets     = new Insets(5,10,5,10);
297     generalPanel.add(nameLabel, gbc);
298 
299     gbc.gridx      = 2;    
300     gbc.weightx    = 0.1;
301     gbc.insets     = new Insets(5,0,5,0);
302     generalPanel.add(nameField, gbc);
303 
304     gbc.gridx      = 1;
305     gbc.gridy      = 2;
306     gbc.weightx    = 0;
307     gbc.insets     = new Insets(5,10,5,10);
308     generalPanel.add(fileNameLabel, gbc);
309     
310     gbc.gridx      = 2;
311     gbc.weightx    = 0.1;
312     gbc.insets     = new Insets(5,0,5,0);
313     generalPanel.add(fileNameField, gbc);
314     
315     generalPanel.setBorder(BorderFactory.createCompoundBorder(
316                            BorderFactory.createEtchedBorder(),
317                            BorderFactory.createEmptyBorder(5,5,5,5)));                           
318   }
319   
320   
321   private void updateGeneralPanel(String fileName)
322   {
323     nameField.setText(expConfig.getExperimentName());
324     fileNameField.setText(fileName);
325   }
326 }