| 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 |