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: public AccessibleRole getAccessibleRole()
105: {
106: return AccessibleRole.WINDOW;
107: }
108:
109: public AccessibleStateSet getAccessibleStateSet()
110: {
111: AccessibleStateSet states = super.getAccessibleStateSet();
112: if (isActive())
113: states.add(AccessibleState.ACTIVE);
114: return states;
115: }
116: }
117:
118:
124: Window()
125: {
126: visible = false;
127:
128:
129: focusCycleRoot = true;
130: setLayout(new BorderLayout());
131:
132: addWindowFocusListener (new WindowAdapter ()
133: {
134: public void windowGainedFocus (WindowEvent event)
135: {
136: if (windowFocusOwner != null)
137: {
138:
139:
140: EventQueue eq = Toolkit.getDefaultToolkit ().getSystemEventQueue ();
141: synchronized (eq)
142: {
143: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
144: Component currentFocusOwner = manager.getGlobalPermanentFocusOwner ();
145: if (currentFocusOwner != null)
146: {
147: eq.postEvent (new FocusEvent (currentFocusOwner, FocusEvent.FOCUS_LOST,
148: false, windowFocusOwner));
149: eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED,
150: false, currentFocusOwner));
151: }
152: else
153: eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED, false));
154: }
155: }
156: }
157: });
158: }
159:
160: Window(GraphicsConfiguration gc)
161: {
162: this();
163: graphicsConfiguration = gc;
164: }
165:
166:
176: public Window(Frame owner)
177: {
178: this (owner, owner.getGraphicsConfiguration ());
179: }
180:
181:
191: public Window(Window owner)
192: {
193: this (owner, owner.getGraphicsConfiguration ());
194: }
195:
196:
206: public Window(Window owner, GraphicsConfiguration gc)
207: {
208: this ();
209:
210: synchronized (getTreeLock())
211: {
212: if (owner == null)
213: throw new IllegalArgumentException ("owner must not be null");
214:
215: parent = owner;
216: owner.ownedWindows.add(new WeakReference(this));
217: }
218:
219:
220: SecurityManager s = System.getSecurityManager();
221: if (s != null && ! s.checkTopLevelWindow(this))
222: warningString = System.getProperty("awt.appletWarning");
223:
224: if (gc != null
225: && gc.getDevice().getType() != GraphicsDevice.TYPE_RASTER_SCREEN)
226: throw new IllegalArgumentException ("gc must be from a screen device");
227:
228: if (gc == null)
229: graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment()
230: .getDefaultScreenDevice()
231: .getDefaultConfiguration();
232: else
233: graphicsConfiguration = gc;
234: }
235:
236: GraphicsConfiguration getGraphicsConfigurationImpl()
237: {
238: if (graphicsConfiguration != null)
239: return graphicsConfiguration;
240:
241: return super.getGraphicsConfigurationImpl();
242: }
243:
244:
247: public void addNotify()
248: {
249: if (peer == null)
250: peer = getToolkit().createWindow(this);
251: super.addNotify();
252: }
253:
254:
260: public void pack()
261: {
262: if (parent != null && !parent.isDisplayable())
263: parent.addNotify();
264: if (peer == null)
265: addNotify();
266:
267: setSize(getPreferredSize());
268:
269: validate();
270: }
271:
272:
276: public void show()
277: {
278: if (parent != null && !parent.isDisplayable())
279: parent.addNotify();
280: if (peer == null)
281: addNotify();
282:
283:
284: synchronized (getTreeLock())
285: {
286: Iterator e = ownedWindows.iterator();
287: while(e.hasNext())
288: {
289: Window w = (Window)(((Reference) e.next()).get());
290: if (w != null)
291: {
292: if (w.isVisible())
293: w.getPeer().setVisible(true);
294: }
295: else
296:
297:
298:
299:
300: e.remove();
301: }
302: }
303: validate();
304: super.show();
305: toFront();
306:
307: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
308: manager.setGlobalFocusedWindow (this);
309:
310: if (!shown)
311: {
312: FocusTraversalPolicy policy = getFocusTraversalPolicy ();
313: Component initialFocusOwner = null;
314:
315: if (policy != null)
316: initialFocusOwner = policy.getInitialComponent (this);
317:
318: if (initialFocusOwner != null)
319: initialFocusOwner.requestFocusInWindow ();
320:
321: shown = true;
322: }
323: }
324:
325: public void hide()
326: {
327:
328: synchronized (getTreeLock ())
329: {
330: Iterator e = ownedWindows.iterator();
331: while(e.hasNext())
332: {
333: Window w = (Window)(((Reference) e.next()).get());
334: if (w != null)
335: {
336: if (w.isVisible() && w.getPeer() != null)
337: w.getPeer().setVisible(false);
338: }
339: else
340: e.remove();
341: }
342: }
343: super.hide();
344: }
345:
346: public boolean isDisplayable()
347: {
348: if (super.isDisplayable())
349: return true;
350: return peer != null;
351: }
352:
353:
357: public void dispose()
358: {
359: hide();
360:
361: synchronized (getTreeLock ())
362: {
363: Iterator e = ownedWindows.iterator();
364: while(e.hasNext())
365: {
366: Window w = (Window)(((Reference) e.next()).get());
367: if (w != null)
368: w.dispose();
369: else
370:
371: e.remove();
372: }
373:
374: for (int i = 0; i < ncomponents; ++i)
375: component[i].removeNotify();
376: this.removeNotify();
377:
378:
379: WindowEvent we = new WindowEvent(this, WindowEvent.WINDOW_CLOSED);
380: getToolkit().getSystemEventQueue().postEvent(we);
381: }
382: }
383:
384:
388: public void toBack()
389: {
390: if (peer != null)
391: {
392: WindowPeer wp = (WindowPeer) peer;
393: wp.toBack();
394: }
395: }
396:
397:
401: public void toFront()
402: {
403: if (peer != null)
404: {
405: WindowPeer wp = (WindowPeer) peer;
406: wp.toFront();
407: }
408: }
409:
410:
418: public Toolkit getToolkit()
419: {
420: return Toolkit.getDefaultToolkit();
421: }
422:
423:
429: public final String getWarningString()
430: {
431: return warningString;
432: }
433:
434:
439: public Locale getLocale()
440: {
441: return locale == null ? Locale.getDefault() : locale;
442: }
443:
444:
451:
452:
457: public void setCursor(Cursor cursor)
458: {
459: super.setCursor(cursor);
460: }
461:
462: public Window getOwner()
463: {
464: return (Window) parent;
465: }
466:
467:
468: public Window[] getOwnedWindows()
469: {
470: Window [] trimmedList;
471: synchronized (getTreeLock ())
472: {
473:
474: Window [] validList = new Window [ownedWindows.size()];
475:
476: Iterator e = ownedWindows.iterator();
477: int numValid = 0;
478: while (e.hasNext())
479: {
480: Window w = (Window)(((Reference) e.next()).get());
481: if (w != null)
482: validList[numValid++] = w;
483: else
484:
485: e.remove();
486: }
487:
488: if (numValid != validList.length)
489: {
490: trimmedList = new Window [numValid];
491: System.arraycopy (validList, 0, trimmedList, 0, numValid);
492: }
493: else
494: trimmedList = validList;
495: }
496: return trimmedList;
497: }
498:
499:
505: public synchronized void addWindowListener(WindowListener listener)
506: {
507: windowListener = AWTEventMulticaster.add(windowListener, listener);
508: }
509:
510:
516: public synchronized void removeWindowListener(WindowListener listener)
517: {
518: windowListener = AWTEventMulticaster.remove(windowListener, listener);
519: }
520:
521:
526: public synchronized WindowListener[] getWindowListeners()
527: {
528: return (WindowListener[])
529: AWTEventMulticaster.getListeners(windowListener,
530: WindowListener.class);
531: }
532:
533:
539: public synchronized WindowFocusListener[] getWindowFocusListeners()
540: {
541: return (WindowFocusListener[])
542: AWTEventMulticaster.getListeners(windowFocusListener,
543: WindowFocusListener.class);
544: }
545:
546:
552: public synchronized WindowStateListener[] getWindowStateListeners()
553: {
554: return (WindowStateListener[])
555: AWTEventMulticaster.getListeners(windowStateListener,
556: WindowStateListener.class);
557: }
558:
559:
562: public void addWindowFocusListener (WindowFocusListener wfl)
563: {
564: windowFocusListener = AWTEventMulticaster.add (windowFocusListener, wfl);
565: }
566:
567:
572: public void addWindowStateListener (WindowStateListener wsl)
573: {
574: windowStateListener = AWTEventMulticaster.add (windowStateListener, wsl);
575: }
576:
577:
580: public void removeWindowFocusListener (WindowFocusListener wfl)
581: {
582: windowFocusListener = AWTEventMulticaster.remove (windowFocusListener, wfl);
583: }
584:
585:
590: public void removeWindowStateListener (WindowStateListener wsl)
591: {
592: windowStateListener = AWTEventMulticaster.remove (windowStateListener, wsl);
593: }
594:
595:
605: public EventListener[] getListeners(Class listenerType)
606: {
607: if (listenerType == WindowListener.class)
608: return getWindowListeners();
609: return super.getListeners(listenerType);
610: }
611:
612: void dispatchEventImpl(AWTEvent e)
613: {
614:
615: if (e.id <= WindowEvent.WINDOW_LAST
616: && e.id >= WindowEvent.WINDOW_FIRST
617: && (windowListener != null
618: || windowFocusListener != null
619: || windowStateListener != null
620: || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0))
621: processEvent(e);
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 windowFocusOwner;
745: }
746:
747:
756: void setFocusOwner (Component windowFocusOwner)
757: {
758: this.windowFocusOwner = windowFocusOwner;
759: }
760:
761:
768: public boolean postEvent(Event e)
769: {
770: return handleEvent (e);
771: }
772:
773:
783: public boolean isShowing()
784: {
785: return isVisible();
786: }
787:
788: public void setLocationRelativeTo (Component c)
789: {
790: if (c == null || !c.isShowing ())
791: {
792: int x = 0;
793: int y = 0;
794:
795: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment ();
796: Point center = ge.getCenterPoint ();
797: x = center.x - (width / 2);
798: y = center.y - (height / 2);
799: setLocation (x, y);
800: }
801:
802: }
803:
804:
807: private class WindowBltBufferStrategy extends BltBufferStrategy
808: {
809:
816: WindowBltBufferStrategy(int numBuffers, boolean accelerated)
817: {
818: super(numBuffers,
819: new BufferCapabilities(new ImageCapabilities(accelerated),
820: new ImageCapabilities(accelerated),
821: BufferCapabilities.FlipContents.COPIED));
822: }
823: }
824:
825:
828: private class WindowFlipBufferStrategy extends FlipBufferStrategy
829: {
830:
838: WindowFlipBufferStrategy(int numBuffers)
839: throws AWTException
840: {
841: super(numBuffers,
842: new BufferCapabilities(new ImageCapabilities(true),
843: new ImageCapabilities(true),
844: BufferCapabilities.FlipContents.COPIED));
845: }
846: }
847:
848:
871: public void createBufferStrategy(int numBuffers)
872: {
873: if (numBuffers < 1)
874: throw new IllegalArgumentException("Window.createBufferStrategy: number"
875: + " of buffers is less than one");
876:
877: if (!isDisplayable())
878: throw new IllegalStateException("Window.createBufferStrategy: window is"
879: + " not displayable");
880:
881: BufferStrategy newStrategy = null;
882:
883:
884: try
885: {
886: newStrategy = new WindowFlipBufferStrategy(numBuffers);
887: }
888: catch (AWTException e)
889: {
890: }
891:
892:
893: if (newStrategy == null)
894: newStrategy = new WindowBltBufferStrategy(numBuffers, true);
895:
896: bufferStrategy = newStrategy;
897: }
898:
899:
918: public void createBufferStrategy(int numBuffers,
919: BufferCapabilities caps)
920: {
921: if (numBuffers < 1)
922: throw new IllegalArgumentException("Window.createBufferStrategy: number"
923: + " of buffers is less than one");
924:
925: if (caps == null)
926: throw new IllegalArgumentException("Window.createBufferStrategy:"
927: + " capabilities object is null");
928:
929:
930: if (caps.isPageFlipping())
931: {
932: try
933: {
934: bufferStrategy = new WindowFlipBufferStrategy(numBuffers);
935: }
936: catch (AWTException e)
937: {
938: }
939: }
940: else
941: bufferStrategy = new WindowBltBufferStrategy(numBuffers, true);
942: }
943:
944:
950: public BufferStrategy getBufferStrategy()
951: {
952: return bufferStrategy;
953: }
954:
955:
960: public void applyResourceBundle(ResourceBundle rb)
961: {
962: throw new Error ("Not implemented");
963: }
964:
965:
970: public void applyResourceBundle(String rbName)
971: {
972: ResourceBundle rb = ResourceBundle.getBundle(rbName, Locale.getDefault(),
973: ClassLoader.getSystemClassLoader());
974: if (rb != null)
975: applyResourceBundle(rb);
976: }
977:
978:
984: public AccessibleContext getAccessibleContext()
985: {
986:
987: if (accessibleContext == null)
988: accessibleContext = new AccessibleAWTWindow();
989: return accessibleContext;
990: }
991:
992:
997: public GraphicsConfiguration getGraphicsConfiguration()
998: {
999: if (graphicsConfiguration != null) return graphicsConfiguration;
1000: if (peer != null) return peer.getGraphicsConfiguration();
1001: return null;
1002: }
1003:
1004: protected void processWindowFocusEvent(WindowEvent event)
1005: {
1006: if (windowFocusListener != null)
1007: {
1008: switch (event.getID ())
1009: {
1010: case WindowEvent.WINDOW_GAINED_FOCUS:
1011: windowFocusListener.windowGainedFocus (event);
1012: break;
1013:
1014: case WindowEvent.WINDOW_LOST_FOCUS:
1015: windowFocusListener.windowLostFocus (event);
1016: break;
1017:
1018: default:
1019: break;
1020: }
1021: }
1022: }
1023:
1024:
1027: protected void processWindowStateEvent(WindowEvent event)
1028: {
1029: if (windowStateListener != null
1030: && event.getID () == WindowEvent.WINDOW_STATE_CHANGED)
1031: windowStateListener.windowStateChanged (event);
1032: }
1033:
1034:
1039: public final boolean isFocusableWindow ()
1040: {
1041: if (getFocusableWindowState () == false)
1042: return false;
1043:
1044: if (this instanceof Dialog
1045: || this instanceof Frame)
1046: return true;
1047:
1048:
1049:
1050: return false;
1051: }
1052:
1053:
1058: public boolean getFocusableWindowState ()
1059: {
1060: return focusableWindowState;
1061: }
1062:
1063:
1068: public void setFocusableWindowState (boolean focusableWindowState)
1069: {
1070: this.focusableWindowState = focusableWindowState;
1071: }
1072:
1073:
1074:
1075:
1076:
1077:
1078:
1079:
1080:
1081:
1082:
1083:
1084:
1085:
1086: void setBoundsCallback (int x, int y, int w, int h)
1087: {
1088: if (this.x == x && this.y == y && width == w && height == h)
1089: return;
1090: invalidate();
1091: boolean resized = width != w || height != h;
1092: boolean moved = this.x != x || this.y != y;
1093: this.x = x;
1094: this.y = y;
1095: width = w;
1096: height = h;
1097: if (resized && isShowing ())
1098: {
1099: ComponentEvent ce =
1100: new ComponentEvent(this, ComponentEvent.COMPONENT_RESIZED);
1101: getToolkit().getSystemEventQueue().postEvent(ce);
1102: }
1103: if (moved && isShowing ())
1104: {
1105: ComponentEvent ce =
1106: new ComponentEvent(this, ComponentEvent.COMPONENT_MOVED);
1107: getToolkit().getSystemEventQueue().postEvent(ce);
1108: }
1109: }
1110:
1111:
1116: String generateName()
1117: {
1118: return "win" + getUniqueLong();
1119: }
1120:
1121: private static synchronized long getUniqueLong()
1122: {
1123: return next_window_number++;
1124: }
1125: }