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:
91: private transient boolean shown;
92:
93:
94: transient Component windowFocusOwner;
95:
96:
99: private static transient long next_window_number;
100:
101: protected class AccessibleAWTWindow extends AccessibleAWTContainer
102: {
103: private static final long serialVersionUID = 4215068635060671780L;
104:
105: public AccessibleRole getAccessibleRole()
106: {
107: return AccessibleRole.WINDOW;
108: }
109:
110: public AccessibleStateSet getAccessibleStateSet()
111: {
112: AccessibleStateSet states = super.getAccessibleStateSet();
113: if (isActive())
114: states.add(AccessibleState.ACTIVE);
115: return states;
116: }
117: }
118:
119:
125: Window()
126: {
127: visible = false;
128:
129:
130: focusCycleRoot = true;
131: setLayout(new BorderLayout());
132:
133: GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
134: graphicsConfig = g.getDefaultScreenDevice().getDefaultConfiguration();
135: }
136:
137: Window(GraphicsConfiguration gc)
138: {
139: this();
140: graphicsConfig = gc;
141: }
142:
143:
153: public Window(Frame owner)
154: {
155: this (owner, owner.getGraphicsConfiguration ());
156: }
157:
158:
168: public Window(Window owner)
169: {
170: this (owner, owner.getGraphicsConfiguration ());
171: }
172:
173:
183: public Window(Window owner, GraphicsConfiguration gc)
184: {
185: this ();
186:
187: synchronized (getTreeLock())
188: {
189: if (owner == null)
190: throw new IllegalArgumentException ("owner must not be null");
191:
192: parent = owner;
193: owner.ownedWindows.add(new WeakReference(this));
194: }
195:
196:
197: SecurityManager s = System.getSecurityManager();
198: if (s != null && ! s.checkTopLevelWindow(this))
199: warningString = System.getProperty("awt.appletWarning");
200:
201: if (gc != null
202: && gc.getDevice().getType() != GraphicsDevice.TYPE_RASTER_SCREEN)
203: throw new IllegalArgumentException ("gc must be from a screen device");
204:
205: if (gc == null)
206: graphicsConfig = GraphicsEnvironment.getLocalGraphicsEnvironment()
207: .getDefaultScreenDevice()
208: .getDefaultConfiguration();
209: else
210: graphicsConfig = gc;
211: }
212:
213:
216: public void addNotify()
217: {
218: if (peer == null)
219: peer = getToolkit().createWindow(this);
220: super.addNotify();
221: }
222:
223:
229: public void pack()
230: {
231: if (parent != null && !parent.isDisplayable())
232: parent.addNotify();
233: if (peer == null)
234: addNotify();
235:
236: setSize(getPreferredSize());
237:
238: validate();
239: }
240:
241:
246: @Deprecated
247: public void show()
248: {
249: synchronized (getTreeLock())
250: {
251: if (peer == null)
252: addNotify();
253:
254: validate();
255: if (visible)
256: toFront();
257: else
258: {
259: super.show();
260:
261: Iterator e = ownedWindows.iterator();
262: while (e.hasNext())
263: {
264: Window w = (Window) (((Reference) e.next()).get());
265: if (w != null)
266: {
267: if (w.isVisible())
268: w.getPeer().setVisible(true);
269: }
270: else
271:
272:
273:
274:
275: e.remove();
276: }
277: }
278: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
279: manager.setGlobalFocusedWindow(this);
280:
281: if (! shown)
282: {
283: FocusTraversalPolicy policy = getFocusTraversalPolicy();
284: Component initialFocusOwner = null;
285:
286: if (policy != null)
287: initialFocusOwner = policy.getInitialComponent(this);
288:
289: if (initialFocusOwner != null)
290: initialFocusOwner.requestFocusInWindow();
291:
292:
293: if (windowListener != null
294: || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0)
295: {
296: WindowEvent ev = new WindowEvent(this,
297: WindowEvent.WINDOW_OPENED);
298: Toolkit tk = Toolkit.getDefaultToolkit();
299: tk.getSystemEventQueue().postEvent(ev);
300: }
301: shown = true;
302: }
303: }
304: }
305:
306:
309: @Deprecated
310: public void hide()
311: {
312:
313: synchronized (getTreeLock ())
314: {
315: Iterator e = ownedWindows.iterator();
316: while(e.hasNext())
317: {
318: Window w = (Window)(((Reference) e.next()).get());
319: if (w != null)
320: {
321: if (w.isVisible() && w.getPeer() != null)
322: w.getPeer().setVisible(false);
323: }
324: else
325: e.remove();
326: }
327: }
328: super.hide();
329: }
330:
331:
335: public void dispose()
336: {
337: hide();
338:
339: synchronized (getTreeLock ())
340: {
341: Iterator e = ownedWindows.iterator();
342: while(e.hasNext())
343: {
344: Window w = (Window)(((Reference) e.next()).get());
345: if (w != null)
346: w.dispose();
347: else
348:
349: e.remove();
350: }
351:
352: for (int i = 0; i < ncomponents; ++i)
353: component[i].removeNotify();
354: this.removeNotify();
355:
356:
357: if (windowListener != null
358: || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0)
359: {
360: WindowEvent ev = new WindowEvent(this,
361: WindowEvent.WINDOW_CLOSED);
362: Toolkit tk = Toolkit.getDefaultToolkit();
363: tk.getSystemEventQueue().postEvent(ev);
364: }
365: }
366: }
367:
368:
375: public void toBack()
376: {
377: if (peer != null)
378: {
379: if( alwaysOnTop )
380: setAlwaysOnTop( false );
381: ( (WindowPeer) peer ).toBack();
382: }
383: }
384:
385:
389: public void toFront()
390: {
391: if (peer != null)
392: ( (WindowPeer) peer ).toFront();
393: }
394:
395:
403: public Toolkit getToolkit()
404: {
405: return Toolkit.getDefaultToolkit();
406: }
407:
408:
414: public final String getWarningString()
415: {
416: return warningString;
417: }
418:
419:
424: public Locale getLocale()
425: {
426: return locale == null ? Locale.getDefault() : locale;
427: }
428:
429:
436:
437:
442: public void setCursor(Cursor cursor)
443: {
444: super.setCursor(cursor);
445: }
446:
447: public Window getOwner()
448: {
449: return (Window) parent;
450: }
451:
452:
453: public Window[] getOwnedWindows()
454: {
455: Window [] trimmedList;
456: synchronized (getTreeLock ())
457: {
458:
459: Window [] validList = new Window [ownedWindows.size()];
460:
461: Iterator e = ownedWindows.iterator();
462: int numValid = 0;
463: while (e.hasNext())
464: {
465: Window w = (Window)(((Reference) e.next()).get());
466: if (w != null)
467: validList[numValid++] = w;
468: else
469:
470: e.remove();
471: }
472:
473: if (numValid != validList.length)
474: {
475: trimmedList = new Window [numValid];
476: System.arraycopy (validList, 0, trimmedList, 0, numValid);
477: }
478: else
479: trimmedList = validList;
480: }
481: return trimmedList;
482: }
483:
484:
490: public synchronized void addWindowListener(WindowListener listener)
491: {
492: if (listener != null)
493: {
494: newEventsOnly = true;
495: windowListener = AWTEventMulticaster.add(windowListener, listener);
496: }
497: }
498:
499:
505: public synchronized void removeWindowListener(WindowListener listener)
506: {
507: windowListener = AWTEventMulticaster.remove(windowListener, listener);
508: }
509:
510:
515: public synchronized WindowListener[] getWindowListeners()
516: {
517: return (WindowListener[])
518: AWTEventMulticaster.getListeners(windowListener,
519: WindowListener.class);
520: }
521:
522:
528: public synchronized WindowFocusListener[] getWindowFocusListeners()
529: {
530: return (WindowFocusListener[])
531: AWTEventMulticaster.getListeners(windowFocusListener,
532: WindowFocusListener.class);
533: }
534:
535:
541: public synchronized WindowStateListener[] getWindowStateListeners()
542: {
543: return (WindowStateListener[])
544: AWTEventMulticaster.getListeners(windowStateListener,
545: WindowStateListener.class);
546: }
547:
548:
551: public void addWindowFocusListener (WindowFocusListener wfl)
552: {
553: if (wfl != null)
554: {
555: newEventsOnly = true;
556: windowFocusListener = AWTEventMulticaster.add (windowFocusListener,
557: wfl);
558: }
559: }
560:
561:
566: public void addWindowStateListener (WindowStateListener wsl)
567: {
568: if (wsl != null)
569: {
570: newEventsOnly = true;
571: windowStateListener = AWTEventMulticaster.add (windowStateListener,
572: wsl);
573: }
574: }
575:
576:
579: public void removeWindowFocusListener (WindowFocusListener wfl)
580: {
581: windowFocusListener = AWTEventMulticaster.remove (windowFocusListener, wfl);
582: }
583:
584:
589: public void removeWindowStateListener (WindowStateListener wsl)
590: {
591: windowStateListener = AWTEventMulticaster.remove (windowStateListener, wsl);
592: }
593:
594:
604: public <T extends EventListener> T[] getListeners(Class<T> listenerType)
605: {
606: if (listenerType == WindowListener.class)
607: return (T[]) getWindowListeners();
608: return super.getListeners(listenerType);
609: }
610:
611: void dispatchEventImpl(AWTEvent e)
612: {
613: if (e.getID() == ComponentEvent.COMPONENT_RESIZED)
614: {
615: invalidate();
616: validate();
617: }
618: super.dispatchEventImpl(e);
619: }
620:
621:
629: protected void processEvent(AWTEvent evt)
630: {
631: if (evt instanceof WindowEvent)
632: {
633: WindowEvent we = (WindowEvent) evt;
634: switch (evt.getID())
635: {
636: case WindowEvent.WINDOW_OPENED:
637: case WindowEvent.WINDOW_CLOSED:
638: case WindowEvent.WINDOW_CLOSING:
639: case WindowEvent.WINDOW_ICONIFIED:
640: case WindowEvent.WINDOW_DEICONIFIED:
641: case WindowEvent.WINDOW_ACTIVATED:
642: case WindowEvent.WINDOW_DEACTIVATED:
643: processWindowEvent(we);
644: break;
645: case WindowEvent.WINDOW_GAINED_FOCUS:
646: case WindowEvent.WINDOW_LOST_FOCUS:
647: processWindowFocusEvent(we);
648: break;
649: case WindowEvent.WINDOW_STATE_CHANGED:
650: processWindowStateEvent(we);
651: break;
652: }
653: }
654: else
655: super.processEvent(evt);
656: }
657:
658:
666: protected void processWindowEvent(WindowEvent evt)
667: {
668: if (windowListener != null)
669: {
670: switch (evt.getID())
671: {
672: case WindowEvent.WINDOW_ACTIVATED:
673: windowListener.windowActivated(evt);
674: break;
675: case WindowEvent.WINDOW_CLOSED:
676: windowListener.windowClosed(evt);
677: break;
678: case WindowEvent.WINDOW_CLOSING:
679: windowListener.windowClosing(evt);
680: break;
681: case WindowEvent.WINDOW_DEACTIVATED:
682: windowListener.windowDeactivated(evt);
683: break;
684: case WindowEvent.WINDOW_DEICONIFIED:
685: windowListener.windowDeiconified(evt);
686: break;
687: case WindowEvent.WINDOW_ICONIFIED:
688: windowListener.windowIconified(evt);
689: break;
690: case WindowEvent.WINDOW_OPENED:
691: windowListener.windowOpened(evt);
692: break;
693: }
694: }
695: }
696:
697:
704: public boolean isActive()
705: {
706: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
707: return manager.getActiveWindow() == this;
708: }
709:
710:
717: public boolean isFocused()
718: {
719: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
720: return manager.getFocusedWindow() == this;
721: }
722:
723:
731: public Component getFocusOwner ()
732: {
733: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
734:
735: Window activeWindow = manager.getActiveWindow ();
736:
737:
738: if (activeWindow == this)
739: return manager.getFocusOwner ();
740: else
741: return null;
742: }
743:
744:
757: public Component getMostRecentFocusOwner ()
758: {
759: return windowFocusOwner;
760: }
761:
762:
771: void setFocusOwner (Component windowFocusOwner)
772: {
773: this.windowFocusOwner = windowFocusOwner;
774: }
775:
776:
783: public boolean postEvent(Event e)
784: {
785: return handleEvent (e);
786: }
787:
788:
798: public boolean isShowing()
799: {
800: return isVisible();
801: }
802:
803: public void setLocationRelativeTo(Component c)
804: {
805: int x = 0;
806: int y = 0;
807:
808: if (c == null || !c.isShowing())
809: {
810: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
811: Point center = ge.getCenterPoint();
812: x = center.x - (width / 2);
813: y = center.y - (height / 2);
814: }
815: else
816: {
817: int cWidth = c.getWidth();
818: int cHeight = c.getHeight();
819: Dimension screenSize = getToolkit().getScreenSize();
820:
821: x = c.getLocationOnScreen().x;
822: y = c.getLocationOnScreen().y;
823:
824:
825:
826: if ((y + cHeight) > screenSize.height)
827: {
828:
829: if ((screenSize.width / 2 - x) <= 0)
830: {
831: if ((x - width) >= 0)
832: x -= width;
833: else
834: x = 0;
835: }
836: else
837: {
838: if ((x + cWidth + width) <= screenSize.width)
839: x += cWidth;
840: else
841: x = screenSize.width - width;
842: }
843:
844: y = screenSize.height - height;
845: }
846: else if (cWidth > width || cHeight > height)
847: {
848:
849: if ((x + width) > screenSize.width)
850: x = screenSize.width - width;
851:
852: else if (x < 0)
853: x = 0;
854: else
855: x += (cWidth - width) / 2;
856:
857: y += (cHeight - height) / 2;
858: }
859: else
860: {
861:
862: if ((x + width) > screenSize.width)
863: x = screenSize.width - width;
864:
865: else if (x < 0 || (x - (width - cWidth) / 2) < 0)
866: x = 0;
867: else
868: x -= (width - cWidth) / 2;
869:
870: if ((y - (height - cHeight) / 2) > 0)
871: y -= (height - cHeight) / 2;
872: else
873: y = 0;
874: }
875: }
876:
877: setLocation(x, y);
878: }
879:
880:
883: private class WindowBltBufferStrategy extends BltBufferStrategy
884: {
885:
892: WindowBltBufferStrategy(int numBuffers, boolean accelerated)
893: {
894: super(numBuffers,
895: new BufferCapabilities(new ImageCapabilities(accelerated),
896: new ImageCapabilities(accelerated),
897: BufferCapabilities.FlipContents.COPIED));
898: }
899: }
900:
901:
904: private class WindowFlipBufferStrategy extends FlipBufferStrategy
905: {
906:
914: WindowFlipBufferStrategy(int numBuffers)
915: throws AWTException
916: {
917: super(numBuffers,
918: new BufferCapabilities(new ImageCapabilities(true),
919: new ImageCapabilities(true),
920: BufferCapabilities.FlipContents.COPIED));
921: }
922: }
923:
924:
947: public void createBufferStrategy(int numBuffers)
948: {
949: if (numBuffers < 1)
950: throw new IllegalArgumentException("Window.createBufferStrategy: number"
951: + " of buffers is less than one");
952:
953: if (!isDisplayable())
954: throw new IllegalStateException("Window.createBufferStrategy: window is"
955: + " not displayable");
956:
957: BufferStrategy newStrategy = null;
958:
959:
960: try
961: {
962: newStrategy = new WindowFlipBufferStrategy(numBuffers);
963: }
964: catch (AWTException e)
965: {
966: }
967:
968:
969: if (newStrategy == null)
970: newStrategy = new WindowBltBufferStrategy(numBuffers, true);
971:
972: bufferStrategy = newStrategy;
973: }
974:
975:
994: public void createBufferStrategy(int numBuffers, BufferCapabilities caps)
995: throws AWTException
996: {
997: if (numBuffers < 1)
998: throw new IllegalArgumentException("Window.createBufferStrategy: number"
999: + " of buffers is less than one");
1000:
1001: if (caps == null)
1002: throw new IllegalArgumentException("Window.createBufferStrategy:"
1003: + " capabilities object is null");
1004:
1005:
1006: if (caps.isPageFlipping())
1007: bufferStrategy = new WindowFlipBufferStrategy(numBuffers);
1008: else
1009: bufferStrategy = new WindowBltBufferStrategy(numBuffers, true);
1010: }
1011:
1012:
1018: public BufferStrategy getBufferStrategy()
1019: {
1020: return bufferStrategy;
1021: }
1022:
1023:
1028: public void applyResourceBundle(ResourceBundle rb)
1029: {
1030: applyComponentOrientation(ComponentOrientation.getOrientation(rb));
1031: }
1032:
1033:
1038: public void applyResourceBundle(String rbName)
1039: {
1040: ResourceBundle rb = ResourceBundle.getBundle(rbName, Locale.getDefault(),
1041: ClassLoader.getSystemClassLoader());
1042: if (rb != null)
1043: applyResourceBundle(rb);
1044: }
1045:
1046:
1052: public AccessibleContext getAccessibleContext()
1053: {
1054:
1055: if (accessibleContext == null)
1056: accessibleContext = new AccessibleAWTWindow();
1057: return accessibleContext;
1058: }
1059:
1060:
1065: public GraphicsConfiguration getGraphicsConfiguration()
1066: {
1067: GraphicsConfiguration conf = graphicsConfig;
1068: if (conf == null)
1069: {
1070: conf = GraphicsEnvironment.getLocalGraphicsEnvironment()
1071: .getDefaultScreenDevice().getDefaultConfiguration();
1072: graphicsConfig = conf;
1073: }
1074: return conf;
1075: }
1076:
1077: protected void processWindowFocusEvent(WindowEvent event)
1078: {
1079: if (windowFocusListener != null)
1080: {
1081: switch (event.getID ())
1082: {
1083: case WindowEvent.WINDOW_GAINED_FOCUS:
1084: windowFocusListener.windowGainedFocus (event);
1085: break;
1086:
1087: case WindowEvent.WINDOW_LOST_FOCUS:
1088: windowFocusListener.windowLostFocus (event);
1089: break;
1090:
1091: default:
1092: break;
1093: }
1094: }
1095: }
1096:
1097:
1100: protected void processWindowStateEvent(WindowEvent event)
1101: {
1102: if (windowStateListener != null
1103: && event.getID () == WindowEvent.WINDOW_STATE_CHANGED)
1104: windowStateListener.windowStateChanged (event);
1105: }
1106:
1107:
1112: public final boolean isFocusableWindow ()
1113: {
1114: if (getFocusableWindowState () == false)
1115: return false;
1116:
1117: if (this instanceof Dialog
1118: || this instanceof Frame)
1119: return true;
1120:
1121:
1122:
1123: return false;
1124: }
1125:
1126:
1131: public boolean getFocusableWindowState ()
1132: {
1133: return focusableWindowState;
1134: }
1135:
1136:
1141: public void setFocusableWindowState (boolean focusableWindowState)
1142: {
1143: this.focusableWindowState = focusableWindowState;
1144: }
1145:
1146:
1155: public final boolean isFocusCycleRoot()
1156: {
1157: return true;
1158: }
1159:
1160:
1169: public final void setFocusCycleRoot(boolean focusCycleRoot)
1170: {
1171:
1172: }
1173:
1174:
1182: public final Container getFocusCycleRootAncestor()
1183: {
1184: return null;
1185: }
1186:
1187:
1195: public final boolean isAlwaysOnTop()
1196: {
1197: return alwaysOnTop;
1198: }
1199:
1200:
1215: public final void setAlwaysOnTop(boolean alwaysOnTop)
1216: {
1217: SecurityManager sm = System.getSecurityManager();
1218: if (sm != null)
1219: sm.checkPermission( new AWTPermission("setWindowAlwaysOnTop") );
1220:
1221: if( this.alwaysOnTop == alwaysOnTop )
1222: return;
1223:
1224: if( alwaysOnTop )
1225: toFront();
1226:
1227: firePropertyChange("alwaysOnTop", this.alwaysOnTop, alwaysOnTop );
1228: this.alwaysOnTop = alwaysOnTop;
1229:
1230: if (peer != null)
1231: ( (WindowPeer) peer).updateAlwaysOnTop();
1232: else
1233: System.out.println("Null peer?!");
1234: }
1235:
1236:
1241: String generateName()
1242: {
1243: return "win" + getUniqueLong();
1244: }
1245:
1246:
1252: boolean eventTypeEnabled(int type)
1253: {
1254: boolean enabled = false;
1255: switch (type)
1256: {
1257: case WindowEvent.WINDOW_OPENED:
1258: case WindowEvent.WINDOW_CLOSED:
1259: case WindowEvent.WINDOW_CLOSING:
1260: case WindowEvent.WINDOW_ICONIFIED:
1261: case WindowEvent.WINDOW_DEICONIFIED:
1262: case WindowEvent.WINDOW_ACTIVATED:
1263: case WindowEvent.WINDOW_DEACTIVATED:
1264: enabled = ((eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0)
1265: || windowListener != null;
1266: break;
1267: case WindowEvent.WINDOW_GAINED_FOCUS:
1268: case WindowEvent.WINDOW_LOST_FOCUS:
1269: enabled = ((eventMask & AWTEvent.WINDOW_FOCUS_EVENT_MASK) != 0)
1270: || windowFocusListener != null;
1271: break;
1272: case WindowEvent.WINDOW_STATE_CHANGED:
1273: enabled = ((eventMask & AWTEvent.WINDOW_STATE_EVENT_MASK) != 0)
1274: || windowStateListener != null;
1275: break;
1276: default:
1277: enabled = super.eventTypeEnabled(type);
1278: }
1279: return enabled;
1280: }
1281:
1282: private static synchronized long getUniqueLong()
1283: {
1284: return next_window_number++;
1285: }
1286: }