1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57:
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63:
64:
70: public class Window extends Container implements Accessible
71: {
72: private static final long serialVersionUID = 4497834738069338734L;
73:
74:
75: private String warningString = null;
76: private int windowSerializedDataVersion = 0;
77:
78:
79:
80: private int state = 0;
81:
82: private boolean focusableWindowState = true;
83:
84:
85: private transient Vector ownedWindows = new Vector();
86:
87: private transient WindowListener windowListener;
88: private transient WindowFocusListener windowFocusListener;
89: private transient WindowStateListener windowStateListener;
90: private transient GraphicsConfiguration graphicsConfiguration;
91:
92: private transient boolean shown;
93:
94:
95: transient Component windowFocusOwner;
96:
97:
100: private static transient long next_window_number;
101:
102: protected class AccessibleAWTWindow extends AccessibleAWTContainer
103: {
104: private static final long serialVersionUID = 4215068635060671780L;
105:
106: public AccessibleRole getAccessibleRole()
107: {
108: return AccessibleRole.WINDOW;
109: }
110:
111: public AccessibleStateSet getAccessibleStateSet()
112: {
113: AccessibleStateSet states = super.getAccessibleStateSet();
114: if (isActive())
115: states.add(AccessibleState.ACTIVE);
116: return states;
117: }
118: }
119:
120:
126: Window()
127: {
128: visible = false;
129:
130:
131: focusCycleRoot = true;
132: setLayout(new BorderLayout());
133:
134: addWindowFocusListener (new WindowAdapter ()
135: {
136: public void windowGainedFocus (WindowEvent event)
137: {
138: if (windowFocusOwner != null)
139: {
140:
141:
142: EventQueue eq = Toolkit.getDefaultToolkit ().getSystemEventQueue ();
143: synchronized (eq)
144: {
145: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
146: Component currentFocusOwner = manager.getGlobalPermanentFocusOwner ();
147: if (currentFocusOwner != null)
148: {
149: eq.postEvent (new FocusEvent (currentFocusOwner, FocusEvent.FOCUS_LOST,
150: false, windowFocusOwner));
151: eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED,
152: false, currentFocusOwner));
153: }
154: else
155: eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED, false));
156: }
157: }
158: }
159: });
160:
161: GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
162: graphicsConfiguration = g.getDefaultScreenDevice().getDefaultConfiguration();
163: }
164:
165: Window(GraphicsConfiguration gc)
166: {
167: this();
168: graphicsConfiguration = gc;
169: }
170:
171:
181: public Window(Frame owner)
182: {
183: this (owner, owner.getGraphicsConfiguration ());
184: }
185:
186:
196: public Window(Window owner)
197: {
198: this (owner, owner.getGraphicsConfiguration ());
199: }
200:
201:
211: public Window(Window owner, GraphicsConfiguration gc)
212: {
213: this ();
214:
215: synchronized (getTreeLock())
216: {
217: if (owner == null)
218: throw new IllegalArgumentException ("owner must not be null");
219:
220: parent = owner;
221: owner.ownedWindows.add(new WeakReference(this));
222: }
223:
224:
225: SecurityManager s = System.getSecurityManager();
226: if (s != null && ! s.checkTopLevelWindow(this))
227: warningString = System.getProperty("awt.appletWarning");
228:
229: if (gc != null
230: && gc.getDevice().getType() != GraphicsDevice.TYPE_RASTER_SCREEN)
231: throw new IllegalArgumentException ("gc must be from a screen device");
232:
233: if (gc == null)
234: graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment()
235: .getDefaultScreenDevice()
236: .getDefaultConfiguration();
237: else
238: graphicsConfiguration = gc;
239: }
240:
241: GraphicsConfiguration getGraphicsConfigurationImpl()
242: {
243: if (graphicsConfiguration != null)
244: return graphicsConfiguration;
245:
246: return super.getGraphicsConfigurationImpl();
247: }
248:
249:
252: public void addNotify()
253: {
254: if (peer == null)
255: peer = getToolkit().createWindow(this);
256: super.addNotify();
257: }
258:
259:
265: public void pack()
266: {
267: if (parent != null && !parent.isDisplayable())
268: parent.addNotify();
269: if (peer == null)
270: addNotify();
271:
272: setSize(getPreferredSize());
273:
274: validate();
275: }
276:
277:
281: public void show()
282: {
283: synchronized (getTreeLock())
284: {
285: if (parent != null && !parent.isDisplayable())
286: parent.addNotify();
287: if (peer == null)
288: addNotify();
289:
290:
291: Iterator e = ownedWindows.iterator();
292: while(e.hasNext())
293: {
294: Window w = (Window)(((Reference) e.next()).get());
295: if (w != null)
296: {
297: if (w.isVisible())
298: w.getPeer().setVisible(true);
299: }
300: else
301:
302:
303:
304:
305: e.remove();
306: }
307: validate();
308: super.show();
309: toFront();
310:
311: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
312: manager.setGlobalFocusedWindow (this);
313:
314: if (!shown)
315: {
316: FocusTraversalPolicy policy = getFocusTraversalPolicy ();
317: Component initialFocusOwner = null;
318:
319: if (policy != null)
320: initialFocusOwner = policy.getInitialComponent (this);
321:
322: if (initialFocusOwner != null)
323: initialFocusOwner.requestFocusInWindow ();
324:
325: shown = true;
326: }
327: }
328: }
329:
330: public void hide()
331: {
332:
333: synchronized (getTreeLock ())
334: {
335: Iterator e = ownedWindows.iterator();
336: while(e.hasNext())
337: {
338: Window w = (Window)(((Reference) e.next()).get());
339: if (w != null)
340: {
341: if (w.isVisible() && w.getPeer() != null)
342: w.getPeer().setVisible(false);
343: }
344: else
345: e.remove();
346: }
347: }
348: super.hide();
349: }
350:
351:
355: public void dispose()
356: {
357: hide();
358:
359: synchronized (getTreeLock ())
360: {
361: Iterator e = ownedWindows.iterator();
362: while(e.hasNext())
363: {
364: Window w = (Window)(((Reference) e.next()).get());
365: if (w != null)
366: w.dispose();
367: else
368:
369: e.remove();
370: }
371:
372: for (int i = 0; i < ncomponents; ++i)
373: component[i].removeNotify();
374: this.removeNotify();
375:
376:
377: WindowEvent we = new WindowEvent(this, WindowEvent.WINDOW_CLOSED);
378: getToolkit().getSystemEventQueue().postEvent(we);
379: }
380: }
381:
382:
386: public void toBack()
387: {
388: if (peer != null)
389: {
390: WindowPeer wp = (WindowPeer) peer;
391: wp.toBack();
392: }
393: }
394:
395:
399: public void toFront()
400: {
401: if (peer != null)
402: {
403: WindowPeer wp = (WindowPeer) peer;
404: wp.toFront();
405: }
406: }
407:
408:
416: public Toolkit getToolkit()
417: {
418: return Toolkit.getDefaultToolkit();
419: }
420:
421:
427: public final String getWarningString()
428: {
429: return warningString;
430: }
431:
432:
437: public Locale getLocale()
438: {
439: return locale == null ? Locale.getDefault() : locale;
440: }
441:
442:
449:
450:
455: public void setCursor(Cursor cursor)
456: {
457: super.setCursor(cursor);
458: }
459:
460: public Window getOwner()
461: {
462: return (Window) parent;
463: }
464:
465:
466: public Window[] getOwnedWindows()
467: {
468: Window [] trimmedList;
469: synchronized (getTreeLock ())
470: {
471:
472: Window [] validList = new Window [ownedWindows.size()];
473:
474: Iterator e = ownedWindows.iterator();
475: int numValid = 0;
476: while (e.hasNext())
477: {
478: Window w = (Window)(((Reference) e.next()).get());
479: if (w != null)
480: validList[numValid++] = w;
481: else
482:
483: e.remove();
484: }
485:
486: if (numValid != validList.length)
487: {
488: trimmedList = new Window [numValid];
489: System.arraycopy (validList, 0, trimmedList, 0, numValid);
490: }
491: else
492: trimmedList = validList;
493: }
494: return trimmedList;
495: }
496:
497:
503: public synchronized void addWindowListener(WindowListener listener)
504: {
505: windowListener = AWTEventMulticaster.add(windowListener, listener);
506: }
507:
508:
514: public synchronized void removeWindowListener(WindowListener listener)
515: {
516: windowListener = AWTEventMulticaster.remove(windowListener, listener);
517: }
518:
519:
524: public synchronized WindowListener[] getWindowListeners()
525: {
526: return (WindowListener[])
527: AWTEventMulticaster.getListeners(windowListener,
528: WindowListener.class);
529: }
530:
531:
537: public synchronized WindowFocusListener[] getWindowFocusListeners()
538: {
539: return (WindowFocusListener[])
540: AWTEventMulticaster.getListeners(windowFocusListener,
541: WindowFocusListener.class);
542: }
543:
544:
550: public synchronized WindowStateListener[] getWindowStateListeners()
551: {
552: return (WindowStateListener[])
553: AWTEventMulticaster.getListeners(windowStateListener,
554: WindowStateListener.class);
555: }
556:
557:
560: public void addWindowFocusListener (WindowFocusListener wfl)
561: {
562: windowFocusListener = AWTEventMulticaster.add (windowFocusListener, wfl);
563: }
564:
565:
570: public void addWindowStateListener (WindowStateListener wsl)
571: {
572: windowStateListener = AWTEventMulticaster.add (windowStateListener, wsl);
573: }
574:
575:
578: public void removeWindowFocusListener (WindowFocusListener wfl)
579: {
580: windowFocusListener = AWTEventMulticaster.remove (windowFocusListener, wfl);
581: }
582:
583:
588: public void removeWindowStateListener (WindowStateListener wsl)
589: {
590: windowStateListener = AWTEventMulticaster.remove (windowStateListener, wsl);
591: }
592:
593:
603: public EventListener[] getListeners(Class listenerType)
604: {
605: if (listenerType == WindowListener.class)
606: return getWindowListeners();
607: return super.getListeners(listenerType);
608: }
609:
610: void dispatchEventImpl(AWTEvent e)
611: {
612:
613: if (e.id <= WindowEvent.WINDOW_LAST
614: && e.id >= WindowEvent.WINDOW_FIRST
615: && (windowListener != null
616: || windowFocusListener != null
617: || windowStateListener != null
618: || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0))
619: processEvent(e);
620: else if (e.id == ComponentEvent.COMPONENT_RESIZED)
621: validate ();
622: else
623: super.dispatchEventImpl(e);
624: }
625:
626:
634: protected void processEvent(AWTEvent evt)
635: {
636: if (evt instanceof WindowEvent)
637: processWindowEvent((WindowEvent) evt);
638: else
639: super.processEvent(evt);
640: }
641:
642:
650: protected void processWindowEvent(WindowEvent evt)
651: {
652: int id = evt.getID();
653:
654: if (id == WindowEvent.WINDOW_GAINED_FOCUS
655: || id == WindowEvent.WINDOW_LOST_FOCUS)
656: processWindowFocusEvent (evt);
657: else if (id == WindowEvent.WINDOW_STATE_CHANGED)
658: processWindowStateEvent (evt);
659: else
660: {
661: if (windowListener != null)
662: {
663: switch (evt.getID())
664: {
665: case WindowEvent.WINDOW_ACTIVATED:
666: windowListener.windowActivated(evt);
667: break;
668:
669: case WindowEvent.WINDOW_CLOSED:
670: windowListener.windowClosed(evt);
671: break;
672:
673: case WindowEvent.WINDOW_CLOSING:
674: windowListener.windowClosing(evt);
675: break;
676:
677: case WindowEvent.WINDOW_DEACTIVATED:
678: windowListener.windowDeactivated(evt);
679: break;
680:
681: case WindowEvent.WINDOW_DEICONIFIED:
682: windowListener.windowDeiconified(evt);
683: break;
684:
685: case WindowEvent.WINDOW_ICONIFIED:
686: windowListener.windowIconified(evt);
687: break;
688:
689: case WindowEvent.WINDOW_OPENED:
690: windowListener.windowOpened(evt);
691: break;
692:
693: default:
694: break;
695: }
696: }
697: }
698: }
699:
700:
707: public boolean isActive()
708: {
709: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
710: return manager.getActiveWindow() == this;
711: }
712:
713:
720: public boolean isFocused()
721: {
722: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
723: return manager.getFocusedWindow() == this;
724: }
725:
726:
734: public Component getFocusOwner ()
735: {
736: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
737:
738: Window activeWindow = manager.getActiveWindow ();
739:
740:
741: if (activeWindow == this)
742: return manager.getFocusOwner ();
743: else
744: return null;
745: }
746:
747:
760: public Component getMostRecentFocusOwner ()
761: {
762: return windowFocusOwner;
763: }
764:
765:
774: void setFocusOwner (Component windowFocusOwner)
775: {
776: this.windowFocusOwner = windowFocusOwner;
777: }
778:
779:
786: public boolean postEvent(Event e)
787: {
788: return handleEvent (e);
789: }
790:
791:
801: public boolean isShowing()
802: {
803: return isVisible();
804: }
805:
806: public void setLocationRelativeTo (Component c)
807: {
808: if (c == null || !c.isShowing ())
809: {
810: int x = 0;
811: int y = 0;
812:
813: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment ();
814: Point center = ge.getCenterPoint ();
815: x = center.x - (width / 2);
816: y = center.y - (height / 2);
817: setLocation (x, y);
818: }
819:
820: }
821:
822:
825: private class WindowBltBufferStrategy extends BltBufferStrategy
826: {
827:
834: WindowBltBufferStrategy(int numBuffers, boolean accelerated)
835: {
836: super(numBuffers,
837: new BufferCapabilities(new ImageCapabilities(accelerated),
838: new ImageCapabilities(accelerated),
839: BufferCapabilities.FlipContents.COPIED));
840: }
841: }
842:
843:
846: private class WindowFlipBufferStrategy extends FlipBufferStrategy
847: {
848:
856: WindowFlipBufferStrategy(int numBuffers)
857: throws AWTException
858: {
859: super(numBuffers,
860: new BufferCapabilities(new ImageCapabilities(true),
861: new ImageCapabilities(true),
862: BufferCapabilities.FlipContents.COPIED));
863: }
864: }
865:
866:
889: public void createBufferStrategy(int numBuffers)
890: {
891: if (numBuffers < 1)
892: throw new IllegalArgumentException("Window.createBufferStrategy: number"
893: + " of buffers is less than one");
894:
895: if (!isDisplayable())
896: throw new IllegalStateException("Window.createBufferStrategy: window is"
897: + " not displayable");
898:
899: BufferStrategy newStrategy = null;
900:
901:
902: try
903: {
904: newStrategy = new WindowFlipBufferStrategy(numBuffers);
905: }
906: catch (AWTException e)
907: {
908: }
909:
910:
911: if (newStrategy == null)
912: newStrategy = new WindowBltBufferStrategy(numBuffers, true);
913:
914: bufferStrategy = newStrategy;
915: }
916:
917:
936: public void createBufferStrategy(int numBuffers, BufferCapabilities caps)
937: throws AWTException
938: {
939: if (numBuffers < 1)
940: throw new IllegalArgumentException("Window.createBufferStrategy: number"
941: + " of buffers is less than one");
942:
943: if (caps == null)
944: throw new IllegalArgumentException("Window.createBufferStrategy:"
945: + " capabilities object is null");
946:
947:
948: if (caps.isPageFlipping())
949: bufferStrategy = new WindowFlipBufferStrategy(numBuffers);
950: else
951: bufferStrategy = new WindowBltBufferStrategy(numBuffers, true);
952: }
953:
954:
960: public BufferStrategy getBufferStrategy()
961: {
962: return bufferStrategy;
963: }
964:
965:
970: public void applyResourceBundle(ResourceBundle rb)
971: {
972: throw new Error ("Not implemented");
973: }
974:
975:
980: public void applyResourceBundle(String rbName)
981: {
982: ResourceBundle rb = ResourceBundle.getBundle(rbName, Locale.getDefault(),
983: ClassLoader.getSystemClassLoader());
984: if (rb != null)
985: applyResourceBundle(rb);
986: }
987:
988:
994: public AccessibleContext getAccessibleContext()
995: {
996:
997: if (accessibleContext == null)
998: accessibleContext = new AccessibleAWTWindow();
999: return accessibleContext;
1000: }
1001:
1002:
1007: public GraphicsConfiguration getGraphicsConfiguration()
1008: {
1009: if (graphicsConfiguration != null) return graphicsConfiguration;
1010: if (peer != null) return peer.getGraphicsConfiguration();
1011: return null;
1012: }
1013:
1014: protected void processWindowFocusEvent(WindowEvent event)
1015: {
1016: if (windowFocusListener != null)
1017: {
1018: switch (event.getID ())
1019: {
1020: case WindowEvent.WINDOW_GAINED_FOCUS:
1021: windowFocusListener.windowGainedFocus (event);
1022: break;
1023:
1024: case WindowEvent.WINDOW_LOST_FOCUS:
1025: windowFocusListener.windowLostFocus (event);
1026: break;
1027:
1028: default:
1029: break;
1030: }
1031: }
1032: }
1033:
1034:
1037: protected void processWindowStateEvent(WindowEvent event)
1038: {
1039: if (windowStateListener != null
1040: && event.getID () == WindowEvent.WINDOW_STATE_CHANGED)
1041: windowStateListener.windowStateChanged (event);
1042: }
1043:
1044:
1049: public final boolean isFocusableWindow ()
1050: {
1051: if (getFocusableWindowState () == false)
1052: return false;
1053:
1054: if (this instanceof Dialog
1055: || this instanceof Frame)
1056: return true;
1057:
1058:
1059:
1060: return false;
1061: }
1062:
1063:
1068: public boolean getFocusableWindowState ()
1069: {
1070: return focusableWindowState;
1071: }
1072:
1073:
1078: public void setFocusableWindowState (boolean focusableWindowState)
1079: {
1080: this.focusableWindowState = focusableWindowState;
1081: }
1082:
1083:
1088: String generateName()
1089: {
1090: return "win" + getUniqueLong();
1091: }
1092:
1093: private static synchronized long getUniqueLong()
1094: {
1095: return next_window_number++;
1096: }
1097: }