1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
55
56 public class ExperimentView
57 implements GUIview
58 {
59
60
61
62
63
64 public static final String DEFAULT_EXPERIMENT_CONFIG_DIR
65 = new String("config" + File.separator + "experiment");
66
67
68
69
70
71
72
73 JButton viewButton;
74
75
76 JPanel mainPanel;
77
78
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
95
96
97
98 FileList fileList;
99
100
101 File experimentDir;
102
103
104
105
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
126
127
128
129
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
169
170
171 public JButton getView()
172 {
173 return viewButton;
174 }
175
176
177
178
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
236 fileList = new FileList(experimentDir);
237 listModel = new DefaultListModel();
238
239
240 addList();
241
242
243 expList = new JList(listModel);
244
245
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
254 expList.addListSelectionListener(new ListSelectionListener() {
255 public void valueChanged(ListSelectionEvent e) {
256 if(e.getValueIsAdjusting() == false) {
257 JList list = (JList) e.getSource();
258
259
260
261 if(list.isSelectionEmpty())
262 return;
263
264 String fileName = experimentDir + File.separator
265 + (String) expList.getSelectedValue();
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288 try {
289 tabManager.updateExperimentTabs(fileName);
290 } catch (ConfigurationException e1) {
291 e1.printStackTrace();
292 }
293
294
295
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
356 for (int i = 0; i < fileListArray.length; i++) {
357 listModel.addElement(fileListArray[i]);
358 }
359 }
360 }
361
362
363