1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package jgroup.experiment.gui;
20
21 import java.awt.Dimension;
22 import java.awt.GridBagConstraints;
23 import java.awt.GridBagLayout;
24 import java.awt.Toolkit;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.io.File;
28 import java.util.LinkedList;
29
30 import javax.swing.BorderFactory;
31 import javax.swing.Box;
32 import javax.swing.BoxLayout;
33 import javax.swing.JButton;
34 import javax.swing.JFrame;
35 import javax.swing.JPanel;
36 import javax.swing.SwingUtilities;
37 import javax.swing.UIManager;
38 import javax.swing.border.BevelBorder;
39 import javax.swing.border.EtchedBorder;
40
41 import jgroup.experiment.gui.views.helpers.FileList;
42
43
44
45
46 public class GUIapp {
47
48
49
50
51
52
53
54
55 static GUIview[] views;
56 static LinkedList buttonList;
57
58
59 static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
60
61
62
63
64
65
66
67
68 JFrame topFrame;
69
70
71 Box leftBox;
72
73
74 JPanel mainPanel;
75
76
77 GUIview currentView = null;
78
79
80
81
82
83 GUIapp() throws Exception {
84
85
86
87
88 leftBox = new Box(BoxLayout.Y_AXIS);
89 leftBox.setBorder(BorderFactory.createCompoundBorder(
90 BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
91 BorderFactory.createCompoundBorder(
92 BorderFactory.createTitledBorder("Views:"),
93 BorderFactory.createEmptyBorder(5, 5, 5, 5))));
94
95 mainPanel = new JPanel(new GridBagLayout());
96
97 initViews();
98 initTopFrame();
99
100
101 JButton button = views[0].getView();
102 button.doClick();
103 }
104
105
106
107
108
109 public static void main(String[] args) throws Exception {
110 findViews();
111
112
113 javax.swing.SwingUtilities.invokeLater(new Runnable() {
114 public void run() {
115 try {
116 new GUIapp();
117 } catch (Exception e) {
118 e.printStackTrace();
119 }
120 }
121 });
122 }
123
124
125
126
127
128
129
130 public static Dimension getScreenSize()
131 {
132 return screenSize;
133 }
134
135
136
137
138
139
140 private static void findViews() throws Exception {
141
142 File viewDir = new File("jgroup-experiment/src/main/java/jgroup/experiment/gui/views");
143 if (!viewDir.exists())
144 throw new Exception("The GUI views directory could not be found.");
145
146 String[] viewFiles = new FileList(viewDir, ".java").getFileList();
147 views = new GUIview[viewFiles.length];
148
149 for (int i = 0; i < viewFiles.length; i++) {
150
151 String fileName = viewFiles[i];
152 String className =
153 "jgroup.experiment.gui.views."
154 + fileName.substring(0, fileName.length() - 5);
155
156
157 views[i] = (GUIview) Class.forName(className).newInstance();
158 }
159 }
160
161 private void initViews() {
162 JButton button;
163 buttonList = new LinkedList();
164
165
166 for (int i = 0; i < views.length; i++) {
167 button = views[i].getView();
168 button.setBorder(BorderFactory.createCompoundBorder(
169 BorderFactory.createBevelBorder(BevelBorder.RAISED),
170 BorderFactory.createEmptyBorder(5, 5, 5, 5)));
171 buttonList.add(button);
172
173
174 button.addActionListener(new ActionListener() {
175 public void actionPerformed(ActionEvent e) {
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 int index = buttonList.indexOf(e.getSource());
195 mainPanel.removeAll();
196 GridBagConstraints gbc = new GridBagConstraints();
197 gbc.fill = GridBagConstraints.BOTH;
198 gbc.anchor = GridBagConstraints.FIRST_LINE_START;
199 gbc.weightx = 1.0;
200 gbc.weighty = 1.0;
201 gbc.gridwidth = GridBagConstraints.REMAINDER;
202 gbc.gridheight = GridBagConstraints.REMAINDER;
203 gbc.gridx = 0;
204 gbc.gridy = 0;
205 mainPanel.add(views[index].makeMainPanel(), gbc);
206 mainPanel.revalidate();
207
208
209
210 }
211 });
212
213 leftBox.add(button);
214 }
215 }
216
217 private void initTopFrame() {
218 topFrame = new JFrame("Jgroup GUI for experiments");
219 topFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
220
221 topFrame.getContentPane().setLayout(new GridBagLayout());
222 GridBagConstraints gbc = new GridBagConstraints();
223
224 gbc.fill = GridBagConstraints.VERTICAL;
225 gbc.anchor = GridBagConstraints.LINE_START;
226 gbc.gridwidth = GridBagConstraints.RELATIVE;
227 gbc.gridheight = GridBagConstraints.REMAINDER;
228 gbc.gridx = 0;
229 gbc.gridy = 0;
230 gbc.weightx = 0.1;
231 gbc.weighty = 0.1;
232 topFrame.getContentPane().add(leftBox, gbc);
233
234 gbc.fill = GridBagConstraints.BOTH;
235 gbc.anchor = GridBagConstraints.FIRST_LINE_START;
236 gbc.gridwidth = GridBagConstraints.REMAINDER;
237 gbc.gridx = GridBagConstraints.RELATIVE;
238 gbc.weightx = 1.0;
239 gbc.weighty = 0.1;
240 topFrame.getContentPane().add(mainPanel, gbc);
241
242 topFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
243
244 try {
245 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
246 } catch (Exception e) {
247 SwingUtilities.updateComponentTreeUI(topFrame);
248 }
249
250 topFrame.pack();
251 topFrame.setSize( (int) (screenSize.width/1.3), (int) (screenSize.height/1.4) );
252 topFrame.setVisible(true);
253 }
254 }