1. /*
  2. * @(#)Frame.java 1.147 04/05/18
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.awt.peer.FramePeer;
  9. import java.awt.event.*;
  10. import java.util.Vector;
  11. import java.io.Serializable;
  12. import java.io.ObjectOutputStream;
  13. import java.io.ObjectInputStream;
  14. import java.io.IOException;
  15. import sun.awt.AppContext;
  16. import sun.awt.SunToolkit;
  17. import java.lang.ref.WeakReference;
  18. import javax.accessibility.*;
  19. /**
  20. * A <code>Frame</code> is a top-level window with a title and a border.
  21. * <p>
  22. * The size of the frame includes any area designated for the
  23. * border. The dimensions of the border area may be obtained
  24. * using the <code>getInsets</code> method, however, since
  25. * these dimensions are platform-dependent, a valid insets
  26. * value cannot be obtained until the frame is made displayable
  27. * by either calling <code>pack</code> or <code>show</code>.
  28. * Since the border area is included in the overall size of the
  29. * frame, the border effectively obscures a portion of the frame,
  30. * constraining the area available for rendering and/or displaying
  31. * subcomponents to the rectangle which has an upper-left corner
  32. * location of <code>(insets.left, insets.top)</code>, and has a size of
  33. * <code>width - (insets.left + insets.right)</code> by
  34. * <code>height - (insets.top + insets.bottom)</code>.
  35. * <p>
  36. * The default layout for a frame is <code>BorderLayout</code>.
  37. * <p>
  38. * A frame may have its native decorations (i.e. <code>Frame</code>
  39. * and <code>Titlebar</code>) turned off
  40. * with <code>setUndecorated</code>. This can only be done while the frame
  41. * is not {@link Component#isDisplayable() displayable}.
  42. * <p>
  43. * In a multi-screen environment, you can create a <code>Frame</code>
  44. * on a different screen device by constructing the <code>Frame</code>
  45. * with {@link #Frame(GraphicsConfiguration)} or
  46. * {@link #Frame(String title, GraphicsConfiguration)}. The
  47. * <code>GraphicsConfiguration</code> object is one of the
  48. * <code>GraphicsConfiguration</code> objects of the target screen
  49. * device.
  50. * <p>
  51. * In a virtual device multi-screen environment in which the desktop
  52. * area could span multiple physical screen devices, the bounds of all
  53. * configurations are relative to the virtual-coordinate system. The
  54. * origin of the virtual-coordinate system is at the upper left-hand
  55. * corner of the primary physical screen. Depending on the location
  56. * of the primary screen in the virtual device, negative coordinates
  57. * are possible, as shown in the following figure.
  58. * <p>
  59. * <img src="doc-files/MultiScreen.gif"
  60. * alt="Diagram of virtual device encompassing three physical screens and one primary physical screen. The primary physical screen
  61. * shows (0,0) coords while a different physical screen shows (-80,-100) coords."
  62. * ALIGN=center HSPACE=10 VSPACE=7>
  63. * <p>
  64. * In such an environment, when calling <code>setLocation</code>,
  65. * you must pass a virtual coordinate to this method. Similarly,
  66. * calling <code>getLocationOnScreen</code> on a <code>Frame</code>
  67. * returns virtual device coordinates. Call the <code>getBounds</code>
  68. * method of a <code>GraphicsConfiguration</code> to find its origin in
  69. * the virtual coordinate system.
  70. * <p>
  71. * The following code sets the
  72. * location of the <code>Frame</code> at (10, 10) relative
  73. * to the origin of the physical screen of the corresponding
  74. * <code>GraphicsConfiguration</code>. If the bounds of the
  75. * <code>GraphicsConfiguration</code> is not taken into account, the
  76. * <code>Frame</code> location would be set at (10, 10) relative to the
  77. * virtual-coordinate system and would appear on the primary physical
  78. * screen, which might be different from the physical screen of the
  79. * specified <code>GraphicsConfiguration</code>.
  80. *
  81. * <pre>
  82. * Frame f = new Frame(GraphicsConfiguration gc);
  83. * Rectangle bounds = gc.getBounds();
  84. * f.setLocation(10 + bounds.x, 10 + bounds.y);
  85. * </pre>
  86. *
  87. * <p>
  88. * Frames are capable of generating the following types of
  89. * <code>WindowEvent</code>s:
  90. * <ul>
  91. * <li><code>WINDOW_OPENED</code>
  92. * <li><code>WINDOW_CLOSING</code>:
  93. * <br>If the program doesn't
  94. * explicitly hide or dispose the window while processing
  95. * this event, the window close operation is canceled.
  96. * <li><code>WINDOW_CLOSED</code>
  97. * <li><code>WINDOW_ICONIFIED</code>
  98. * <li><code>WINDOW_DEICONIFIED</code>
  99. * <li><code>WINDOW_ACTIVATED</code>
  100. * <li><code>WINDOW_DEACTIVATED</code>
  101. * <li><code>WINDOW_GAINED_FOCUS</code>
  102. * <li><code>WINDOW_LOST_FOCUS</code>
  103. * <li><code>WINDOW_STATE_CHANGED</code>
  104. * </ul>
  105. *
  106. * @version 1.147, 05/18/04
  107. * @author Sami Shaio
  108. * @see WindowEvent
  109. * @see Window#addWindowListener
  110. * @since JDK1.0
  111. */
  112. public class Frame extends Window implements MenuContainer {
  113. /* Note: These are being obsoleted; programs should use the Cursor class
  114. * variables going forward. See Cursor and Component.setCursor.
  115. */
  116. /**
  117. * @deprecated replaced by <code>Cursor.DEFAULT_CURSOR</code>.
  118. */
  119. @Deprecated
  120. public static final int DEFAULT_CURSOR = Cursor.DEFAULT_CURSOR;
  121. /**
  122. * @deprecated replaced by <code>Cursor.CROSSHAIR_CURSOR</code>.
  123. */
  124. @Deprecated
  125. public static final int CROSSHAIR_CURSOR = Cursor.CROSSHAIR_CURSOR;
  126. /**
  127. * @deprecated replaced by <code>Cursor.TEXT_CURSOR</code>.
  128. */
  129. @Deprecated
  130. public static final int TEXT_CURSOR = Cursor.TEXT_CURSOR;
  131. /**
  132. * @deprecated replaced by <code>Cursor.WAIT_CURSOR</code>.
  133. */
  134. @Deprecated
  135. public static final int WAIT_CURSOR = Cursor.WAIT_CURSOR;
  136. /**
  137. * @deprecated replaced by <code>Cursor.SW_RESIZE_CURSOR</code>.
  138. */
  139. @Deprecated
  140. public static final int SW_RESIZE_CURSOR = Cursor.SW_RESIZE_CURSOR;
  141. /**
  142. * @deprecated replaced by <code>Cursor.SE_RESIZE_CURSOR</code>.
  143. */
  144. @Deprecated
  145. public static final int SE_RESIZE_CURSOR = Cursor.SE_RESIZE_CURSOR;
  146. /**
  147. * @deprecated replaced by <code>Cursor.NW_RESIZE_CURSOR</code>.
  148. */
  149. @Deprecated
  150. public static final int NW_RESIZE_CURSOR = Cursor.NW_RESIZE_CURSOR;
  151. /**
  152. * @deprecated replaced by <code>Cursor.NE_RESIZE_CURSOR</code>.
  153. */
  154. @Deprecated
  155. public static final int NE_RESIZE_CURSOR = Cursor.NE_RESIZE_CURSOR;
  156. /**
  157. * @deprecated replaced by <code>Cursor.N_RESIZE_CURSOR</code>.
  158. */
  159. @Deprecated
  160. public static final int N_RESIZE_CURSOR = Cursor.N_RESIZE_CURSOR;
  161. /**
  162. * @deprecated replaced by <code>Cursor.S_RESIZE_CURSOR</code>.
  163. */
  164. @Deprecated
  165. public static final int S_RESIZE_CURSOR = Cursor.S_RESIZE_CURSOR;
  166. /**
  167. * @deprecated replaced by <code>Cursor.W_RESIZE_CURSOR</code>.
  168. */
  169. @Deprecated
  170. public static final int W_RESIZE_CURSOR = Cursor.W_RESIZE_CURSOR;
  171. /**
  172. * @deprecated replaced by <code>Cursor.E_RESIZE_CURSOR</code>.
  173. */
  174. @Deprecated
  175. public static final int E_RESIZE_CURSOR = Cursor.E_RESIZE_CURSOR;
  176. /**
  177. * @deprecated replaced by <code>Cursor.HAND_CURSOR</code>.
  178. */
  179. @Deprecated
  180. public static final int HAND_CURSOR = Cursor.HAND_CURSOR;
  181. /**
  182. * @deprecated replaced by <code>Cursor.MOVE_CURSOR</code>.
  183. */
  184. @Deprecated
  185. public static final int MOVE_CURSOR = Cursor.MOVE_CURSOR;
  186. /**
  187. * Frame is in the "normal" state. This symbolic constant names a
  188. * frame state with all state bits cleared.
  189. * @see #setExtendedState(int)
  190. * @see #getExtendedState
  191. */
  192. public static final int NORMAL = 0;
  193. /**
  194. * This state bit indicates that frame is iconified.
  195. * @see #setExtendedState(int)
  196. * @see #getExtendedState
  197. */
  198. public static final int ICONIFIED = 1;
  199. /**
  200. * This state bit indicates that frame is maximized in the
  201. * horizontal direction.
  202. * @see #setExtendedState(int)
  203. * @see #getExtendedState
  204. * @since 1.4
  205. */
  206. public static final int MAXIMIZED_HORIZ = 2;
  207. /**
  208. * This state bit indicates that frame is maximized in the
  209. * vertical direction.
  210. * @see #setExtendedState(int)
  211. * @see #getExtendedState
  212. * @since 1.4
  213. */
  214. public static final int MAXIMIZED_VERT = 4;
  215. /**
  216. * This state bit mask indicates that frame is fully maximized
  217. * (that is both horizontally and vertically). It is just a
  218. * convenience alias for
  219. * <code>MAXIMIZED_VERT | MAXIMIZED_HORIZ</code>.
  220. *
  221. * <p>Note that the correct test for frame being fully maximized is
  222. * <pre>
  223. * (state & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH
  224. * </pre>
  225. *
  226. * <p>To test is frame is maximized in <em>some</em> direction use
  227. * <pre>
  228. * (state & Frame.MAXIMIZED_BOTH) != 0
  229. * </pre>
  230. *
  231. * @see #setExtendedState(int)
  232. * @see #getExtendedState
  233. * @since 1.4
  234. */
  235. public static final int MAXIMIZED_BOTH = MAXIMIZED_VERT | MAXIMIZED_HORIZ;
  236. /**
  237. * Maximized bounds for this frame.
  238. * @see #setMaximizedBounds(Rectangle)
  239. * @see #getMaximizedBounds
  240. * @serial
  241. * @since 1.4
  242. */
  243. Rectangle maximizedBounds;
  244. /**
  245. * This is the title of the frame. It can be changed
  246. * at any time. <code>title</code> can be null and if
  247. * this is the case the <code>title</code> = "".
  248. *
  249. * @serial
  250. * @see #getTitle
  251. * @see #setTitle(String)
  252. */
  253. String title = "Untitled";
  254. /**
  255. * <code>icon</code> is the graphical way we can
  256. * represent the frame.
  257. * <code>icon</code> can be null, but obviously if
  258. * you try to set the icon image <code>icon</code>
  259. * cannot be null.
  260. *
  261. * @serial
  262. * @see #getIconImage
  263. * @see #setIconImage(Image)
  264. */
  265. transient Image icon;
  266. /**
  267. * The frames menubar. If <code>menuBar</code> = null
  268. * the frame will not have a menubar.
  269. *
  270. * @serial
  271. * @see #getMenuBar
  272. * @see #setMenuBar(MenuBar)
  273. */
  274. MenuBar menuBar;
  275. /**
  276. * This field indicates whether the frame is resizable.
  277. * This property can be changed at any time.
  278. * <code>resizable</code> will be true if the frame is
  279. * resizable, otherwise it will be false.
  280. *
  281. * @serial
  282. * @see #isResizable()
  283. */
  284. boolean resizable = true;
  285. /**
  286. * This field indicates whether the frame is undecorated.
  287. * This property can only be changed while the frame is not displayable.
  288. * <code>undecorated</code> will be true if the frame is
  289. * undecorated, otherwise it will be false.
  290. *
  291. * @serial
  292. * @see #setUndecorated(boolean)
  293. * @see #isUndecorated()
  294. * @see Component#isDisplayable()
  295. * @since 1.4
  296. */
  297. boolean undecorated = false;
  298. /**
  299. * <code>mbManagement</code> is only used by the Motif implementation.
  300. *
  301. * @serial
  302. */
  303. boolean mbManagement = false; /* used only by the Motif impl. */
  304. // XXX: uwe: abuse old field for now
  305. // will need to take care of serialization
  306. private int state = NORMAL;
  307. /*
  308. * The Windows owned by the Frame.
  309. * Note: in 1.2 this has been superceded by Window.ownedWindowList
  310. *
  311. * @serial
  312. * @see java.awt.Window#ownedWindowList
  313. */
  314. Vector ownedWindows;
  315. /*
  316. * We insert a weak reference into the Vector of all Frames
  317. * instead of 'this' so that garbage collection can still take
  318. * place correctly.
  319. */
  320. transient private WeakReference weakThis;
  321. private static final String base = "frame";
  322. private static int nameCounter = 0;
  323. /*
  324. * JDK 1.1 serialVersionUID
  325. */
  326. private static final long serialVersionUID = 2673458971256075116L;
  327. static {
  328. /* ensure that the necessary native libraries are loaded */
  329. Toolkit.loadLibraries();
  330. if (!GraphicsEnvironment.isHeadless()) {
  331. initIDs();
  332. }
  333. }
  334. /**
  335. * Constructs a new instance of <code>Frame</code> that is
  336. * initially invisible. The title of the <code>Frame</code>
  337. * is empty.
  338. * @exception HeadlessException when GraphicsEnvironment.isHeadless()
  339. * returns true
  340. * @see java.awt.GraphicsEnvironment#isHeadless()
  341. * @see Component#setSize
  342. * @see Component#setVisible(boolean)
  343. */
  344. public Frame() throws HeadlessException {
  345. this("");
  346. }
  347. /**
  348. * Create a <code>Frame</code> with the specified
  349. * <code>GraphicsConfiguration</code> of
  350. * a screen device.
  351. * @param gc the <code>GraphicsConfiguration</code>
  352. * of the target screen device. If <code>gc</code>
  353. * is <code>null</code>, the system default
  354. * <code>GraphicsConfiguration</code> is assumed.
  355. * @exception IllegalArgumentException if
  356. * <code>gc</code> is not from a screen device.
  357. * This exception is always thrown
  358. * when GraphicsEnvironment.isHeadless() returns true
  359. * @see java.awt.GraphicsEnvironment#isHeadless()
  360. * @since 1.3
  361. */
  362. public Frame(GraphicsConfiguration gc) {
  363. this("", gc);
  364. }
  365. /**
  366. * Constructs a new, initially invisible <code>Frame</code> object
  367. * with the specified title.
  368. * @param title the title to be displayed in the frame's border.
  369. * A <code>null</code> value
  370. * is treated as an empty string, "".
  371. * @exception HeadlessException when GraphicsEnvironment.isHeadless()
  372. * returns true
  373. * @see java.awt.GraphicsEnvironment#isHeadless()
  374. * @see java.awt.Component#setSize
  375. * @see java.awt.Component#setVisible(boolean)
  376. * @see java.awt.GraphicsConfiguration#getBounds
  377. */
  378. public Frame(String title) throws HeadlessException {
  379. init(title, null);
  380. }
  381. /**
  382. * Constructs a new, initially invisible <code>Frame</code> object
  383. * with the specified title and a
  384. * <code>GraphicsConfiguration</code>.
  385. * @param title the title to be displayed in the frame's border.
  386. * A <code>null</code> value
  387. * is treated as an empty string, "".
  388. * @param gc the <code>GraphicsConfiguration</code>
  389. * of the target screen device. If <code>gc</code> is
  390. * <code>null</code>, the system default
  391. * <code>GraphicsConfiguration</code> is assumed.
  392. * @exception IllegalArgumentException if <code>gc</code>
  393. * is not from a screen device.
  394. * This exception is always thrown
  395. * when GraphicsEnvironment.isHeadless() returns true
  396. * @see java.awt.GraphicsEnvironment#isHeadless()
  397. * @see java.awt.Component#setSize
  398. * @see java.awt.Component#setVisible(boolean)
  399. * @see java.awt.GraphicsConfiguration#getBounds
  400. */
  401. public Frame(String title, GraphicsConfiguration gc) {
  402. super(gc);
  403. init(title, gc);
  404. }
  405. private void init(String title, GraphicsConfiguration gc) {
  406. this.title = title;
  407. weakThis = new WeakReference(this);
  408. addToFrameList();
  409. SunToolkit.checkAndSetPolicy(this, false);
  410. }
  411. /**
  412. * Disposes of the input methods and context, and removes
  413. * this Frame from the AppContext. Subclasses that override
  414. * this method should call super.finalize().
  415. */
  416. protected void finalize() throws Throwable {
  417. // We have to remove the (hard) reference to weakThis in the
  418. // AppContext's Frame list Vector, otherwise the WeakReference
  419. // instance that points to this Frame will never get garbage
  420. // collected.
  421. removeFromFrameList();
  422. super.finalize();
  423. }
  424. /**
  425. * Construct a name for this component. Called by getName() when the
  426. * name is null.
  427. */
  428. String constructComponentName() {
  429. synchronized (getClass()) {
  430. return base + nameCounter++;
  431. }
  432. }
  433. /**
  434. * Makes this Frame displayable by connecting it to
  435. * a native screen resource. Making a frame displayable will
  436. * cause any of its children to be made displayable.
  437. * This method is called internally by the toolkit and should
  438. * not be called directly by programs.
  439. * @see Component#isDisplayable
  440. * @see #removeNotify
  441. */
  442. public void addNotify() {
  443. synchronized (getTreeLock()) {
  444. if (peer == null) {
  445. peer = getToolkit().createFrame(this);
  446. }
  447. FramePeer p = (FramePeer)peer;
  448. MenuBar menuBar = this.menuBar;
  449. if (menuBar != null) {
  450. mbManagement = true;
  451. menuBar.addNotify();
  452. p.setMenuBar(menuBar);
  453. }
  454. p.setMaximizedBounds(maximizedBounds);
  455. super.addNotify();
  456. }
  457. }
  458. /**
  459. * Gets the title of the frame. The title is displayed in the
  460. * frame's border.
  461. * @return the title of this frame, or an empty string ("")
  462. * if this frame doesn't have a title.
  463. * @see #setTitle(String)
  464. */
  465. public String getTitle() {
  466. return title;
  467. }
  468. /**
  469. * Sets the title for this frame to the specified string.
  470. * @param title the title to be displayed in the frame's border.
  471. * A <code>null</code> value
  472. * is treated as an empty string, "".
  473. * @see #getTitle
  474. */
  475. public void setTitle(String title) {
  476. String oldTitle = this.title;
  477. if (title == null) {
  478. title = "";
  479. }
  480. synchronized(this) {
  481. this.title = title;
  482. FramePeer peer = (FramePeer)this.peer;
  483. if (peer != null) {
  484. peer.setTitle(title);
  485. }
  486. }
  487. firePropertyChange("title", oldTitle, title);
  488. }
  489. /**
  490. * Gets the image to be displayed in the minimized icon
  491. * for this frame.
  492. * @return the icon image for this frame, or <code>null</code>
  493. * if this frame doesn't have an icon image.
  494. * @see #setIconImage(Image)
  495. */
  496. public Image getIconImage() {
  497. return icon;
  498. }
  499. /**
  500. * Sets the image to be displayed in the minimized icon for this frame.
  501. * Not all platforms support the concept of minimizing a window.
  502. * @param image the icon image to be displayed.
  503. * If this parameter is <code>null</code> then the
  504. * icon image is set to the default image, which may vary
  505. * with platform.
  506. * @see #getIconImage
  507. */
  508. public synchronized void setIconImage(Image image) {
  509. this.icon = image;
  510. FramePeer peer = (FramePeer)this.peer;
  511. if (peer != null) {
  512. peer.setIconImage(image);
  513. }
  514. }
  515. /**
  516. * Gets the menu bar for this frame.
  517. * @return the menu bar for this frame, or <code>null</code>
  518. * if this frame doesn't have a menu bar.
  519. * @see #setMenuBar(MenuBar)
  520. */
  521. public MenuBar getMenuBar() {
  522. return menuBar;
  523. }
  524. /**
  525. * Sets the menu bar for this frame to the specified menu bar.
  526. * @param mb the menu bar being set.
  527. * If this parameter is <code>null</code> then any
  528. * existing menu bar on this frame is removed.
  529. * @see #getMenuBar
  530. */
  531. public void setMenuBar(MenuBar mb) {
  532. synchronized (getTreeLock()) {
  533. if (menuBar == mb) {
  534. return;
  535. }
  536. if ((mb != null) && (mb.parent != null)) {
  537. mb.parent.remove(mb);
  538. }
  539. if (menuBar != null) {
  540. remove(menuBar);
  541. }
  542. menuBar = mb;
  543. if (menuBar != null) {
  544. menuBar.parent = this;
  545. FramePeer peer = (FramePeer)this.peer;
  546. if (peer != null) {
  547. mbManagement = true;
  548. menuBar.addNotify();
  549. if (valid) {
  550. invalidate();
  551. }
  552. peer.setMenuBar(menuBar);
  553. }
  554. }
  555. }
  556. }
  557. /**
  558. * Indicates whether this frame is resizable by the user.
  559. * By default, all frames are initially resizable.
  560. * @return <code>true</code> if the user can resize this frame;
  561. * <code>false</code> otherwise.
  562. * @see java.awt.Frame#setResizable(boolean)
  563. */
  564. public boolean isResizable() {
  565. return resizable;
  566. }
  567. /**
  568. * Sets whether this frame is resizable by the user.
  569. * @param resizable <code>true</code> if this frame is resizable;
  570. * <code>false</code> otherwise.
  571. * @see java.awt.Frame#isResizable
  572. */
  573. public void setResizable(boolean resizable) {
  574. boolean oldResizable = this.resizable;
  575. boolean testvalid = false;
  576. synchronized (this) {
  577. this.resizable = resizable;
  578. FramePeer peer = (FramePeer)this.peer;
  579. if (peer != null) {
  580. peer.setResizable(resizable);
  581. testvalid = true;
  582. }
  583. }
  584. // On some platforms, changing the resizable state affects
  585. // the insets of the Frame. If we could, we'd call invalidate()
  586. // from the peer, but we need to guarantee that we're not holding
  587. // the Frame lock when we call invalidate().
  588. if (testvalid && valid) {
  589. invalidate();
  590. }
  591. firePropertyChange("resizable", oldResizable, resizable);
  592. }
  593. /**
  594. * Sets the state of this frame (obsolete).
  595. * <p>
  596. * In older versions of JDK a frame state could only be NORMAL or
  597. * ICONIFIED. Since JDK 1.4 set of supported frame states is
  598. * expanded and frame state is represented as a bitwise mask.
  599. * <p>
  600. * For compatibility with old programs this method still accepts
  601. * <code>Frame.NORMAL</code> and <code>Frame.ICONIFIED</code> but
  602. * it only changes the iconic state of the frame, other aspects of
  603. * frame state are not affected by this method.
  604. *
  605. * @param state either <code>Frame.NORMAL</code> or
  606. * <code>Frame.ICONIFIED</code>.
  607. * @see #getState
  608. * @see #setExtendedState(int)
  609. */
  610. public synchronized void setState(int state) {
  611. int current = getExtendedState();
  612. if (state == ICONIFIED && (current & ICONIFIED) == 0) {
  613. setExtendedState(current | ICONIFIED);
  614. }
  615. else if (state == NORMAL && (current & ICONIFIED) != 0) {
  616. setExtendedState(current & ~ICONIFIED);
  617. }
  618. }
  619. /**
  620. * Sets the state of this frame. The state is
  621. * represented as a bitwise mask.
  622. * <ul>
  623. * <li><code>NORMAL</code>
  624. * <br>Indicates that no state bits are set.
  625. * <li><code>ICONIFIED</code>
  626. * <li><code>MAXIMIZED_HORIZ</code>
  627. * <li><code>MAXIMIZED_VERT</code>
  628. * <li><code>MAXIMIZED_BOTH</code>
  629. * <br>Concatenates <code>MAXIMIZED_HORIZ</code>
  630. * and <code>MAXIMIZED_VERT</code>.
  631. * </ul>
  632. * <p>Note that if the state is not supported on a
  633. * given platform, nothing will happen. The application
  634. * may determine if a specific state is available via
  635. * the <code>java.awt.Toolkit#isFrameStateSupported(int state)</code>
  636. * method.
  637. *
  638. * @param state a bitwise mask of frame state constants
  639. * @see #getExtendedState
  640. * @see java.awt.Toolkit#isFrameStateSupported(int)
  641. * @since 1.4
  642. */
  643. public synchronized void setExtendedState(int state) {
  644. if ( !isFrameStateSupported( state ) ) {
  645. return;
  646. }
  647. this.state = state;
  648. FramePeer peer = (FramePeer)this.peer;
  649. if (peer != null) {
  650. peer.setState(state);
  651. }
  652. }
  653. private boolean isFrameStateSupported(int state) {
  654. if( !getToolkit().isFrameStateSupported( state ) ) {
  655. // * Toolkit.isFrameStateSupported returns always false
  656. // on compound state even if all parts are supported;
  657. // * if part of state is not supported, state is not supported;
  658. // * MAXIMIZED_BOTH is not a compound state.
  659. if( ((state & ICONIFIED) != 0) &&
  660. !getToolkit().isFrameStateSupported( ICONIFIED )) {
  661. return false;
  662. }else {
  663. state &= ~ICONIFIED;
  664. }
  665. return getToolkit().isFrameStateSupported( state );
  666. }
  667. return true;
  668. }
  669. /**
  670. * Gets the state of this frame (obsolete).
  671. * <p>
  672. * In older versions of JDK a frame state could only be NORMAL or
  673. * ICONIFIED. Since JDK 1.4 set of supported frame states is
  674. * expanded and frame state is represented as a bitwise mask.
  675. * <p>
  676. * For compatibility with old programs this method still returns
  677. * <code>Frame.NORMAL</code> and <code>Frame.ICONIFIED</code> but
  678. * it only reports the iconic state of the frame, other aspects of
  679. * frame state are not reported by this method.
  680. *
  681. * @return <code>Frame.NORMAL</code> or <code>Frame.ICONIFIED</code>.
  682. * @see #setState(int)
  683. * @see #getExtendedState
  684. */
  685. public synchronized int getState() {
  686. return (getExtendedState() & ICONIFIED) != 0 ? ICONIFIED : NORMAL;
  687. }
  688. /**
  689. * Gets the state of this frame. The state is
  690. * represented as a bitwise mask.
  691. * <ul>
  692. * <li><code>NORMAL</code>
  693. * <br>Indicates that no state bits are set.
  694. * <li><code>ICONIFIED</code>
  695. * <li><code>MAXIMIZED_HORIZ</code>
  696. * <li><code>MAXIMIZED_VERT</code>
  697. * <li><code>MAXIMIZED_BOTH</code>
  698. * <br>Concatenates <code>MAXIMIZED_HORIZ</code>
  699. * and <code>MAXIMIZED_VERT</code>.
  700. * </ul>
  701. *
  702. * @return a bitwise mask of frame state constants
  703. * @see #setExtendedState(int)
  704. * @since 1.4
  705. */
  706. public synchronized int getExtendedState() {
  707. FramePeer peer = (FramePeer)this.peer;
  708. if (peer != null) {
  709. state = peer.getState();
  710. }
  711. return state;
  712. }
  713. /**
  714. * Sets the maximized bounds for this frame.
  715. * <p>
  716. * When a frame is in maximized state the system supplies some
  717. * defaults bounds. This method allows some or all of those
  718. * system supplied values to be overridden.
  719. * <p>
  720. * If <code>bounds</code> is <code>null</code>, accept bounds
  721. * supplied by the system. If non-<code>null</code> you can
  722. * override some of the system supplied values while accepting
  723. * others by setting those fields you want to accept from system
  724. * to <code>Integer.MAX_VALUE</code>.
  725. * <p>
  726. * On some systems only the size portion of the bounds is taken
  727. * into account.
  728. *
  729. * @param bounds bounds for the maximized state
  730. * @see #getMaximizedBounds()
  731. * @since 1.4
  732. */
  733. public synchronized void setMaximizedBounds(Rectangle bounds) {
  734. this.maximizedBounds = bounds;
  735. FramePeer peer = (FramePeer)this.peer;
  736. if (peer != null) {
  737. peer.setMaximizedBounds(bounds);
  738. }
  739. }
  740. /**
  741. * Gets maximized bounds for this frame.
  742. * Some fields may contain <code>Integer.MAX_VALUE</code> to indicate
  743. * that system supplied values for this field must be used.
  744. *
  745. * @return maximized bounds for this frame; may be <code>null</code>
  746. * @see #setMaximizedBounds(Rectangle)
  747. * @since 1.4
  748. */
  749. public Rectangle getMaximizedBounds() {
  750. return maximizedBounds;
  751. }
  752. /**
  753. * Disables or enables decorations for this frame.
  754. * This method can only be called while the frame is not displayable.
  755. * @param undecorated <code>true</code> if no frame decorations are
  756. * to be enabled;
  757. * <code>false</code> if frame decorations are to be enabled.
  758. * @throws <code>IllegalComponentStateException</code> if the frame
  759. * is displayable.
  760. * @see #isUndecorated
  761. * @see Component#isDisplayable
  762. * @see javax.swing.JFrame#setDefaultLookAndFeelDecorated(boolean)
  763. * @since 1.4
  764. */
  765. public void setUndecorated(boolean undecorated) {
  766. /* Make sure we don't run in the middle of peer creation.*/
  767. synchronized (getTreeLock()) {
  768. if (isDisplayable()) {
  769. throw new IllegalComponentStateException("The frame is displayable.");
  770. }
  771. this.undecorated = undecorated;
  772. }
  773. }
  774. /**
  775. * Indicates whether this frame is undecorated.
  776. * By default, all frames are initially decorated.
  777. * @return <code>true</code> if frame is undecorated;
  778. * <code>false</code> otherwise.
  779. * @see java.awt.Frame#setUndecorated(boolean)
  780. * @since 1.4
  781. */
  782. public boolean isUndecorated() {
  783. return undecorated;
  784. }
  785. /**
  786. * Removes the specified menu bar from this frame.
  787. * @param m the menu component to remove.
  788. * If <code>m</code> is <code>null</code>, then
  789. * no action is taken
  790. */
  791. public void remove(MenuComponent m) {
  792. synchronized (getTreeLock()) {
  793. if (m == menuBar) {
  794. menuBar = null;
  795. FramePeer peer = (FramePeer)this.peer;
  796. if (peer != null) {
  797. mbManagement = true;
  798. if (valid) {
  799. invalidate();
  800. }
  801. peer.setMenuBar(null);
  802. m.removeNotify();
  803. }
  804. m.parent = null;
  805. } else {
  806. super.remove(m);
  807. }
  808. }
  809. }
  810. /**
  811. * Makes this Frame undisplayable by removing its connection
  812. * to its native screen resource. Making a Frame undisplayable
  813. * will cause any of its children to be made undisplayable.
  814. * This method is called by the toolkit internally and should
  815. * not be called directly by programs.
  816. * @see Component#isDisplayable
  817. * @see #addNotify
  818. */
  819. public void removeNotify() {
  820. synchronized (getTreeLock()) {
  821. FramePeer peer = (FramePeer)this.peer;
  822. if (peer != null) {
  823. // get the latest Frame state before disposing
  824. getState();
  825. if (menuBar != null) {
  826. mbManagement = true;
  827. peer.setMenuBar(null);
  828. menuBar.removeNotify();
  829. }
  830. }
  831. super.removeNotify();
  832. }
  833. }
  834. void postProcessKeyEvent(KeyEvent e) {
  835. if (menuBar != null && menuBar.handleShortcut(e)) {
  836. e.consume();
  837. return;
  838. }
  839. super.postProcessKeyEvent(e);
  840. }
  841. /**
  842. * Returns a string representing the state of this <code>Frame</code>.
  843. * This method is intended to be used only for debugging purposes, and the
  844. * content and format of the returned string may vary between
  845. * implementations. The returned string may be empty but may not be
  846. * <code>null</code>.
  847. *
  848. * @return the parameter string of this frame
  849. */
  850. protected String paramString() {
  851. String str = super.paramString();
  852. if (title != null) {
  853. str += ",title=" + title;
  854. }
  855. if (resizable) {
  856. str += ",resizable";
  857. }
  858. getExtendedState(); // sync with peer
  859. if (state == NORMAL) {
  860. str += ",normal";
  861. }
  862. else {
  863. if ((state & ICONIFIED) != 0) {
  864. str += ",iconified";
  865. }
  866. if ((state & MAXIMIZED_BOTH) == MAXIMIZED_BOTH) {
  867. str += ",maximized";
  868. }
  869. else if ((state & MAXIMIZED_HORIZ) != 0) {
  870. str += ",maximized_horiz";
  871. }
  872. else if ((state & MAXIMIZED_VERT) != 0) {
  873. str += ",maximized_vert";
  874. }
  875. }
  876. return str;
  877. }
  878. /**
  879. * @deprecated As of JDK version 1.1,
  880. * replaced by <code>Component.setCursor(Cursor)</code>.
  881. */
  882. @Deprecated
  883. public void setCursor(int cursorType) {
  884. if (cursorType < DEFAULT_CURSOR || cursorType > MOVE_CURSOR) {
  885. throw new IllegalArgumentException("illegal cursor type");
  886. }
  887. setCursor(Cursor.getPredefinedCursor(cursorType));
  888. }
  889. /**
  890. * @deprecated As of JDK version 1.1,
  891. * replaced by <code>Component.getCursor()</code>.
  892. */
  893. @Deprecated
  894. public int getCursorType() {
  895. return (getCursor().getType());
  896. }
  897. /**
  898. * Returns an array containing all Frames created by the application.
  899. * If called from an applet, the array will only include the Frames
  900. * accessible by that applet.
  901. * @since 1.2
  902. */
  903. public static Frame[] getFrames() {
  904. synchronized (Frame.class) {
  905. Frame realCopy[];
  906. Vector frameList =
  907. (Vector)AppContext.getAppContext().get(Frame.class);
  908. if (frameList != null) {
  909. // Recall that frameList is actually a Vector of WeakReferences
  910. // and calling get() on one of these references may return
  911. // null. Make two arrays-- one the size of the Vector
  912. // (fullCopy with size fullSize), and one the size of all
  913. // non-null get()s (realCopy with size realSize).
  914. int fullSize = frameList.size();
  915. int realSize = 0;
  916. Frame fullCopy[] = new Frame[fullSize];
  917. for (int i = 0; i < fullSize; i++) {
  918. fullCopy[realSize] = (Frame)
  919. (((WeakReference) (frameList.elementAt(i))).get());
  920. if (fullCopy[realSize] != null) {
  921. realSize++;
  922. }
  923. }
  924. if (fullSize != realSize) {
  925. realCopy = new Frame[realSize];
  926. System.arraycopy(fullCopy, 0, realCopy, 0, realSize);
  927. } else {
  928. realCopy = fullCopy;
  929. }
  930. } else {
  931. realCopy = new Frame[0];
  932. }
  933. return realCopy;
  934. }
  935. }
  936. void addToFrameList() {
  937. synchronized (Frame.class) {
  938. Vector frameList = (Vector)appContext.get(Frame.class);
  939. if (frameList == null) {
  940. frameList = new Vector();
  941. appContext.put(Frame.class, frameList);
  942. }
  943. frameList.addElement(weakThis);
  944. }
  945. }
  946. void removeFromFrameList() {
  947. synchronized (Frame.class) {
  948. Vector frameList = (Vector)appContext.get(Frame.class);
  949. if (frameList != null) {
  950. frameList.removeElement(weakThis);
  951. }
  952. }
  953. }
  954. /* Serialization support. If there's a MenuBar we restore
  955. * its (transient) parent field here. Likewise for top level
  956. * windows that are "owned" by this frame.
  957. */
  958. /**
  959. * <code>Frame</code>'s Serialized Data Version.
  960. *
  961. * @serial
  962. */
  963. private int frameSerializedDataVersion = 1;
  964. /**
  965. * Writes default serializable fields to stream. Writes
  966. * an optional serializable <code>Icon</code>, which is
  967. * available as of 1.4.
  968. *
  969. * @param s the <code>ObjectOutputStream</code> to write
  970. * @serialData an optional <code>Icon</code>
  971. * @see javax.swing.Icon
  972. * @see #readObject(ObjectInputStream)
  973. */
  974. private void writeObject(ObjectOutputStream s)
  975. throws IOException
  976. {
  977. s.defaultWriteObject();
  978. if (icon instanceof Serializable) {
  979. s.writeObject(icon);
  980. }
  981. else
  982. {
  983. s.writeObject(null);
  984. }
  985. }
  986. /**
  987. * Reads the <code>ObjectInputStream</code>. Tries
  988. * to read an <code>Icon</code>, which is optional
  989. * data available as of 1.4. If an <code>Icon</code>
  990. * is not available, but anything other than an EOF
  991. * is detected, an <code>OptionalDataException</code>
  992. * will be thrown..
  993. * Unrecognized keys or values will be ignored.
  994. *
  995. * @param s the <code>ObjectInputStream</code> to read
  996. * @exception OptionalDataException if an <code>Icon</code>
  997. * is not available, but anything other than an EOF
  998. * is detected
  999. * @exception HeadlessException if
  1000. * <code>GraphicsEnvironment.isHeadless</code> returns
  1001. * <code>true</code>
  1002. * @see java.awt.GraphicsEnvironment#isHeadless()
  1003. * @see javax.swing.Icon
  1004. * @see #writeObject(ObjectOutputStream)
  1005. */
  1006. private void readObject(ObjectInputStream s)
  1007. throws ClassNotFoundException, IOException, HeadlessException
  1008. {
  1009. // HeadlessException is thrown by Window's readObject
  1010. s.defaultReadObject();
  1011. try {
  1012. icon = (Image) s.readObject();
  1013. } catch (java.io.OptionalDataException e) {
  1014. // pre-1.4 instances will not have this optional data.
  1015. // e.eof will be true to indicate that there is no more
  1016. // data available for this object.
  1017. // If e.eof is not true, throw the exception as it
  1018. // might have been caused by unrelated reasons.
  1019. if (!e.eof) {
  1020. throw (e);
  1021. }
  1022. }
  1023. if (menuBar != null)
  1024. menuBar.parent = this;
  1025. // Ensure 1.1 serialized Frames can read & hook-up
  1026. // owned windows properly
  1027. //
  1028. if (ownedWindows != null) {
  1029. for (int i = 0; i < ownedWindows.size(); i++) {
  1030. connectOwnedWindow((Window) ownedWindows.elementAt(i));
  1031. }
  1032. ownedWindows = null;
  1033. }
  1034. weakThis = new WeakReference(this);
  1035. addToFrameList();
  1036. }
  1037. /**
  1038. * Initialize JNI field and method IDs
  1039. */
  1040. private static native void initIDs();
  1041. /*
  1042. * --- Accessibility Support ---
  1043. *
  1044. */
  1045. /**
  1046. * Gets the AccessibleContext associated with this Frame.
  1047. * For frames, the AccessibleContext takes the form of an
  1048. * AccessibleAWTFrame.
  1049. * A new AccessibleAWTFrame instance is created if necessary.
  1050. *
  1051. * @return an AccessibleAWTFrame that serves as the
  1052. * AccessibleContext of this Frame
  1053. */
  1054. public AccessibleContext getAccessibleContext() {
  1055. if (accessibleContext == null) {
  1056. accessibleContext = new AccessibleAWTFrame();
  1057. }
  1058. return accessibleContext;
  1059. }
  1060. /**
  1061. * This class implements accessibility support for the
  1062. * <code>Frame</code> class. It provides an implementation of the
  1063. * Java Accessibility API appropriate to frame user-interface elements.
  1064. */
  1065. protected class AccessibleAWTFrame extends AccessibleAWTWindow
  1066. {
  1067. /*
  1068. * JDK 1.3 serialVersionUID
  1069. */
  1070. private static final long serialVersionUID = -6172960752956030250L;
  1071. /**
  1072. * Get the role of this object.
  1073. *
  1074. * @return an instance of AccessibleRole describing the role of the
  1075. * object
  1076. * @see AccessibleRole
  1077. */
  1078. public AccessibleRole getAccessibleRole() {
  1079. return AccessibleRole.FRAME;
  1080. }
  1081. /**
  1082. * Get the state of this object.
  1083. *
  1084. * @return an instance of AccessibleStateSet containing the current
  1085. * state set of the object
  1086. * @see AccessibleState
  1087. */
  1088. public AccessibleStateSet getAccessibleStateSet() {
  1089. AccessibleStateSet states = super.getAccessibleStateSet();
  1090. if (getFocusOwner() != null) {
  1091. states.add(AccessibleState.ACTIVE);
  1092. }
  1093. if (isResizable()) {
  1094. states.add(AccessibleState.RESIZABLE);
  1095. }
  1096. return states;
  1097. }
  1098. } // inner class AccessibleAWTFrame
  1099. }