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: public AccessibleRole getAccessibleRole()
105: {
106: return AccessibleRole.WINDOW;
107: }
108:
109: public AccessibleStateSet getAccessibleStateSet()
110: {
111: AccessibleStateSet states = super.getAccessibleStateSet();
112: if (isActive())
113: states.add(AccessibleState.ACTIVE);
114: return states;
115: }
116: }
117:
118:
124: Window()
125: {
126: visible = false;
127:
128:
129: focusCycleRoot = true;
130: setLayout(new BorderLayout());
131:
132: addWindowFocusListener (new WindowAdapter ()
133: {
134: public void windowGainedFocus (WindowEvent event)
135: {
136: if (windowFocusOwner != null)
137: {
138:
139:
140: EventQueue eq = Toolkit.getDefaultToolkit ().getSystemEventQueue ();
141: synchronized (eq)
142: {
143: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
144: Component currentFocusOwner = manager.getGlobalPermanentFocusOwner ();
145: if (currentFocusOwner != null)
146: {
147: eq.postEvent (new FocusEvent (currentFocusOwner, FocusEvent.FOCUS_LOST,
148: false, windowFocusOwner));
149: eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED,
150: false, currentFocusOwner));
151: }
152: else
153: eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED, false));
154: }
155: }
156: }
157: });
158:
159: GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
160: graphicsConfiguration = g.getDefaultScreenDevice().getDefaultConfiguration();
161: }
162:
163: Window(GraphicsConfiguration gc)
164: {
165: this();
166: graphicsConfiguration = gc;
167: }
168:
169:
179: public Window(Frame owner)
180: {
181: this (owner, owner.getGraphicsConfiguration ());
182: }
183:
184:
194: public Window(Window owner)
195: {
196: this (owner, owner.getGraphicsConfiguration ());
197: }
198:
199:
209: public Window(Window owner, GraphicsConfiguration gc)
210: {
211: this ();
212:
213: synchronized (getTreeLock())
214: {
215: if (owner == null)
216: throw new IllegalArgumentException ("owner must not be null");
217:
218: parent = owner;
219: owner.ownedWindows.add(new WeakReference(this));
220: }
221:
222:
223: SecurityManager s = System.getSecurityManager();
224: if (s != null && ! s.checkTopLevelWindow(this))
225: warningString = System.getProperty("awt.appletWarning");
226:
227: if (gc != null
228: && gc.getDevice().getType() != GraphicsDevice.TYPE_RASTER_SCREEN)
229: throw new IllegalArgumentException ("gc must be from a screen device");
230:
231: if (gc == null)
232: graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment()
233: .getDefaultScreenDevice()
234: .getDefaultConfiguration();
235: else
236: graphicsConfiguration = gc;
237: }
238:
239: GraphicsConfiguration getGraphicsConfigurationImpl()
240: {
241: if (graphicsConfiguration != null)
242: return graphicsConfiguration;
243:
244: return super.getGraphicsConfigurationImpl();
245: }
246:
247:
250: public void addNotify()
251: {
252: if (peer == null)
253: peer = getToolkit().createWindow(this);
254: super.addNotify();
255: }
256:
257:
263: public void pack()
264: {
265: if (parent != null && !parent.isDisplayable())
266: parent.addNotify();
267: if (peer == null)
268: addNotify();
269:
270: setSize(getPreferredSize());
271:
272: validate();
273: }
274:
275:
279: public void show()
280: {
281: if (parent != null && !parent.isDisplayable())
282: parent.addNotify();
283: if (peer == null)
284: addNotify();
285:
286:
287: synchronized (getTreeLock())
288: {
289: Iterator e = ownedWindows.iterator();
290: while(e.hasNext())
291: {
292: Window w = (Window)(((Reference) e.next()).get());
293: if (w != null)
294: {
295: if (w.isVisible())
296: w.getPeer().setVisible(true);
297: }
298: else
299:
300:
301:
302:
303: e.remove();
304: }
305: }
306: validate();
307: super.show();
308: toFront();
309:
310: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
311: manager.setGlobalFocusedWindow (this);
312:
313: if (!shown)
314: {
315: FocusTraversalPolicy policy = getFocusTraversalPolicy ();
316: Component initialFocusOwner = null;
317:
318: if (policy != null)
319: initialFocusOwner = policy.getInitialComponent (this);
320:
321: if (initialFocusOwner != null)
322: initialFocusOwner.requestFocusInWindow ();
323:
324: shown = true;
325: }
326: }
327:
328: public void hide()
329: {
330:
331: synchronized (getTreeLock ())
332: {
333: Iterator e = ownedWindows.iterator();
334: while(e.hasNext())
335: {
336: Window w = (Window)(((Reference) e.next()).get());
337: if (w != null)
338: {
339: if (w.isVisible() && w.getPeer() != null)
340: w.getPeer().setVisible(false);
341: }
342: else
343: e.remove();
344: }
345: }
346: super.hide();
347: }
348:
349: public boolean isDisplayable()
350: {
351: if (super.isDisplayable())
352: return true;
353: return peer != null;
354: }
355:
356:
360: public void dispose()
361: {
362: hide();
363:
364: synchronized (getTreeLock ())
365: {
366: Iterator e = ownedWindows.iterator();
367: while(e.hasNext())
368: {
369: Window w = (Window)(((Reference) e.next()).get());
370: if (w != null)
371: w.dispose();
372: else
373:
374: e.remove();
375: }
376:
377: for (int i = 0; i < ncomponents; ++i)
378: component[i].removeNotify();
379: this.removeNotify();
380:
381:
382: WindowEvent we = new WindowEvent(this, WindowEvent.WINDOW_CLOSED);
383: getToolkit().getSystemEventQueue().postEvent(we);
384: }
385: }
386:
387:
391: public void toBack()
392: {
393: if (peer != null)
394: {
395: WindowPeer wp = (WindowPeer) peer;
396: wp.toBack();
397: }
398: }
399:
400:
404: public void toFront()
405: {
406: if (peer != null)
407: {
408: WindowPeer wp = (WindowPeer) peer;
409: wp.toFront();
410: }
411: }
412:
413:
421: public Toolkit getToolkit()
422: {
423: return Toolkit.getDefaultToolkit();
424: }
425:
426:
432: public final String getWarningString()
433: {
434: return warningString;
435: }
436:
437:
442: public Locale getLocale()
443: {
444: return locale == null ? Locale.getDefault() : locale;
445: }
446:
447:
454:
455:
460: public void setCursor(Cursor cursor)
461: {
462: super.setCursor(cursor);
463: }
464:
465: public Window getOwner()
466: {
467: return (Window) parent;
468: }
469:
470:
471: public Window[] getOwnedWindows()
472: {
473: Window [] trimmedList;
474: synchronized (getTreeLock ())
475: {
476:
477: Window [] validList = new Window [ownedWindows.size()];
478:
479: Iterator e = ownedWindows.iterator();
480: int numValid = 0;
481: while (e.hasNext())
482: {
483: Window w = (Window)(((Reference) e.next()).get());
484: if (w != null)
485: validList[numValid++] = w;
486: else
487:
488: e.remove();
489: }
490:
491: if (numValid != validList.length)
492: {
493: trimmedList = new Window [numValid];
494: System.arraycopy (validList, 0, trimmedList, 0, numValid);
495: }
496: else
497: trimmedList = validList;
498: }
499: return trimmedList;
500: }
501:
502:
508: public synchronized void addWindowListener(WindowListener listener)
509: {
510: windowListener = AWTEventMulticaster.add(windowListener, listener);
511: }
512:
513:
519: public synchronized void removeWindowListener(WindowListener listener)
520: {
521: windowListener = AWTEventMulticaster.remove(windowListener, listener);
522: }
523:
524:
529: public synchronized WindowListener[] getWindowListeners()
530: {
531: return (WindowListener[])
532: AWTEventMulticaster.getListeners(windowListener,
533: WindowListener.class);
534: }
535:
536:
542: public synchronized WindowFocusListener[] getWindowFocusListeners()
543: {
544: return (WindowFocusListener[])
545: AWTEventMulticaster.getListeners(windowFocusListener,
546: WindowFocusListener.class);
547: }
548:
549:
555: public synchronized WindowStateListener[] getWindowStateListeners()
556: {
557: return (WindowStateListener[])
558: AWTEventMulticaster.getListeners(windowStateListener,
559: WindowStateListener.class);
560: }
561:
562:
565: public void addWindowFocusListener (WindowFocusListener wfl)
566: {
567: windowFocusListener = AWTEventMulticaster.add (windowFocusListener, wfl);
568: }
569:
570:
575: public void addWindowStateListener (WindowStateListener wsl)
576: {
577: windowStateListener = AWTEventMulticaster.add (windowStateListener, wsl);
578: }
579:
580:
583: public void removeWindowFocusListener (WindowFocusListener wfl)
584: {
585: windowFocusListener = AWTEventMulticaster.remove (windowFocusListener, wfl);
586: }
587:
588:
593: public void removeWindowStateListener (WindowStateListener wsl)
594: {
595: windowStateListener = AWTEventMulticaster.remove (windowStateListener, wsl);
596: }
597:
598:
608: public EventListener[] getListeners(Class listenerType)
609: {
610: if (listenerType == WindowListener.class)
611: return getWindowListeners();
612: return super.getListeners(listenerType);
613: }
614:
615: void dispatchEventImpl(AWTEvent e)
616: {
617:
618: if (e.id <= WindowEvent.WINDOW_LAST
619: && e.id >= WindowEvent.WINDOW_FIRST
620: && (windowListener != null
621: || windowFocusListener != null
622: || windowStateListener != null
623: || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0))
624: processEvent(e);
625: else if (e.id == ComponentEvent.COMPONENT_RESIZED)
626: validate ();
627: else
628: super.dispatchEventImpl(e);
629: }
630:
631:
639: protected void processEvent(AWTEvent evt)
640: {
641: if (evt instanceof WindowEvent)
642: processWindowEvent((WindowEvent) evt);
643: else
644: super.processEvent(evt);
645: }
646:
647:
655: protected void processWindowEvent(WindowEvent evt)
656: {
657: int id = evt.getID();
658:
659: if (id == WindowEvent.WINDOW_GAINED_FOCUS
660: || id == WindowEvent.WINDOW_LOST_FOCUS)
661: processWindowFocusEvent (evt);
662: else if (id == WindowEvent.WINDOW_STATE_CHANGED)
663: processWindowStateEvent (evt);
664: else
665: {
666: if (windowListener != null)
667: {
668: switch (evt.getID())
669: {
670: case WindowEvent.WINDOW_ACTIVATED:
671: windowListener.windowActivated(evt);
672: break;
673:
674: case WindowEvent.WINDOW_CLOSED:
675: windowListener.windowClosed(evt);
676: break;
677:
678: case WindowEvent.WINDOW_CLOSING:
679: windowListener.windowClosing(evt);
680: break;
681:
682: case WindowEvent.WINDOW_DEACTIVATED:
683: windowListener.windowDeactivated(evt);
684: break;
685:
686: case WindowEvent.WINDOW_DEICONIFIED:
687: windowListener.windowDeiconified(evt);
688: break;
689:
690: case WindowEvent.WINDOW_ICONIFIED:
691: windowListener.windowIconified(evt);
692: break;
693:
694: case WindowEvent.WINDOW_OPENED:
695: windowListener.windowOpened(evt);
696: break;
697:
698: default:
699: break;
700: }
701: }
702: }
703: }
704:
705:
712: public boolean isActive()
713: {
714: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
715: return manager.getActiveWindow() == this;
716: }
717:
718:
725: public boolean isFocused()
726: {
727: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
728: return manager.getFocusedWindow() == this;
729: }
730:
731:
739: public Component getFocusOwner ()
740: {
741: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
742:
743: Window activeWindow = manager.getActiveWindow ();
744:
745:
746: if (activeWindow == this)
747: return manager.getFocusOwner ();
748: else
749: return null;
750: }
751:
752:
765: public Component getMostRecentFocusOwner ()
766: {
767: return windowFocusOwner;
768: }
769:
770:
779: void setFocusOwner (Component windowFocusOwner)
780: {
781: this.windowFocusOwner = windowFocusOwner;
782: }
783:
784:
791: public boolean postEvent(Event e)
792: {
793: return handleEvent (e);
794: }
795:
796:
806: public boolean isShowing()
807: {
808: return isVisible();
809: }
810:
811: public void setLocationRelativeTo (Component c)
812: {
813: if (c == null || !c.isShowing ())
814: {
815: int x = 0;
816: int y = 0;
817:
818: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment ();
819: Point center = ge.getCenterPoint ();
820: x = center.x - (width / 2);
821: y = center.y - (height / 2);
822: setLocation (x, y);
823: }
824:
825: }
826:
827:
830: private class WindowBltBufferStrategy extends BltBufferStrategy
831: {
832:
839: WindowBltBufferStrategy(int numBuffers, boolean accelerated)
840: {
841: super(numBuffers,
842: new BufferCapabilities(new ImageCapabilities(accelerated),
843: new ImageCapabilities(accelerated),
844: BufferCapabilities.FlipContents.COPIED));
845: }
846: }
847:
848:
851: private class WindowFlipBufferStrategy extends FlipBufferStrategy
852: {
853:
861: WindowFlipBufferStrategy(int numBuffers)
862: throws AWTException
863: {
864: super(numBuffers,
865: new BufferCapabilities(new ImageCapabilities(true),
866: new ImageCapabilities(true),
867: BufferCapabilities.FlipContents.COPIED));
868: }
869: }
870:
871:
894: public void createBufferStrategy(int numBuffers)
895: {
896: if (numBuffers < 1)
897: throw new IllegalArgumentException("Window.createBufferStrategy: number"
898: + " of buffers is less than one");
899:
900: if (!isDisplayable())
901: throw new IllegalStateException("Window.createBufferStrategy: window is"
902: + " not displayable");
903:
904: BufferStrategy newStrategy = null;
905:
906:
907: try
908: {
909: newStrategy = new WindowFlipBufferStrategy(numBuffers);
910: }
911: catch (AWTException e)
912: {
913: }
914:
915:
916: if (newStrategy == null)
917: newStrategy = new WindowBltBufferStrategy(numBuffers, true);
918:
919: bufferStrategy = newStrategy;
920: }
921:
922:
941: public void createBufferStrategy(int numBuffers,
942: BufferCapabilities caps)
943: {
944: if (numBuffers < 1)
945: throw new IllegalArgumentException("Window.createBufferStrategy: number"
946: + " of buffers is less than one");
947:
948: if (caps == null)
949: throw new IllegalArgumentException("Window.createBufferStrategy:"
950: + " capabilities object is null");
951:
952:
953: if (caps.isPageFlipping())
954: {
955: try
956: {
957: bufferStrategy = new WindowFlipBufferStrategy(numBuffers);
958: }
959: catch (AWTException e)
960: {
961: }
962: }
963: else
964: bufferStrategy = new WindowBltBufferStrategy(numBuffers, true);
965: }
966:
967:
973: public BufferStrategy getBufferStrategy()
974: {
975: return bufferStrategy;
976: }
977:
978:
983: public void applyResourceBundle(ResourceBundle rb)
984: {
985: throw new Error ("Not implemented");
986: }
987:
988:
993: public void applyResourceBundle(String rbName)
994: {
995: ResourceBundle rb = ResourceBundle.getBundle(rbName, Locale.getDefault(),
996: ClassLoader.getSystemClassLoader());
997: if (rb != null)
998: applyResourceBundle(rb);
999: }
1000:
1001:
1007: public AccessibleContext getAccessibleContext()
1008: {
1009:
1010: if (accessibleContext == null)
1011: accessibleContext = new AccessibleAWTWindow();
1012: return accessibleContext;
1013: }
1014:
1015:
1020: public GraphicsConfiguration getGraphicsConfiguration()
1021: {
1022: if (graphicsConfiguration != null) return graphicsConfiguration;
1023: if (peer != null) return peer.getGraphicsConfiguration();
1024: return null;
1025: }
1026:
1027: protected void processWindowFocusEvent(WindowEvent event)
1028: {
1029: if (windowFocusListener != null)
1030: {
1031: switch (event.getID ())
1032: {
1033: case WindowEvent.WINDOW_GAINED_FOCUS:
1034: windowFocusListener.windowGainedFocus (event);
1035: break;
1036:
1037: case WindowEvent.WINDOW_LOST_FOCUS:
1038: windowFocusListener.windowLostFocus (event);
1039: break;
1040:
1041: default:
1042: break;
1043: }
1044: }
1045: }
1046:
1047:
1050: protected void processWindowStateEvent(WindowEvent event)
1051: {
1052: if (windowStateListener != null
1053: && event.getID () == WindowEvent.WINDOW_STATE_CHANGED)
1054: windowStateListener.windowStateChanged (event);
1055: }
1056:
1057:
1062: public final boolean isFocusableWindow ()
1063: {
1064: if (getFocusableWindowState () == false)
1065: return false;
1066:
1067: if (this instanceof Dialog
1068: || this instanceof Frame)
1069: return true;
1070:
1071:
1072:
1073: return false;
1074: }
1075:
1076:
1081: public boolean getFocusableWindowState ()
1082: {
1083: return focusableWindowState;
1084: }
1085:
1086:
1091: public void setFocusableWindowState (boolean focusableWindowState)
1092: {
1093: this.focusableWindowState = focusableWindowState;
1094: }
1095:
1096:
1101: String generateName()
1102: {
1103: return "win" + getUniqueLong();
1104: }
1105:
1106: private static synchronized long getUniqueLong()
1107: {
1108: return next_window_number++;
1109: }
1110: }