1 7 package java.awt; 8 9 import java.applet.Applet ; 10 import java.awt.peer.WindowPeer; 11 import java.awt.peer.ComponentPeer; 12 import java.awt.event.*; 13 import java.awt.image.BufferStrategy ; 14 import java.util.Vector ; 15 import java.util.Locale ; 16 import java.util.EventListener ; 17 import java.util.Set ; 18 import java.io.Serializable ; 19 import java.io.ObjectOutputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.IOException ; 22 import java.io.OptionalDataException ; 23 import java.awt.im.InputContext ; 24 import java.util.ResourceBundle ; 25 import java.lang.ref.WeakReference ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.security.AccessController ; 28 import java.security.PrivilegedAction ; 29 import javax.accessibility.*; 30 import java.beans.PropertyChangeListener ; 31 import sun.security.action.GetPropertyAction; 32 import sun.security.util.SecurityConstants; 33 import sun.awt.DebugHelper; 34 35 115 public class Window extends Container implements Accessible { 116 117 128 String warningString; 129 130 134 private transient Component temporaryLostComponent; 135 136 static boolean systemSyncLWRequests = false; 137 boolean syncLWRequests = false; 138 transient boolean beforeFirstShow = true; 139 140 static final int OPENED = 0x01; 141 142 149 int state; 150 151 158 private boolean alwaysOnTop; 159 160 166 transient Vector <WeakReference <Window >> ownedWindowList = 167 new Vector <WeakReference <Window >>(); 168 private transient WeakReference <Window > weakThis; 169 170 transient boolean showWithParent; 171 172 transient WindowListener windowListener; 173 transient WindowStateListener windowStateListener; 174 transient WindowFocusListener windowFocusListener; 175 176 transient InputContext inputContext; 177 private transient Object inputContextLock = new Object (); 178 179 185 private FocusManager focusMgr; 186 187 195 private boolean focusableWindowState = true; 196 197 private static final String base = "win"; 198 private static int nameCounter = 0; 199 200 203 private static final long serialVersionUID = 4497834738069338734L; 204 205 private static final DebugHelper dbg = DebugHelper.create(Window .class); 206 207 private static final boolean locationByPlatformProp; 208 209 214 private transient boolean modalExcluded = false; 215 216 static { 217 218 Toolkit.loadLibraries(); 219 if (!GraphicsEnvironment.isHeadless()) { 220 initIDs(); 221 } 222 223 String s = (String ) java.security.AccessController.doPrivileged( 224 new GetPropertyAction("java.awt.syncLWRequests")); 225 systemSyncLWRequests = (s != null && s.equals("true")); 226 s = (String ) java.security.AccessController.doPrivileged( 227 new GetPropertyAction("java.awt.Window.locationByPlatform")); 228 locationByPlatformProp = (s != null && s.equals("true")); 229 } 230 231 235 private static native void initIDs(); 236 237 256 Window(GraphicsConfiguration gc) { 257 init(gc); 258 } 259 260 private void init(GraphicsConfiguration gc) { 261 if (GraphicsEnvironment.isHeadless()) { 262 throw new IllegalArgumentException ("headless environment"); 263 } 264 265 syncLWRequests = systemSyncLWRequests; 266 267 setWarningString(); 268 this.cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); 269 this.visible = false; 270 if (gc == null) { 271 this.graphicsConfig = 272 GraphicsEnvironment.getLocalGraphicsEnvironment(). 273 getDefaultScreenDevice().getDefaultConfiguration(); 274 } else { 275 this.graphicsConfig = gc; 276 } 277 if (graphicsConfig.getDevice().getType() != 278 GraphicsDevice.TYPE_RASTER_SCREEN) { 279 throw new IllegalArgumentException ("not a screen device"); 280 } 281 setLayout(new BorderLayout ()); 282 283 284 285 Rectangle screenBounds = graphicsConfig.getBounds(); 286 Insets screenInsets = getToolkit().getScreenInsets(graphicsConfig); 287 int x = getX() + screenBounds.x + screenInsets.left; 288 int y = getY() + screenBounds.y + screenInsets.top; 289 if (x != this.x || y != this.y) { 290 setLocation(x, y); 291 292 setLocationByPlatform(locationByPlatformProp); 293 } 294 } 295 296 317 Window() throws HeadlessException { 318 GraphicsEnvironment.checkHeadless(); 319 init((GraphicsConfiguration )null); 320 } 321 322 344 public Window(Frame owner) { 345 this(owner == null ? (GraphicsConfiguration )null : 346 owner.getGraphicsConfiguration()); 347 ownedInit(owner); 348 } 349 350 372 public Window(Window owner) { 373 this(owner == null ? (GraphicsConfiguration )null : 374 owner.getGraphicsConfiguration()); 375 ownedInit(owner); 376 } 377 378 408 public Window(Window owner, GraphicsConfiguration gc) { 409 this(gc); 410 ownedInit(owner); 411 } 412 413 private void ownedInit(Window owner) { 414 if (owner == null) { 415 throw new IllegalArgumentException ("null owner window"); 416 } 417 this.parent = owner; 418 this.weakThis = new WeakReference <Window >(this); 419 owner.addOwnedWindow(weakThis); 420 modalExcluded = owner.modalExcluded; 421 } 422 423 428 protected void finalize() throws Throwable { 429 if (parent != null) { 434 ((Window )parent).removeOwnedWindow(weakThis); 435 } 436 super.finalize(); 437 } 438 439 443 String constructComponentName() { 444 synchronized (getClass()) { 445 return base + nameCounter++; 446 } 447 } 448 449 458 public void addNotify() { 459 synchronized (getTreeLock()) { 460 Container parent = this.parent; 461 if (parent != null && parent.getPeer() == null) { 462 parent.addNotify(); 463 } 464 465 if (peer == null) 466 peer = getToolkit().createWindow(this); 467 super.addNotify(); 468 } 469 } 470 471 479 public void pack() { 480 Container parent = this.parent; 481 if (parent != null && parent.getPeer() == null) { 482 parent.addNotify(); 483 } 484 if (peer == null) { 485 addNotify(); 486 } 487 Dimension newSize = getPreferredSize(); 488 if (peer != null) { 489 setClientSize(newSize.width, newSize.height); 490 } 491 492 if(beforeFirstShow) { 493 isPacked = true; 494 } 495 496 validate(); 497 } 498 499 500 void setClientSize(int w, int h) { 501 synchronized (getTreeLock()) { 502 setBoundsOp(ComponentPeer.SET_CLIENT_SIZE); 503 setBounds(x, y, w, h); 504 } 505 } 506 507 511 @Deprecated 512 public void show() { 513 if (peer == null) { 514 addNotify(); 515 } 516 validate(); 517 518 if (visible) { 519 toFront(); 520 } else { 521 beforeFirstShow = false; 522 super.show(); 523 locationByPlatform = false; 524 for (int i = 0; i < ownedWindowList.size(); i++) { 525 Window child = ownedWindowList.elementAt(i).get(); 526 if ((child != null) && child.showWithParent) { 527 child.show(); 528 child.showWithParent = false; 529 } } if (this instanceof Frame || this instanceof Dialog ) { 532 updateChildFocusableWindowState(this); 533 } 534 } 535 536 if ((state & OPENED) == 0) { 538 postWindowEvent(WindowEvent.WINDOW_OPENED); 539 state |= OPENED; 540 } 541 } 542 543 static void updateChildFocusableWindowState(Window w) { 544 if (w.getPeer() != null && w.isShowing()) { 545 ((WindowPeer)w.getPeer()).updateFocusableWindowState(); 546 } 547 for (int i = 0; i < w.ownedWindowList.size(); i++) { 548 Window child = w.ownedWindowList.elementAt(i).get(); 549 if (child != null) { 550 updateChildFocusableWindowState(child); 551 } 552 } 553 } 554 555 synchronized void postWindowEvent(int id) { 556 if (windowListener != null 557 || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0 558 || Toolkit.enabledOnToolkit(AWTEvent.WINDOW_EVENT_MASK)) { 559 WindowEvent e = new WindowEvent(this, id); 560 Toolkit.getEventQueue().postEvent(e); 561 } 562 } 563 564 568 @Deprecated 569 public void hide() { 570 synchronized(ownedWindowList) { 571 for (int i = 0; i < ownedWindowList.size(); i++) { 572 Window child = ownedWindowList.elementAt(i).get(); 573 if ((child != null) && child.visible) { 574 child.hide(); 575 child.showWithParent = true; 576 } 577 } 578 } 579 super.hide(); 580 } 581 582 final void clearMostRecentFocusOwnerOnHide() { 583 584 } 585 586 609 public void dispose() { 610 doDispose(); 611 } 612 613 619 void disposeImpl() { 620 dispose(); 621 if (getPeer() != null) { 622 doDispose(); 623 } 624 } 625 626 void doDispose() { 627 class DisposeAction implements Runnable { 628 public void run() { 629 Object [] ownedWindowArray; 630 synchronized(ownedWindowList) { 631 ownedWindowArray = new Object [ownedWindowList.size()]; 632 ownedWindowList.copyInto(ownedWindowArray); 633 } 634 for (int i = 0; i < ownedWindowArray.length; i++) { 635 Window child = (Window ) (((WeakReference ) 636 (ownedWindowArray[i])).get()); 637 if (child != null) { 638 child.disposeImpl(); 639 } 640 } 641 hide(); 642 beforeFirstShow = true; 643 removeNotify(); 644 synchronized (inputContextLock) { 645 if (inputContext != null) { 646 inputContext.dispose(); 647 inputContext = null; 648 } 649 } 650 clearCurrentFocusCycleRootOnHide(); 651 } 652 } 653 DisposeAction action = new DisposeAction(); 654 if (EventQueue.isDispatchThread()) { 655 action.run(); 656 } 657 else { 658 try { 659 EventQueue.invokeAndWait(action); 660 } 661 catch (InterruptedException e) { 662 System.err.println("Disposal was interrupted:"); 663 e.printStackTrace(); 664 } 665 catch (InvocationTargetException e) { 666 System.err.println("Exception during disposal:"); 667 e.printStackTrace(); 668 } 669 } 670 postWindowEvent(WindowEvent.WINDOW_CLOSED); 674 } 675 676 681 void adjustListeningChildrenOnParent(long mask, int num) { 682 } 683 684 void adjustDecendantsOnParent(int num) { 686 } 689 690 722 public void toFront() { 723 if (visible) { 724 WindowPeer peer = (WindowPeer)this.peer; 725 if (peer != null) { 726 peer.toFront(); 727 } 728 } 729 } 730 731 756 public void toBack() { 757 if (visible) { 758 WindowPeer peer = (WindowPeer)this.peer; 759 if (peer != null) { 760 peer.toBack(); 761 } 762 } 763 } 764 765 772 public Toolkit getToolkit() { 773 return Toolkit.getDefaultToolkit(); 774 } 775 776 793 public final String getWarningString() { 794 return warningString; 795 } 796 797 private void setWarningString() { 798 warningString = null; 799 SecurityManager sm = System.getSecurityManager(); 800 if (sm != null) { 801 if (!sm.checkTopLevelWindow(this)) { 802 warningString = (String ) AccessController.doPrivileged( 806 new GetPropertyAction("awt.appletWarning", 807 "Java Applet Window")); 808 } 809 } 810 } 811 812 821 public Locale getLocale() { 822 if (this.locale == null) { 823 return Locale.getDefault(); 824 } 825 return this.locale; 826 } 827 828 834 public InputContext getInputContext() { 835 if (inputContext == null) { 836 synchronized (inputContextLock) { 837 if (inputContext == null) { 838 inputContext = InputContext.getInstance(); 839 } 840 } 841 } 842 return inputContext; 843 } 844 845 855 public void setCursor(Cursor cursor) { 856 if (cursor == null) { 857 cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); 858 } 859 super.setCursor(cursor); 860 } 861 862 866 public Window getOwner() { 867 return (Window )parent; 868 } 869 870 875 public Window [] getOwnedWindows() { 876 Window realCopy[]; 877 878 synchronized(ownedWindowList) { 879 int fullSize = ownedWindowList.size(); 885 int realSize = 0; 886 Window fullCopy[] = new Window [fullSize]; 887 888 for (int i = 0; i < fullSize; i++) { 889 fullCopy[realSize] = ownedWindowList.elementAt(i).get(); 890 891 if (fullCopy[realSize] != null) { 892 realSize++; 893 } 894 } 895 896 if (fullSize != realSize) { 897 realCopy = new Window [realSize]; 898 System.arraycopy(fullCopy, 0, realCopy, 0, realSize); 899 } else { 900 realCopy = fullCopy; 901 } 902 } 903 904 return realCopy; 905 } 906 907 916 public synchronized void addWindowListener(WindowListener l) { 917 if (l == null) { 918 return; 919 } 920 newEventsOnly = true; 921 windowListener = AWTEventMulticaster.add(windowListener, l); 922 } 923 924 934 public synchronized void addWindowStateListener(WindowStateListener l) { 935 if (l == null) { 936 return; 937 } 938 windowStateListener = AWTEventMulticaster.add(windowStateListener, l); 939 newEventsOnly = true; 940 } 941 942 951 public synchronized void addWindowFocusListener(WindowFocusListener l) { 952 if (l == null) { 953 return; 954 } 955 windowFocusListener = AWTEventMulticaster.add(windowFocusListener, l); 956 newEventsOnly = true; 957 } 958 959 968 public synchronized void removeWindowListener(WindowListener l) { 969 if (l == null) { 970 return; 971 } 972 windowListener = AWTEventMulticaster.remove(windowListener, l); 973 } 974 975 986 public synchronized void removeWindowStateListener(WindowStateListener l) { 987 if (l == null) { 988 return; 989 } 990 windowStateListener = AWTEventMulticaster.remove(windowStateListener, l); 991 } 992 993 1002 public synchronized void removeWindowFocusListener(WindowFocusListener l) { 1003 if (l == null) { 1004 return; 1005 } 1006 windowFocusListener = AWTEventMulticaster.remove(windowFocusListener, l); 1007 } 1008 1009 1021 public synchronized WindowListener[] getWindowListeners() { 1022 return (WindowListener[])(getListeners(WindowListener.class)); 1023 } 1024 1025 1037 public synchronized WindowFocusListener[] getWindowFocusListeners() { 1038 return (WindowFocusListener[])(getListeners(WindowFocusListener.class)); 1039 } 1040 1041 1053 public synchronized WindowStateListener[] getWindowStateListeners() { 1054 return (WindowStateListener[])(getListeners(WindowStateListener.class)); 1055 } 1056 1057 1058 1092 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 1093 EventListener l = null; 1094 if (listenerType == WindowFocusListener.class) { 1095 l = windowFocusListener; 1096 } else if (listenerType == WindowStateListener.class) { 1097 l = windowStateListener; 1098 } else if (listenerType == WindowListener.class) { 1099 l = windowListener; 1100 } else { 1101 return super.getListeners(listenerType); 1102 } 1103 return AWTEventMulticaster.getListeners(l, listenerType); 1104 } 1105 1106 boolean eventEnabled(AWTEvent e) { 1108 switch(e.id) { 1109 case WindowEvent.WINDOW_OPENED: 1110 case WindowEvent.WINDOW_CLOSING: 1111 case WindowEvent.WINDOW_CLOSED: 1112 case WindowEvent.WINDOW_ICONIFIED: 1113 case WindowEvent.WINDOW_DEICONIFIED: 1114 case WindowEvent.WINDOW_ACTIVATED: 1115 case WindowEvent.WINDOW_DEACTIVATED: 1116 if ((eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0 || 1117 windowListener != null) { 1118 return true; 1119 } 1120 return false; 1121 case WindowEvent.WINDOW_GAINED_FOCUS: 1122 case WindowEvent.WINDOW_LOST_FOCUS: 1123 if ((eventMask & AWTEvent.WINDOW_FOCUS_EVENT_MASK) != 0 || 1124 windowFocusListener != null) { 1125 return true; 1126 } 1127 return false; 1128 case WindowEvent.WINDOW_STATE_CHANGED: 1129 if ((eventMask & AWTEvent.WINDOW_STATE_EVENT_MASK) != 0 || 1130 windowStateListener != null) { 1131 return true; 1132 } 1133 return false; 1134 default: 1135 break; 1136 } 1137 return super.eventEnabled(e); 1138 } 1139 1140 1151 protected void processEvent(AWTEvent e) { 1152 if (e instanceof WindowEvent) { 1153 switch (e.getID()) { 1154 case WindowEvent.WINDOW_OPENED: 1155 case WindowEvent.WINDOW_CLOSING: 1156 case WindowEvent.WINDOW_CLOSED: 1157 case WindowEvent.WINDOW_ICONIFIED: 1158 case WindowEvent.WINDOW_DEICONIFIED: 1159 case WindowEvent.WINDOW_ACTIVATED: 1160 case WindowEvent.WINDOW_DEACTIVATED: 1161 processWindowEvent((WindowEvent)e); 1162 break; 1163 case WindowEvent.WINDOW_GAINED_FOCUS: 1164 case WindowEvent.WINDOW_LOST_FOCUS: 1165 processWindowFocusEvent((WindowEvent)e); 1166 break; 1167 case WindowEvent.WINDOW_STATE_CHANGED: 1168 processWindowStateEvent((WindowEvent)e); 1169 default: 1170 break; 1171 } 1172 return; 1173 } 1174 super.processEvent(e); 1175 } 1176 1177 1195 protected void processWindowEvent(WindowEvent e) { 1196 WindowListener listener = windowListener; 1197 if (listener != null) { 1198 switch(e.getID()) { 1199 case WindowEvent.WINDOW_OPENED: 1200 listener.windowOpened(e); 1201 break; 1202 case WindowEvent.WINDOW_CLOSING: 1203 listener.windowClosing(e); 1204 break; 1205 case WindowEvent.WINDOW_CLOSED: 1206 listener.windowClosed(e); 1207 break; 1208 case WindowEvent.WINDOW_ICONIFIED: 1209 listener.windowIconified(e); 1210 break; 1211 case WindowEvent.WINDOW_DEICONIFIED: 1212 listener.windowDeiconified(e); 1213 break; 1214 case WindowEvent.WINDOW_ACTIVATED: 1215 listener.windowActivated(e); 1216 break; 1217 case WindowEvent.WINDOW_DEACTIVATED: 1218 listener.windowDeactivated(e); 1219 break; 1220 default: 1221 break; 1222 } 1223 } 1224 } 1225 1226 1244 protected void processWindowFocusEvent(WindowEvent e) { 1245 WindowFocusListener listener = windowFocusListener; 1246 if (listener != null) { 1247 switch (e.getID()) { 1248 case WindowEvent.WINDOW_GAINED_FOCUS: 1249 listener.windowGainedFocus(e); 1250 break; 1251 case WindowEvent.WINDOW_LOST_FOCUS: 1252 listener.windowLostFocus(e); 1253 break; 1254 default: 1255 break; 1256 } 1257 } 1258 } 1259 1260 1280 protected void processWindowStateEvent(WindowEvent e) { 1281 WindowStateListener listener = windowStateListener; 1282 if (listener != null) { 1283 switch (e.getID()) { 1284 case WindowEvent.WINDOW_STATE_CHANGED: 1285 listener.windowStateChanged(e); 1286 break; 1287 default: 1288 break; 1289 } 1290 } 1291 } 1292 1293 1299 void preProcessKeyEvent(KeyEvent e) { 1300 if (e.isActionKey() && e.getKeyCode() == KeyEvent.VK_F1 && 1302 e.isControlDown() && e.isShiftDown() && 1303 e.getID() == KeyEvent.KEY_PRESSED) { 1304 list(System.out, 0); 1305 } 1306 } 1307 1308 void postProcessKeyEvent(KeyEvent e) { 1309 } 1311 1312 1366 public final void setAlwaysOnTop(boolean alwaysOnTop) 1367 throws SecurityException 1368 { 1369 SecurityManager security = System.getSecurityManager(); 1370 if (security != null) { 1371 security.checkPermission(SecurityConstants.SET_WINDOW_ALWAYS_ON_TOP_PERMISSION); 1372 } 1373 1374 boolean oldAlwaysOnTop; 1375 synchronized(this) { 1376 oldAlwaysOnTop = this.alwaysOnTop; 1377 this.alwaysOnTop = alwaysOnTop; 1378 } 1379 if (oldAlwaysOnTop != alwaysOnTop ) { 1380 WindowPeer peer = (WindowPeer)this.peer; 1381 synchronized(getTreeLock()) { 1382 if (peer != null) { 1383 peer.updateAlwaysOnTop(); 1384 } 1385 } 1386 firePropertyChange("alwaysOnTop", oldAlwaysOnTop, alwaysOnTop); 1387 } 1388 } 1389 1390 1397 public final boolean isAlwaysOnTop() { 1398 return alwaysOnTop; 1399 } 1400 1401 1402 1411 public Component getFocusOwner() { 1412 return (isFocused()) 1413 ? KeyboardFocusManager.getCurrentKeyboardFocusManager(). 1414 getFocusOwner() 1415 : null; 1416 } 1417 1418 1435 public Component getMostRecentFocusOwner() { 1436 if (isFocused()) { 1437 return getFocusOwner(); 1438 } else { 1439 Component mostRecent = 1440 KeyboardFocusManager.getMostRecentFocusOwner(this); 1441 if (mostRecent != null) { 1442 return mostRecent; 1443 } else { 1444 return (isFocusableWindow()) 1445 ? getFocusTraversalPolicy().getInitialComponent(this) 1446 : null; 1447 } 1448 } 1449 } 1450 1451 1462 public boolean isActive() { 1463 return (KeyboardFocusManager.getCurrentKeyboardFocusManager(). 1464 getActiveWindow() == this); 1465 } 1466 1467 1480 public boolean isFocused() { 1481 return (KeyboardFocusManager.getCurrentKeyboardFocusManager(). 1482 getGlobalFocusedWindow() == this); 1483 } 1484 1485 1512 public Set <AWTKeyStroke > getFocusTraversalKeys(int id) { 1513 if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) { 1514 throw new IllegalArgumentException ("invalid focus traversal key identifier"); 1515 } 1516 1517 Set keystrokes = (focusTraversalKeys != null) 1519 ? focusTraversalKeys[id] 1520 : null; 1521 1522 if (keystrokes != null) { 1523 return keystrokes; 1524 } else { 1525 return KeyboardFocusManager.getCurrentKeyboardFocusManager(). 1526 getDefaultFocusTraversalKeys(id); 1527 } 1528 } 1529 1530 1540 public final void setFocusCycleRoot(boolean focusCycleRoot) { 1541 } 1542 1543 1553 public final boolean isFocusCycleRoot() { 1554 return true; 1555 } 1556 1557 1565 public final Container getFocusCycleRootAncestor() { 1566 return null; 1567 } 1568 1569 1589 public final boolean isFocusableWindow() { 1590 if (!getFocusableWindowState()) { 1593 return false; 1594 } 1595 1596 if (this instanceof Frame || this instanceof Dialog ) { 1598 return true; 1599 } 1600 1601 if (getFocusTraversalPolicy().getDefaultComponent(this) == null) { 1604 return false; 1605 } 1606 1607 for (Window owner = getOwner(); owner != null; 1610 owner = owner.getOwner()) 1611 { 1612 if (owner instanceof Frame || owner instanceof Dialog ) { 1613 return owner.isShowing(); 1614 } 1615 } 1616 1617 return false; 1618 } 1619 1620 1640 public boolean getFocusableWindowState() { 1641 return focusableWindowState; 1642 } 1643 1644 1674 public void setFocusableWindowState(boolean focusableWindowState) { 1675 boolean oldFocusableWindowState; 1676 synchronized (this) { 1677 oldFocusableWindowState = this.focusableWindowState; 1678 this.focusableWindowState = focusableWindowState; 1679 } 1680 WindowPeer peer = (WindowPeer)this.peer; 1681 if (peer != null) { 1682 peer.updateFocusableWindowState(); 1683 } 1684 firePropertyChange("focusableWindowState", oldFocusableWindowState, 1685 focusableWindowState); 1686 if (oldFocusableWindowState && !focusableWindowState && isFocused()) { 1687 for (Window owner = (Window )getParent(); 1688 owner != null; 1689 owner = (Window )owner.getParent()) 1690 { 1691 Component toFocus = 1692 KeyboardFocusManager.getMostRecentFocusOwner(owner); 1693 if (toFocus != null && toFocus.requestFocus(false)) { 1694 return; 1695 } 1696 } 1697 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 1698 clearGlobalFocusOwner(); 1699 } 1700 } 1701 1702 1737 public void addPropertyChangeListener(PropertyChangeListener listener) { 1738 super.addPropertyChangeListener(listener); 1739 } 1740 1741 1777 public void addPropertyChangeListener(String propertyName, 1778 PropertyChangeListener listener) { 1779 super.addPropertyChangeListener(propertyName, listener); 1780 } 1781 1782 1786 void dispatchEventImpl(AWTEvent e) { 1787 if (e.getID() == ComponentEvent.COMPONENT_RESIZED) { 1788 invalidate(); 1789 validate(); 1790 } 1791 super.dispatchEventImpl(e); 1792 } 1793 1794 1798 @Deprecated 1799 public boolean postEvent(Event e) { 1800 if (handleEvent(e)) { 1801 e.consume(); 1802 return true; 1803 } 1804 return false; 1805 } 1806 1807 1811 public boolean isShowing() { 1812 return visible; 1813 } 1814 1815 1819 @Deprecated 1820 public void applyResourceBundle(ResourceBundle rb) { 1821 applyComponentOrientation(ComponentOrientation.getOrientation(rb)); 1822 } 1823 1824 1828 @Deprecated 1829 public void applyResourceBundle(String rbName) { 1830 applyResourceBundle(ResourceBundle.getBundle(rbName)); 1831 } 1832 1833 1836 void addOwnedWindow(WeakReference weakWindow) { 1837 if (weakWindow != null) { 1838 synchronized(ownedWindowList) { 1839 if (!ownedWindowList.contains(weakWindow)) { 1842 ownedWindowList.addElement(weakWindow); 1843 } 1844 } 1845 } 1846 } 1847 1848 void removeOwnedWindow(WeakReference weakWindow) { 1849 if (weakWindow != null) { 1850 ownedWindowList.removeElement(weakWindow); 1853 } 1854 } 1855 1856 void connectOwnedWindow(Window child) { 1857 WeakReference weakChild = new WeakReference (child); 1858 child.weakThis = weakChild; 1859 child.parent = this; 1860 addOwnedWindow(weakChild); 1861 } 1862 1863 1868 private int windowSerializedDataVersion = 2; 1869 1870 1894 private void writeObject(ObjectOutputStream s) throws IOException { 1895 synchronized (this) { 1896 focusMgr = new FocusManager(); 1899 focusMgr.focusRoot = this; 1900 focusMgr.focusOwner = getMostRecentFocusOwner(); 1901 1902 s.defaultWriteObject(); 1903 1904 focusMgr = null; 1906 1907 AWTEventMulticaster.save(s, windowListenerK, windowListener); 1908 AWTEventMulticaster.save(s, windowFocusListenerK, windowFocusListener); 1909 AWTEventMulticaster.save(s, windowStateListenerK, windowStateListener); 1910 } 1911 1912 s.writeObject(null); 1913 1914 synchronized (ownedWindowList) { 1915 for (int i = 0; i < ownedWindowList.size(); i++) { 1916 Window child = ownedWindowList.elementAt(i).get(); 1917 if (child != null) { 1918 s.writeObject(ownedWindowK); 1919 s.writeObject(child); 1920 } 1921 } 1922 } 1923 s.writeObject(null); 1924 } 1925 1926 1940 private void readObject(ObjectInputStream s) 1941 throws ClassNotFoundException , IOException , HeadlessException 1942 { 1943 GraphicsEnvironment.checkHeadless(); 1944 setWarningString(); 1945 inputContextLock = new Object (); 1946 1947 visible = false; 1949 weakThis = new WeakReference (this); 1950 ObjectInputStream.GetField f = s.readFields(); 1951 1952 syncLWRequests = f.get("syncLWRequests", systemSyncLWRequests); 1953 state = f.get("state", 0); 1954 focusableWindowState = f.get("focusableWindowState", true); 1955 windowSerializedDataVersion = f.get("windowSerializedDataVersion", 1); 1956 locationByPlatform = f.get("locationByPlatform", locationByPlatformProp); 1957 focusMgr = (FocusManager)f.get("focusMgr", null); 1959 boolean aot = f.get("alwaysOnTop", false); 1960 if(aot) { 1961 setAlwaysOnTop(aot); } 1963 1964 ownedWindowList = new Vector (); 1965 if (windowSerializedDataVersion < 2) { 1966 if (focusMgr != null) { 1970 if (focusMgr.focusOwner != null) { 1971 KeyboardFocusManager. 1972 setMostRecentFocusOwner(this, focusMgr.focusOwner); 1973 } 1974 } 1975 1976 focusableWindowState = true; 1980 } 1981 1982 Object keyOrNull; 1983 while(null != (keyOrNull = s.readObject())) { 1984 String key = ((String )keyOrNull).intern(); 1985 1986 if (windowListenerK == key) { 1987 addWindowListener((WindowListener)(s.readObject())); 1988 } else if (windowFocusListenerK == key) { 1989 addWindowFocusListener((WindowFocusListener)(s.readObject())); 1990 } else if (windowStateListenerK == key) { 1991 addWindowStateListener((WindowStateListener)(s.readObject())); 1992 } else s.readObject(); 1994 } 1995 1996 try { 1997 while (null != (keyOrNull = s.readObject())) { 1998 String key = ((String )keyOrNull).intern(); 1999 2000 if (ownedWindowK == key) 2001 connectOwnedWindow((Window ) s.readObject()); 2002 2003 else s.readObject(); 2005 } 2006 } 2007 catch (OptionalDataException e) { 2008 } 2011 2012 } 2013 2014 2018 2019 2028 public AccessibleContext getAccessibleContext() { 2029 if (accessibleContext == null) { 2030 accessibleContext = new AccessibleAWTWindow(); 2031 } 2032 return accessibleContext; 2033 } 2034 2035 2040 protected class AccessibleAWTWindow extends AccessibleAWTContainer 2041 { 2042 2045 private static final long serialVersionUID = 4215068635060671780L; 2046 2047 2054 public AccessibleRole getAccessibleRole() { 2055 return AccessibleRole.WINDOW; 2056 } 2057 2058 2065 public AccessibleStateSet getAccessibleStateSet() { 2066 AccessibleStateSet states = super.getAccessibleStateSet(); 2067 if (getFocusOwner() != null) { 2068 states.add(AccessibleState.ACTIVE); 2069 } 2070 return states; 2071 } 2072 2073 } 2075 2078 public GraphicsConfiguration getGraphicsConfiguration() { 2079 synchronized(getTreeLock()) { 2083 if (graphicsConfig == null && !GraphicsEnvironment.isHeadless()) { 2084 graphicsConfig = 2085 GraphicsEnvironment. getLocalGraphicsEnvironment(). 2086 getDefaultScreenDevice(). 2087 getDefaultConfiguration(); 2088 } 2089 return graphicsConfig; 2090 } 2091 } 2092 2093 2096 void resetGC() { 2097 if (!GraphicsEnvironment.isHeadless()) { 2098 setGCFromPeer(); 2100 if (graphicsConfig == null) { 2102 graphicsConfig = GraphicsEnvironment. 2103 getLocalGraphicsEnvironment(). 2104 getDefaultScreenDevice(). 2105 getDefaultConfiguration(); 2106 } 2107 if (dbg.on) { 2108 dbg.println("+ Window.resetGC(): new GC is \n+ " + graphicsConfig + "\n+ this is " + this); 2109 } 2110 } 2111 } 2112 2113 2129 public void setLocationRelativeTo(Component c) { 2130 Container root=null; 2131 2132 if (c != null) { 2133 if (c instanceof Window || c instanceof Applet ) { 2134 root = (Container )c; 2135 } else { 2136 Container parent; 2137 for(parent = c.getParent() ; parent != null ; parent = parent.getParent()) { 2138 if (parent instanceof Window || parent instanceof Applet ) { 2139 root = parent; 2140 break; 2141 } 2142 } 2143 } 2144 } 2145 2146 if((c != null && !c.isShowing()) || root == null || 2147 !root.isShowing()) { 2148 Dimension paneSize = getSize(); 2149 Dimension screenSize = getToolkit().getScreenSize(); 2150 2151 setLocation((screenSize.width - paneSize.width) / 2, 2152 (screenSize.height - paneSize.height) / 2); 2153 } else { 2154 Dimension invokerSize = c.getSize(); 2155 Point invokerScreenLocation = c.getLocationOnScreen(); 2156 2157 Rectangle windowBounds = getBounds(); 2158 int dx = invokerScreenLocation.x+((invokerSize.width-windowBounds.width)>>1); 2159 int dy = invokerScreenLocation.y+((invokerSize.height - windowBounds.height)>>1); 2160 Rectangle ss = root.getGraphicsConfiguration().getBounds(); 2161 2162 if (dy+windowBounds.height>ss.height) { 2164 dy = ss.height-windowBounds.height; 2165 if (invokerScreenLocation.x - ss.x + invokerSize.width / 2 < 2166 ss.width / 2) { 2167 dx = invokerScreenLocation.x+invokerSize.width; 2168 } 2169 else { 2170 dx = invokerScreenLocation.x-windowBounds.width; 2171 } 2172 } 2173 2174 if (dx+windowBounds.width > ss.x + ss.width) { 2176 dx = ss.x + ss.width - windowBounds.width; 2177 } 2178 if (dx < ss.x) dx = 0; 2179 if (dy < ss.y) dy = 0; 2180 2181 setLocation(dx, dy); 2182 } 2183 } 2184 2185 2189 void deliverMouseWheelToAncestor(MouseWheelEvent e) {} 2190 2191 2194 boolean dispatchMouseWheelToAncestor(MouseWheelEvent e) {return false;} 2195 2196 2215 public void createBufferStrategy(int numBuffers) { 2216 super.createBufferStrategy(numBuffers); 2217 } 2218 2219 2239 public void createBufferStrategy(int numBuffers, 2240 BufferCapabilities caps) throws AWTException { 2241 super.createBufferStrategy(numBuffers, caps); 2242 } 2243 2244 2249 public BufferStrategy getBufferStrategy() { 2250 return super.getBufferStrategy(); 2251 } 2252 2253 Component getTemporaryLostComponent() { 2254 return temporaryLostComponent; 2255 } 2256 Component setTemporaryLostComponent(Component component) { 2257 Component previousComp = temporaryLostComponent; 2258 if (component == null 2261 || (component.isDisplayable() && component.isVisible() && component.isEnabled() && component.isFocusable())) 2262 { 2263 temporaryLostComponent = component; 2264 } else { 2265 temporaryLostComponent = null; 2266 } 2267 return previousComp; 2268 } 2269 2270 2275 boolean canContainFocusOwner(Component focusOwnerCandidate) { 2276 return super.canContainFocusOwner(focusOwnerCandidate) && isFocusableWindow(); 2277 } 2278 2279 private boolean locationByPlatform = locationByPlatformProp; 2280 2281 2282 2329 public void setLocationByPlatform(boolean locationByPlatform) { 2330 synchronized (getTreeLock()) { 2331 if (locationByPlatform && isShowing()) { 2332 throw new IllegalComponentStateException ("The window is showing on screen."); 2333 } 2334 this.locationByPlatform = locationByPlatform; 2335 } 2336 } 2337 2338 2349 public boolean isLocationByPlatform() { 2350 synchronized (getTreeLock()) { 2351 return locationByPlatform; 2352 } 2353 } 2354 2355 2360 public void setBounds(int x, int y, int width, int height) { 2361 synchronized (getTreeLock()) { 2362 if (getBoundsOp() == ComponentPeer.SET_LOCATION || 2363 getBoundsOp() == ComponentPeer.SET_BOUNDS) 2364 { 2365 locationByPlatform = false; 2366 } 2367 super.setBounds(x, y, width, height); 2368 } 2369 } 2370} 2372 2373 2377class FocusManager implements java.io.Serializable { 2378 Container focusRoot; 2379 Component focusOwner; 2380 2381 2384 static final long serialVersionUID = 2491878825643557906L; 2385} 2386 | Popular Tags |