1. /*
  2. * @(#)Window.java 1.208 04/06/28
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.applet.Applet;
  9. import java.awt.peer.WindowPeer;
  10. import java.awt.peer.ComponentPeer;
  11. import java.awt.event.*;
  12. import java.awt.image.BufferStrategy;
  13. import java.util.Vector;
  14. import java.util.Locale;
  15. import java.util.EventListener;
  16. import java.util.Set;
  17. import java.io.Serializable;
  18. import java.io.ObjectOutputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.IOException;
  21. import java.io.OptionalDataException;
  22. import java.awt.im.InputContext;
  23. import java.util.ResourceBundle;
  24. import java.lang.ref.WeakReference;
  25. import java.lang.reflect.InvocationTargetException;
  26. import java.security.AccessController;
  27. import java.security.PrivilegedAction;
  28. import javax.accessibility.*;
  29. import java.beans.PropertyChangeListener;
  30. import sun.security.action.GetPropertyAction;
  31. import sun.security.util.SecurityConstants;
  32. import sun.awt.DebugHelper;
  33. /**
  34. * A <code>Window</code> object is a top-level window with no borders and no
  35. * menubar.
  36. * The default layout for a window is <code>BorderLayout</code>.
  37. * <p>
  38. * A window must have either a frame, dialog, or another window defined as its
  39. * owner when it's constructed.
  40. * <p>
  41. * In a multi-screen environment, you can create a <code>Window</code>
  42. * on a different screen device by constructing the <code>Window</code>
  43. * with {@link #Window(Window, GraphicsConfiguration)}. The
  44. * <code>GraphicsConfiguration</code> object is one of the
  45. * <code>GraphicsConfiguration</code> objects of the target screen device.
  46. * <p>
  47. * In a virtual device multi-screen environment in which the desktop
  48. * area could span multiple physical screen devices, the bounds of all
  49. * configurations are relative to the virtual device coordinate system.
  50. * The origin of the virtual-coordinate system is at the upper left-hand
  51. * corner of the primary physical screen. Depending on the location of
  52. * the primary screen in the virtual device, negative coordinates are
  53. * possible, as shown in the following figure.
  54. * <p>
  55. * <img src="doc-files/MultiScreen.gif"
  56. * alt="Diagram shows virtual device containing 4 physical screens. Primary physical screen shows coords (0,0), other screen shows (-80,-100)."
  57. * ALIGN=center HSPACE=10 VSPACE=7>
  58. * <p>
  59. * In such an environment, when calling <code>setLocation</code>,
  60. * you must pass a virtual coordinate to this method. Similarly,
  61. * calling <code>getLocationOnScreen</code> on a <code>Window</code> returns
  62. * virtual device coordinates. Call the <code>getBounds</code> method
  63. * of a <code>GraphicsConfiguration</code> to find its origin in the virtual
  64. * coordinate system.
  65. * <p>
  66. * The following code sets the location of a <code>Window</code>
  67. * at (10, 10) relative to the origin of the physical screen
  68. * of the corresponding <code>GraphicsConfiguration</code>. If the
  69. * bounds of the <code>GraphicsConfiguration</code> is not taken
  70. * into account, the <code>Window</code> location would be set
  71. * at (10, 10) relative to the virtual-coordinate system and would appear
  72. * on the primary physical screen, which might be different from the
  73. * physical screen of the specified <code>GraphicsConfiguration</code>.
  74. *
  75. * <pre>
  76. * Window w = new Window(Window owner, GraphicsConfiguration gc);
  77. * Rectangle bounds = gc.getBounds();
  78. * w.setLocation(10 + bounds.x, 10 + bounds.y);
  79. * </pre>
  80. *
  81. * <p>
  82. * Note: the location and size of top-level windows (including
  83. * <code>Window</code>s, <code>Frame</code>s, and <code>Dialog</code>s)
  84. * are under the control of the desktop's window management system.
  85. * Calls to <code>setLocation</code>, <code>setSize</code>, and
  86. * <code>setBounds</code> are requests (not directives) which are
  87. * forwarded to the window management system. Every effort will be
  88. * made to honor such requests. However, in some cases the window
  89. * management system may ignore such requests, or modify the requested
  90. * geometry in order to place and size the <code>Window</code> in a way
  91. * that more closely matches the desktop settings.
  92. *
  93. * Due to the asynchronous nature of native event handling, the results
  94. * returned by <code>getBounds</code>, <code>getLocation</code>,
  95. * <code>getLocationOnScreen</code>, and <code>getSize</code> might not
  96. * reflect the actual geometry of the Window on screen until the last
  97. * request has been processed. During the processing of subsequent
  98. * requests these values might change accordingly while the window
  99. * management system fulfills the requests.
  100. *
  101. * <p>
  102. * Windows are capable of generating the following WindowEvents:
  103. * WindowOpened, WindowClosed, WindowGainedFocus, WindowLostFocus.
  104. *
  105. * @version 1.208, 06/28/04
  106. * @author Sami Shaio
  107. * @author Arthur van Hoff
  108. * @see WindowEvent
  109. * @see #addWindowListener
  110. * @see java.awt.BorderLayout
  111. * @since JDK1.0
  112. */
  113. public class Window extends Container implements Accessible {
  114. /**
  115. * This represents the warning message that is
  116. * to be displayed in a non secure window. ie :
  117. * a window that has a security manager installed for
  118. * which calling SecurityManager.checkTopLevelWindow()
  119. * is false. This message can be displayed anywhere in
  120. * the window.
  121. *
  122. * @serial
  123. * @see #getWarningString
  124. */
  125. String warningString;
  126. /**
  127. * Holds the reference to the component which last had focus in this window
  128. * before it lost focus.
  129. */
  130. private transient Component temporaryLostComponent;
  131. static boolean systemSyncLWRequests = false;
  132. boolean syncLWRequests = false;
  133. transient boolean beforeFirstShow = true;
  134. static final int OPENED = 0x01;
  135. /**
  136. * An Integer value representing the Window State.
  137. *
  138. * @serial
  139. * @since 1.2
  140. * @see #show
  141. */
  142. int state;
  143. /**
  144. * A boolean value representing Window always-on-top state
  145. * @since 1.5
  146. * @serial
  147. * @see #setAlwaysOnTop
  148. * @see #isAlwaysOnTop
  149. */
  150. private boolean alwaysOnTop;
  151. /**
  152. * A vector containing all the windows this
  153. * window currently owns.
  154. * @since 1.2
  155. * @see #getOwnedWindows
  156. */
  157. transient Vector ownedWindowList = new Vector();
  158. private transient WeakReference weakThis;
  159. private transient boolean showWithParent = false;
  160. transient WindowListener windowListener;
  161. transient WindowStateListener windowStateListener;
  162. transient WindowFocusListener windowFocusListener;
  163. transient InputContext inputContext;
  164. private transient Object inputContextLock = new Object();
  165. /**
  166. * Unused. Maintained for serialization backward-compatibility.
  167. *
  168. * @serial
  169. * @since 1.2
  170. */
  171. private FocusManager focusMgr;
  172. /**
  173. * Indicates whether this Window can become the focused Window.
  174. *
  175. * @serial
  176. * @see #getFocusableWindowState
  177. * @see #setFocusableWindowState
  178. * @since 1.4
  179. */
  180. private boolean focusableWindowState = true;
  181. private static final String base = "win";
  182. private static int nameCounter = 0;
  183. /*
  184. * JDK 1.1 serialVersionUID
  185. */
  186. private static final long serialVersionUID = 4497834738069338734L;
  187. private static final DebugHelper dbg = DebugHelper.create(Window.class);
  188. private static final boolean locationByPlatformProp;
  189. static {
  190. /* ensure that the necessary native libraries are loaded */
  191. Toolkit.loadLibraries();
  192. if (!GraphicsEnvironment.isHeadless()) {
  193. initIDs();
  194. }
  195. String s = (String) java.security.AccessController.doPrivileged(
  196. new GetPropertyAction("java.awt.syncLWRequests"));
  197. systemSyncLWRequests = (s != null && s.equals("true"));
  198. s = (String) java.security.AccessController.doPrivileged(
  199. new GetPropertyAction("java.awt.Window.locationByPlatform"));
  200. locationByPlatformProp = (s != null && s.equals("true"));
  201. }
  202. /**
  203. * Initialize JNI field and method IDs for fields that may be
  204. accessed from C.
  205. */
  206. private static native void initIDs();
  207. /**
  208. * Constructs a new window in default size with the
  209. * specified <code>GraphicsConfiguration</code>.
  210. * <p>
  211. * If there is a security manager, this method first calls
  212. * the security manager's <code>checkTopLevelWindow</code>
  213. * method with <code>this</code>
  214. * as its argument to determine whether or not the window
  215. * must be displayed with a warning banner.
  216. * @param gc the <code>GraphicsConfiguration</code>
  217. * of the target screen device. If <code>gc</code> is
  218. * <code>null</code>, the system default
  219. * <code>GraphicsConfiguration</code> is assumed.
  220. * @exception IllegalArgumentException if <code>gc</code>
  221. * is not from a screen device. This exception is always
  222. * thrown when GraphicsEnvironment.isHeadless() returns true.
  223. * @see java.awt.GraphicsEnvironment#isHeadless
  224. * @see java.lang.SecurityManager#checkTopLevelWindow
  225. */
  226. Window(GraphicsConfiguration gc) {
  227. init(gc);
  228. }
  229. private void init(GraphicsConfiguration gc) {
  230. if (GraphicsEnvironment.isHeadless()) {
  231. throw new IllegalArgumentException("headless environment");
  232. }
  233. syncLWRequests = systemSyncLWRequests;
  234. setWarningString();
  235. this.cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
  236. this.visible = false;
  237. if (gc == null) {
  238. this.graphicsConfig =
  239. GraphicsEnvironment.getLocalGraphicsEnvironment().
  240. getDefaultScreenDevice().getDefaultConfiguration();
  241. } else {
  242. this.graphicsConfig = gc;
  243. }
  244. if (graphicsConfig.getDevice().getType() !=
  245. GraphicsDevice.TYPE_RASTER_SCREEN) {
  246. throw new IllegalArgumentException("not a screen device");
  247. }
  248. setLayout(new BorderLayout());
  249. /* offset the initial location with the original of the screen */
  250. /* and any insets */
  251. Rectangle screenBounds = graphicsConfig.getBounds();
  252. Insets screenInsets = getToolkit().getScreenInsets(graphicsConfig);
  253. int x = getX() + screenBounds.x + screenInsets.left;
  254. int y = getY() + screenBounds.y + screenInsets.top;
  255. if (x != this.x || y != this.y) {
  256. setLocation(x, y);
  257. /* reset after setLocation */
  258. setLocationByPlatform(locationByPlatformProp);
  259. }
  260. }
  261. /**
  262. * Constructs a new window in the default size.
  263. *
  264. * <p>First, if there is a security manager, its
  265. * <code>checkTopLevelWindow</code>
  266. * method is called with <code>this</code>
  267. * as its argument
  268. * to see if it's ok to display the window without a warning banner.
  269. * If the default implementation of <code>checkTopLevelWindow</code>
  270. * is used (that is, that method is not overriden), then this results in
  271. * a call to the security manager's <code>checkPermission</code> method
  272. * with an <code>AWTPermission("showWindowWithoutWarningBanner")</code>
  273. * permission. It that method raises a SecurityException,
  274. * <code>checkTopLevelWindow</code> returns false, otherwise it
  275. * returns true. If it returns false, a warning banner is created.
  276. *
  277. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  278. * returns true
  279. * @see java.awt.GraphicsEnvironment#isHeadless
  280. * @see java.lang.SecurityManager#checkTopLevelWindow
  281. */
  282. Window() throws HeadlessException {
  283. GraphicsEnvironment.checkHeadless();
  284. init((GraphicsConfiguration)null);
  285. }
  286. /**
  287. * Constructs a new invisible window with the specified
  288. * <code>Frame</code> as its owner. The Window will not be focusable
  289. * unless its owner is showing on the screen.
  290. * <p>
  291. * If there is a security manager, this method first calls
  292. * the security manager's <code>checkTopLevelWindow</code>
  293. * method with <code>this</code>
  294. * as its argument to determine whether or not the window
  295. * must be displayed with a warning banner.
  296. *
  297. * @param owner the <code>Frame</code> to act as owner
  298. * @exception IllegalArgumentException if the <code>owner</code>'s
  299. * <code>GraphicsConfiguration</code> is not from a screen device
  300. * @exception java.lang.IllegalArgumentException if
  301. * <code>owner</code> is <code>null</code> this exception
  302. * is always thrown when <code>GraphicsEnvironment.isHeadless</code>
  303. * returns true
  304. * @see java.awt.GraphicsEnvironment#isHeadless
  305. * @see java.lang.SecurityManager#checkTopLevelWindow
  306. * @see #isShowing
  307. */
  308. public Window(Frame owner) {
  309. this(owner == null ? (GraphicsConfiguration)null :
  310. owner.getGraphicsConfiguration());
  311. ownedInit(owner);
  312. }
  313. /**
  314. * Constructs a new invisible window with the specified
  315. * <code>Window</code> as its owner. The Window will not be focusable
  316. * unless its nearest owning Frame or Dialog is showing on the screen.
  317. * <p>
  318. * If there is a security manager, this method first calls
  319. * the security manager's <code>checkTopLevelWindow</code>
  320. * method with <code>this</code>
  321. * as its argument to determine whether or not the window
  322. * must be displayed with a warning banner.
  323. *
  324. * @param owner the <code>Window</code> to act as owner
  325. * @exception IllegalArgumentException if the <code>owner</code>'s
  326. * <code>GraphicsConfiguration</code> is not from a screen device
  327. * @exception java.lang.IllegalArgumentException if <code>owner</code>
  328. * is <code>null</code>. This exception is always thrown
  329. * when GraphicsEnvironment.isHeadless() returns true.
  330. * @see java.awt.GraphicsEnvironment#isHeadless
  331. * @see java.lang.SecurityManager#checkTopLevelWindow
  332. * @see #isShowing
  333. * @since 1.2
  334. */
  335. public Window(Window owner) {
  336. this(owner == null ? (GraphicsConfiguration)null :
  337. owner.getGraphicsConfiguration());
  338. ownedInit(owner);
  339. }
  340. /**
  341. * Constructs a new invisible window with the specified
  342. * window as its owner and a
  343. * <code>GraphicsConfiguration</code> of a screen device. The Window will
  344. * not be focusable unless its nearest owning Frame or Dialog is showing on
  345. * the screen.
  346. * <p>
  347. * If there is a security manager, this method first calls
  348. * the security manager's <code>checkTopLevelWindow</code>
  349. * method with <code>this</code>
  350. * as its argument to determine whether or not the window
  351. * must be displayed with a warning banner.
  352. *
  353. * @param owner the window to act as owner
  354. * @param gc the <code>GraphicsConfiguration</code>
  355. * of the target screen device; if <code>gc</code> is
  356. * <code>null</code>, the system default
  357. * <code>GraphicsConfiguration</code> is assumed
  358. * @throws IllegalArgumentException if
  359. * <code>owner</code> is <code>null</code>
  360. * @throws IllegalArgumentException if <code>gc</code> is not from
  361. * a screen device; this exception is always thrown when
  362. * <code>GraphicsEnvironment.isHeadless</code> returns
  363. * <code>true</code>
  364. * @see java.awt.GraphicsEnvironment#isHeadless
  365. * @see java.lang.SecurityManager#checkTopLevelWindow
  366. * @see GraphicsConfiguration#getBounds
  367. * @see #isShowing
  368. * @since 1.3
  369. */
  370. public Window(Window owner, GraphicsConfiguration gc) {
  371. this(gc);
  372. ownedInit(owner);
  373. }
  374. private void ownedInit(Window owner) {
  375. if (owner == null) {
  376. throw new IllegalArgumentException("null owner window");
  377. }
  378. this.parent = owner;
  379. this.weakThis = new WeakReference(this);
  380. owner.addOwnedWindow(weakThis);
  381. }
  382. /**
  383. * Disposes of the input methods and context, and removes
  384. * this Window from the GUI hierarchy. Subclasses that override
  385. * this method should call super.finalize().
  386. */
  387. protected void finalize() throws Throwable {
  388. // We have to remove the (hard) reference to weakThis in the
  389. // parent's ownedWindowList, otherwise the WeakReference
  390. // instance that points to this Window will never get garbage
  391. // collected.
  392. if (parent != null) {
  393. ((Window)parent).removeOwnedWindow(weakThis);
  394. }
  395. super.finalize();
  396. }
  397. /**
  398. * Construct a name for this component. Called by getName() when the
  399. * name is null.
  400. */
  401. String constructComponentName() {
  402. synchronized (getClass()) {
  403. return base + nameCounter++;
  404. }
  405. }
  406. /**
  407. * Makes this Window displayable by creating the connection to its
  408. * native screen resource.
  409. * This method is called internally by the toolkit and should
  410. * not be called directly by programs.
  411. * @see Component#isDisplayable
  412. * @see Container#removeNotify
  413. * @since JDK1.0
  414. */
  415. public void addNotify() {
  416. synchronized (getTreeLock()) {
  417. Container parent = this.parent;
  418. if (parent != null && parent.getPeer() == null) {
  419. parent.addNotify();
  420. }
  421. if (peer == null)
  422. peer = getToolkit().createWindow(this);
  423. super.addNotify();
  424. }
  425. }
  426. /**
  427. * Causes this Window to be sized to fit the preferred size
  428. * and layouts of its subcomponents. If the window and/or its owner
  429. * are not yet displayable, both are made displayable before
  430. * calculating the preferred size. The Window will be validated
  431. * after the preferredSize is calculated.
  432. * @see Component#isDisplayable
  433. */
  434. public void pack() {
  435. Container parent = this.parent;
  436. if (parent != null && parent.getPeer() == null) {
  437. parent.addNotify();
  438. }
  439. if (peer == null) {
  440. addNotify();
  441. }
  442. Dimension newSize = getPreferredSize();
  443. if (peer != null) {
  444. setClientSize(newSize.width, newSize.height);
  445. }
  446. if(beforeFirstShow) {
  447. isPacked = true;
  448. }
  449. validate();
  450. }
  451. void setClientSize(int w, int h) {
  452. synchronized (getTreeLock()) {
  453. setBoundsOp(ComponentPeer.SET_CLIENT_SIZE);
  454. setBounds(x, y, w, h);
  455. }
  456. }
  457. /**
  458. * @deprecated As of JDK 5, replaced by
  459. * {@link Component#setVisible(boolean) Component.setVisible(boolean)}.
  460. */
  461. @Deprecated
  462. public void show() {
  463. if (peer == null) {
  464. addNotify();
  465. }
  466. validate();
  467. if (visible) {
  468. toFront();
  469. } else {
  470. beforeFirstShow = false;
  471. super.show();
  472. locationByPlatform = false;
  473. for (int i = 0; i < ownedWindowList.size(); i++) {
  474. Window child = (Window) (((WeakReference)
  475. (ownedWindowList.elementAt(i))).get());
  476. if ((child != null) && child.showWithParent) {
  477. child.show();
  478. child.showWithParent = false;
  479. } // endif
  480. } // endfor
  481. }
  482. // If first time shown, generate WindowOpened event
  483. if ((state & OPENED) == 0) {
  484. postWindowEvent(WindowEvent.WINDOW_OPENED);
  485. state |= OPENED;
  486. }
  487. }
  488. synchronized void postWindowEvent(int id) {
  489. if (windowListener != null
  490. || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0
  491. || Toolkit.enabledOnToolkit(AWTEvent.WINDOW_EVENT_MASK)) {
  492. WindowEvent e = new WindowEvent(this, id);
  493. Toolkit.getEventQueue().postEvent(e);
  494. }
  495. }
  496. /**
  497. * @deprecated As of JDK 5, replaced by
  498. * {@link Component#setVisible(boolean) Component.setVisible(boolean)}.
  499. */
  500. @Deprecated
  501. public void hide() {
  502. synchronized(ownedWindowList) {
  503. for (int i = 0; i < ownedWindowList.size(); i++) {
  504. Window child = (Window) (((WeakReference)
  505. (ownedWindowList.elementAt(i))).get());
  506. if ((child != null) && child.visible) {
  507. child.hide();
  508. child.showWithParent = true;
  509. }
  510. }
  511. }
  512. super.hide();
  513. }
  514. final void clearMostRecentFocusOwnerOnHide() {
  515. /* do nothing */
  516. }
  517. /**
  518. * Releases all of the native screen resources used by this
  519. * <code>Window</code>, its subcomponents, and all of its owned
  520. * children. That is, the resources for these <code>Component</code>s
  521. * will be destroyed, any memory they consume will be returned to the
  522. * OS, and they will be marked as undisplayable.
  523. * <p>
  524. * The <code>Window</code> and its subcomponents can be made displayable
  525. * again by rebuilding the native resources with a subsequent call to
  526. * <code>pack</code> or <code>show</code>. The states of the recreated
  527. * <code>Window</code> and its subcomponents will be identical to the
  528. * states of these objects at the point where the <code>Window</code>
  529. * was disposed (not accounting for additional modifications between
  530. * those actions).
  531. * <p>
  532. * <b>Note</b>: When the last displayable window
  533. * within the Java virtual machine (VM) is disposed of, the VM may
  534. * terminate. See <a href="doc-files/AWTThreadIssues.html">
  535. * AWT Threading Issues</a> for more information.
  536. * @see Component#isDisplayable
  537. * @see #pack
  538. * @see #show
  539. */
  540. public void dispose() {
  541. doDispose();
  542. }
  543. /*
  544. * Fix for 4872170.
  545. * If dispose() is called on parent then its children have to be disposed as well
  546. * as reported in javadoc. So we need to implement this functionality even if a
  547. * child overrides dispose() in a wrong way without calling super.dispose().
  548. */
  549. void disposeImpl() {
  550. dispose();
  551. if (getPeer() != null) {
  552. doDispose();
  553. }
  554. }
  555. void doDispose() {
  556. class DisposeAction implements Runnable {
  557. public void run() {
  558. Object[] ownedWindowArray;
  559. synchronized(ownedWindowList) {
  560. ownedWindowArray = new Object[ownedWindowList.size()];
  561. ownedWindowList.copyInto(ownedWindowArray);
  562. }
  563. for (int i = 0; i < ownedWindowArray.length; i++) {
  564. Window child = (Window) (((WeakReference)
  565. (ownedWindowArray[i])).get());
  566. if (child != null) {
  567. child.disposeImpl();
  568. }
  569. }
  570. hide();
  571. beforeFirstShow = true;
  572. removeNotify();
  573. synchronized (inputContextLock) {
  574. if (inputContext != null) {
  575. inputContext.dispose();
  576. inputContext = null;
  577. }
  578. }
  579. clearCurrentFocusCycleRootOnHide();
  580. }
  581. }
  582. DisposeAction action = new DisposeAction();
  583. if (EventQueue.isDispatchThread()) {
  584. action.run();
  585. }
  586. else {
  587. try {
  588. EventQueue.invokeAndWait(action);
  589. }
  590. catch (InterruptedException e) {
  591. System.err.println("Disposal was interrupted:");
  592. e.printStackTrace();
  593. }
  594. catch (InvocationTargetException e) {
  595. System.err.println("Exception during disposal:");
  596. e.printStackTrace();
  597. }
  598. }
  599. // Execute outside the Runnable because postWindowEvent is
  600. // synchronized on (this). We don't need to synchronize the call
  601. // on the EventQueue anyways.
  602. postWindowEvent(WindowEvent.WINDOW_CLOSED);
  603. }
  604. /*
  605. * Should only be called while holding the tree lock.
  606. * It's overridden here because parent == owner in Window,
  607. * and we shouldn't adjust counter on owner
  608. */
  609. void adjustListeningChildrenOnParent(long mask, int num) {
  610. }
  611. // Should only be called while holding tree lock
  612. void adjustDecendantsOnParent(int num) {
  613. // do nothing since parent == owner and we shouldn't
  614. // ajust counter on owner
  615. }
  616. /**
  617. * If this Window is visible, brings this Window to the front and may make
  618. * it the focused Window.
  619. * <p>
  620. * Places this Window at the top of the stacking order and shows it in
  621. * front of any other Windows in this VM. No action will take place if this
  622. * Window is not visible. Some platforms do not allow Windows which own
  623. * other Windows to appear on top of those owned Windows. Some platforms
  624. * may not permit this VM to place its Windows above windows of native
  625. * applications, or Windows of other VMs. This permission may depend on
  626. * whether a Window in this VM is already focused. Every attempt will be
  627. * made to move this Window as high as possible in the stacking order;
  628. * however, developers should not assume that this method will move this
  629. * Window above all other windows in every situation.
  630. * <p>
  631. * Because of variations in native windowing systems, no guarantees about
  632. * changes to the focused and active Windows can be made. Developers must
  633. * never assume that this Window is the focused or active Window until this
  634. * Window receives a WINDOW_GAINED_FOCUS or WINDOW_ACTIVATED event. On
  635. * platforms where the top-most window is the focused window, this method
  636. * will <b>probably</b> focus this Window, if it is not already focused. On
  637. * platforms where the stacking order does not typically affect the focused
  638. * window, this method will <b>probably</b> leave the focused and active
  639. * Windows unchanged.
  640. * <p>
  641. * If this method causes this Window to be focused, and this Window is a
  642. * Frame or a Dialog, it will also become activated. If this Window is
  643. * focused, but it is not a Frame or a Dialog, then the first Frame or
  644. * Dialog that is an owner of this Window will be activated.
  645. *
  646. * @see #toBack
  647. */
  648. public void toFront() {
  649. if (visible) {
  650. WindowPeer peer = (WindowPeer)this.peer;
  651. if (peer != null) {
  652. peer.toFront();
  653. }
  654. }
  655. }
  656. /**
  657. * If this Window is visible, sends this Window to the back and may cause
  658. * it to lose focus or activation if it is the focused or active Window.
  659. * <p>
  660. * Places this Window at the bottom of the stacking order and shows it
  661. * behind any other Windows in this VM. No action will take place is this
  662. * Window is not visible. Some platforms do not allow Windows which are
  663. * owned by other Windows to appear below their owners. Every attempt will
  664. * be made to move this Window as low as possible in the stacking order;
  665. * however, developers should not assume that this method will move this
  666. * Window below all other windows in every situation.
  667. * <p>
  668. * Because of variations in native windowing systems, no guarantees about
  669. * changes to the focused and active Windows can be made. Developers must
  670. * never assume that this Window is no longer the focused or active Window
  671. * until this Window receives a WINDOW_LOST_FOCUS or WINDOW_DEACTIVATED
  672. * event. On platforms where the top-most window is the focused window,
  673. * this method will <b>probably</b> cause this Window to lose focus. In
  674. * that case, the next highest, focusable Window in this VM will receive
  675. * focus. On platforms where the stacking order does not typically affect
  676. * the focused window, this method will <b>probably</b> leave the focused
  677. * and active Windows unchanged.
  678. *
  679. * @see #toFront
  680. */
  681. public void toBack() {
  682. if (visible) {
  683. WindowPeer peer = (WindowPeer)this.peer;
  684. if (peer != null) {
  685. peer.toBack();
  686. }
  687. }
  688. }
  689. /**
  690. * Returns the toolkit of this frame.
  691. * @return the toolkit of this window.
  692. * @see Toolkit
  693. * @see Toolkit#getDefaultToolkit
  694. * @see Component#getToolkit
  695. */
  696. public Toolkit getToolkit() {
  697. return Toolkit.getDefaultToolkit();
  698. }
  699. /**
  700. * Gets the warning string that is displayed with this window.
  701. * If this window is insecure, the warning string is displayed
  702. * somewhere in the visible area of the window. A window is
  703. * insecure if there is a security manager, and the security
  704. * manager's <code>checkTopLevelWindow</code> method returns
  705. * <code>false</code> when this window is passed to it as an
  706. * argument.
  707. * <p>
  708. * If the window is secure, then <code>getWarningString</code>
  709. * returns <code>null</code>. If the window is insecure, this
  710. * method checks for the system property
  711. * <code>awt.appletWarning</code>
  712. * and returns the string value of that property.
  713. * @return the warning string for this window.
  714. * @see java.lang.SecurityManager#checkTopLevelWindow(java.lang.Object)
  715. */
  716. public final String getWarningString() {
  717. return warningString;
  718. }
  719. private void setWarningString() {
  720. warningString = null;
  721. SecurityManager sm = System.getSecurityManager();
  722. if (sm != null) {
  723. if (!sm.checkTopLevelWindow(this)) {
  724. // make sure the privileged action is only
  725. // for getting the property! We don't want the
  726. // above checkTopLevelWindow call to always succeed!
  727. warningString = (String) AccessController.doPrivileged(
  728. new GetPropertyAction("awt.appletWarning",
  729. "Java Applet Window"));
  730. }
  731. }
  732. }
  733. /**
  734. * Gets the <code>Locale</code> object that is associated
  735. * with this window, if the locale has been set.
  736. * If no locale has been set, then the default locale
  737. * is returned.
  738. * @return the locale that is set for this window.
  739. * @see java.util.Locale
  740. * @since JDK1.1
  741. */
  742. public Locale getLocale() {
  743. if (this.locale == null) {
  744. return Locale.getDefault();
  745. }
  746. return this.locale;
  747. }
  748. /**
  749. * Gets the input context for this window. A window always has an input context,
  750. * which is shared by subcomponents unless they create and set their own.
  751. * @see Component#getInputContext
  752. * @since 1.2
  753. */
  754. public InputContext getInputContext() {
  755. if (inputContext == null) {
  756. synchronized (inputContextLock) {
  757. if (inputContext == null) {
  758. inputContext = InputContext.getInstance();
  759. }
  760. }
  761. }
  762. return inputContext;
  763. }
  764. /**
  765. * Set the cursor image to a specified cursor.
  766. * @param cursor One of the constants defined
  767. * by the <code>Cursor</code> class. If this parameter is null
  768. * then the cursor for this window will be set to the type
  769. * Cursor.DEFAULT_CURSOR.
  770. * @see Component#getCursor
  771. * @see Cursor
  772. * @since JDK1.1
  773. */
  774. public void setCursor(Cursor cursor) {
  775. if (cursor == null) {
  776. cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
  777. }
  778. super.setCursor(cursor);
  779. }
  780. /**
  781. * Returns the owner of this window.
  782. * @since 1.2
  783. */
  784. public Window getOwner() {
  785. return (Window)parent;
  786. }
  787. /**
  788. * Return an array containing all the windows this
  789. * window currently owns.
  790. * @since 1.2
  791. */
  792. public Window[] getOwnedWindows() {
  793. Window realCopy[];
  794. synchronized(ownedWindowList) {
  795. // Recall that ownedWindowList is actually a Vector of
  796. // WeakReferences and calling get() on one of these references
  797. // may return null. Make two arrays-- one the size of the
  798. // Vector (fullCopy with size fullSize), and one the size of
  799. // all non-null get()s (realCopy with size realSize).
  800. int fullSize = ownedWindowList.size();
  801. int realSize = 0;
  802. Window fullCopy[] = new Window[fullSize];
  803. for (int i = 0; i < fullSize; i++) {
  804. fullCopy[realSize] = (Window) (((WeakReference)
  805. (ownedWindowList.elementAt(i))).get());
  806. if (fullCopy[realSize] != null) {
  807. realSize++;
  808. }
  809. }
  810. if (fullSize != realSize) {
  811. realCopy = new Window[realSize];
  812. System.arraycopy(fullCopy, 0, realCopy, 0, realSize);
  813. } else {
  814. realCopy = fullCopy;
  815. }
  816. }
  817. return realCopy;
  818. }
  819. /**
  820. * Adds the specified window listener to receive window events from
  821. * this window.
  822. * If l is null, no exception is thrown and no action is performed.
  823. *
  824. * @param l the window listener
  825. * @see #removeWindowListener
  826. * @see #getWindowListeners
  827. */
  828. public synchronized void addWindowListener(WindowListener l) {
  829. if (l == null) {
  830. return;
  831. }
  832. newEventsOnly = true;
  833. windowListener = AWTEventMulticaster.add(windowListener, l);
  834. }
  835. /**
  836. * Adds the specified window state listener to receive window
  837. * events from this window. If <code>l</code> is </code>null</code>,
  838. * no exception is thrown and no action is performed.
  839. *
  840. * @param l the window state listener
  841. * @see #removeWindowStateListener
  842. * @see #getWindowStateListeners
  843. * @since 1.4
  844. */
  845. public synchronized void addWindowStateListener(WindowStateListener l) {
  846. if (l == null) {
  847. return;
  848. }
  849. windowStateListener = AWTEventMulticaster.add(windowStateListener, l);
  850. newEventsOnly = true;
  851. }
  852. /**
  853. * Adds the specified window focus listener to receive window events
  854. * from this window.
  855. * If l is null, no exception is thrown and no action is performed.
  856. *
  857. * @param l the window focus listener
  858. * @see #removeWindowFocusListener
  859. * @see #getWindowFocusListeners
  860. */
  861. public synchronized void addWindowFocusListener(WindowFocusListener l) {
  862. if (l == null) {
  863. return;
  864. }
  865. windowFocusListener = AWTEventMulticaster.add(windowFocusListener, l);
  866. newEventsOnly = true;
  867. }
  868. /**
  869. * Removes the specified window listener so that it no longer
  870. * receives window events from this window.
  871. * If l is null, no exception is thrown and no action is performed.
  872. *
  873. * @param l the window listener
  874. * @see #addWindowListener
  875. * @see #getWindowListeners
  876. */
  877. public synchronized void removeWindowListener(WindowListener l) {
  878. if (l == null) {
  879. return;
  880. }
  881. windowListener = AWTEventMulticaster.remove(windowListener, l);
  882. }
  883. /**
  884. * Removes the specified window state listener so that it no
  885. * longer receives window events from this window. If
  886. * <code>l</code> is <code>null</code>, no exception is thrown and
  887. * no action is performed.
  888. *
  889. * @param l the window state listener
  890. * @see #addWindowStateListener
  891. * @see #getWindowStateListeners
  892. * @since 1.4
  893. */
  894. public synchronized void removeWindowStateListener(WindowStateListener l) {
  895. if (l == null) {
  896. return;
  897. }
  898. windowStateListener = AWTEventMulticaster.remove(windowStateListener, l);
  899. }
  900. /**
  901. * Removes the specified window focus listener so that it no longer
  902. * receives window events from this window.
  903. * If l is null, no exception is thrown and no action is performed.
  904. *
  905. * @param l the window focus listener
  906. * @see #addWindowFocusListener
  907. * @see #getWindowFocusListeners
  908. */
  909. public synchronized void removeWindowFocusListener(WindowFocusListener l) {
  910. if (l == null) {
  911. return;
  912. }
  913. windowFocusListener = AWTEventMulticaster.remove(windowFocusListener, l);
  914. }
  915. /**
  916. * Returns an array of all the window listeners
  917. * registered on this window.
  918. *
  919. * @return all of this window's <code>WindowListener</code>s
  920. * or an empty array if no window
  921. * listeners are currently registered
  922. *
  923. * @see #addWindowListener
  924. * @see #removeWindowListener
  925. * @since 1.4
  926. */
  927. public synchronized WindowListener[] getWindowListeners() {
  928. return (WindowListener[])(getListeners(WindowListener.class));
  929. }
  930. /**
  931. * Returns an array of all the window focus listeners
  932. * registered on this window.
  933. *
  934. * @return all of this window's <code>WindowFocusListener</code>s
  935. * or an empty array if no window focus
  936. * listeners are currently registered
  937. *
  938. * @see #addWindowFocusListener
  939. * @see #removeWindowFocusListener
  940. * @since 1.4
  941. */
  942. public synchronized WindowFocusListener[] getWindowFocusListeners() {
  943. return (WindowFocusListener[])(getListeners(WindowFocusListener.class));
  944. }
  945. /**
  946. * Returns an array of all the window state listeners
  947. * registered on this window.
  948. *
  949. * @return all of this window's <code>WindowStateListener</code>s
  950. * or an empty array if no window state
  951. * listeners are currently registered
  952. *
  953. * @see #addWindowStateListener
  954. * @see #removeWindowStateListener
  955. * @since 1.4
  956. */
  957. public synchronized WindowStateListener[] getWindowStateListeners() {
  958. return (WindowStateListener[])(getListeners(WindowStateListener.class));
  959. }
  960. /**
  961. * Returns an array of all the objects currently registered
  962. * as <code><em>Foo</em>Listener</code>s
  963. * upon this <code>Window</code>.
  964. * <code><em>Foo</em>Listener</code>s are registered using the
  965. * <code>add<em>Foo</em>Listener</code> method.
  966. *
  967. * <p>
  968. *
  969. * You can specify the <code>listenerType</code> argument
  970. * with a class literal, such as
  971. * <code><em>Foo</em>Listener.class</code>.
  972. * For example, you can query a
  973. * <code>Window</code> <code>w</code>
  974. * for its window listeners with the following code:
  975. *
  976. * <pre>WindowListener[] wls = (WindowListener[])(w.getListeners(WindowListener.class));</pre>
  977. *
  978. * If no such listeners exist, this method returns an empty array.
  979. *
  980. * @param listenerType the type of listeners requested; this parameter
  981. * should specify an interface that descends from
  982. * <code>java.util.EventListener</code>
  983. * @return an array of all objects registered as
  984. * <code><em>Foo</em>Listener</code>s on this window,
  985. * or an empty array if no such
  986. * listeners have been added
  987. * @exception ClassCastException if <code>listenerType</code>
  988. * doesn't specify a class or interface that implements
  989. * <code>java.util.EventListener</code>
  990. *
  991. * @see #getWindowListeners
  992. * @since 1.3
  993. */
  994. public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
  995. EventListener l = null;
  996. if (listenerType == WindowFocusListener.class) {
  997. l = windowFocusListener;
  998. } else if (listenerType == WindowStateListener.class) {
  999. l = windowStateListener;
  1000. } else if (listenerType == WindowListener.class) {
  1001. l = windowListener;
  1002. } else {
  1003. return super.getListeners(listenerType);
  1004. }
  1005. return AWTEventMulticaster.getListeners(l, listenerType);
  1006. }
  1007. // REMIND: remove when filtering is handled at lower level
  1008. boolean eventEnabled(AWTEvent e) {
  1009. switch(e.id) {
  1010. case WindowEvent.WINDOW_OPENED:
  1011. case WindowEvent.WINDOW_CLOSING:
  1012. case WindowEvent.WINDOW_CLOSED:
  1013. case WindowEvent.WINDOW_ICONIFIED:
  1014. case WindowEvent.WINDOW_DEICONIFIED:
  1015. case WindowEvent.WINDOW_ACTIVATED:
  1016. case WindowEvent.WINDOW_DEACTIVATED:
  1017. if ((eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0 ||
  1018. windowListener != null) {
  1019. return true;
  1020. }
  1021. return false;
  1022. case WindowEvent.WINDOW_GAINED_FOCUS:
  1023. case WindowEvent.WINDOW_LOST_FOCUS:
  1024. if ((eventMask & AWTEvent.WINDOW_FOCUS_EVENT_MASK) != 0 ||
  1025. windowFocusListener != null) {
  1026. return true;
  1027. }
  1028. return false;
  1029. case WindowEvent.WINDOW_STATE_CHANGED:
  1030. if ((eventMask & AWTEvent.WINDOW_STATE_EVENT_MASK) != 0 ||
  1031. windowStateListener != null) {
  1032. return true;
  1033. }
  1034. return false;
  1035. default:
  1036. break;
  1037. }
  1038. return super.eventEnabled(e);
  1039. }
  1040. /**
  1041. * Processes events on this window. If the event is an
  1042. * <code>WindowEvent</code>, it invokes the
  1043. * <code>processWindowEvent</code> method, else it invokes its
  1044. * superclass's <code>processEvent</code>.
  1045. * <p>Note that if the event parameter is <code>null</code>
  1046. * the behavior is unspecified and may result in an
  1047. * exception.
  1048. *
  1049. * @param e the event
  1050. */
  1051. protected void processEvent(AWTEvent e) {
  1052. if (e instanceof WindowEvent) {
  1053. switch (e.getID()) {
  1054. case WindowEvent.WINDOW_OPENED:
  1055. case WindowEvent.WINDOW_CLOSING:
  1056. case WindowEvent.WINDOW_CLOSED:
  1057. case WindowEvent.WINDOW_ICONIFIED:
  1058. case WindowEvent.WINDOW_DEICONIFIED:
  1059. case WindowEvent.WINDOW_ACTIVATED:
  1060. case WindowEvent.WINDOW_DEACTIVATED:
  1061. processWindowEvent((WindowEvent)e);
  1062. break;
  1063. case WindowEvent.WINDOW_GAINED_FOCUS:
  1064. case WindowEvent.WINDOW_LOST_FOCUS:
  1065. processWindowFocusEvent((WindowEvent)e);
  1066. break;
  1067. case WindowEvent.WINDOW_STATE_CHANGED:
  1068. processWindowStateEvent((WindowEvent)e);
  1069. default:
  1070. break;
  1071. }
  1072. return;
  1073. }
  1074. super.processEvent(e);
  1075. }
  1076. /**
  1077. * Processes window events occurring on this window by
  1078. * dispatching them to any registered WindowListener objects.
  1079. * NOTE: This method will not be called unless window events
  1080. * are enabled for this component; this happens when one of the
  1081. * following occurs:
  1082. * <ul>
  1083. * <li>A WindowListener object is registered via
  1084. * <code>addWindowListener</code>
  1085. * <li>Window events are enabled via <code>enableEvents</code>
  1086. * </ul>
  1087. * <p>Note that if the event parameter is <code>null</code>
  1088. * the behavior is unspecified and may result in an
  1089. * exception.
  1090. *
  1091. * @param e the window event
  1092. * @see Component#enableEvents
  1093. */
  1094. protected void processWindowEvent(WindowEvent e) {
  1095. WindowListener listener = windowListener;
  1096. if (listener != null) {
  1097. switch(e.getID()) {
  1098. case WindowEvent.WINDOW_OPENED:
  1099. listener.windowOpened(e);
  1100. break;
  1101. case WindowEvent.WINDOW_CLOSING:
  1102. listener.windowClosing(e);
  1103. break;
  1104. case WindowEvent.WINDOW_CLOSED:
  1105. listener.windowClosed(e);
  1106. break;
  1107. case WindowEvent.WINDOW_ICONIFIED:
  1108. listener.windowIconified(e);
  1109. break;
  1110. case WindowEvent.WINDOW_DEICONIFIED:
  1111. listener.windowDeiconified(e);
  1112. break;
  1113. case WindowEvent.WINDOW_ACTIVATED:
  1114. listener.windowActivated(e);
  1115. break;
  1116. case WindowEvent.WINDOW_DEACTIVATED:
  1117. listener.windowDeactivated(e);
  1118. break;
  1119. default:
  1120. break;
  1121. }
  1122. }
  1123. }
  1124. /**
  1125. * Processes window focus event occuring on this window by
  1126. * dispatching them to any registered WindowFocusListener objects.
  1127. * NOTE: this method will not be called unless window focus events
  1128. * are enabled for this window. This happens when one of the
  1129. * following occurs:
  1130. * <ul>
  1131. * <li>a WindowFocusListener is registered via
  1132. * <code>addWindowFocusListener</code>
  1133. * <li>Window focus events are enabled via <code>enableEvents</code>
  1134. * </ul>
  1135. * <p>Note that if the event parameter is <code>null</code>
  1136. * the behavior is unspecified and may result in an
  1137. * exception.
  1138. *
  1139. * @param e the window focus event
  1140. * @see Component#enableEvents
  1141. */
  1142. protected void processWindowFocusEvent(WindowEvent e) {
  1143. WindowFocusListener listener = windowFocusListener;
  1144. if (listener != null) {
  1145. switch (e.getID()) {
  1146. case WindowEvent.WINDOW_GAINED_FOCUS:
  1147. listener.windowGainedFocus(e);
  1148. break;
  1149. case WindowEvent.WINDOW_LOST_FOCUS:
  1150. listener.windowLostFocus(e);
  1151. break;
  1152. default:
  1153. break;
  1154. }
  1155. }
  1156. }
  1157. /**
  1158. * Processes window state event occuring on this window by
  1159. * dispatching them to any registered <code>WindowStateListener</code>
  1160. * objects.
  1161. * NOTE: this method will not be called unless window state events
  1162. * are enabled for this window. This happens when one of the
  1163. * following occurs:
  1164. * <ul>
  1165. * <li>a <code>WindowStateListener</code> is registered via
  1166. * <code>addWindowStateListener</code>
  1167. * <li>window state events are enabled via <code>enableEvents</code>
  1168. * </ul>
  1169. * <p>Note that if the event parameter is <code>null</code>
  1170. * the behavior is unspecified and may result in an
  1171. * exception.
  1172. *
  1173. * @param e the window state event
  1174. * @see java.awt.Component#enableEvents
  1175. * @since 1.4
  1176. */
  1177. protected void processWindowStateEvent(WindowEvent e) {
  1178. WindowStateListener listener = windowStateListener;
  1179. if (listener != null) {
  1180. switch (e.getID()) {
  1181. case WindowEvent.WINDOW_STATE_CHANGED:
  1182. listener.windowStateChanged(e);
  1183. break;
  1184. default:
  1185. break;
  1186. }
  1187. }
  1188. }
  1189. /**
  1190. * Implements a debugging hook -- checks to see if
  1191. * the user has typed <i>control-shift-F1</i>. If so,
  1192. * the list of child windows is dumped to <code>System.out</code>.
  1193. * @param e the keyboard event
  1194. */
  1195. void preProcessKeyEvent(KeyEvent e) {
  1196. // Dump the list of child windows to System.out.
  1197. if (e.isActionKey() && e.getKeyCode() == KeyEvent.VK_F1 &&
  1198. e.isControlDown() && e.isShiftDown() &&
  1199. e.getID() == KeyEvent.KEY_PRESSED) {
  1200. list(System.out, 0);
  1201. }
  1202. }
  1203. void postProcessKeyEvent(KeyEvent e) {
  1204. // Do nothing
  1205. }
  1206. /**
  1207. * Changes the always-on-top window state. An always-on-top window
  1208. * is a window that stays above all other windows except maybe
  1209. * other always-on-top windows. If there are several always-on-top
  1210. * windows the order in which they stay relative to each other is
  1211. * not specified and is platform dependent.
  1212. * <p>
  1213. * If some other window already is always-on-top then the
  1214. * relative order between these windows is unspecified (depends on
  1215. * platform). No window can be brought to be over always-on-top
  1216. * window except maybe another always-on-top window.
  1217. * <p>
  1218. * All owned windows of an always-on-top window automatically
  1219. * become always-on-top windows. If a window ceases to be
  1220. * always-on-top its owned windows cease to be always-on-top.
  1221. * <p> When an always-on-top window is sent {@link #toBack toBack}
  1222. * its always-on-top state is set to <code>false</code>.
  1223. * <p>
  1224. * This method makes the window always-on-top if
  1225. * <code>alwaysOnTop</code> is <code>true</code>. If the window is
  1226. * visible, this includes bringing window <code>toFront</code>, then
  1227. * "sticking" it to the top-most position. If the window is not
  1228. * visible it does nothing other than setting the always-on-top
  1229. * property. If later the window is shown, it will be always-on-top.
  1230. * If the Window is already always-on-top, this call does nothing.
  1231. * <p>
  1232. * If <code>alwaysOnTop</code> is <code>false</code> this
  1233. * method changes the state from always-on-top to normal. The window
  1234. * remains top-most but its z-order can be changed in the normal way
  1235. * as for any other window. Does nothing if this Window is not
  1236. * always-on-top. Has no effect on relative z-order of windows if
  1237. * there are no other always-on-top windows.
  1238. * <p>
  1239. * <b>Note</b>: some platforms might not support always-on-top
  1240. * windows. There is no public API to detect if the platform
  1241. * supports always-on-top at runtime.
  1242. * <p>
  1243. * If a SecurityManager is installed, the calling thread must be
  1244. * granted the AWTPermission "setWindowAlwaysOnTop" in
  1245. * order to set the value of this property. If this
  1246. * permission is not granted, this method will throw a
  1247. * SecurityException, and the current value of the property will
  1248. * be left unchanged.
  1249. *
  1250. * @param alwaysOnTop new value of always-on-top state of the
  1251. * window
  1252. * @throws SecurityException if the calling thread does not have permission
  1253. * to set the value of always-on-top property
  1254. * @see #isAlwaysOnTop
  1255. * @see #toFront
  1256. * @see #toBack
  1257. * @see AWTPermission
  1258. * @since 1.5
  1259. */
  1260. public final void setAlwaysOnTop(boolean alwaysOnTop)
  1261. throws SecurityException
  1262. {
  1263. SecurityManager security = System.getSecurityManager();
  1264. if (security != null) {
  1265. security.checkPermission(SecurityConstants.SET_WINDOW_ALWAYS_ON_TOP_PERMISSION);
  1266. }
  1267. boolean oldAlwaysOnTop;
  1268. synchronized(this) {
  1269. oldAlwaysOnTop = this.alwaysOnTop;
  1270. this.alwaysOnTop = alwaysOnTop;
  1271. }
  1272. if (oldAlwaysOnTop != alwaysOnTop ) {
  1273. WindowPeer peer = (WindowPeer)this.peer;
  1274. synchronized(getTreeLock()) {
  1275. if (peer != null) {
  1276. peer.updateAlwaysOnTop();
  1277. }
  1278. }
  1279. firePropertyChange("alwaysOnTop", oldAlwaysOnTop, alwaysOnTop);
  1280. }
  1281. }
  1282. /**
  1283. * Returns whether this window is an always-on-top window.
  1284. * @return <code>true</code>, if the window is in always-on-top state,
  1285. * <code>false</code> otherwise
  1286. * @see #setAlwaysOnTop
  1287. * @since 1.5
  1288. */
  1289. public final boolean isAlwaysOnTop() {
  1290. return alwaysOnTop;
  1291. }
  1292. /**
  1293. * Returns the child Component of this Window that has focus if this Window
  1294. * is focused; returns null otherwise.
  1295. *
  1296. * @return the child Component with focus, or null if this Window is not
  1297. * focused
  1298. * @see #getMostRecentFocusOwner
  1299. * @see #isFocused
  1300. */
  1301. public Component getFocusOwner() {
  1302. return (isFocused())
  1303. ? KeyboardFocusManager.getCurrentKeyboardFocusManager().
  1304. getFocusOwner()
  1305. : null;
  1306. }
  1307. /**
  1308. * Returns the child Component of this Window that will receive the focus
  1309. * when this Window is focused. If this Window is currently focused, this
  1310. * method returns the same Component as <code>getFocusOwner()</code>. If
  1311. * this Window is not focused, then the child Component that most recently
  1312. * requested focus will be returned. If no child Component has ever
  1313. * requested focus, and this is a focusable Window, then this Window's
  1314. * initial focusable Component is returned. If no child Component has ever
  1315. * requested focus, and this is a non-focusable Window, null is returned.
  1316. *
  1317. * @return the child Component that will receive focus when this Window is
  1318. * focused
  1319. * @see #getFocusOwner
  1320. * @see #isFocused
  1321. * @see #isFocusableWindow
  1322. * @since 1.4
  1323. */
  1324. public Component getMostRecentFocusOwner() {
  1325. if (isFocused()) {
  1326. return getFocusOwner();
  1327. } else {
  1328. Component mostRecent =
  1329. KeyboardFocusManager.getMostRecentFocusOwner(this);
  1330. if (mostRecent != null) {
  1331. return mostRecent;
  1332. } else {
  1333. return (isFocusableWindow())
  1334. ? getFocusTraversalPolicy().getInitialComponent(this)
  1335. : null;
  1336. }
  1337. }
  1338. }
  1339. /**
  1340. * Returns whether this Window is active. Only a Frame or a Dialog may be
  1341. * active. The native windowing system may denote the active Window or its
  1342. * children with special decorations, such as a highlighted title bar. The
  1343. * active Window is always either the focused Window, or the first Frame or
  1344. * Dialog that is an owner of the focused Window.
  1345. *
  1346. * @return whether this is the active Window.
  1347. * @see #isFocused
  1348. * @since 1.4
  1349. */
  1350. public boolean isActive() {
  1351. return (KeyboardFocusManager.getCurrentKeyboardFocusManager().
  1352. getActiveWindow() == this);
  1353. }
  1354. /**
  1355. * Returns whether this Window is focused. If there exists a focus owner,
  1356. * the focused Window is the Window that is, or contains, that focus owner.
  1357. * If there is no focus owner, then no Window is focused.
  1358. * <p>
  1359. * If the focused Window is a Frame or a Dialog it is also the active
  1360. * Window. Otherwise, the active Window is the first Frame or Dialog that
  1361. * is an owner of the focused Window.
  1362. *
  1363. * @return whether this is the focused Window.
  1364. * @see #isActive
  1365. * @since 1.4
  1366. */
  1367. public boolean isFocused() {
  1368. return (KeyboardFocusManager.getCurrentKeyboardFocusManager().
  1369. getGlobalFocusedWindow() == this);
  1370. }
  1371. /**
  1372. * Gets a focus traversal key for this Window. (See <code>
  1373. * setFocusTraversalKeys</code> for a full description of each key.)
  1374. * <p>
  1375. * If the traversal key has not been explicitly set for this Window,
  1376. * then this Window's parent's traversal key is returned. If the
  1377. * traversal key has not been explicitly set for any of this Window's
  1378. * ancestors, then the current KeyboardFocusManager's default traversal key
  1379. * is returned.
  1380. *
  1381. * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  1382. * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
  1383. * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
  1384. * KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
  1385. * @return the AWTKeyStroke for the specified key
  1386. * @see Container#setFocusTraversalKeys
  1387. * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
  1388. * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
  1389. * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
  1390. * @see KeyboardFocusManager#DOWN_CYCLE_TRAVERSAL_KEYS
  1391. * @throws IllegalArgumentException if id is not one of
  1392. * KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  1393. * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
  1394. * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
  1395. * KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
  1396. * @since 1.4
  1397. */
  1398. public Set<AWTKeyStroke> getFocusTraversalKeys(int id) {
  1399. if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) {
  1400. throw new IllegalArgumentException("invalid focus traversal key identifier");
  1401. }
  1402. // Okay to return Set directly because it is an unmodifiable view
  1403. Set keystrokes = (focusTraversalKeys != null)
  1404. ? focusTraversalKeys[id]
  1405. : null;
  1406. if (keystrokes != null) {
  1407. return keystrokes;
  1408. } else {
  1409. return KeyboardFocusManager.getCurrentKeyboardFocusManager().
  1410. getDefaultFocusTraversalKeys(id);
  1411. }
  1412. }
  1413. /**
  1414. * Does nothing because Windows must always be roots of a focus traversal
  1415. * cycle. The passed-in value is ignored.
  1416. *
  1417. * @param focusCycleRoot this value is ignored
  1418. * @see #isFocusCycleRoot
  1419. * @see Container#setFocusTraversalPolicy
  1420. * @see Container#getFocusTraversalPolicy
  1421. * @since 1.4
  1422. */
  1423. public final void setFocusCycleRoot(boolean focusCycleRoot) {
  1424. }
  1425. /**
  1426. * Always returns <code>true</code> because all Windows must be roots of a
  1427. * focus traversal cycle.
  1428. *
  1429. * @return <code>true</code>
  1430. * @see #setFocusCycleRoot
  1431. * @see Container#setFocusTraversalPolicy
  1432. * @see Container#getFocusTraversalPolicy
  1433. * @since 1.4
  1434. */
  1435. public final boolean isFocusCycleRoot() {
  1436. return true;
  1437. }
  1438. /**
  1439. * Always returns <code>null</code> because Windows have no ancestors; they
  1440. * represent the top of the Component hierarchy.
  1441. *
  1442. * @return <code>null</code>
  1443. * @see Container#isFocusCycleRoot()
  1444. * @since 1.4
  1445. */
  1446. public final Container getFocusCycleRootAncestor() {
  1447. return null;
  1448. }
  1449. /**
  1450. * Returns whether this Window can become the focused Window, that is,
  1451. * whether this Window or any of its subcomponents can become the focus
  1452. * owner. For a Frame or Dialog to be focusable, its focusable Window state
  1453. * must be set to <code>true</code>. For a Window which is not a Frame or
  1454. * Dialog to be focusable, its focusable Window state must be set to
  1455. * <code>true</code>, its nearest owning Frame or Dialog must be
  1456. * showing on the screen, and it must contain at least one Component in
  1457. * its focus traversal cycle. If any of these conditions is not met, then
  1458. * neither this Window nor any of its subcomponents can become the focus
  1459. * owner.
  1460. *
  1461. * @return <code>true</code> if this Window can be the focused Window;
  1462. * <code>false</code> otherwise
  1463. * @see #getFocusableWindowState
  1464. * @see #setFocusableWindowState
  1465. * @see #isShowing
  1466. * @see Component#isFocusable
  1467. * @since 1.4
  1468. */
  1469. public final boolean isFocusableWindow() {
  1470. // If a Window/Frame/Dialog was made non-focusable, then it is always
  1471. // non-focusable.
  1472. if (!getFocusableWindowState()) {
  1473. return false;
  1474. }
  1475. // All other tests apply only to Windows.
  1476. if (this instanceof Frame || this instanceof Dialog) {
  1477. return true;
  1478. }
  1479. // A Window must have at least one Component in its root focus
  1480. // traversal cycle to be focusable.
  1481. if (getFocusTraversalPolicy().getDefaultComponent(this) == null) {
  1482. return false;
  1483. }
  1484. // A Window's nearest owning Frame or Dialog must be showing on the
  1485. // screen.
  1486. for (Window owner = getOwner(); owner != null;
  1487. owner = owner.getOwner())
  1488. {
  1489. if (owner instanceof Frame || owner instanceof Dialog) {
  1490. return owner.isShowing();
  1491. }
  1492. }
  1493. return false;
  1494. }
  1495. /**
  1496. * Returns whether this Window can become the focused Window if it meets
  1497. * the other requirements outlined in <code>isFocusableWindow</code>. If
  1498. * this method returns <code>false</code>, then
  1499. * <code>isFocusableWindow</code> will return <code>false</code> as well.
  1500. * If this method returns <code>true</code>, then
  1501. * <code>isFocusableWindow</code> may return <code>true</code> or
  1502. * <code>false</code> depending upon the other requirements which must be
  1503. * met in order for a Window to be focusable.
  1504. * <p>
  1505. * By default, all Windows have a focusable Window state of
  1506. * <code>true</code>.
  1507. *
  1508. * @return whether this Window can be the focused Window
  1509. * @see #isFocusableWindow
  1510. * @see #setFocusableWindowState
  1511. * @see #isShowing
  1512. * @see Component#setFocusable
  1513. * @since 1.4
  1514. */
  1515. public boolean getFocusableWindowState() {
  1516. return focusableWindowState;
  1517. }
  1518. /**
  1519. * Sets whether this Window can become the focused Window if it meets
  1520. * the other requirements outlined in <code>isFocusableWindow</code>. If
  1521. * this Window's focusable Window state is set to <code>false</code>, then
  1522. * <code>isFocusableWindow</code> will return <code>false</code>. If this
  1523. * Window's focusable Window state is set to <code>true</code>, then
  1524. * <code>isFocusableWindow</code> may return <code>true</code> or
  1525. * <code>false</code> depending upon the other requirements which must be
  1526. * met in order for a Window to be focusable.
  1527. * <p>
  1528. * Setting a Window's focusability state to <code>false</code> is the
  1529. * standard mechanism for an application to identify to the AWT a Window
  1530. * which will be used as a floating palette or toolbar, and thus should be
  1531. * a non-focusable Window.
  1532. *
  1533. * Setting the focusability state on a visible <code>Window</code>
  1534. * can have a delayed effect on some platforms — the actual
  1535. * change may happen only when the <code>Window</code> becomes
  1536. * hidden and then visible again. To ensure consistent behavior
  1537. * across platforms, set the <code>Window</code>'s focusable state
  1538. * when the <code>WIndow</code> is invisible and then show it.
  1539. *
  1540. * @param focusableWindowState whether this Window can be the focused
  1541. * Window
  1542. * @see #isFocusableWindow
  1543. * @see #getFocusableWindowState
  1544. * @see #isShowing
  1545. * @see Component#setFocusable
  1546. * @since 1.4
  1547. */
  1548. public void setFocusableWindowState(boolean focusableWindowState) {
  1549. boolean oldFocusableWindowState;
  1550. synchronized (this) {
  1551. oldFocusableWindowState = this.focusableWindowState;
  1552. this.focusableWindowState = focusableWindowState;
  1553. }
  1554. firePropertyChange("focusableWindowState", oldFocusableWindowState,
  1555. focusableWindowState);
  1556. if (oldFocusableWindowState && !focusableWindowState && isFocused()) {
  1557. for (Window owner = (Window)getParent();
  1558. owner != null;
  1559. owner = (Window)owner.getParent())
  1560. {
  1561. Component toFocus =
  1562. KeyboardFocusManager.getMostRecentFocusOwner(owner);
  1563. if (toFocus != null && toFocus.requestFocus(false)) {
  1564. return;
  1565. }
  1566. }
  1567. KeyboardFocusManager.getCurrentKeyboardFocusManager().
  1568. clearGlobalFocusOwner();
  1569. }
  1570. }
  1571. /**
  1572. * Adds a PropertyChangeListener to the listener list. The listener is
  1573. * registered for all bound properties of this class, including the
  1574. * following:
  1575. * <ul>
  1576. * <li>this Window's font ("font")</li>
  1577. * <li>this Window's background color ("background")</li>
  1578. * <li>this Window's foreground color ("foreground")</li>
  1579. * <li>this Window's focusability ("focusable")</li>
  1580. * <li>this Window's focus traversal keys enabled state
  1581. * ("focusTraversalKeysEnabled")</li>
  1582. * <li>this Window's Set of FORWARD_TRAVERSAL_KEYS
  1583. * ("forwardFocusTraversalKeys")</li>
  1584. * <li>this Window's Set of BACKWARD_TRAVERSAL_KEYS
  1585. * ("backwardFocusTraversalKeys")</li>
  1586. * <li>this Window's Set of UP_CYCLE_TRAVERSAL_KEYS
  1587. * ("upCycleFocusTraversalKeys")</li>
  1588. * <li>this Window's Set of DOWN_CYCLE_TRAVERSAL_KEYS
  1589. * ("downCycleFocusTraversalKeys")</li>
  1590. * <li>this Window's focus traversal policy ("focusTraversalPolicy")
  1591. * </li>
  1592. * <li>this Window's focusable Window state ("focusableWindowState")
  1593. * </li>
  1594. * <li>this Window's always-on-top state("alwaysOnTop")</li>
  1595. * </ul>
  1596. * Note that if this Window is inheriting a bound property, then no
  1597. * event will be fired in response to a change in the inherited property.
  1598. * <p>
  1599. * If listener is null, no exception is thrown and no action is performed.
  1600. *
  1601. * @param listener the PropertyChangeListener to be added
  1602. *
  1603. * @see Component#removePropertyChangeListener
  1604. * @see #addPropertyChangeListener(java.lang.String,java.beans.PropertyChangeListener)
  1605. */
  1606. public void addPropertyChangeListener(PropertyChangeListener listener) {
  1607. super.addPropertyChangeListener(listener);
  1608. }
  1609. /**
  1610. * Adds a PropertyChangeListener to the listener list for a specific
  1611. * property. The specified property may be user-defined, or one of the
  1612. * following:
  1613. * <ul>
  1614. * <li>this Window's font ("font")</li>
  1615. * <li>this Window's background color ("background")</li>
  1616. * <li>this Window's foreground color ("foreground")</li>
  1617. * <li>this Window's focusability ("focusable")</li>
  1618. * <li>this Window's focus traversal keys enabled state
  1619. * ("focusTraversalKeysEnabled")</li>
  1620. * <li>this Window's Set of FORWARD_TRAVERSAL_KEYS
  1621. * ("forwardFocusTraversalKeys")</li>
  1622. * <li>this Window's Set of BACKWARD_TRAVERSAL_KEYS
  1623. * ("backwardFocusTraversalKeys")</li>
  1624. * <li>this Window's Set of UP_CYCLE_TRAVERSAL_KEYS
  1625. * ("upCycleFocusTraversalKeys")</li>
  1626. * <li>this Window's Set of DOWN_CYCLE_TRAVERSAL_KEYS
  1627. * ("downCycleFocusTraversalKeys")</li>
  1628. * <li>this Window's focus traversal policy ("focusTraversalPolicy")
  1629. * </li>
  1630. * <li>this Window's focusable Window state ("focusableWindowState")
  1631. * </li>
  1632. * <li>this Window's always-on-top state("alwaysOnTop")</li>
  1633. * </ul>
  1634. * Note that if this Window is inheriting a bound property, then no
  1635. * event will be fired in response to a change in the inherited property.
  1636. * <p>
  1637. * If listener is null, no exception is thrown and no action is performed.
  1638. *
  1639. * @param propertyName one of the property names listed above
  1640. * @param listener the PropertyChangeListener to be added
  1641. *
  1642. * @see #addPropertyChangeListener(java.beans.PropertyChangeListener)
  1643. * @see Component#removePropertyChangeListener
  1644. */
  1645. public void addPropertyChangeListener(String propertyName,
  1646. PropertyChangeListener listener) {
  1647. super.addPropertyChangeListener(propertyName, listener);
  1648. }
  1649. /**
  1650. * Dispatches an event to this window or one of its sub components.
  1651. * @param e the event
  1652. */
  1653. void dispatchEventImpl(AWTEvent e) {
  1654. if (e.getID() == ComponentEvent.COMPONENT_RESIZED) {
  1655. invalidate();
  1656. validate();
  1657. }
  1658. super.dispatchEventImpl(e);
  1659. }
  1660. /**
  1661. * @deprecated As of JDK version 1.1
  1662. * replaced by <code>dispatchEvent(AWTEvent)</code>.
  1663. */
  1664. @Deprecated
  1665. public boolean postEvent(Event e) {
  1666. if (handleEvent(e)) {
  1667. e.consume();
  1668. return true;
  1669. }
  1670. return false;
  1671. }
  1672. /**
  1673. * Checks if this Window is showing on screen.
  1674. * @see Component#setVisible
  1675. */
  1676. public boolean isShowing() {
  1677. return visible;
  1678. }
  1679. /**
  1680. * @deprecated As of J2SE 1.4, replaced by
  1681. * {@link Component#applyComponentOrientation Component.applyComponentOrientation}.
  1682. */
  1683. @Deprecated
  1684. public void applyResourceBundle(ResourceBundle rb) {
  1685. applyComponentOrientation(ComponentOrientation.getOrientation(rb));
  1686. }
  1687. /**
  1688. * @deprecated As of J2SE 1.4, replaced by
  1689. * {@link Component#applyComponentOrientation Component.applyComponentOrientation}.
  1690. */
  1691. @Deprecated
  1692. public void applyResourceBundle(String rbName) {
  1693. applyResourceBundle(ResourceBundle.getBundle(rbName));
  1694. }
  1695. /*
  1696. * Support for tracking all windows owned by this window
  1697. */
  1698. void addOwnedWindow(WeakReference weakWindow) {
  1699. if (weakWindow != null) {
  1700. synchronized(ownedWindowList) {
  1701. // this if statement should really be an assert, but we don't
  1702. // have asserts...
  1703. if (!ownedWindowList.contains(weakWindow)) {
  1704. ownedWindowList.addElement(weakWindow);
  1705. }
  1706. }
  1707. }
  1708. }
  1709. void removeOwnedWindow(WeakReference weakWindow) {
  1710. if (weakWindow != null) {
  1711. // synchronized block not required since removeElement is
  1712. // already synchronized
  1713. ownedWindowList.removeElement(weakWindow);
  1714. }
  1715. }
  1716. void connectOwnedWindow(Window child) {
  1717. WeakReference weakChild = new WeakReference(child);
  1718. child.weakThis = weakChild;
  1719. child.parent = this;
  1720. addOwnedWindow(weakChild);
  1721. }
  1722. /**
  1723. * The window serialized data version.
  1724. *
  1725. * @serial
  1726. */
  1727. private int windowSerializedDataVersion = 2;
  1728. /**
  1729. * Writes default serializable fields to stream. Writes
  1730. * a list of serializable <code>WindowListener</code>s and
  1731. * <code>WindowFocusListener</code>s as optional data.
  1732. * Writes a list of child windows as optional data.
  1733. *
  1734. * @param s the <code>ObjectOutputStream</code> to write
  1735. * @serialData <code>null</code> terminated sequence of
  1736. * 0 or more pairs; the pair consists of a <code>String</code>
  1737. * and and <code>Object</code> the <code>String</code>
  1738. * indicates the type of object and is one of the following:
  1739. * <code>windowListenerK</code> indicating a
  1740. * <code>WindowListener</code> object;
  1741. * <code>windowFocusWindowK</code> indicating a
  1742. * <code>WindowFocusListener</code> object;
  1743. * <code>ownedWindowK</code> indicating a child
  1744. * <code>Window</code> object
  1745. *
  1746. * @see AWTEventMulticaster#save(java.io.ObjectOutputStream, java.lang.String, java.util.EventListener)
  1747. * @see Component#windowListenerK
  1748. * @see Component#windowFocusListenerK
  1749. * @see Component#ownedWindowK
  1750. * @see #readObject(ObjectInputStream)
  1751. */
  1752. private void writeObject(ObjectOutputStream s) throws IOException {
  1753. synchronized (this) {
  1754. // Update old focusMgr fields so that our object stream can be read
  1755. // by previous releases
  1756. focusMgr = new FocusManager();
  1757. focusMgr.focusRoot = this;
  1758. focusMgr.focusOwner = getMostRecentFocusOwner();
  1759. s.defaultWriteObject();
  1760. // Clear fields so that we don't keep extra references around
  1761. focusMgr = null;
  1762. AWTEventMulticaster.save(s, windowListenerK, windowListener);
  1763. AWTEventMulticaster.save(s, windowFocusListenerK, windowFocusListener);
  1764. AWTEventMulticaster.save(s, windowStateListenerK, windowStateListener);
  1765. }
  1766. s.writeObject(null);
  1767. synchronized (ownedWindowList) {
  1768. for (int i = 0; i < ownedWindowList.size(); i++) {
  1769. Window child = (Window) (((WeakReference)
  1770. (ownedWindowList.elementAt(i))).get());
  1771. if (child != null) {
  1772. s.writeObject(ownedWindowK);
  1773. s.writeObject(child);
  1774. }
  1775. }
  1776. }
  1777. s.writeObject(null);
  1778. }
  1779. /**
  1780. * Reads the <code>ObjectInputStream</code> and an optional
  1781. * list of listeners to receive various events fired by
  1782. * the component; also reads a list of
  1783. * (possibly <code>null</code>) child windows.
  1784. * Unrecognized keys or values will be ignored.
  1785. *
  1786. * @param s the <code>ObjectInputStream</code> to read
  1787. * @exception HeadlessException if
  1788. * <code>GraphicsEnvironment.isHeadless</code> returns
  1789. * <code>true</code>
  1790. * @see java.awt.GraphicsEnvironment#isHeadless
  1791. * @see #writeObject
  1792. */
  1793. private void readObject(ObjectInputStream s)
  1794. throws ClassNotFoundException, IOException, HeadlessException
  1795. {
  1796. GraphicsEnvironment.checkHeadless();
  1797. s.defaultReadObject();
  1798. if (windowSerializedDataVersion < 2) {
  1799. // Translate old-style focus tracking to new model. For 1.4 and
  1800. // later releases, we'll rely on the Window's initial focusable
  1801. // Component.
  1802. if (focusMgr != null) {
  1803. if (focusMgr.focusOwner != null) {
  1804. KeyboardFocusManager.
  1805. setMostRecentFocusOwner(this, focusMgr.focusOwner);
  1806. }
  1807. }
  1808. // This field is non-transient and relies on default serialization.
  1809. // However, the default value is insufficient, so we need to set
  1810. // it explicitly for object data streams prior to 1.4.
  1811. focusableWindowState = true;
  1812. }
  1813. // 1.4 doesn't use this field, so just null it out.
  1814. focusMgr = null;
  1815. ownedWindowList = new Vector();
  1816. Object keyOrNull;
  1817. while(null != (keyOrNull = s.readObject())) {
  1818. String key = ((String)keyOrNull).intern();
  1819. if (windowListenerK == key) {
  1820. addWindowListener((WindowListener)(s.readObject()));
  1821. } else if (windowFocusListenerK == key) {
  1822. addWindowFocusListener((WindowFocusListener)(s.readObject()));
  1823. } else if (windowStateListenerK == key) {
  1824. addWindowStateListener((WindowStateListener)(s.readObject()));
  1825. } else // skip value for unrecognized key
  1826. s.readObject();
  1827. }
  1828. try {
  1829. while (null != (keyOrNull = s.readObject())) {
  1830. String key = ((String)keyOrNull).intern();
  1831. if (ownedWindowK == key)
  1832. connectOwnedWindow((Window) s.readObject());
  1833. else // skip value for unrecognized key
  1834. s.readObject();
  1835. }
  1836. }
  1837. catch (OptionalDataException e) {
  1838. // 1.1 serialized form
  1839. // ownedWindowList will be updated by Frame.readObject
  1840. }
  1841. setWarningString();
  1842. inputContextLock = new Object();
  1843. // Deserialized Windows are not yet visible.
  1844. visible = false;
  1845. }
  1846. /*
  1847. * --- Accessibility Support ---
  1848. *
  1849. */
  1850. /**
  1851. * Gets the AccessibleContext associated with this Window.
  1852. * For windows, the AccessibleContext takes the form of an
  1853. * AccessibleAWTWindow.
  1854. * A new AccessibleAWTWindow instance is created if necessary.
  1855. *
  1856. * @return an AccessibleAWTWindow that serves as the
  1857. * AccessibleContext of this Window
  1858. */
  1859. public AccessibleContext getAccessibleContext() {
  1860. if (accessibleContext == null) {
  1861. accessibleContext = new AccessibleAWTWindow();
  1862. }
  1863. return accessibleContext;
  1864. }
  1865. /**
  1866. * This class implements accessibility support for the
  1867. * <code>Window</code> class. It provides an implementation of the
  1868. * Java Accessibility API appropriate to window user-interface elements.
  1869. */
  1870. protected class AccessibleAWTWindow extends AccessibleAWTContainer
  1871. {
  1872. /*
  1873. * JDK 1.3 serialVersionUID
  1874. */
  1875. private static final long serialVersionUID = 4215068635060671780L;
  1876. /**
  1877. * Get the role of this object.
  1878. *
  1879. * @return an instance of AccessibleRole describing the role of the
  1880. * object
  1881. * @see javax.accessibility.AccessibleRole
  1882. */
  1883. public AccessibleRole getAccessibleRole() {
  1884. return AccessibleRole.WINDOW;
  1885. }
  1886. /**
  1887. * Get the state of this object.
  1888. *
  1889. * @return an instance of AccessibleStateSet containing the current
  1890. * state set of the object
  1891. * @see javax.accessibility.AccessibleState
  1892. */
  1893. public AccessibleStateSet getAccessibleStateSet() {
  1894. AccessibleStateSet states = super.getAccessibleStateSet();
  1895. if (getFocusOwner() != null) {
  1896. states.add(AccessibleState.ACTIVE);
  1897. }
  1898. return states;
  1899. }
  1900. } // inner class AccessibleAWTWindow
  1901. /**
  1902. * This method returns the GraphicsConfiguration used by this Window.
  1903. */
  1904. public GraphicsConfiguration getGraphicsConfiguration() {
  1905. //NOTE: for multiscreen, this will need to take into account
  1906. //which screen the window is on/mostly on instead of returning the
  1907. //default or constructor argument config.
  1908. synchronized(getTreeLock()) {
  1909. if (graphicsConfig == null && !GraphicsEnvironment.isHeadless()) {
  1910. graphicsConfig =
  1911. GraphicsEnvironment. getLocalGraphicsEnvironment().
  1912. getDefaultScreenDevice().
  1913. getDefaultConfiguration();
  1914. }
  1915. return graphicsConfig;
  1916. }
  1917. }
  1918. /**
  1919. * Reset this Window's GraphicsConfiguration to match its peer.
  1920. */
  1921. void resetGC() {
  1922. if (!GraphicsEnvironment.isHeadless()) {
  1923. // use the peer's GC
  1924. setGCFromPeer();
  1925. // if it's still null, use the default
  1926. if (graphicsConfig == null) {
  1927. graphicsConfig = GraphicsEnvironment.
  1928. getLocalGraphicsEnvironment().
  1929. getDefaultScreenDevice().
  1930. getDefaultConfiguration();
  1931. }
  1932. if (dbg.on) {
  1933. dbg.println("+ Window.resetGC(): new GC is \n+ " + graphicsConfig + "\n+ this is " + this);
  1934. }
  1935. }
  1936. }
  1937. /**
  1938. * Sets the location of the window relative to the specified
  1939. * component. If the component is not currently showing,
  1940. * or <code>c</code> is <code>null</code>, the
  1941. * window is centered on the screen. If the bottom of the
  1942. * component is offscreen, the window is placed to the
  1943. * side of the <code>Component</code> that is closest
  1944. * to the center of the screen. So if the
  1945. * <code>Component</code> is on the right part of the
  1946. * screen, the <code>Window</code> is placed to its left,
  1947. * and visa versa.
  1948. *
  1949. * @param c the component in relation to which the window's location
  1950. * is determined
  1951. * @since 1.4
  1952. */
  1953. public void setLocationRelativeTo(Component c) {
  1954. Container root=null;
  1955. if (c != null) {
  1956. if (c instanceof Window || c instanceof Applet) {
  1957. root = (Container)c;
  1958. } else {
  1959. Container parent;
  1960. for(parent = c.getParent() ; parent != null ; parent = parent.getParent()) {
  1961. if (parent instanceof Window || parent instanceof Applet) {
  1962. root = parent;
  1963. break;
  1964. }
  1965. }
  1966. }
  1967. }
  1968. if((c != null && !c.isShowing()) || root == null ||
  1969. !root.isShowing()) {
  1970. Dimension paneSize = getSize();
  1971. Dimension screenSize = getToolkit().getScreenSize();
  1972. setLocation((screenSize.width - paneSize.width) / 2,
  1973. (screenSize.height - paneSize.height) / 2);
  1974. } else {
  1975. Dimension invokerSize = c.getSize();
  1976. Point invokerScreenLocation = c.getLocationOnScreen();
  1977. Rectangle windowBounds = getBounds();
  1978. int dx = invokerScreenLocation.x+((invokerSize.width-windowBounds.width)>>1);
  1979. int dy = invokerScreenLocation.y+((invokerSize.height - windowBounds.height)>>1);
  1980. Rectangle ss = root.getGraphicsConfiguration().getBounds();
  1981. // Adjust for bottom edge being offscreen
  1982. if (dy+windowBounds.height>ss.height) {
  1983. dy = ss.height-windowBounds.height;
  1984. if (invokerScreenLocation.x - ss.x + invokerSize.width / 2 <
  1985. ss.width / 2) {
  1986. dx = invokerScreenLocation.x+invokerSize.width;
  1987. }
  1988. else {
  1989. dx = invokerScreenLocation.x-windowBounds.width;
  1990. }
  1991. }
  1992. // Avoid being placed off the edge of the screen
  1993. if (dx+windowBounds.width > ss.x + ss.width) {
  1994. dx = ss.x + ss.width - windowBounds.width;
  1995. }
  1996. if (dx < ss.x) dx = 0;
  1997. if (dy < ss.y) dy = 0;
  1998. setLocation(dx, dy);
  1999. }
  2000. }
  2001. /**
  2002. * Overridden from Component. Top-level Windows should not propagate a
  2003. * MouseWheelEvent beyond themselves into their owning Windows.
  2004. */
  2005. void deliverMouseWheelToAncestor(MouseWheelEvent e) {}
  2006. /**
  2007. * Overridden from Component. Top-level Windows don't dispatch to ancestors
  2008. */
  2009. boolean dispatchMouseWheelToAncestor(MouseWheelEvent e) {return false;}
  2010. /**
  2011. * Creates a new strategy for multi-buffering on this component.
  2012. * Multi-buffering is useful for rendering performance. This method
  2013. * attempts to create the best strategy available with the number of
  2014. * buffers supplied. It will always create a <code>BufferStrategy</code>
  2015. * with that number of buffers.
  2016. * A page-flipping strategy is attempted first, then a blitting strategy
  2017. * using accelerated buffers. Finally, an unaccelerated blitting
  2018. * strategy is used.
  2019. * <p>
  2020. * Each time this method is called,
  2021. * the existing buffer strategy for this component is discarded.
  2022. * @param numBuffers number of buffers to create
  2023. * @exception IllegalArgumentException if numBuffers is less than 1.
  2024. * @exception IllegalStateException if the component is not displayable
  2025. * @see #isDisplayable
  2026. * @see #getBufferStrategy
  2027. * @since 1.4
  2028. */
  2029. public void createBufferStrategy(int numBuffers) {
  2030. super.createBufferStrategy(numBuffers);
  2031. }
  2032. /**
  2033. * Creates a new strategy for multi-buffering on this component with the
  2034. * required buffer capabilities. This is useful, for example, if only
  2035. * accelerated memory or page flipping is desired (as specified by the
  2036. * buffer capabilities).
  2037. * <p>
  2038. * Each time this method
  2039. * is called, the existing buffer strategy for this component is discarded.
  2040. * @param numBuffers number of buffers to create, including the front buffer
  2041. * @param caps the required capabilities for creating the buffer strategy;
  2042. * cannot be <code>null</code>
  2043. * @exception AWTException if the capabilities supplied could not be
  2044. * supported or met; this may happen, for example, if there is not enough
  2045. * accelerated memory currently available, or if page flipping is specified
  2046. * but not possible.
  2047. * @exception IllegalArgumentException if numBuffers is less than 1, or if
  2048. * caps is <code>null</code>
  2049. * @see #getBufferStrategy
  2050. * @since 1.4
  2051. */
  2052. public void createBufferStrategy(int numBuffers,
  2053. BufferCapabilities caps) throws AWTException {
  2054. super.createBufferStrategy(numBuffers, caps);
  2055. }
  2056. /**
  2057. * @return the buffer strategy used by this component
  2058. * @see #createBufferStrategy
  2059. * @since 1.4
  2060. */
  2061. public BufferStrategy getBufferStrategy() {
  2062. return super.getBufferStrategy();
  2063. }
  2064. Component getTemporaryLostComponent() {
  2065. return temporaryLostComponent;
  2066. }
  2067. Component setTemporaryLostComponent(Component component) {
  2068. Component previousComp = temporaryLostComponent;
  2069. // Check that "component" is an acceptable focus owner and don't store it otherwise
  2070. // - or later we will have problems with opposite while handling WINDOW_GAINED_FOCUS
  2071. if (component == null
  2072. || (component.isDisplayable() && component.isVisible() && component.isEnabled() && component.isFocusable()))
  2073. {
  2074. temporaryLostComponent = component;
  2075. } else {
  2076. temporaryLostComponent = null;
  2077. }
  2078. return previousComp;
  2079. }
  2080. /**
  2081. * Checks whether this window can contain focus owner.
  2082. * Verifies that it is focusable and as container it can container focus owner.
  2083. * @since 1.5
  2084. */
  2085. boolean canContainFocusOwner(Component focusOwnerCandidate) {
  2086. return super.canContainFocusOwner(focusOwnerCandidate) && isFocusableWindow();
  2087. }
  2088. private boolean locationByPlatform = locationByPlatformProp;
  2089. /**
  2090. * Sets whether this Window should appear at the default location for the
  2091. * native windowing system or at the current location (returned by
  2092. * <code>getLocation</code>) the next time the Window is made visible.
  2093. * This behavior resembles a native window shown without programmatically
  2094. * setting its location. Most windowing systems cascade windows if their
  2095. * locations are not explicitly set. The actual location is determined once the
  2096. * window is shown on the screen.
  2097. * <p>
  2098. * This behavior can also be enabled by setting the System Property
  2099. * "java.awt.Window.locationByPlatform" to "true", though calls to this method
  2100. * take precedence.
  2101. * <p>
  2102. * Calls to <code>setVisible</code>, <code>setLocation</code> and
  2103. * <code>setBounds</code> after calling <code>setLocationByPlatform</code> clear
  2104. * this property of the Window.
  2105. * <p>
  2106. * For example, after the following code is executed:
  2107. * <pre><blockquote>
  2108. * setLocationByPlatform(true);
  2109. * setVisible(true);
  2110. * boolean flag = isLocationByPlatform();
  2111. * </blockquote></pre>
  2112. * The window will be shown at platform's default location and
  2113. * <code>flag</code> will be <code>false</code>.
  2114. * <p>
  2115. * In the following sample:
  2116. * <pre><blockquote>
  2117. * setLocationByPlatform(true);
  2118. * setLocation(10, 10);
  2119. * boolean flag = isLocationByPlatform();
  2120. * setVisible(true);
  2121. * </blockquote></pre>
  2122. * The window will be shown at (10, 10) and <code>flag</code> will be
  2123. * <code>false</code>.
  2124. *
  2125. * @param locationByPlatform <code>true</code> if this Window should appear
  2126. * at the default location, <code>false</code> if at the current location
  2127. * @throws <code>IllegalComponentStateException</code> if the window
  2128. * is showing on screen and locationByPlatform is <code>true</code>.
  2129. * @see #setLocation
  2130. * @see #isShowing
  2131. * @see #setVisible
  2132. * @see #isLocationByPlatform
  2133. * @see java.lang.System#getProperty(String)
  2134. * @since 1.5
  2135. */
  2136. public void setLocationByPlatform(boolean locationByPlatform) {
  2137. synchronized (getTreeLock()) {
  2138. if (locationByPlatform && isShowing()) {
  2139. throw new IllegalComponentStateException("The window is showing on screen.");
  2140. }
  2141. this.locationByPlatform = locationByPlatform;
  2142. }
  2143. }
  2144. /**
  2145. * Returns <code>true</code> if this Window will appear at the default location
  2146. * for the native windowing system the next time this Window is made visible.
  2147. * This method always returns <code>false</code> if the Window is showing on the
  2148. * screen.
  2149. *
  2150. * @return whether this Window will appear at the default location
  2151. * @see #setLocationByPlatform
  2152. * @see #isShowing
  2153. * @since 1.5
  2154. */
  2155. public boolean isLocationByPlatform() {
  2156. synchronized (getTreeLock()) {
  2157. return locationByPlatform;
  2158. }
  2159. }
  2160. /**
  2161. * @since 1.5
  2162. * @see #setLocationByPlatform
  2163. * @see #isLocationByPlatform
  2164. */
  2165. public void setBounds(int x, int y, int width, int height) {
  2166. synchronized (getTreeLock()) {
  2167. if (getBoundsOp() == ComponentPeer.SET_LOCATION ||
  2168. getBoundsOp() == ComponentPeer.SET_BOUNDS)
  2169. {
  2170. locationByPlatform = false;
  2171. }
  2172. super.setBounds(x, y, width, height);
  2173. }
  2174. }
  2175. } // class Window
  2176. /**
  2177. * This class is no longer used, but is maintained for Serialization
  2178. * backward-compatibility.
  2179. */
  2180. class FocusManager implements java.io.Serializable {
  2181. Container focusRoot;
  2182. Component focusOwner;
  2183. /*
  2184. * JDK 1.1 serialVersionUID
  2185. */
  2186. static final long serialVersionUID = 2491878825643557906L;
  2187. }