1:
37:
38:
39: package ;
40:
41: import ;
42:
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: import ;
58: import ;
59:
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65:
66:
72: public class Window extends Container implements Accessible
73: {
74: private static final long serialVersionUID = 4497834738069338734L;
75:
76:
77: private String warningString = null;
78: private int windowSerializedDataVersion = 0;
79:
80:
81:
82: private int state = 0;
83:
84: private boolean focusableWindowState = true;
85:
86:
87: private transient Vector ownedWindows = new Vector();
88:
89: private transient WindowListener windowListener;
90: private transient WindowFocusListener windowFocusListener;
91: private transient WindowStateListener windowStateListener;
92: private transient GraphicsConfiguration graphicsConfiguration;
93:
94: private transient boolean shown;
95:
96:
97: transient Component windowFocusOwner;
98:
99:
102: private static transient long next_window_number;
103:
104: protected class AccessibleAWTWindow extends AccessibleAWTContainer
105: {
106: private static final long serialVersionUID = 4215068635060671780L;
107:
108: public AccessibleRole getAccessibleRole()
109: {
110: return AccessibleRole.WINDOW;
111: }
112:
113: public AccessibleStateSet getAccessibleStateSet()
114: {
115: AccessibleStateSet states = super.getAccessibleStateSet();
116: if (isActive())
117: states.add(AccessibleState.ACTIVE);
118: return states;
119: }
120: }
121:
122:
128: Window()
129: {
130: visible = false;
131:
132:
133: focusCycleRoot = true;
134: setLayout(new BorderLayout());
135:
136: addWindowFocusListener (new WindowAdapter ()
137: {
138: public void windowGainedFocus (WindowEvent event)
139: {
140: if (windowFocusOwner != null)
141: {
142:
143:
144: EventQueue eq = Toolkit.getDefaultToolkit ().getSystemEventQueue ();
145: synchronized (eq)
146: {
147: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
148: Component currentFocusOwner = manager.getGlobalPermanentFocusOwner ();
149: if (currentFocusOwner != null)
150: {
151: eq.postEvent (new FocusEvent (currentFocusOwner, FocusEvent.FOCUS_LOST,
152: false, windowFocusOwner));
153: eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED,
154: false, currentFocusOwner));
155: }
156: else
157: eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED, false));
158: }
159: }
160: }
161: });
162:
163: GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
164: graphicsConfiguration = g.getDefaultScreenDevice().getDefaultConfiguration();
165: }
166:
167: Window(GraphicsConfiguration gc)
168: {
169: this();
170: graphicsConfiguration = gc;
171: }
172:
173:
183: public Window(Frame owner)
184: {
185: this (owner, owner.getGraphicsConfiguration ());
186: }
187:
188:
198: public Window(Window owner)
199: {
200: this (owner, owner.getGraphicsConfiguration ());
201: }
202:
203:
213: public Window(Window owner, GraphicsConfiguration gc)
214: {
215: this ();
216:
217: synchronized (getTreeLock())
218: {
219: if (owner == null)
220: throw new IllegalArgumentException ("owner must not be null");
221:
222: parent = owner;
223: owner.ownedWindows.add(new WeakReference(this));
224: }
225:
226:
227: SecurityManager s = System.getSecurityManager();
228: if (s != null && ! s.checkTopLevelWindow(this))
229: warningString = System.getProperty("awt.appletWarning");
230:
231: if (gc != null
232: && gc.getDevice().getType() != GraphicsDevice.TYPE_RASTER_SCREEN)
233: throw new IllegalArgumentException ("gc must be from a screen device");
234:
235: if (gc == null)
236: graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment()
237: .getDefaultScreenDevice()
238: .getDefaultConfiguration();
239: else
240: graphicsConfiguration = gc;
241: }
242:
243: GraphicsConfiguration getGraphicsConfigurationImpl()
244: {
245: if (graphicsConfiguration != null)
246: return graphicsConfiguration;
247:
248: return super.getGraphicsConfigurationImpl();
249: }
250:
251:
254: public void addNotify()
255: {
256: if (peer == null)
257: peer = getToolkit().createWindow(this);
258: super.addNotify();
259: }
260:
261:
267: public void pack()
268: {
269: if (parent != null && !parent.isDisplayable())
270: parent.addNotify();
271: if (peer == null)
272: addNotify();
273:
274: setSize(getPreferredSize());
275:
276: validate();
277: }
278:
279:
283: public void show()
284: {
285: synchronized (getTreeLock())
286: {
287: if (parent != null && ! parent.isDisplayable())
288: parent.addNotify();
289: if (peer == null)
290: addNotify();
291:
292: validate();
293: if (visible)
294: toFront();
295: else
296: {
297: super.show();
298:
299: Iterator e = ownedWindows.iterator();
300: while (e.hasNext())
301: {
302: Window w = (Window) (((Reference) e.next()).get());
303: if (w != null)
304: {
305: if (w.isVisible())
306: w.getPeer().setVisible(true);
307: }
308: else
309:
310:
311:
312:
313: e.remove();
314: }
315: }
316: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
317: manager.setGlobalFocusedWindow(this);
318:
319: if (! shown)
320: {
321: FocusTraversalPolicy policy = getFocusTraversalPolicy();
322: Component initialFocusOwner = null;
323:
324: if (policy != null)
325: initialFocusOwner = policy.getInitialComponent(this);
326:
327: if (initialFocusOwner != null)
328: initialFocusOwner.requestFocusInWindow();
329:
330: shown = true;
331: }
332: }
333: }
334:
335: public void hide()
336: {
337:
338: synchronized (getTreeLock ())
339: {
340: Iterator e = ownedWindows.iterator();
341: while(e.hasNext())
342: {
343: Window w = (Window)(((Reference) e.next()).get());
344: if (w != null)
345: {
346: if (w.isVisible() && w.getPeer() != null)
347: w.getPeer().setVisible(false);
348: }
349: else
350: e.remove();
351: }
352: }
353: super.hide();
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
626: {
627: if (peer != null && (e.id == ComponentEvent.COMPONENT_RESIZED
628: || e.id == ComponentEvent.COMPONENT_MOVED))
629: {
630: Rectangle bounds = peer.getBounds();
631: x = bounds.x;
632: y = bounds.y;
633: height = bounds.height;
634: width = bounds.width;
635:
636: if (e.id == ComponentEvent.COMPONENT_RESIZED)
637: {
638: invalidate();
639: validate();
640: }
641: }
642: super.dispatchEventImpl(e);
643: }
644: }
645:
646:
654: protected void processEvent(AWTEvent evt)
655: {
656: if (evt instanceof WindowEvent)
657: processWindowEvent((WindowEvent) evt);
658: else
659: super.processEvent(evt);
660: }
661:
662:
670: protected void processWindowEvent(WindowEvent evt)
671: {
672: int id = evt.getID();
673:
674: if (id == WindowEvent.WINDOW_GAINED_FOCUS
675: || id == WindowEvent.WINDOW_LOST_FOCUS)
676: processWindowFocusEvent (evt);
677: else if (id == WindowEvent.WINDOW_STATE_CHANGED)
678: processWindowStateEvent (evt);
679: else
680: {
681: if (windowListener != null)
682: {
683: switch (evt.getID())
684: {
685: case WindowEvent.WINDOW_ACTIVATED:
686: windowListener.windowActivated(evt);
687: break;
688:
689: case WindowEvent.WINDOW_CLOSED:
690: windowListener.windowClosed(evt);
691: break;
692:
693: case WindowEvent.WINDOW_CLOSING:
694: windowListener.windowClosing(evt);
695: break;
696:
697: case WindowEvent.WINDOW_DEACTIVATED:
698: windowListener.windowDeactivated(evt);
699: break;
700:
701: case WindowEvent.WINDOW_DEICONIFIED:
702: windowListener.windowDeiconified(evt);
703: break;
704:
705: case WindowEvent.WINDOW_ICONIFIED:
706: windowListener.windowIconified(evt);
707: break;
708:
709: case WindowEvent.WINDOW_OPENED:
710: windowListener.windowOpened(evt);
711: break;
712:
713: default:
714: break;
715: }
716: }
717: }
718: }
719:
720:
727: public boolean isActive()
728: {
729: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
730: return manager.getActiveWindow() == this;
731: }
732:
733:
740: public boolean isFocused()
741: {
742: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
743: return manager.getFocusedWindow() == this;
744: }
745:
746:
754: public Component getFocusOwner ()
755: {
756: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
757:
758: Window activeWindow = manager.getActiveWindow ();
759:
760:
761: if (activeWindow == this)
762: return manager.getFocusOwner ();
763: else
764: return null;
765: }
766:
767:
780: public Component getMostRecentFocusOwner ()
781: {
782: return windowFocusOwner;
783: }
784:
785:
794: void setFocusOwner (Component windowFocusOwner)
795: {
796: this.windowFocusOwner = windowFocusOwner;
797: }
798:
799:
806: public boolean postEvent(Event e)
807: {
808: return handleEvent (e);
809: }
810:
811:
821: public boolean isShowing()
822: {
823: return isVisible();
824: }
825:
826: public void setLocationRelativeTo(Component c)
827: {
828: int x = 0;
829: int y = 0;
830:
831: if (c == null || !c.isShowing())
832: {
833: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
834: Point center = ge.getCenterPoint();
835: x = center.x - (width / 2);
836: y = center.y - (height / 2);
837: }
838: else
839: {
840: int cWidth = c.getWidth();
841: int cHeight = c.getHeight();
842: Dimension screenSize = getToolkit().getScreenSize();
843:
844: x = c.getLocationOnScreen().x;
845: y = c.getLocationOnScreen().y;
846:
847:
848:
849: if ((y + cHeight) > screenSize.height)
850: {
851:
852: if ((screenSize.width / 2 - x) <= 0)
853: {
854: if ((x - width) >= 0)
855: x -= width;
856: else
857: x = 0;
858: }
859: else
860: {
861: if ((x + cWidth + width) <= screenSize.width)
862: x += cWidth;
863: else
864: x = screenSize.width - width;
865: }
866:
867: y = screenSize.height - height;
868: }
869: else if (cWidth > width || cHeight > height)
870: {
871:
872: if ((x + width) > screenSize.width)
873: x = screenSize.width - width;
874:
875: else if (x < 0)
876: x = 0;
877: else
878: x += (cWidth - width) / 2;
879:
880: y += (cHeight - height) / 2;
881: }
882: else
883: {
884:
885: if ((x + width) > screenSize.width)
886: x = screenSize.width - width;
887:
888: else if (x < 0 || (x - (width - cWidth) / 2) < 0)
889: x = 0;
890: else
891: x -= (width - cWidth) / 2;
892:
893: if ((y - (height - cHeight) / 2) > 0)
894: y -= (height - cHeight) / 2;
895: else
896: y = 0;
897: }
898: }
899:
900: setLocation(x, y);
901: }
902:
903:
906: private class WindowBltBufferStrategy extends BltBufferStrategy
907: {
908:
915: WindowBltBufferStrategy(int numBuffers, boolean accelerated)
916: {
917: super(numBuffers,
918: new BufferCapabilities(new ImageCapabilities(accelerated),
919: new ImageCapabilities(accelerated),
920: BufferCapabilities.FlipContents.COPIED));
921: }
922: }
923:
924:
927: private class WindowFlipBufferStrategy extends FlipBufferStrategy
928: {
929:
937: WindowFlipBufferStrategy(int numBuffers)
938: throws AWTException
939: {
940: super(numBuffers,
941: new BufferCapabilities(new ImageCapabilities(true),
942: new ImageCapabilities(true),
943: BufferCapabilities.FlipContents.COPIED));
944: }
945: }
946:
947:
970: public void createBufferStrategy(int numBuffers)
971: {
972: if (numBuffers < 1)
973: throw new IllegalArgumentException("Window.createBufferStrategy: number"
974: + " of buffers is less than one");
975:
976: if (!isDisplayable())
977: throw new IllegalStateException("Window.createBufferStrategy: window is"
978: + " not displayable");
979:
980: BufferStrategy newStrategy = null;
981:
982:
983: try
984: {
985: newStrategy = new WindowFlipBufferStrategy(numBuffers);
986: }
987: catch (AWTException e)
988: {
989: }
990:
991:
992: if (newStrategy == null)
993: newStrategy = new WindowBltBufferStrategy(numBuffers, true);
994:
995: bufferStrategy = newStrategy;
996: }
997:
998:
1017: public void createBufferStrategy(int numBuffers, BufferCapabilities caps)
1018: throws AWTException
1019: {
1020: if (numBuffers < 1)
1021: throw new IllegalArgumentException("Window.createBufferStrategy: number"
1022: + " of buffers is less than one");
1023:
1024: if (caps == null)
1025: throw new IllegalArgumentException("Window.createBufferStrategy:"
1026: + " capabilities object is null");
1027:
1028:
1029: if (caps.isPageFlipping())
1030: bufferStrategy = new WindowFlipBufferStrategy(numBuffers);
1031: else
1032: bufferStrategy = new WindowBltBufferStrategy(numBuffers, true);
1033: }
1034:
1035:
1041: public BufferStrategy getBufferStrategy()
1042: {
1043: return bufferStrategy;
1044: }
1045:
1046:
1051: public void applyResourceBundle(ResourceBundle rb)
1052: throws NotImplementedException
1053: {
1054: throw new Error ("Not implemented");
1055: }
1056:
1057:
1062: public void applyResourceBundle(String rbName)
1063: {
1064: ResourceBundle rb = ResourceBundle.getBundle(rbName, Locale.getDefault(),
1065: ClassLoader.getSystemClassLoader());
1066: if (rb != null)
1067: applyResourceBundle(rb);
1068: }
1069:
1070:
1076: public AccessibleContext getAccessibleContext()
1077: {
1078:
1079: if (accessibleContext == null)
1080: accessibleContext = new AccessibleAWTWindow();
1081: return accessibleContext;
1082: }
1083:
1084:
1089: public GraphicsConfiguration getGraphicsConfiguration()
1090: {
1091: if (graphicsConfiguration != null) return graphicsConfiguration;
1092: if (peer != null) return peer.getGraphicsConfiguration();
1093: return null;
1094: }
1095:
1096: protected void processWindowFocusEvent(WindowEvent event)
1097: {
1098: if (windowFocusListener != null)
1099: {
1100: switch (event.getID ())
1101: {
1102: case WindowEvent.WINDOW_GAINED_FOCUS:
1103: windowFocusListener.windowGainedFocus (event);
1104: break;
1105:
1106: case WindowEvent.WINDOW_LOST_FOCUS:
1107: windowFocusListener.windowLostFocus (event);
1108: break;
1109:
1110: default:
1111: break;
1112: }
1113: }
1114: }
1115:
1116:
1119: protected void processWindowStateEvent(WindowEvent event)
1120: {
1121: if (windowStateListener != null
1122: && event.getID () == WindowEvent.WINDOW_STATE_CHANGED)
1123: windowStateListener.windowStateChanged (event);
1124: }
1125:
1126:
1131: public final boolean isFocusableWindow ()
1132: {
1133: if (getFocusableWindowState () == false)
1134: return false;
1135:
1136: if (this instanceof Dialog
1137: || this instanceof Frame)
1138: return true;
1139:
1140:
1141:
1142: return false;
1143: }
1144:
1145:
1150: public boolean getFocusableWindowState ()
1151: {
1152: return focusableWindowState;
1153: }
1154:
1155:
1160: public void setFocusableWindowState (boolean focusableWindowState)
1161: {
1162: this.focusableWindowState = focusableWindowState;
1163: }
1164:
1165:
1174: public final boolean isFocusCycleRoot()
1175: {
1176: return true;
1177: }
1178:
1179:
1188: public final void setFocusCycleRoot(boolean focusCycleRoot)
1189: {
1190:
1191: }
1192:
1193:
1201: public final Container getFocusCycleRootAncestor()
1202: {
1203: return null;
1204: }
1205:
1206:
1211: String generateName()
1212: {
1213: return "win" + getUniqueLong();
1214: }
1215:
1216: private static synchronized long getUniqueLong()
1217: {
1218: return next_window_number++;
1219: }
1220: }