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:
134: addWindowFocusListener (new WindowAdapter ()
135: {
136: public void windowGainedFocus (WindowEvent event)
137: {
138: if (windowFocusOwner != null)
139: {
140:
141:
142: EventQueue eq = Toolkit.getDefaultToolkit ().getSystemEventQueue ();
143: synchronized (eq)
144: {
145: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
146: Component currentFocusOwner = manager.getGlobalPermanentFocusOwner ();
147: if (currentFocusOwner != null)
148: {
149: eq.postEvent (new FocusEvent (currentFocusOwner, FocusEvent.FOCUS_LOST,
150: false, windowFocusOwner));
151: eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED,
152: false, currentFocusOwner));
153: }
154: else
155: eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED, false));
156: }
157: }
158: }
159: });
160:
161: GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
162: graphicsConfiguration = g.getDefaultScreenDevice().getDefaultConfiguration();
163: }
164:
165: Window(GraphicsConfiguration gc)
166: {
167: this();
168: graphicsConfiguration = gc;
169: }
170:
171:
181: public Window(Frame owner)
182: {
183: this (owner, owner.getGraphicsConfiguration ());
184: }
185:
186:
196: public Window(Window owner)
197: {
198: this (owner, owner.getGraphicsConfiguration ());
199: }
200:
201:
211: public Window(Window owner, GraphicsConfiguration gc)
212: {
213: this ();
214:
215: synchronized (getTreeLock())
216: {
217: if (owner == null)
218: throw new IllegalArgumentException ("owner must not be null");
219:
220: parent = owner;
221: owner.ownedWindows.add(new WeakReference(this));
222: }
223:
224:
225: SecurityManager s = System.getSecurityManager();
226: if (s != null && ! s.checkTopLevelWindow(this))
227: warningString = System.getProperty("awt.appletWarning");
228:
229: if (gc != null
230: && gc.getDevice().getType() != GraphicsDevice.TYPE_RASTER_SCREEN)
231: throw new IllegalArgumentException ("gc must be from a screen device");
232:
233: if (gc == null)
234: graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment()
235: .getDefaultScreenDevice()
236: .getDefaultConfiguration();
237: else
238: graphicsConfiguration = gc;
239: }
240:
241: GraphicsConfiguration getGraphicsConfigurationImpl()
242: {
243: if (graphicsConfiguration != null)
244: return graphicsConfiguration;
245:
246: return super.getGraphicsConfigurationImpl();
247: }
248:
249:
252: public void addNotify()
253: {
254: if (peer == null)
255: peer = getToolkit().createWindow(this);
256: super.addNotify();
257: }
258:
259:
265: public void pack()
266: {
267: if (parent != null && !parent.isDisplayable())
268: parent.addNotify();
269: if (peer == null)
270: addNotify();
271:
272: setSize(getPreferredSize());
273:
274: validate();
275: }
276:
277:
281: public void show()
282: {
283: synchronized (getTreeLock())
284: {
285: if (parent != null && !parent.isDisplayable())
286: parent.addNotify();
287: if (peer == null)
288: addNotify();
289:
290:
291: Iterator e = ownedWindows.iterator();
292: while(e.hasNext())
293: {
294: Window w = (Window)(((Reference) e.next()).get());
295: if (w != null)
296: {
297: if (w.isVisible())
298: w.getPeer().setVisible(true);
299: }
300: else
301:
302:
303:
304:
305: e.remove();
306: }
307: validate();
308: super.show();
309: toFront();
310:
311: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
312: manager.setGlobalFocusedWindow (this);
313:
314: if (!shown)
315: {
316: FocusTraversalPolicy policy = getFocusTraversalPolicy ();
317: Component initialFocusOwner = null;
318:
319: if (policy != null)
320: initialFocusOwner = policy.getInitialComponent (this);
321:
322: if (initialFocusOwner != null)
323: initialFocusOwner.requestFocusInWindow ();
324:
325: shown = true;
326: }
327: }
328: }
329:
330: public void hide()
331: {
332:
333: synchronized (getTreeLock ())
334: {
335: Iterator e = ownedWindows.iterator();
336: while(e.hasNext())
337: {
338: Window w = (Window)(((Reference) e.next()).get());
339: if (w != null)
340: {
341: if (w.isVisible() && w.getPeer() != null)
342: w.getPeer().setVisible(false);
343: }
344: else
345: e.remove();
346: }
347: }
348: super.hide();
349: }
350:
351:
355: public void dispose()
356: {
357: hide();
358:
359: synchronized (getTreeLock ())
360: {
361: Iterator e = ownedWindows.iterator();
362: while(e.hasNext())
363: {
364: Window w = (Window)(((Reference) e.next()).get());
365: if (w != null)
366: w.dispose();
367: else
368:
369: e.remove();
370: }
371:
372: for (int i = 0; i < ncomponents; ++i)
373: component[i].removeNotify();
374: this.removeNotify();
375:
376:
377: WindowEvent we = new WindowEvent(this, WindowEvent.WINDOW_CLOSED);
378: getToolkit().getSystemEventQueue().postEvent(we);
379: }
380: }
381:
382:
386: public void toBack()
387: {
388: if (peer != null)
389: {
390: WindowPeer wp = (WindowPeer) peer;
391: wp.toBack();
392: }
393: }
394:
395:
399: public void toFront()
400: {
401: if (peer != null)
402: {
403: WindowPeer wp = (WindowPeer) peer;
404: wp.toFront();
405: }
406: }
407:
408:
416: public Toolkit getToolkit()
417: {
418: return Toolkit.getDefaultToolkit();
419: }
420:
421:
427: public final String getWarningString()
428: {
429: return warningString;
430: }
431:
432:
437: public Locale getLocale()
438: {
439: return locale == null ? Locale.getDefault() : locale;
440: }
441:
442:
449:
450:
455: public void setCursor(Cursor cursor)
456: {
457: super.setCursor(cursor);
458: }
459:
460: public Window getOwner()
461: {
462: return (Window) parent;
463: }
464:
465:
466: public Window[] getOwnedWindows()
467: {
468: Window [] trimmedList;
469: synchronized (getTreeLock ())
470: {
471:
472: Window [] validList = new Window [ownedWindows.size()];
473:
474: Iterator e = ownedWindows.iterator();
475: int numValid = 0;
476: while (e.hasNext())
477: {
478: Window w = (Window)(((Reference) e.next()).get());
479: if (w != null)
480: validList[numValid++] = w;
481: else
482:
483: e.remove();
484: }
485:
486: if (numValid != validList.length)
487: {
488: trimmedList = new Window [numValid];
489: System.arraycopy (validList, 0, trimmedList, 0, numValid);
490: }
491: else
492: trimmedList = validList;
493: }
494: return trimmedList;
495: }
496:
497:
503: public synchronized void addWindowListener(WindowListener listener)
504: {
505: windowListener = AWTEventMulticaster.add(windowListener, listener);
506: }
507:
508:
514: public synchronized void removeWindowListener(WindowListener listener)
515: {
516: windowListener = AWTEventMulticaster.remove(windowListener, listener);
517: }
518:
519:
524: public synchronized WindowListener[] getWindowListeners()
525: {
526: return (WindowListener[])
527: AWTEventMulticaster.getListeners(windowListener,
528: WindowListener.class);
529: }
530:
531:
537: public synchronized WindowFocusListener[] getWindowFocusListeners()
538: {
539: return (WindowFocusListener[])
540: AWTEventMulticaster.getListeners(windowFocusListener,
541: WindowFocusListener.class);
542: }
543:
544:
550: public synchronized WindowStateListener[] getWindowStateListeners()
551: {
552: return (WindowStateListener[])
553: AWTEventMulticaster.getListeners(windowStateListener,
554: WindowStateListener.class);
555: }
556:
557:
560: public void addWindowFocusListener (WindowFocusListener wfl)
561: {
562: windowFocusListener = AWTEventMulticaster.add (windowFocusListener, wfl);
563: }
564:
565:
570: public void addWindowStateListener (WindowStateListener wsl)
571: {
572: windowStateListener = AWTEventMulticaster.add (windowStateListener, wsl);
573: }
574:
575:
578: public void removeWindowFocusListener (WindowFocusListener wfl)
579: {
580: windowFocusListener = AWTEventMulticaster.remove (windowFocusListener, wfl);
581: }
582:
583:
588: public void removeWindowStateListener (WindowStateListener wsl)
589: {
590: windowStateListener = AWTEventMulticaster.remove (windowStateListener, wsl);
591: }
592:
593:
603: public EventListener[] getListeners(Class listenerType)
604: {
605: if (listenerType == WindowListener.class)
606: return getWindowListeners();
607: return super.getListeners(listenerType);
608: }
609:
610: void dispatchEventImpl(AWTEvent e)
611: {
612:
613: if (e.id <= WindowEvent.WINDOW_LAST
614: && e.id >= WindowEvent.WINDOW_FIRST
615: && (windowListener != null
616: || windowFocusListener != null
617: || windowStateListener != null
618: || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0))
619: processEvent(e);
620: else if (e.id == ComponentEvent.COMPONENT_RESIZED)
621: validate ();
622: else
623: super.dispatchEventImpl(e);
624: }
625:
626:
634: protected void processEvent(AWTEvent evt)
635: {
636: if (evt instanceof WindowEvent)
637: processWindowEvent((WindowEvent) evt);
638: else
639: super.processEvent(evt);
640: }
641:
642:
650: protected void processWindowEvent(WindowEvent evt)
651: {
652: int id = evt.getID();
653:
654: if (id == WindowEvent.WINDOW_GAINED_FOCUS
655: || id == WindowEvent.WINDOW_LOST_FOCUS)
656: processWindowFocusEvent (evt);
657: else if (id == WindowEvent.WINDOW_STATE_CHANGED)
658: processWindowStateEvent (evt);
659: else
660: {
661: if (windowListener != null)
662: {
663: switch (evt.getID())
664: {
665: case WindowEvent.WINDOW_ACTIVATED:
666: windowListener.windowActivated(evt);
667: break;
668:
669: case WindowEvent.WINDOW_CLOSED:
670: windowListener.windowClosed(evt);
671: break;
672:
673: case WindowEvent.WINDOW_CLOSING:
674: windowListener.windowClosing(evt);
675: break;
676:
677: case WindowEvent.WINDOW_DEACTIVATED:
678: windowListener.windowDeactivated(evt);
679: break;
680:
681: case WindowEvent.WINDOW_DEICONIFIED:
682: windowListener.windowDeiconified(evt);
683: break;
684:
685: case WindowEvent.WINDOW_ICONIFIED:
686: windowListener.windowIconified(evt);
687: break;
688:
689: case WindowEvent.WINDOW_OPENED:
690: windowListener.windowOpened(evt);
691: break;
692:
693: default:
694: break;
695: }
696: }
697: }
698: }
699:
700:
707: public boolean isActive()
708: {
709: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
710: return manager.getActiveWindow() == this;
711: }
712:
713:
720: public boolean isFocused()
721: {
722: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
723: return manager.getFocusedWindow() == this;
724: }
725:
726:
734: public Component getFocusOwner ()
735: {
736: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
737:
738: Window activeWindow = manager.getActiveWindow ();
739:
740:
741: if (activeWindow == this)
742: return manager.getFocusOwner ();
743: else
744: return null;
745: }
746:
747:
760: public Component getMostRecentFocusOwner ()
761: {
762: return windowFocusOwner;
763: }
764:
765:
774: void setFocusOwner (Component windowFocusOwner)
775: {
776: this.windowFocusOwner = windowFocusOwner;
777: }
778:
779:
786: public boolean postEvent(Event e)
787: {
788: return handleEvent (e);
789: }
790:
791:
801: public boolean isShowing()
802: {
803: return isVisible();
804: }
805:
806: public void setLocationRelativeTo(Component c)
807: {
808: int x = 0;
809: int y = 0;
810:
811: if (c == null || !c.isShowing())
812: {
813: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
814: Point center = ge.getCenterPoint();
815: x = center.x - (width / 2);
816: y = center.y - (height / 2);
817: }
818: else
819: {
820: int cWidth = c.getWidth();
821: int cHeight = c.getHeight();
822: Dimension screenSize = getToolkit().getScreenSize();
823:
824: x = c.getLocationOnScreen().x;
825: y = c.getLocationOnScreen().y;
826:
827:
828:
829: if ((y + cHeight) > screenSize.height)
830: {
831:
832: if ((screenSize.width / 2 - x) <= 0)
833: {
834: if ((x - width) >= 0)
835: x -= width;
836: else
837: x = 0;
838: }
839: else
840: {
841: if ((x + cWidth + width) <= screenSize.width)
842: x += cWidth;
843: else
844: x = screenSize.width - width;
845: }
846:
847: y = screenSize.height - height;
848: }
849: else if (cWidth > width || cHeight > height)
850: {
851:
852: if ((x + width) > screenSize.width)
853: x = screenSize.width - width;
854:
855: else if (x < 0)
856: x = 0;
857: else
858: x += (cWidth - width) / 2;
859:
860: y += (cHeight - height) / 2;
861: }
862: else
863: {
864:
865: if ((x + width) > screenSize.width)
866: x = screenSize.width - width;
867:
868: else if (x < 0 || (x - (width - cWidth) / 2) < 0)
869: x = 0;
870: else
871: x -= (width - cWidth) / 2;
872:
873: if ((y - (height - cHeight) / 2) > 0)
874: y -= (height - cHeight) / 2;
875: else
876: y = 0;
877: }
878: }
879:
880: setLocation(x, y);
881: }
882:
883:
886: private class WindowBltBufferStrategy extends BltBufferStrategy
887: {
888:
895: WindowBltBufferStrategy(int numBuffers, boolean accelerated)
896: {
897: super(numBuffers,
898: new BufferCapabilities(new ImageCapabilities(accelerated),
899: new ImageCapabilities(accelerated),
900: BufferCapabilities.FlipContents.COPIED));
901: }
902: }
903:
904:
907: private class WindowFlipBufferStrategy extends FlipBufferStrategy
908: {
909:
917: WindowFlipBufferStrategy(int numBuffers)
918: throws AWTException
919: {
920: super(numBuffers,
921: new BufferCapabilities(new ImageCapabilities(true),
922: new ImageCapabilities(true),
923: BufferCapabilities.FlipContents.COPIED));
924: }
925: }
926:
927:
950: public void createBufferStrategy(int numBuffers)
951: {
952: if (numBuffers < 1)
953: throw new IllegalArgumentException("Window.createBufferStrategy: number"
954: + " of buffers is less than one");
955:
956: if (!isDisplayable())
957: throw new IllegalStateException("Window.createBufferStrategy: window is"
958: + " not displayable");
959:
960: BufferStrategy newStrategy = null;
961:
962:
963: try
964: {
965: newStrategy = new WindowFlipBufferStrategy(numBuffers);
966: }
967: catch (AWTException e)
968: {
969: }
970:
971:
972: if (newStrategy == null)
973: newStrategy = new WindowBltBufferStrategy(numBuffers, true);
974:
975: bufferStrategy = newStrategy;
976: }
977:
978:
997: public void createBufferStrategy(int numBuffers, BufferCapabilities caps)
998: throws AWTException
999: {
1000: if (numBuffers < 1)
1001: throw new IllegalArgumentException("Window.createBufferStrategy: number"
1002: + " of buffers is less than one");
1003:
1004: if (caps == null)
1005: throw new IllegalArgumentException("Window.createBufferStrategy:"
1006: + " capabilities object is null");
1007:
1008:
1009: if (caps.isPageFlipping())
1010: bufferStrategy = new WindowFlipBufferStrategy(numBuffers);
1011: else
1012: bufferStrategy = new WindowBltBufferStrategy(numBuffers, true);
1013: }
1014:
1015:
1021: public BufferStrategy getBufferStrategy()
1022: {
1023: return bufferStrategy;
1024: }
1025:
1026:
1031: public void applyResourceBundle(ResourceBundle rb)
1032: {
1033: throw new Error ("Not implemented");
1034: }
1035:
1036:
1041: public void applyResourceBundle(String rbName)
1042: {
1043: ResourceBundle rb = ResourceBundle.getBundle(rbName, Locale.getDefault(),
1044: ClassLoader.getSystemClassLoader());
1045: if (rb != null)
1046: applyResourceBundle(rb);
1047: }
1048:
1049:
1055: public AccessibleContext getAccessibleContext()
1056: {
1057:
1058: if (accessibleContext == null)
1059: accessibleContext = new AccessibleAWTWindow();
1060: return accessibleContext;
1061: }
1062:
1063:
1068: public GraphicsConfiguration getGraphicsConfiguration()
1069: {
1070: if (graphicsConfiguration != null) return graphicsConfiguration;
1071: if (peer != null) return peer.getGraphicsConfiguration();
1072: return null;
1073: }
1074:
1075: protected void processWindowFocusEvent(WindowEvent event)
1076: {
1077: if (windowFocusListener != null)
1078: {
1079: switch (event.getID ())
1080: {
1081: case WindowEvent.WINDOW_GAINED_FOCUS:
1082: windowFocusListener.windowGainedFocus (event);
1083: break;
1084:
1085: case WindowEvent.WINDOW_LOST_FOCUS:
1086: windowFocusListener.windowLostFocus (event);
1087: break;
1088:
1089: default:
1090: break;
1091: }
1092: }
1093: }
1094:
1095:
1098: protected void processWindowStateEvent(WindowEvent event)
1099: {
1100: if (windowStateListener != null
1101: && event.getID () == WindowEvent.WINDOW_STATE_CHANGED)
1102: windowStateListener.windowStateChanged (event);
1103: }
1104:
1105:
1110: public final boolean isFocusableWindow ()
1111: {
1112: if (getFocusableWindowState () == false)
1113: return false;
1114:
1115: if (this instanceof Dialog
1116: || this instanceof Frame)
1117: return true;
1118:
1119:
1120:
1121: return false;
1122: }
1123:
1124:
1129: public boolean getFocusableWindowState ()
1130: {
1131: return focusableWindowState;
1132: }
1133:
1134:
1139: public void setFocusableWindowState (boolean focusableWindowState)
1140: {
1141: this.focusableWindowState = focusableWindowState;
1142: }
1143:
1144:
1149: String generateName()
1150: {
1151: return "win" + getUniqueLong();
1152: }
1153:
1154: private static synchronized long getUniqueLong()
1155: {
1156: return next_window_number++;
1157: }
1158: }