Source code: org/mitre/cvw/WindowMgr.java
1 /*
2 * Copyright (c) 1996-2000. The MITRE Corporation (http://www.mitre.org/).
3 * All rights reserved.
4 * CVW comes with ABSOLUTELY NO WARRANTY. See license for details.
5 */
6
7 package org.mitre.cvw;
8
9 import java.io.*;
10 import java.util.Hashtable;
11 import java.util.Vector;
12 import java.util.Enumeration;
13 import java.awt.*;
14 import java.awt.event.WindowEvent;
15
16 /**
17 * This singleton class manages any open window. There is a vector to
18 * manage the tool windows and the popup pages window. Currently not used is
19 * a vector to manage system windows. To manage windows that represent CVWObjects,
20 * a hashtable of hashtables is used, where the object number
21 * of the CVWObject is the key into the first hashtable and the value of this
22 * element is a hashtable. This second hashtable is contains the window class
23 * name as the key and the instance to the window as the value. There is also
24 * a pointer to the Whiteboard Manager which has the same functions.
25 *
26 * This class has the following functions: adding a window,
27 * seeing if a window already exists and bringing it to the front,
28 * closing a window.
29 *
30 * @version 1.0
31 * @author Deb Ercolini
32 */
33 public class WindowMgr {
34
35 public static final int PAGE = 1;
36 public static final int SYSTEM = 2;
37 public static final int OBJECT = 3;
38 public static final int TOOL = 4;
39 public static final int WHITEBOARD = 5;
40
41 private static WindowMgr windowMgr;
42
43 /**
44 * Returns the current instance of the window manager.
45 * @return the current instance
46 */
47 public static WindowMgr getInstance() {
48 return windowMgr;
49 }
50
51 /**
52 * Returns the current instance of the window manager.
53 * Should be getInstance().
54 * @return the current instance
55 * @deprecated see getInstance()
56 */
57 public static WindowMgr getWindowMgr() {
58 return windowMgr;
59 }
60
61 /**
62 * Sets the current instance of the window manager.
63 * @param winmgr the current instance
64 */
65 private static void setWindowMgr(WindowMgr winmgr) {
66 windowMgr = winmgr;
67 }
68
69 /* 8/29/97 dage - intent for object windows is that there may be
70 * many types of windows open for an object ... i.e. CVWNote can
71 * have a getInfo window and a NotePad window open at the same
72 * time ... but only one of each type should be open at a time.
73 */
74
75 /* instance vars */
76 Vector pageWindows;
77 Vector sysWindows;
78 Vector toolWindows;
79 Hashtable objWindows;
80 WBMgr wbMgr;
81 Vector allWindows;
82
83 /**
84 * Constructor
85 */
86 WindowMgr() {
87 pageWindows = new Vector(10);
88 sysWindows = new Vector(10);
89 toolWindows = new Vector(10);
90 objWindows = new Hashtable(10);
91 wbMgr = new WBMgr();
92 WindowMgr.setWindowMgr(this);
93 }
94
95 /**
96 * Adds a window given a defined window type, except object and whiteboard windows.
97 * @param type the type of this window
98 * @param win the window to be added
99 * @see #addObjectWindow
100 * @see #addWBWindow
101 */
102 public synchronized void addWindow(int type, Window win) {
103
104 switch(type) {
105 case PAGE:
106 pageWindows.addElement(win);
107 break;
108 case SYSTEM:
109 sysWindows.addElement(win);
110 break;
111 case TOOL:
112 toolWindows.addElement(win);
113 break;
114 case OBJECT:
115 // cant get objNum unless cast to a class which understands... java sucks
116 //System.err.println("in addWindow for object type");
117 break;
118 case WHITEBOARD:
119 break;
120 default:
121 System.err.println("win type not defined");
122 }
123 }
124
125 /**
126 * Adds a window which is representing an object.
127 * @param type should always be OBJECT
128 * @param win the window to be added
129 * @param oNum the object number of the CVWObject this window represents
130 */
131 public synchronized void addObjectWindow(int type, Window win, CVWObjNum oNum) {
132 if (type != OBJECT) {
133 System.err.println("in addObjWindow w/o proper object type");
134 return;
135 }
136 Hashtable hash;
137 hash = (Hashtable)objWindows.get(oNum);
138 if (hash == null) {
139 //System.err.println("hash is null " + oNum);
140 hash = new Hashtable(2);
141 hash.put(win.getClass().getName(), win);
142 objWindows.put(oNum, hash);
143 //System.err.println(objWindows.toString());
144 }
145 else {
146 //System.err.println("hash ! null " + oNum);
147 String name = win.getClass().getName();
148 Window oldWin = (Window)hash.get(name);
149 //System.err.println("old win " + oldWin);
150 hash.put(name, win);
151 //System.err.println(objWindows.toString());
152 }
153
154 }
155
156
157 /**
158 * Adds a whiteboard window.
159 * @param wb the whiteboard CVWObject
160 */
161 public synchronized void addWBWindow(CVWWhiteboard wb) {
162 wbMgr.addWB(wb);
163 }
164
165
166 /**
167 * Brings all popup page windows to the front.
168 */
169 public void showAllPages() {
170 showAllWindows(pageWindows);
171 }
172
173 /**
174 * Brings all system windows to the front (CURRENTLY NOT USED).
175 */
176 public void showAllSystem() {
177 showAllWindows(sysWindows);
178 }
179
180 /**
181 * Brings all tool windows to the front.
182 */
183 public void showAllTools() {
184 showAllWindows(toolWindows);
185 }
186
187 /**
188 * Brings all registered to the front.
189 */
190 public synchronized void showAllWindows() {
191 showAllWindows(pageWindows);
192 showAllWindows(sysWindows);
193 showAllWindows(toolWindows);
194 showAllObjWindows();
195 showAllWBWindows();
196 }
197
198 /**
199 * Brings the specified windows to the front.
200 * @param type the type of window desired
201 */
202 public synchronized void showAllWindows(int type) {
203 switch(type) {
204 case PAGE:
205 showAllWindows(pageWindows);
206 break;
207 case SYSTEM:
208 showAllWindows(sysWindows);
209 break;
210 case TOOL:
211 showAllWindows(toolWindows);
212 break;
213 case OBJECT:
214 showAllObjWindows();
215 // cant get objNum unless cast to a class which understands... java sucks
216 break;
217 case WHITEBOARD:
218 showAllWBWindows();
219 default:
220 System.err.println("win type not defined");
221 }
222 }
223
224 /**
225 * Given a vector of windows, it brings them all to the front, removing
226 * windows that are no longer visible that didnot unregister with the window
227 * manager.
228 * @param v the vector of windows to be shown
229 */
230 public void showAllWindows(Vector v) {
231 Window win;
232 Enumeration e = v.elements();
233 Vector oldWins = new Vector();
234 while(e.hasMoreElements()) {
235 win = (Window)e.nextElement();
236 //System.err.println("vis " + win.isVisible() + " show " + win.isShowing() + " " + win.toString());
237 if (win.isVisible() && win.isShowing())
238 win.toFront();
239 else
240 oldWins.addElement(win);
241 }
242 //System.err.println("old wins " + oldWins.size());
243 for (int i=0;i<oldWins.size();i++) {
244 win = (Window)oldWins.elementAt(i);
245 v.removeElement(win);
246 }
247 }
248
249 /**
250 * Brings all object windows to the front, removing
251 * windows that are no longer visible that didnot unregister with the window
252 * manager.
253 */
254 public void showAllObjWindows() {
255 Window win;
256 Hashtable hash;
257 for (Enumeration e = objWindows.elements() ; e.hasMoreElements() ;) {
258 Vector oldWins = new Vector(2);
259 hash = (Hashtable)e.nextElement();
260 //System.err.println(hash.toString());
261 for (Enumeration h = hash.elements() ; h.hasMoreElements() ;) {
262 win = (Window)h.nextElement();
263 //System.err.println("vis " + win.isVisible() + " show "
264 //+ win.isShowing() + " " + win.toString());
265 if (win.isVisible() && win.isShowing())
266 win.toFront();
267 else
268 oldWins.addElement(win);
269 }
270 //System.err.println("old wins " + oldWins.size());
271 for(int i=0; i<oldWins.size(); i++) {
272 win = (Window)oldWins.elementAt(i);
273 hash.remove(win);
274 }
275 }
276 }
277
278
279 /**
280 * Brings all whiteboard windows to the front.
281 */
282 public void showAllWBWindows() {
283 wbMgr.showAllWBWindows();
284 }
285
286
287 /**
288 * Clears all window vectors and hashtables.
289 */
290 public synchronized void clearAllWindows() {
291 pageWindows = new Vector(10);
292 sysWindows = new Vector(10);
293 toolWindows = new Vector(10);
294 objWindows = new Hashtable(10);
295 wbMgr.clearAllWBWindows(10);
296 }
297
298
299 /**
300 * Closes all windows.
301 */
302 public synchronized void closeAllWindows() {
303 CVWAdminTool at;
304
305 closeAllWindows(pageWindows.elements());
306 closeAllWindows(sysWindows.elements());
307 closeAllWindows(toolWindows.elements());
308 closeAllObjWindows();
309 wbMgr.closeAllWBWindows();
310
311 at = CVWAdminTool.getInstance();
312 if (at != null) at.closeWindow();
313 }
314
315 /**
316 * Closes all windows given an enumeration of windows to be closed.
317 * @param e the enumeration of windows to be closed
318 */
319 public void closeAllWindows(Enumeration e) {
320 Window win;
321 while(e.hasMoreElements()) {
322 win = (Window)e.nextElement();
323 //System.err.println("vis " + win.isVisible() +
324 //" show " + win.isShowing() + " " + win.toString());
325 if (win.isVisible() && win.isShowing())
326 win.setVisible(false);
327 }
328 }
329
330 /* 12/11/96 dage - ask tim if should dispose() of window rather than hide()
331 */
332
333 /**
334 * Closes all object windows.
335 */
336 public void closeAllObjWindows() {
337 Window win;
338 Hashtable hash;
339 for (Enumeration e = objWindows.elements() ; e.hasMoreElements() ;) {
340 hash = (Hashtable)e.nextElement();
341 for (Enumeration h = hash.elements() ; h.hasMoreElements() ;) {
342 win = (Window)h.nextElement();
343 if (win.isVisible() && win.isShowing())
344 win.setVisible(false); //hide();
345 //System.err.println(win.toString());
346 }
347 }
348 }
349
350 /* 9/2/98 dage - added
351 */
352 /**
353 * Closes all object windows that were opened in the room.
354 */
355 public void closeAllObjInRoom() {
356
357 closeAllWBInRoom();
358
359 if (objWindows.isEmpty())
360 return;
361 Enumeration keys = objWindows.keys();
362 while (keys.hasMoreElements()) {
363 CVWObjNum obj = (CVWObjNum)keys.nextElement();
364 CVWObject cvwObj = CVWCache.getInstance().get(obj);
365 if (cvwObj == null)
366 break; //shouldnt happen
367 CVWObject topObj = cvwObj.getTopLocation();
368 if (topObj instanceof CVWRoom) {
369 Hashtable hash = (Hashtable)objWindows.get(obj);
370 Window win;
371 for (Enumeration h = hash.elements() ; h.hasMoreElements() ;) {
372 win = (Window)h.nextElement();
373 if (win.isVisible() && win.isShowing())
374 win.dispatchEvent(new WindowEvent(win, WindowEvent.WINDOW_CLOSING));
375 //System.err.println(win.toString());
376 }
377 objWindows.remove(obj);
378 }
379 } //end while
380 }
381
382 /**
383 * Returns the instance of a window given the object number it represents and the
384 * class name desired.
385 * @param className the class name of the window desired
386 * @param objNum the object number of the CVWObject the window represents
387 * @return the window stored in the hash table
388 */
389 public synchronized Window findObjectWindow(String className, CVWObjNum objNum) {
390 Window win = null;
391 Hashtable hash = (Hashtable)objWindows.get(objNum);
392 if (hash != null) {
393 //System.err.println("got hash " + hash.toString());
394 win = (Window)hash.get(className);
395 //System.err.println("got win " + win);
396 }
397 return win;
398 }
399
400 /* 3/3/97 dage -if window exists, bring to front and return true, otherwise return false
401 */
402 /**
403 * Returns whether the window requested is visible
404 * @param className the class name of the window desired
405 * @param objNum the object number of the CVWObject the window represents
406 * @return <code>true</code> if the window is registered and visible
407 */
408 public synchronized boolean objectWindowExists(String className, CVWObjNum objNum) {
409 Window win = findObjectWindow(className, objNum);
410 if (win != null) {
411 //System.err.println("vis " + win.isVisible() +
412 // " show " + win.isShowing() + " " + win.toString());
413 if (win.isVisible() && win.isShowing()) {
414 win.toFront();
415 return true; // dont open a new one
416 }
417 }
418 return false;
419 }
420
421
422 /* 6/1/98 dage -if window exists, bring to front and return it, otherwise return null
423 */
424 /**
425 * Returns the window if it is registered and currently visible.
426 * @param className the class name of the window desired
427 * @param objNum the object number of the CVWObject the window represents
428 * @return the window if it is registered and visible
429 */
430 public synchronized Window getObjectWindow(String className, CVWObjNum objNum) {
431 Window win = findObjectWindow(className, objNum);
432 if (win != null) {
433 if (win.isVisible() && win.isShowing()) {
434 win.toFront();
435 return win; // dont open a new one
436 }
437 }
438 return null;
439 }
440
441 // RJT
442 /**
443 * Returns whether the whiteboard is currently open.
444 * @param wbObject the whiteboard CVWObject
445 * @return whether the whiteboard is currently open
446 */
447 public synchronized boolean whiteboardWindowExists(CVWWhiteboard wbObject) {
448
449 CVWObjNum objNum = wbObject.objNum;
450 CVWWhiteboard wb = wbMgr.getWB(objNum);
451
452 if (wb != null) {
453 return true;
454 }
455 return false;
456 }
457
458
459 /* 8/29/97 dage - assuming there is only one instance of the tool
460 * open at a time.
461 */
462 /**
463 * Returns the tool window if it has been registered.
464 * @param className the class name of the desired window
465 * @return the registered window
466 */
467 public synchronized Window findToolWindow(String className) {
468 Window win = null;
469 for (Enumeration e = toolWindows.elements() ; e.hasMoreElements() ;) {
470 win = (Window)e.nextElement();
471 if (className == null) return null;
472
473 if (win.getClass().getName().indexOf(className) > -1)
474 return win;
475 }
476 return null;
477 }
478
479 /* 8/29/97 dage -if window exists, bring to front and return true, otherwise return false
480 */
481 /**
482 * Returns whether the window has been registered and is visible.
483 * @param className the class name of the desired window
484 * @return <code>true</code> if the window is registered and is visible
485 */
486 public synchronized boolean toolWindowExists(String className) {
487 Window win = findToolWindow(className);
488 if (win != null) {
489 //System.err.println("vis " + win.isVisible() + " show " + win.isShowing() + " " + win.toString());
490 if (win.isVisible() && win.isShowing()) {
491 win.setVisible(true);
492 return true; // dont open a new one
493 }
494 }
495 return false;
496 }
497
498 /**
499 * Remove a tool window from the registered list.
500 * @param win the window to be removed
501 * @return whether or not the window was removed
502 */
503 public boolean removeToolWindow(Frame win) {
504 boolean result;
505 result = toolWindows.removeElement(win);
506 //System.err.println("tool window removed " + result);
507 return result;
508 }
509
510 /**
511 * Removes a registered window from the list of windows associatied with an CVWObject.
512 * @param win the window to be removed
513 * @param oNum the object number of the CVWObject
514 * @return whether the window was removed
515 */
516 public boolean removeObjectWindow(Window win, CVWObjNum oNum) {
517 boolean result = false;
518 Hashtable hash = (Hashtable)objWindows.get(oNum);
519 if (hash == null) {
520 //System.err.println("hash is null " + oNum);
521 }
522 else {
523 //System.err.println("hash ! null " + oNum);
524 String name = win.getClass().getName();
525 Window oldWin = (Window)hash.remove(name);
526 //System.err.println("old win " + oldWin);
527 if (win.equals(oldWin)) // should always be true
528 result = true;
529 else
530 System.err.println("old win doesnt equal passed in win" + oldWin);
531 }
532 return result;
533 }
534
535 /**
536 * Closes all opened whiteboards which are located in the room.
537 */
538 public void closeAllWBInRoom() {
539 wbMgr.closeAllWBInRoom();
540 }
541
542 /**
543 * Removes a whiteboard window given a whiteboard CVWObject
544 * @param wb the whiteboard CVWObject
545 */
546 public synchronized void removeWhiteboardWindow(CVWWhiteboard wb) {
547 CVWObjNum objNum = wb.objNum;
548
549 wbMgr.rmWB(objNum);
550 }
551
552
553 /**
554 * Returns the number of popup windows currently opened.
555 * @return the number of popup windows currently opened
556 */
557 public int countAllPages() {
558 Window win;
559 Enumeration e = pageWindows.elements();
560 while(e.hasMoreElements()) {
561 win = (Window)e.nextElement();
562 //System.err.println("vis " + win.isVisible() +
563 //" show " + win.isShowing() + " " + win.toString());
564 if (! (win.isVisible() && win.isShowing()))
565 pageWindows.removeElement(win);
566 }
567 return pageWindows.size();
568 }
569
570 }