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