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: addWindowFocusListener();
134:
135: GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
136: graphicsConfiguration = g.getDefaultScreenDevice().getDefaultConfiguration();
137: }
138:
139: Window(GraphicsConfiguration gc)
140: {
141: this();
142: graphicsConfiguration = gc;
143: }
144:
145: private void addWindowFocusListener()
146: {
147: addWindowFocusListener(new WindowAdapter()
148: {
149: public void windowGainedFocus(WindowEvent event)
150: {
151: EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
152: if (windowFocusOwner != null)
153: {
154: synchronized (eq)
155: {
156: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
157: Component currentFocusOwner = manager.getGlobalPermanentFocusOwner();
158: if (currentFocusOwner != null)
159: {
160: eq.postEvent(new FocusEvent(currentFocusOwner,
161: FocusEvent.FOCUS_LOST, false,
162: windowFocusOwner));
163: eq.postEvent(new FocusEvent(windowFocusOwner,
164: FocusEvent.FOCUS_GAINED, false,
165: currentFocusOwner));
166: }
167: else
168: eq.postEvent(new FocusEvent(windowFocusOwner,
169: FocusEvent.FOCUS_GAINED, false));
170: }
171: }
172: else
173: eq.postEvent(new FocusEvent(Window.this, FocusEvent.FOCUS_GAINED,
174: false));
175: }
176:
177: public void windowLostFocus(WindowEvent event)
178: {
179: EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
180: if (windowFocusOwner != null)
181: {
182: synchronized (eq)
183: {
184: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
185: Component currentFocusOwner = manager.getGlobalPermanentFocusOwner();
186: if (currentFocusOwner != null)
187: {
188: eq.postEvent(new FocusEvent(currentFocusOwner,
189: FocusEvent.FOCUS_GAINED, false,
190: windowFocusOwner));
191: eq.postEvent(new FocusEvent(windowFocusOwner,
192: FocusEvent.FOCUS_LOST, false,
193: currentFocusOwner));
194: }
195: else
196: eq.postEvent(new FocusEvent(windowFocusOwner,
197: FocusEvent.FOCUS_LOST, false));
198: }
199: }
200: else
201: eq.postEvent(new FocusEvent(Window.this, FocusEvent.FOCUS_LOST, false));
202: }
203: });
204: }
205:
206:
216: public Window(Frame owner)
217: {
218: this (owner, owner.getGraphicsConfiguration ());
219: }
220:
221:
231: public Window(Window owner)
232: {
233: this (owner, owner.getGraphicsConfiguration ());
234: }
235:
236:
246: public Window(Window owner, GraphicsConfiguration gc)
247: {
248: this ();
249:
250: synchronized (getTreeLock())
251: {
252: if (owner == null)
253: throw new IllegalArgumentException ("owner must not be null");
254:
255: parent = owner;
256: owner.ownedWindows.add(new WeakReference(this));
257: }
258:
259:
260: SecurityManager s = System.getSecurityManager();
261: if (s != null && ! s.checkTopLevelWindow(this))
262: warningString = System.getProperty("awt.appletWarning");
263:
264: if (gc != null
265: && gc.getDevice().getType() != GraphicsDevice.TYPE_RASTER_SCREEN)
266: throw new IllegalArgumentException ("gc must be from a screen device");
267:
268: if (gc == null)
269: graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment()
270: .getDefaultScreenDevice()
271: .getDefaultConfiguration();
272: else
273: graphicsConfiguration = gc;
274: }
275:
276: GraphicsConfiguration getGraphicsConfigurationImpl()
277: {
278: if (graphicsConfiguration != null)
279: return graphicsConfiguration;
280:
281: return super.getGraphicsConfigurationImpl();
282: }
283:
284:
287: public void addNotify()
288: {
289: if (peer == null)
290: peer = getToolkit().createWindow(this);
291: super.addNotify();
292: }
293:
294:
300: public void pack()
301: {
302: if (parent != null && !parent.isDisplayable())
303: parent.addNotify();
304: if (peer == null)
305: addNotify();
306:
307: setSize(getPreferredSize());
308:
309: validate();
310: }
311:
312:
316: public void show()
317: {
318: synchronized (getTreeLock())
319: {
320: if (parent != null && ! parent.isDisplayable())
321: parent.addNotify();
322: if (peer == null)
323: addNotify();
324:
325: validate();
326: if (visible)
327: toFront();
328: else
329: {
330: super.show();
331:
332: Iterator e = ownedWindows.iterator();
333: while (e.hasNext())
334: {
335: Window w = (Window) (((Reference) e.next()).get());
336: if (w != null)
337: {
338: if (w.isVisible())
339: w.getPeer().setVisible(true);
340: }
341: else
342:
343:
344:
345:
346: e.remove();
347: }
348: }
349: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
350: manager.setGlobalFocusedWindow(this);
351:
352: if (! shown)
353: {
354: FocusTraversalPolicy policy = getFocusTraversalPolicy();
355: Component initialFocusOwner = null;
356:
357: if (policy != null)
358: initialFocusOwner = policy.getInitialComponent(this);
359:
360: if (initialFocusOwner != null)
361: initialFocusOwner.requestFocusInWindow();
362:
363: shown = true;
364: }
365: }
366: }
367:
368: public void hide()
369: {
370:
371: synchronized (getTreeLock ())
372: {
373: Iterator e = ownedWindows.iterator();
374: while(e.hasNext())
375: {
376: Window w = (Window)(((Reference) e.next()).get());
377: if (w != null)
378: {
379: if (w.isVisible() && w.getPeer() != null)
380: w.getPeer().setVisible(false);
381: }
382: else
383: e.remove();
384: }
385: }
386: super.hide();
387: }
388:
389:
393: public void dispose()
394: {
395: hide();
396:
397: synchronized (getTreeLock ())
398: {
399: Iterator e = ownedWindows.iterator();
400: while(e.hasNext())
401: {
402: Window w = (Window)(((Reference) e.next()).get());
403: if (w != null)
404: w.dispose();
405: else
406:
407: e.remove();
408: }
409:
410: for (int i = 0; i < ncomponents; ++i)
411: component[i].removeNotify();
412: this.removeNotify();
413:
414:
415: WindowEvent we = new WindowEvent(this, WindowEvent.WINDOW_CLOSED);
416: getToolkit().getSystemEventQueue().postEvent(we);
417: }
418: }
419:
420:
424: public void toBack()
425: {
426: if (peer != null)
427: {
428: WindowPeer wp = (WindowPeer) peer;
429: wp.toBack();
430: }
431: }
432:
433:
437: public void toFront()
438: {
439: if (peer != null)
440: {
441: WindowPeer wp = (WindowPeer) peer;
442: wp.toFront();
443: }
444: }
445:
446:
454: public Toolkit getToolkit()
455: {
456: return Toolkit.getDefaultToolkit();
457: }
458:
459:
465: public final String getWarningString()
466: {
467: return warningString;
468: }
469:
470:
475: public Locale getLocale()
476: {
477: return locale == null ? Locale.getDefault() : locale;
478: }
479:
480:
487:
488:
493: public void setCursor(Cursor cursor)
494: {
495: super.setCursor(cursor);
496: }
497:
498: public Window getOwner()
499: {
500: return (Window) parent;
501: }
502:
503:
504: public Window[] getOwnedWindows()
505: {
506: Window [] trimmedList;
507: synchronized (getTreeLock ())
508: {
509:
510: Window [] validList = new Window [ownedWindows.size()];
511:
512: Iterator e = ownedWindows.iterator();
513: int numValid = 0;
514: while (e.hasNext())
515: {
516: Window w = (Window)(((Reference) e.next()).get());
517: if (w != null)
518: validList[numValid++] = w;
519: else
520:
521: e.remove();
522: }
523:
524: if (numValid != validList.length)
525: {
526: trimmedList = new Window [numValid];
527: System.arraycopy (validList, 0, trimmedList, 0, numValid);
528: }
529: else
530: trimmedList = validList;
531: }
532: return trimmedList;
533: }
534:
535:
541: public synchronized void addWindowListener(WindowListener listener)
542: {
543: windowListener = AWTEventMulticaster.add(windowListener, listener);
544: }
545:
546:
552: public synchronized void removeWindowListener(WindowListener listener)
553: {
554: windowListener = AWTEventMulticaster.remove(windowListener, listener);
555: }
556:
557:
562: public synchronized WindowListener[] getWindowListeners()
563: {
564: return (WindowListener[])
565: AWTEventMulticaster.getListeners(windowListener,
566: WindowListener.class);
567: }
568:
569:
575: public synchronized WindowFocusListener[] getWindowFocusListeners()
576: {
577: return (WindowFocusListener[])
578: AWTEventMulticaster.getListeners(windowFocusListener,
579: WindowFocusListener.class);
580: }
581:
582:
588: public synchronized WindowStateListener[] getWindowStateListeners()
589: {
590: return (WindowStateListener[])
591: AWTEventMulticaster.getListeners(windowStateListener,
592: WindowStateListener.class);
593: }
594:
595:
598: public void addWindowFocusListener (WindowFocusListener wfl)
599: {
600: windowFocusListener = AWTEventMulticaster.add (windowFocusListener, wfl);
601: }
602:
603:
608: public void addWindowStateListener (WindowStateListener wsl)
609: {
610: windowStateListener = AWTEventMulticaster.add (windowStateListener, wsl);
611: }
612:
613:
616: public void removeWindowFocusListener (WindowFocusListener wfl)
617: {
618: windowFocusListener = AWTEventMulticaster.remove (windowFocusListener, wfl);
619: }
620:
621:
626: public void removeWindowStateListener (WindowStateListener wsl)
627: {
628: windowStateListener = AWTEventMulticaster.remove (windowStateListener, wsl);
629: }
630:
631:
641: public EventListener[] getListeners(Class listenerType)
642: {
643: if (listenerType == WindowListener.class)
644: return getWindowListeners();
645: return super.getListeners(listenerType);
646: }
647:
648: void dispatchEventImpl(AWTEvent e)
649: {
650:
651: if (e.id <= WindowEvent.WINDOW_LAST
652: && e.id >= WindowEvent.WINDOW_FIRST
653: && (windowListener != null
654: || windowFocusListener != null
655: || windowStateListener != null
656: || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0))
657: processEvent(e);
658: else
659: {
660: if (peer != null && (e.id == ComponentEvent.COMPONENT_RESIZED
661: || e.id == ComponentEvent.COMPONENT_MOVED))
662: {
663: Rectangle bounds = peer.getBounds();
664: x = bounds.x;
665: y = bounds.y;
666: height = bounds.height;
667: width = bounds.width;
668:
669: if (e.id == ComponentEvent.COMPONENT_RESIZED)
670: {
671: invalidate();
672: validate();
673: }
674: }
675: super.dispatchEventImpl(e);
676: }
677: }
678:
679:
687: protected void processEvent(AWTEvent evt)
688: {
689: if (evt instanceof WindowEvent)
690: processWindowEvent((WindowEvent) evt);
691: else
692: super.processEvent(evt);
693: }
694:
695:
703: protected void processWindowEvent(WindowEvent evt)
704: {
705: int id = evt.getID();
706:
707: if (id == WindowEvent.WINDOW_GAINED_FOCUS
708: || id == WindowEvent.WINDOW_LOST_FOCUS)
709: processWindowFocusEvent (evt);
710: else if (id == WindowEvent.WINDOW_STATE_CHANGED)
711: processWindowStateEvent (evt);
712: else
713: {
714: if (windowListener != null)
715: {
716: switch (evt.getID())
717: {
718: case WindowEvent.WINDOW_ACTIVATED:
719: windowListener.windowActivated(evt);
720: break;
721:
722: case WindowEvent.WINDOW_CLOSED:
723: windowListener.windowClosed(evt);
724: break;
725:
726: case WindowEvent.WINDOW_CLOSING:
727: windowListener.windowClosing(evt);
728: break;
729:
730: case WindowEvent.WINDOW_DEACTIVATED:
731: windowListener.windowDeactivated(evt);
732: break;
733:
734: case WindowEvent.WINDOW_DEICONIFIED:
735: windowListener.windowDeiconified(evt);
736: break;
737:
738: case WindowEvent.WINDOW_ICONIFIED:
739: windowListener.windowIconified(evt);
740: break;
741:
742: case WindowEvent.WINDOW_OPENED:
743: windowListener.windowOpened(evt);
744: break;
745:
746: default:
747: break;
748: }
749: }
750: }
751: }
752:
753:
760: public boolean isActive()
761: {
762: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
763: return manager.getActiveWindow() == this;
764: }
765:
766:
773: public boolean isFocused()
774: {
775: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
776: return manager.getFocusedWindow() == this;
777: }
778:
779:
787: public Component getFocusOwner ()
788: {
789: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
790:
791: Window activeWindow = manager.getActiveWindow ();
792:
793:
794: if (activeWindow == this)
795: return manager.getFocusOwner ();
796: else
797: return null;
798: }
799:
800:
813: public Component getMostRecentFocusOwner ()
814: {
815: return windowFocusOwner;
816: }
817:
818:
827: void setFocusOwner (Component windowFocusOwner)
828: {
829: this.windowFocusOwner = windowFocusOwner;
830: }
831:
832:
839: public boolean postEvent(Event e)
840: {
841: return handleEvent (e);
842: }
843:
844:
854: public boolean isShowing()
855: {
856: return isVisible();
857: }
858:
859: public void setLocationRelativeTo(Component c)
860: {
861: int x = 0;
862: int y = 0;
863:
864: if (c == null || !c.isShowing())
865: {
866: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
867: Point center = ge.getCenterPoint();
868: x = center.x - (width / 2);
869: y = center.y - (height / 2);
870: }
871: else
872: {
873: int cWidth = c.getWidth();
874: int cHeight = c.getHeight();
875: Dimension screenSize = getToolkit().getScreenSize();
876:
877: x = c.getLocationOnScreen().x;
878: y = c.getLocationOnScreen().y;
879:
880:
881:
882: if ((y + cHeight) > screenSize.height)
883: {
884:
885: if ((screenSize.width / 2 - x) <= 0)
886: {
887: if ((x - width) >= 0)
888: x -= width;
889: else
890: x = 0;
891: }
892: else
893: {
894: if ((x + cWidth + width) <= screenSize.width)
895: x += cWidth;
896: else
897: x = screenSize.width - width;
898: }
899:
900: y = screenSize.height - height;
901: }
902: else if (cWidth > width || cHeight > height)
903: {
904:
905: if ((x + width) > screenSize.width)
906: x = screenSize.width - width;
907:
908: else if (x < 0)
909: x = 0;
910: else
911: x += (cWidth - width) / 2;
912:
913: y += (cHeight - height) / 2;
914: }
915: else
916: {
917:
918: if ((x + width) > screenSize.width)
919: x = screenSize.width - width;
920:
921: else if (x < 0 || (x - (width - cWidth) / 2) < 0)
922: x = 0;
923: else
924: x -= (width - cWidth) / 2;
925:
926: if ((y - (height - cHeight) / 2) > 0)
927: y -= (height - cHeight) / 2;
928: else
929: y = 0;
930: }
931: }
932:
933: setLocation(x, y);
934: }
935:
936:
939: private class WindowBltBufferStrategy extends BltBufferStrategy
940: {
941:
948: WindowBltBufferStrategy(int numBuffers, boolean accelerated)
949: {
950: super(numBuffers,
951: new BufferCapabilities(new ImageCapabilities(accelerated),
952: new ImageCapabilities(accelerated),
953: BufferCapabilities.FlipContents.COPIED));
954: }
955: }
956:
957:
960: private class WindowFlipBufferStrategy extends FlipBufferStrategy
961: {
962:
970: WindowFlipBufferStrategy(int numBuffers)
971: throws AWTException
972: {
973: super(numBuffers,
974: new BufferCapabilities(new ImageCapabilities(true),
975: new ImageCapabilities(true),
976: BufferCapabilities.FlipContents.COPIED));
977: }
978: }
979:
980:
1003: public void createBufferStrategy(int numBuffers)
1004: {
1005: if (numBuffers < 1)
1006: throw new IllegalArgumentException("Window.createBufferStrategy: number"
1007: + " of buffers is less than one");
1008:
1009: if (!isDisplayable())
1010: throw new IllegalStateException("Window.createBufferStrategy: window is"
1011: + " not displayable");
1012:
1013: BufferStrategy newStrategy = null;
1014:
1015:
1016: try
1017: {
1018: newStrategy = new WindowFlipBufferStrategy(numBuffers);
1019: }
1020: catch (AWTException e)
1021: {
1022: }
1023:
1024:
1025: if (newStrategy == null)
1026: newStrategy = new WindowBltBufferStrategy(numBuffers, true);
1027:
1028: bufferStrategy = newStrategy;
1029: }
1030:
1031:
1050: public void createBufferStrategy(int numBuffers, BufferCapabilities caps)
1051: throws AWTException
1052: {
1053: if (numBuffers < 1)
1054: throw new IllegalArgumentException("Window.createBufferStrategy: number"
1055: + " of buffers is less than one");
1056:
1057: if (caps == null)
1058: throw new IllegalArgumentException("Window.createBufferStrategy:"
1059: + " capabilities object is null");
1060:
1061:
1062: if (caps.isPageFlipping())
1063: bufferStrategy = new WindowFlipBufferStrategy(numBuffers);
1064: else
1065: bufferStrategy = new WindowBltBufferStrategy(numBuffers, true);
1066: }
1067:
1068:
1074: public BufferStrategy getBufferStrategy()
1075: {
1076: return bufferStrategy;
1077: }
1078:
1079:
1084: public void applyResourceBundle(ResourceBundle rb)
1085: {
1086: applyComponentOrientation(ComponentOrientation.getOrientation(rb));
1087: }
1088:
1089:
1094: public void applyResourceBundle(String rbName)
1095: {
1096: ResourceBundle rb = ResourceBundle.getBundle(rbName, Locale.getDefault(),
1097: ClassLoader.getSystemClassLoader());
1098: if (rb != null)
1099: applyResourceBundle(rb);
1100: }
1101:
1102:
1108: public AccessibleContext getAccessibleContext()
1109: {
1110:
1111: if (accessibleContext == null)
1112: accessibleContext = new AccessibleAWTWindow();
1113: return accessibleContext;
1114: }
1115:
1116:
1121: public GraphicsConfiguration getGraphicsConfiguration()
1122: {
1123: if (graphicsConfiguration != null) return graphicsConfiguration;
1124: if (peer != null) return peer.getGraphicsConfiguration();
1125: return null;
1126: }
1127:
1128: protected void processWindowFocusEvent(WindowEvent event)
1129: {
1130: if (windowFocusListener != null)
1131: {
1132: switch (event.getID ())
1133: {
1134: case WindowEvent.WINDOW_GAINED_FOCUS:
1135: windowFocusListener.windowGainedFocus (event);
1136: break;
1137:
1138: case WindowEvent.WINDOW_LOST_FOCUS:
1139: windowFocusListener.windowLostFocus (event);
1140: break;
1141:
1142: default:
1143: break;
1144: }
1145: }
1146: }
1147:
1148:
1151: protected void processWindowStateEvent(WindowEvent event)
1152: {
1153: if (windowStateListener != null
1154: && event.getID () == WindowEvent.WINDOW_STATE_CHANGED)
1155: windowStateListener.windowStateChanged (event);
1156: }
1157:
1158:
1163: public final boolean isFocusableWindow ()
1164: {
1165: if (getFocusableWindowState () == false)
1166: return false;
1167:
1168: if (this instanceof Dialog
1169: || this instanceof Frame)
1170: return true;
1171:
1172:
1173:
1174: return false;
1175: }
1176:
1177:
1182: public boolean getFocusableWindowState ()
1183: {
1184: return focusableWindowState;
1185: }
1186:
1187:
1192: public void setFocusableWindowState (boolean focusableWindowState)
1193: {
1194: this.focusableWindowState = focusableWindowState;
1195: }
1196:
1197:
1206: public final boolean isFocusCycleRoot()
1207: {
1208: return true;
1209: }
1210:
1211:
1220: public final void setFocusCycleRoot(boolean focusCycleRoot)
1221: {
1222:
1223: }
1224:
1225:
1233: public final Container getFocusCycleRootAncestor()
1234: {
1235: return null;
1236: }
1237:
1238:
1243: String generateName()
1244: {
1245: return "win" + getUniqueLong();
1246: }
1247:
1248: private static synchronized long getUniqueLong()
1249: {
1250: return next_window_number++;
1251: }
1252: }