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