1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
45
46
47
48 public class ExperimentTabManager
49 {
50
51
52
53
54
55 private JTabbedPane tabs;
56
57
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
68 private JLabel nameLabel;
69 private JLabel fileNameLabel;
70 private JTextField nameField;
71 private JTextField fileNameField;
72
73
74
75
76
77
78
79
80 private ExperimentConfig expConfig;
81
82
83 private PanelManager[] panelManagers;
84
85
86 private File experimentDir;
87
88
89
90
91
92
93 public ExperimentTabManager(File dir)
94 {
95 experimentDir = dir;
96 }
97
98
99
100
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
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
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
183 if(answer == JOptionPane.YES_OPTION)
184 save();
185 }
186
187
188 expConfig = new ExperimentConfig();
189
190
191 nameField.setText("");
192 fileNameField.setText("");
193
194
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
206
207 public void save() { save(false); }
208
209
210 public void save(boolean overwrite)
211 {
212 String fileName = fileNameField.getText();
213
214
215
216
217
218
219
220
221
222
223
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
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
244 if(answer == JOptionPane.NO_OPTION)
245 return;
246 }
247
248
249 XMLcreator xmlcreator = new XMLcreator(experimentFile,
250 "ExperimentConfiguration", "0.1",
251 "experiment.dtd", "us-ascii");
252
253 Document document = xmlcreator.getDocument();
254
255 Element rootElement = document.getDocumentElement();
256 Element experimentElement = document.createElement("Experiment");
257 rootElement.appendChild(experimentElement);
258
259
260 for(int i = 0; i < panelManagers.length; i++) {
261 panelManagers[i].save(experimentElement, document);
262 }
263
264
265 NodeList nodeList = document.getElementsByTagName("Exp");
266 Element expElement = (Element) nodeList.item(0);
267 expElement.setAttribute("name", nameField.getText());
268
269
270 xmlcreator.save();
271 }
272
273
274
275
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
286
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 }