Source code: com/trapezium/humanoid/BeanManager.java
1 /*
2 This simple extension of the java.awt.Frame class
3 contains all the elements necessary to act as the
4 main window of an application.
5 */
6 package com.trapezium.humanoid;
7
8 import com.trapezium.bean.HumanBean;
9
10 import java.awt.*;
11 import java.io.*;
12
13 public class BeanManager extends Frame
14 {
15
16 class BeanFilter implements FilenameFilter {
17 public boolean accept( File dir, String name ) {
18 return( name.indexOf( ".bean" ) > 0 );
19 }
20 }
21 BeanFilter theBeanFilter;
22 HumanBeanTracker theTracker;
23
24 public BeanManager()
25 {
26 // This code is automatically generated by Visual Cafe when you add
27 // components to the visual environment. It instantiates and initializes
28 // the components. To modify the code, only use code syntax that matches
29 // what Visual Cafe can generate, or Visual Cafe may be unable to back
30 // parse your Java file into its visual environment.
31
32 //{{INIT_CONTROLS
33 setLayout(null);
34 setVisible(false);
35 setSize(405,420);
36 openFileDialog1 = new java.awt.FileDialog(this);
37 openFileDialog1.setMode(FileDialog.LOAD);
38 openFileDialog1.setTitle("Open");
39 //$$ openFileDialog1.move(60,372);
40 Group1 = new CheckboxGroup();
41 humanoid1 = new java.awt.Checkbox("(available)", Group1, false);
42 humanoid1.setBounds(36,36,144,40);
43 add(humanoid1);
44 humanoid2 = new java.awt.Checkbox("(available)", Group1, false);
45 humanoid2.setBounds(36,72,144,40);
46 add(humanoid2);
47 humanoid3 = new java.awt.Checkbox("(available)", Group1, false);
48 humanoid3.setBounds(36,108,144,40);
49 add(humanoid3);
50 humanoid4 = new java.awt.Checkbox("(available)", Group1, false);
51 humanoid4.setBounds(36,144,144,40);
52 add(humanoid4);
53 humanoid5 = new java.awt.Checkbox("(available)", Group1, false);
54 humanoid5.setBounds(36,180,144,40);
55 add(humanoid5);
56 humanoid6 = new java.awt.Checkbox("(available)", Group1, false);
57 humanoid6.setBounds(36,216,144,40);
58 add(humanoid6);
59 humanoid7 = new java.awt.Checkbox("(available)", Group1, false);
60 humanoid7.setBounds(36,252,144,40);
61 add(humanoid7);
62 humanoid8 = new java.awt.Checkbox("(available)", Group1, false);
63 humanoid8.setBounds(36,288,144,40);
64 add(humanoid8);
65 humanoid9 = new java.awt.Checkbox("(available)", Group1, false);
66 humanoid9.setBounds(36,324,144,40);
67 add(humanoid9);
68 label1 = new java.awt.Label("Humanoids");
69 label1.setBounds(24,0,100,40);
70 label1.setFont(new Font("Dialog", Font.BOLD, 12));
71 add(label1);
72 label2 = new java.awt.Label("Behaviors");
73 label2.setBounds(228,0,100,40);
74 label2.setFont(new Font("Dialog", Font.BOLD, 12));
75 add(label2);
76 behavior1 = new java.awt.Checkbox("(available)");
77 behavior1.setBounds(228,36,156,24);
78 add(behavior1);
79 behavior2 = new java.awt.Checkbox("(available)");
80 behavior2.setBounds(228,60,156,24);
81 add(behavior2);
82 behavior3 = new java.awt.Checkbox("(available)");
83 behavior3.setBounds(228,84,156,24);
84 add(behavior3);
85 behavior4 = new java.awt.Checkbox("(available)");
86 behavior4.setBounds(228,108,156,24);
87 add(behavior4);
88 behavior5 = new java.awt.Checkbox("(available)");
89 behavior5.setBounds(228,132,156,24);
90 add(behavior5);
91 behavior6 = new java.awt.Checkbox("(available)");
92 behavior6.setBounds(228,156,156,24);
93 add(behavior6);
94 behavior7 = new java.awt.Checkbox("(available)");
95 behavior7.setBounds(228,180,156,24);
96 add(behavior7);
97 behavior8 = new java.awt.Checkbox("(available)");
98 behavior8.setBounds(228,204,156,24);
99 add(behavior8);
100 behavior9 = new java.awt.Checkbox("(available)");
101 behavior9.setBounds(228,228,156,24);
102 add(behavior9);
103 behavior10 = new java.awt.Checkbox("(available)");
104 behavior10.setBounds(228,252,156,24);
105 add(behavior10);
106 behavior11 = new java.awt.Checkbox("(available)");
107 behavior11.setBounds(228,276,156,24);
108 add(behavior11);
109 behavior12 = new java.awt.Checkbox("(available)");
110 behavior12.setBounds(228,300,156,24);
111 add(behavior12);
112 behavior13 = new java.awt.Checkbox("(available)");
113 behavior13.setBounds(228,324,156,24);
114 add(behavior13);
115 behavior14 = new java.awt.Checkbox("(available)");
116 behavior14.setBounds(228,348,156,24);
117 add(behavior14);
118 statusLabel = new java.awt.Label("");
119 statusLabel.setBounds(36,396,300,28);
120 statusLabel.setFont(new Font("Dialog", Font.BOLD, 12));
121 add(statusLabel);
122 setTitle("Human Bean Manager, version 1.0");
123 //}}
124 theTracker = new HumanBeanTracker();
125 theTracker.addCheckboxH( humanoid1 );
126 theTracker.addCheckboxH( humanoid2 );
127 theTracker.addCheckboxH( humanoid3 );
128 theTracker.addCheckboxH( humanoid4 );
129 theTracker.addCheckboxH( humanoid5 );
130 theTracker.addCheckboxH( humanoid6 );
131 theTracker.addCheckboxH( humanoid7 );
132 theTracker.addCheckboxH( humanoid8 );
133 theTracker.addCheckboxH( humanoid9 );
134
135 theBeanFilter = new BeanFilter();
136
137 theTracker.addCheckboxB( behavior1 );
138 theTracker.addCheckboxB( behavior2 );
139 theTracker.addCheckboxB( behavior3 );
140 theTracker.addCheckboxB( behavior4 );
141 theTracker.addCheckboxB( behavior5 );
142 theTracker.addCheckboxB( behavior6 );
143 theTracker.addCheckboxB( behavior7 );
144 theTracker.addCheckboxB( behavior8 );
145 theTracker.addCheckboxB( behavior9 );
146 theTracker.addCheckboxB( behavior10 );
147 theTracker.addCheckboxB( behavior11 );
148 theTracker.addCheckboxB( behavior12 );
149 theTracker.addCheckboxB( behavior13 );
150 theTracker.addCheckboxB( behavior14 );
151
152 //{{INIT_MENUS
153 mainMenuBar = new java.awt.MenuBar();
154 menu1 = new java.awt.Menu("File");
155 miOpen = new java.awt.MenuItem("Open Bean...");
156 miOpen.setShortcut(new MenuShortcut(java.awt.event.KeyEvent.VK_O,false));
157 menu1.add(miOpen);
158 miSave = new java.awt.MenuItem("Save VRML");
159 miSave.setShortcut(new MenuShortcut(java.awt.event.KeyEvent.VK_S,false));
160 menu1.add(miSave);
161 miSaveAs = new java.awt.MenuItem("Save VRML As...");
162 menu1.add(miSaveAs);
163 menu1.addSeparator();
164 miExit = new java.awt.MenuItem("Exit");
165 menu1.add(miExit);
166 mainMenuBar.add(menu1);
167 menu3 = new java.awt.Menu("Help");
168 mainMenuBar.setHelpMenu(menu3);
169 miAbout = new java.awt.MenuItem("About..");
170 menu3.add(miAbout);
171 mainMenuBar.add(menu3);
172 setMenuBar(mainMenuBar);
173 //$$ mainMenuBar.move(12,372);
174 //}}
175
176 //{{REGISTER_LISTENERS
177 SymWindow aSymWindow = new SymWindow();
178 this.addWindowListener(aSymWindow);
179 SymAction lSymAction = new SymAction();
180 miOpen.addActionListener(lSymAction);
181 miAbout.addActionListener(lSymAction);
182 miExit.addActionListener(lSymAction);
183 SymItem lSymItem = new SymItem();
184 humanoid1.addItemListener(lSymItem);
185 humanoid2.addItemListener(lSymItem);
186 humanoid3.addItemListener(lSymItem);
187 humanoid4.addItemListener(lSymItem);
188 humanoid5.addItemListener(lSymItem);
189 humanoid6.addItemListener(lSymItem);
190 humanoid7.addItemListener(lSymItem);
191 humanoid8.addItemListener(lSymItem);
192 humanoid9.addItemListener(lSymItem);
193 behavior1.addItemListener(lSymItem);
194 behavior2.addItemListener(lSymItem);
195 behavior3.addItemListener(lSymItem);
196 behavior4.addItemListener(lSymItem);
197 behavior5.addItemListener(lSymItem);
198 behavior6.addItemListener(lSymItem);
199 behavior7.addItemListener(lSymItem);
200 behavior8.addItemListener(lSymItem);
201 behavior9.addItemListener(lSymItem);
202 behavior10.addItemListener(lSymItem);
203 behavior11.addItemListener(lSymItem);
204 behavior12.addItemListener(lSymItem);
205 behavior13.addItemListener(lSymItem);
206 behavior14.addItemListener(lSymItem);
207 miSave.addActionListener(lSymAction);
208 //}}
209 }
210
211 public BeanManager(String title)
212 {
213 this();
214 setTitle(title);
215 }
216
217 /**
218 * Shows or hides the component depending on the boolean flag b.
219 * @param b if true, show the component; otherwise, hide the component.
220 * @see java.awt.Component#isVisible
221 */
222 public void setVisible(boolean b)
223 {
224 if(b)
225 {
226 setLocation(50, 50);
227 }
228 super.setVisible(b);
229 }
230
231 static public void main(String args[])
232 {
233 (new BeanManager()).setVisible(true);
234 }
235
236 public void addNotify()
237 {
238 // Record the size of the window prior to calling parents addNotify.
239 Dimension d = getSize();
240
241 super.addNotify();
242
243 if (fComponentsAdjusted)
244 return;
245
246 // Adjust components according to the insets
247 setSize(insets().left + insets().right + d.width, insets().top + insets().bottom + d.height);
248 Component components[] = getComponents();
249 for (int i = 0; i < components.length; i++)
250 {
251 Point p = components[i].getLocation();
252 p.translate(insets().left, insets().top);
253 components[i].setLocation(p);
254 }
255 fComponentsAdjusted = true;
256 }
257
258 // Used for addNotify check.
259 boolean fComponentsAdjusted = false;
260
261 //{{DECLARE_CONTROLS
262 java.awt.FileDialog openFileDialog1;
263 java.awt.Checkbox humanoid1;
264 CheckboxGroup Group1;
265 java.awt.Checkbox humanoid2;
266 java.awt.Checkbox humanoid3;
267 java.awt.Checkbox humanoid4;
268 java.awt.Checkbox humanoid5;
269 java.awt.Checkbox humanoid6;
270 java.awt.Checkbox humanoid7;
271 java.awt.Checkbox humanoid8;
272 java.awt.Checkbox humanoid9;
273 java.awt.Label label1;
274 java.awt.Label label2;
275 java.awt.Checkbox behavior1;
276 java.awt.Checkbox behavior2;
277 java.awt.Checkbox behavior3;
278 java.awt.Checkbox behavior4;
279 java.awt.Checkbox behavior5;
280 java.awt.Checkbox behavior6;
281 java.awt.Checkbox behavior7;
282 java.awt.Checkbox behavior8;
283 java.awt.Checkbox behavior9;
284 java.awt.Checkbox behavior10;
285 java.awt.Checkbox behavior11;
286 java.awt.Checkbox behavior12;
287 java.awt.Checkbox behavior13;
288 java.awt.Checkbox behavior14;
289 java.awt.Label statusLabel;
290 //}}
291
292 //{{DECLARE_MENUS
293 java.awt.MenuBar mainMenuBar;
294 java.awt.Menu menu1;
295 java.awt.MenuItem miOpen;
296 java.awt.MenuItem miSave;
297 java.awt.MenuItem miSaveAs;
298 java.awt.MenuItem miExit;
299 java.awt.Menu menu3;
300 java.awt.MenuItem miAbout;
301 //}}
302
303 class SymWindow extends java.awt.event.WindowAdapter
304 {
305 public void windowClosing(java.awt.event.WindowEvent event)
306 {
307 Object object = event.getSource();
308 if (object == BeanManager.this)
309 BeanManager_WindowClosing(event);
310 }
311 }
312
313 void BeanManager_WindowClosing(java.awt.event.WindowEvent event)
314 {
315 setVisible(false); // hide the Frame
316 dispose(); // free the system resources
317 System.exit(0); // close the application
318 }
319
320 class SymAction implements java.awt.event.ActionListener
321 {
322 public void actionPerformed(java.awt.event.ActionEvent event)
323 {
324 Object object = event.getSource();
325 if (object == miOpen)
326 miOpen_Action(event);
327 else if (object == miAbout)
328 miAbout_Action(event);
329 else if (object == miExit)
330 miExit_Action(event);
331 else if (object == miSave)
332 miSave_ActionPerformed(event);
333 }
334 }
335
336 void miAbout_Action(java.awt.event.ActionEvent event)
337 {
338 //{{CONNECTION
339 // Action from About Create and show as modal
340 (new AboutDialog(this, true)).setVisible(true);
341 //}}
342 }
343
344 void miExit_Action(java.awt.event.ActionEvent event)
345 {
346 //{{CONNECTION
347 // Action from Exit Create and show as modal
348 (new QuitDialog(this, true)).setVisible(true);
349 //}}
350 }
351
352 void miOpen_Action(java.awt.event.ActionEvent event)
353 {
354 //{{CONNECTION
355 // Action from Open... Show the OpenFileDialog
356 int defMode = openFileDialog1.getMode();
357 String defTitle = openFileDialog1.getTitle();
358 String defDirectory = openFileDialog1.getDirectory();
359 String defFile = openFileDialog1.getFile();
360
361 openFileDialog1 = new java.awt.FileDialog(this, defTitle, defMode);
362 openFileDialog1.setDirectory(defDirectory);
363 openFileDialog1.setFile(defFile);
364 openFileDialog1.setVisible(true);
365 openFileDialog1.setFilenameFilter(theBeanFilter);
366 //}}
367 String result = openFileDialog1.getFile();
368 String dir = openFileDialog1.getDirectory();
369 if (( dir != null ) && ( result != null )) {
370 result = dir + result;
371 System.out.println( "Loading '" + result + "'" );
372 statusLabel.setText( "Loading '" + result + "' ..." );
373 try {
374 HumanBean newBean = HumanBean.load( result );
375 System.out.println( "calling saveBean" );
376 theTracker.saveBean( newBean );
377 System.out.println( "calling saveBehaviors" );
378 theTracker.saveBehaviors( newBean );
379 System.out.println( "calling updateBehaviors" );
380 theTracker.updateBehaviors();
381 System.out.println( "done." );
382 updateStatus();
383 } catch ( Exception e ) {
384 e.printStackTrace();
385 statusLabel.setText( "Exception loading '" + result + "'" );
386 }
387 }
388 }
389
390 class SymItem implements java.awt.event.ItemListener
391 {
392 public void itemStateChanged(java.awt.event.ItemEvent event)
393 {
394 Object object = event.getSource();
395 theTracker.updateHumanoidSelection( object );
396 theTracker.updateBehaviorSelection( object );
397 }
398 }
399
400 void miSave_ActionPerformed(java.awt.event.ActionEvent event)
401 {
402 // to do: code goes here.
403 statusLabel.setText( "Saving '" + theTracker.getSaveName() + "'" );
404 theTracker.saveHumanoidVRML();
405 updateStatus();
406 }
407
408 void updateStatus() {
409 int nHumanoids = theTracker.getNumberHumanoids();
410 int nBehaviors = theTracker.getNumberBehaviors();
411 if (( nHumanoids == 1 ) && ( nBehaviors == 1 )) {
412 statusLabel.setText( "1 humanoid, 1 behavior" );
413 } else if (( nHumanoids == 1 ) && ( nBehaviors > 1 )) {
414 statusLabel.setText( "1 humanoid, " + nBehaviors + " behaviors" );
415 } else if (( nHumanoids > 1 ) && ( nBehaviors == 1 )) {
416 statusLabel.setText( nHumanoids + " humanoids, 1 behavior" );
417 } else {
418 statusLabel.setText( nHumanoids + " humanoids, " + nBehaviors + " behaviors" );
419 }
420 }
421 }
422