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:
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61:
62:
68: public class Window extends Container implements Accessible
69: {
70: private static final long serialVersionUID = 4497834738069338734L;
71:
72:
73: private String warningString = null;
74: private int windowSerializedDataVersion = 0;
75:
76:
77:
78: private int state = 0;
79:
80: private boolean focusableWindowState = true;
81:
82: private boolean alwaysOnTop = false;
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: GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
135: graphicsConfiguration = g.getDefaultScreenDevice().getDefaultConfiguration();
136: }
137:
138: Window(GraphicsConfiguration gc)
139: {
140: this();
141: graphicsConfiguration = gc;
142: }
143:
144:
154: public Window(Frame owner)
155: {
156: this (owner, owner.getGraphicsConfiguration ());
157: }
158:
159:
169: public Window(Window owner)
170: {
171: this (owner, owner.getGraphicsConfiguration ());
172: }
173:
174:
184: public Window(Window owner, GraphicsConfiguration gc)
185: {
186: this ();
187:
188: synchronized (getTreeLock())
189: {
190: if (owner == null)
191: throw new IllegalArgumentException ("owner must not be null");
192:
193: parent = owner;
194: owner.ownedWindows.add(new WeakReference(this));
195: }
196:
197:
198: SecurityManager s = System.getSecurityManager();
199: if (s != null && ! s.checkTopLevelWindow(this))
200: warningString = System.getProperty("awt.appletWarning");
201:
202: if (gc != null
203: && gc.getDevice().getType() != GraphicsDevice.TYPE_RASTER_SCREEN)
204: throw new IllegalArgumentException ("gc must be from a screen device");
205:
206: if (gc == null)
207: graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment()
208: .getDefaultScreenDevice()
209: .getDefaultConfiguration();
210: else
211: graphicsConfiguration = gc;
212: }
213:
214: GraphicsConfiguration getGraphicsConfigurationImpl()
215: {
216: if (graphicsConfiguration != null)
217: return graphicsConfiguration;
218:
219: return super.getGraphicsConfigurationImpl();
220: }
221:
222:
225: public void addNotify()
226: {
227: if (peer == null)
228: peer = getToolkit().createWindow(this);
229: super.addNotify();
230: }
231:
232:
238: public void pack()
239: {
240: if (parent != null && !parent.isDisplayable())
241: parent.addNotify();
242: if (peer == null)
243: addNotify();
244:
245: setSize(getPreferredSize());
246:
247: validate();
248: }
249:
250:
254: public void show()
255: {
256: synchronized (getTreeLock())
257: {
258: if (parent != null && ! parent.isDisplayable())
259: parent.addNotify();
260: if (peer == null)
261: addNotify();
262:
263: validate();
264: if (visible)
265: toFront();
266: else
267: {
268: super.show();
269:
270: Iterator e = ownedWindows.iterator();
271: while (e.hasNext())
272: {
273: Window w = (Window) (((Reference) e.next()).get());
274: if (w != null)
275: {
276: if (w.isVisible())
277: w.getPeer().setVisible(true);
278: }
279: else
280:
281:
282:
283:
284: e.remove();
285: }
286: }
287: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
288: manager.setGlobalFocusedWindow(this);
289:
290: if (! shown)
291: {
292: FocusTraversalPolicy policy = getFocusTraversalPolicy();
293: Component initialFocusOwner = null;
294:
295: if (policy != null)
296: initialFocusOwner = policy.getInitialComponent(this);
297:
298: if (initialFocusOwner != null)
299: initialFocusOwner.requestFocusInWindow();
300:
301: shown = true;
302: }
303: }
304: }
305:
306: public void hide()
307: {
308:
309: synchronized (getTreeLock ())
310: {
311: Iterator e = ownedWindows.iterator();
312: while(e.hasNext())
313: {
314: Window w = (Window)(((Reference) e.next()).get());
315: if (w != null)
316: {
317: if (w.isVisible() && w.getPeer() != null)
318: w.getPeer().setVisible(false);
319: }
320: else
321: e.remove();
322: }
323: }
324: super.hide();
325: }
326:
327:
331: public void dispose()
332: {
333: hide();
334:
335: synchronized (getTreeLock ())
336: {
337: Iterator e = ownedWindows.iterator();
338: while(e.hasNext())
339: {
340: Window w = (Window)(((Reference) e.next()).get());
341: if (w != null)
342: w.dispose();
343: else
344:
345: e.remove();
346: }
347:
348: for (int i = 0; i < ncomponents; ++i)
349: component[i].removeNotify();
350: this.removeNotify();
351:
352:
353: WindowEvent we = new WindowEvent(this, WindowEvent.WINDOW_CLOSED);
354: getToolkit().getSystemEventQueue().postEvent(we);
355: }
356: }
357:
358:
365: public void toBack()
366: {
367: if (peer != null)
368: {
369: if( alwaysOnTop )
370: setAlwaysOnTop( false );
371: ( (WindowPeer) peer ).toBack();
372: }
373: }
374:
375:
379: public void toFront()
380: {
381: if (peer != null)
382: ( (WindowPeer) peer ).toFront();
383: }
384:
385:
393: public Toolkit getToolkit()
394: {
395: return Toolkit.getDefaultToolkit();
396: }
397:
398:
404: public final String getWarningString()
405: {
406: return warningString;
407: }
408:
409:
414: public Locale getLocale()
415: {
416: return locale == null ? Locale.getDefault() : locale;
417: }
418:
419:
426:
427:
432: public void setCursor(Cursor cursor)
433: {
434: super.setCursor(cursor);
435: }
436:
437: public Window getOwner()
438: {
439: return (Window) parent;
440: }
441:
442:
443: public Window[] getOwnedWindows()
444: {
445: Window [] trimmedList;
446: synchronized (getTreeLock ())
447: {
448:
449: Window [] validList = new Window [ownedWindows.size()];
450:
451: Iterator e = ownedWindows.iterator();
452: int numValid = 0;
453: while (e.hasNext())
454: {
455: Window w = (Window)(((Reference) e.next()).get());
456: if (w != null)
457: validList[numValid++] = w;
458: else
459:
460: e.remove();
461: }
462:
463: if (numValid != validList.length)
464: {
465: trimmedList = new Window [numValid];
466: System.arraycopy (validList, 0, trimmedList, 0, numValid);
467: }
468: else
469: trimmedList = validList;
470: }
471: return trimmedList;
472: }
473:
474:
480: public synchronized void addWindowListener(WindowListener listener)
481: {
482: windowListener = AWTEventMulticaster.add(windowListener, listener);
483: }
484:
485:
491: public synchronized void removeWindowListener(WindowListener listener)
492: {
493: windowListener = AWTEventMulticaster.remove(windowListener, listener);
494: }
495:
496:
501: public synchronized WindowListener[] getWindowListeners()
502: {
503: return (WindowListener[])
504: AWTEventMulticaster.getListeners(windowListener,
505: WindowListener.class);
506: }
507:
508:
514: public synchronized WindowFocusListener[] getWindowFocusListeners()
515: {
516: return (WindowFocusListener[])
517: AWTEventMulticaster.getListeners(windowFocusListener,
518: WindowFocusListener.class);
519: }
520:
521:
527: public synchronized WindowStateListener[] getWindowStateListeners()
528: {
529: return (WindowStateListener[])
530: AWTEventMulticaster.getListeners(windowStateListener,
531: WindowStateListener.class);
532: }
533:
534:
537: public void addWindowFocusListener (WindowFocusListener wfl)
538: {
539: windowFocusListener = AWTEventMulticaster.add (windowFocusListener, wfl);
540: }
541:
542:
547: public void addWindowStateListener (WindowStateListener wsl)
548: {
549: windowStateListener = AWTEventMulticaster.add (windowStateListener, wsl);
550: }
551:
552:
555: public void removeWindowFocusListener (WindowFocusListener wfl)
556: {
557: windowFocusListener = AWTEventMulticaster.remove (windowFocusListener, wfl);
558: }
559:
560:
565: public void removeWindowStateListener (WindowStateListener wsl)
566: {
567: windowStateListener = AWTEventMulticaster.remove (windowStateListener, wsl);
568: }
569:
570:
580: public EventListener[] getListeners(Class listenerType)
581: {
582: if (listenerType == WindowListener.class)
583: return getWindowListeners();
584: return super.getListeners(listenerType);
585: }
586:
587: void dispatchEventImpl(AWTEvent e)
588: {
589:
590: if (e.id <= WindowEvent.WINDOW_LAST
591: && e.id >= WindowEvent.WINDOW_FIRST
592: && (windowListener != null
593: || windowFocusListener != null
594: || windowStateListener != null
595: || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0))
596: processEvent(e);
597: else
598: {
599: if (peer != null && (e.id == ComponentEvent.COMPONENT_RESIZED
600: || e.id == ComponentEvent.COMPONENT_MOVED))
601: {
602: Rectangle bounds = peer.getBounds();
603: x = bounds.x;
604: y = bounds.y;
605: height = bounds.height;
606: width = bounds.width;
607:
608: if (e.id == ComponentEvent.COMPONENT_RESIZED)
609: {
610: invalidate();
611: validate();
612: }
613: }
614: super.dispatchEventImpl(e);
615: }
616: }
617:
618:
626: protected void processEvent(AWTEvent evt)
627: {
628: if (evt instanceof WindowEvent)
629: processWindowEvent((WindowEvent) evt);
630: else
631: super.processEvent(evt);
632: }
633:
634:
642: protected void processWindowEvent(WindowEvent evt)
643: {
644: int id = evt.getID();
645:
646: if (id == WindowEvent.WINDOW_GAINED_FOCUS
647: || id == WindowEvent.WINDOW_LOST_FOCUS)
648: processWindowFocusEvent (evt);
649: else if (id == WindowEvent.WINDOW_STATE_CHANGED)
650: processWindowStateEvent (evt);
651: else
652: {
653: if (windowListener != null)
654: {
655: switch (evt.getID())
656: {
657: case WindowEvent.WINDOW_ACTIVATED:
658: windowListener.windowActivated(evt);
659: break;
660:
661: case WindowEvent.WINDOW_CLOSED:
662: windowListener.windowClosed(evt);
663: break;
664:
665: case WindowEvent.WINDOW_CLOSING:
666: windowListener.windowClosing(evt);
667: break;
668:
669: case WindowEvent.WINDOW_DEACTIVATED:
670: windowListener.windowDeactivated(evt);
671: break;
672:
673: case WindowEvent.WINDOW_DEICONIFIED:
674: windowListener.windowDeiconified(evt);
675: break;
676:
677: case WindowEvent.WINDOW_ICONIFIED:
678: windowListener.windowIconified(evt);
679: break;
680:
681: case WindowEvent.WINDOW_OPENED:
682: windowListener.windowOpened(evt);
683: break;
684:
685: default:
686: break;
687: }
688: }
689: }
690: }
691:
692:
699: public boolean isActive()
700: {
701: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
702: return manager.getActiveWindow() == this;
703: }
704:
705:
712: public boolean isFocused()
713: {
714: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
715: return manager.getFocusedWindow() == this;
716: }
717:
718:
726: public Component getFocusOwner ()
727: {
728: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
729:
730: Window activeWindow = manager.getActiveWindow ();
731:
732:
733: if (activeWindow == this)
734: return manager.getFocusOwner ();
735: else
736: return null;
737: }
738:
739:
752: public Component getMostRecentFocusOwner ()
753: {
754: return windowFocusOwner;
755: }
756:
757:
766: void setFocusOwner (Component windowFocusOwner)
767: {
768: this.windowFocusOwner = windowFocusOwner;
769: }
770:
771:
778: public boolean postEvent(Event e)
779: {
780: return handleEvent (e);
781: }
782:
783:
793: public boolean isShowing()
794: {
795: return isVisible();
796: }
797:
798: public void setLocationRelativeTo(Component c)
799: {
800: int x = 0;
801: int y = 0;
802:
803: if (c == null || !c.isShowing())
804: {
805: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
806: Point center = ge.getCenterPoint();
807: x = center.x - (width / 2);
808: y = center.y - (height / 2);
809: }
810: else
811: {
812: int cWidth = c.getWidth();
813: int cHeight = c.getHeight();
814: Dimension screenSize = getToolkit().getScreenSize();
815:
816: x = c.getLocationOnScreen().x;
817: y = c.getLocationOnScreen().y;
818:
819:
820:
821: if ((y + cHeight) > screenSize.height)
822: {
823:
824: if ((screenSize.width / 2 - x) <= 0)
825: {
826: if ((x - width) >= 0)
827: x -= width;
828: else
829: x = 0;
830: }
831: else
832: {
833: if ((x + cWidth + width) <= screenSize.width)
834: x += cWidth;
835: else
836: x = screenSize.width - width;
837: }
838:
839: y = screenSize.height - height;
840: }
841: else if (cWidth > width || cHeight > height)
842: {
843:
844: if ((x + width) > screenSize.width)
845: x = screenSize.width - width;
846:
847: else if (x < 0)
848: x = 0;
849: else
850: x += (cWidth - width) / 2;
851:
852: y += (cHeight - height) / 2;
853: }
854: else
855: {
856:
857: if ((x + width) > screenSize.width)
858: x = screenSize.width - width;
859:
860: else if (x < 0 || (x - (width - cWidth) / 2) < 0)
861: x = 0;
862: else
863: x -= (width - cWidth) / 2;
864:
865: if ((y - (height - cHeight) / 2) > 0)
866: y -= (height - cHeight) / 2;
867: else
868: y = 0;
869: }
870: }
871:
872: setLocation(x, y);
873: }
874:
875:
878: private class WindowBltBufferStrategy extends BltBufferStrategy
879: {
880:
887: WindowBltBufferStrategy(int numBuffers, boolean accelerated)
888: {
889: super(numBuffers,
890: new BufferCapabilities(new ImageCapabilities(accelerated),
891: new ImageCapabilities(accelerated),
892: BufferCapabilities.FlipContents.COPIED));
893: }
894: }
895:
896:
899: private class WindowFlipBufferStrategy extends FlipBufferStrategy
900: {
901:
909: WindowFlipBufferStrategy(int numBuffers)
910: throws AWTException
911: {
912: super(numBuffers,
913: new BufferCapabilities(new ImageCapabilities(true),
914: new ImageCapabilities(true),
915: BufferCapabilities.FlipContents.COPIED));
916: }
917: }
918:
919:
942: public void createBufferStrategy(int numBuffers)
943: {
944: if (numBuffers < 1)
945: throw new IllegalArgumentException("Window.createBufferStrategy: number"
946: + " of buffers is less than one");
947:
948: if (!isDisplayable())
949: throw new IllegalStateException("Window.createBufferStrategy: window is"
950: + " not displayable");
951:
952: BufferStrategy newStrategy = null;
953:
954:
955: try
956: {
957: newStrategy = new WindowFlipBufferStrategy(numBuffers);
958: }
959: catch (AWTException e)
960: {
961: }
962:
963:
964: if (newStrategy == null)
965: newStrategy = new WindowBltBufferStrategy(numBuffers, true);
966:
967: bufferStrategy = newStrategy;
968: }
969:
970:
989: public void createBufferStrategy(int numBuffers, BufferCapabilities caps)
990: throws AWTException
991: {
992: if (numBuffers < 1)
993: throw new IllegalArgumentException("Window.createBufferStrategy: number"
994: + " of buffers is less than one");
995:
996: if (caps == null)
997: throw new IllegalArgumentException("Window.createBufferStrategy:"
998: + " capabilities object is null");
999:
1000:
1001: if (caps.isPageFlipping())
1002: bufferStrategy = new WindowFlipBufferStrategy(numBuffers);
1003: else
1004: bufferStrategy = new WindowBltBufferStrategy(numBuffers, true);
1005: }
1006:
1007:
1013: public BufferStrategy getBufferStrategy()
1014: {
1015: return bufferStrategy;
1016: }
1017:
1018:
1023: public void applyResourceBundle(ResourceBundle rb)
1024: {
1025: applyComponentOrientation(ComponentOrientation.getOrientation(rb));
1026: }
1027:
1028:
1033: public void applyResourceBundle(String rbName)
1034: {
1035: ResourceBundle rb = ResourceBundle.getBundle(rbName, Locale.getDefault(),
1036: ClassLoader.getSystemClassLoader());
1037: if (rb != null)
1038: applyResourceBundle(rb);
1039: }
1040:
1041:
1047: public AccessibleContext getAccessibleContext()
1048: {
1049:
1050: if (accessibleContext == null)
1051: accessibleContext = new AccessibleAWTWindow();
1052: return accessibleContext;
1053: }
1054:
1055:
1060: public GraphicsConfiguration getGraphicsConfiguration()
1061: {
1062: if (graphicsConfiguration != null) return graphicsConfiguration;
1063: if (peer != null) return peer.getGraphicsConfiguration();
1064: return null;
1065: }
1066:
1067: protected void processWindowFocusEvent(WindowEvent event)
1068: {
1069: if (windowFocusListener != null)
1070: {
1071: switch (event.getID ())
1072: {
1073: case WindowEvent.WINDOW_GAINED_FOCUS:
1074: windowFocusListener.windowGainedFocus (event);
1075: break;
1076:
1077: case WindowEvent.WINDOW_LOST_FOCUS:
1078: windowFocusListener.windowLostFocus (event);
1079: break;
1080:
1081: default:
1082: break;
1083: }
1084: }
1085: }
1086:
1087:
1090: protected void processWindowStateEvent(WindowEvent event)
1091: {
1092: if (windowStateListener != null
1093: && event.getID () == WindowEvent.WINDOW_STATE_CHANGED)
1094: windowStateListener.windowStateChanged (event);
1095: }
1096:
1097:
1102: public final boolean isFocusableWindow ()
1103: {
1104: if (getFocusableWindowState () == false)
1105: return false;
1106:
1107: if (this instanceof Dialog
1108: || this instanceof Frame)
1109: return true;
1110:
1111:
1112:
1113: return false;
1114: }
1115:
1116:
1121: public boolean getFocusableWindowState ()
1122: {
1123: return focusableWindowState;
1124: }
1125:
1126:
1131: public void setFocusableWindowState (boolean focusableWindowState)
1132: {
1133: this.focusableWindowState = focusableWindowState;
1134: }
1135:
1136:
1145: public final boolean isFocusCycleRoot()
1146: {
1147: return true;
1148: }
1149:
1150:
1159: public final void setFocusCycleRoot(boolean focusCycleRoot)
1160: {
1161:
1162: }
1163:
1164:
1172: public final Container getFocusCycleRootAncestor()
1173: {
1174: return null;
1175: }
1176:
1177:
1185: public final boolean isAlwaysOnTop()
1186: {
1187: return alwaysOnTop;
1188: }
1189:
1190:
1205: public final void setAlwaysOnTop(boolean alwaysOnTop)
1206: {
1207: SecurityManager sm = System.getSecurityManager();
1208: if (sm != null)
1209: sm.checkPermission( new AWTPermission("setWindowAlwaysOnTop") );
1210:
1211: if( this.alwaysOnTop == alwaysOnTop )
1212: return;
1213:
1214: if( alwaysOnTop )
1215: toFront();
1216:
1217: firePropertyChange("alwaysOnTop", this.alwaysOnTop, alwaysOnTop );
1218: this.alwaysOnTop = alwaysOnTop;
1219:
1220: if (peer != null)
1221: ( (WindowPeer) peer).updateAlwaysOnTop();
1222: else
1223: System.out.println("Null peer?!");
1224: }
1225:
1226:
1231: String generateName()
1232: {
1233: return "win" + getUniqueLong();
1234: }
1235:
1236: private static synchronized long getUniqueLong()
1237: {
1238: return next_window_number++;
1239: }
1240: }