1. /*
  2. * @(#)JInternalFrame.java 1.139 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.beans.PropertyVetoException;
  11. import java.beans.PropertyChangeEvent;
  12. import java.util.EventListener;
  13. import javax.swing.border.Border;
  14. import javax.swing.event.InternalFrameEvent;
  15. import javax.swing.event.InternalFrameListener;
  16. import javax.swing.plaf.*;
  17. import javax.accessibility.*;
  18. import java.io.ObjectOutputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.IOException;
  21. /**
  22. * A lightweight object that provides many of the features of
  23. * a native frame, including dragging, closing, becoming an icon,
  24. * resizing, title display, and support for a menu bar.
  25. * For task-oriented documentation and examples of using internal frames,
  26. * see <a
  27. href="http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html" target="_top">How to Use Internal Frames</a>,
  28. * a section in <em>The Java Tutorial</em>.
  29. *
  30. * <p>
  31. *
  32. * Generally,
  33. * you add <code>JInternalFrame</code>s to a <code>JDesktopPane</code>. The UI
  34. * delegates the look-and-feel-specific actions to the
  35. * <code>DesktopManager</code>
  36. * object maintained by the <code>JDesktopPane</code>.
  37. * <p>
  38. * The <code>JInternalFrame</code> content pane
  39. * is where you add child components.
  40. * So, to create a <code>JInternalFrame</code> that has a number of
  41. * buttons arranged
  42. * with the content pane's default <code>BorderLayout</code> object,
  43. * you might do something like this:
  44. * <pre>
  45. * JComponent c = (JComponent) internalFrame.getContentPane();
  46. * c.add(new JButton(), BorderLayout.NORTH);
  47. * c.add(new JButton(), BorderLayout.CENTER);
  48. * </pre>
  49. * The content pane is actually managed by an instance of
  50. * <code>JRootPane</code>,
  51. * which also manages a layout pane, glass pane, and
  52. * optional menu bar for the internal frame. Please see the
  53. * <code>JRootPane</code>
  54. * documentation for a complete description of these components.
  55. * <p>
  56. * For the keyboard keys used by this component in the standard look and
  57. * feel renditions, see the
  58. * <a href="doc-files/Key-Index.html#JInternalFrame"><code>JInternalFrame</code> key assignments</a>.
  59. * <p>
  60. * <strong>Warning:</strong>
  61. * Serialized objects of this class will not be compatible with
  62. * future Swing releases. The current serialization support is
  63. * appropriate for short term storage or RMI between applications running
  64. * the same version of Swing. As of 1.4, support for long term storage
  65. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  66. * has been added to the <code>java.beans</code> package.
  67. * Please see {@link java.beans.XMLEncoder}.
  68. *
  69. * @see InternalFrameEvent
  70. * @see JDesktopPane
  71. * @see DesktopManager
  72. * @see JInternalFrame.JDesktopIcon
  73. * @see JRootPane
  74. *
  75. * @version 1.139 01/23/03
  76. * @author David Kloba
  77. * @author Rich Schiavi
  78. * @beaninfo
  79. * attribute: isContainer true
  80. * attribute: containerDelegate getContentPane
  81. * description: A frame container which is contained within
  82. * another window.
  83. */
  84. public class JInternalFrame extends JComponent implements
  85. Accessible, WindowConstants,
  86. RootPaneContainer
  87. {
  88. /**
  89. * @see #getUIClassID
  90. * @see #readObject
  91. */
  92. private static final String uiClassID = "InternalFrameUI";
  93. /**
  94. * The <code>JRootPane</code> instance that manages the
  95. * content pane
  96. * and optional menu bar for this internal frame, as well as the
  97. * glass pane.
  98. *
  99. * @see JRootPane
  100. * @see RootPaneContainer
  101. */
  102. protected JRootPane rootPane;
  103. /**
  104. * If <code>true</code> then calls to <code>add</code> and <code>setLayout</code>
  105. * cause an exception to be thrown.
  106. */
  107. protected boolean rootPaneCheckingEnabled = false;
  108. /** The frame can be closed. */
  109. protected boolean closable;
  110. /** The frame has been closed. */
  111. protected boolean isClosed;
  112. /** The frame can be expanded to the size of the desktop pane. */
  113. protected boolean maximizable;
  114. /**
  115. * The frame has been expanded to its maximum size.
  116. * @see #maximizable
  117. */
  118. protected boolean isMaximum;
  119. /**
  120. * The frame can "iconified" (shrunk down and displayed as
  121. * an icon-image).
  122. * @see JInternalFrame.JDesktopIcon
  123. * @see #setIconifiable
  124. */
  125. protected boolean iconable;
  126. /**
  127. * The frame has been iconified.
  128. * @see #isIcon()
  129. */
  130. protected boolean isIcon;
  131. /** The frame's size can be changed. */
  132. protected boolean resizable;
  133. /** The frame is currently selected. */
  134. protected boolean isSelected;
  135. /** The icon shown in the top-left corner of this internal frame. */
  136. protected Icon frameIcon;
  137. /** The title displayed in this internal frame's title bar. */
  138. protected String title;
  139. /**
  140. * The icon that is displayed when this internal frame is iconified.
  141. * @see #iconable
  142. */
  143. protected JDesktopIcon desktopIcon;
  144. private boolean opened;
  145. private Rectangle normalBounds = null;
  146. private int defaultCloseOperation = DISPOSE_ON_CLOSE;
  147. /**
  148. * Contains the Component that focus is to go when
  149. * <code>restoreSubcomponentFocus</code> is invoked, that is,
  150. * <code>restoreSubcomponentFocus</code> sets this to the value returned
  151. * from <code>getMostRecentFocusOwner</code>.
  152. */
  153. private Component lastFocusOwner;
  154. /** Bound property name. */
  155. public final static String CONTENT_PANE_PROPERTY = "contentPane";
  156. /** Bound property name. */
  157. public final static String MENU_BAR_PROPERTY = "JMenuBar";
  158. /** Bound property name. */
  159. public final static String TITLE_PROPERTY = "title";
  160. /** Bound property name. */
  161. public final static String LAYERED_PANE_PROPERTY = "layeredPane";
  162. /** Bound property name. */
  163. public final static String ROOT_PANE_PROPERTY = "rootPane";
  164. /** Bound property name. */
  165. public final static String GLASS_PANE_PROPERTY = "glassPane";
  166. /** Bound property name. */
  167. public final static String FRAME_ICON_PROPERTY = "frameIcon";
  168. /**
  169. * Constrained property name indicated that this frame has
  170. * selected status.
  171. */
  172. public final static String IS_SELECTED_PROPERTY = "selected";
  173. /** Constrained property name indicating that the internal frame is closed. */
  174. public final static String IS_CLOSED_PROPERTY = "closed";
  175. /** Constrained property name indicating that the internal frame is maximized. */
  176. public final static String IS_MAXIMUM_PROPERTY = "maximum";
  177. /** Constrained property name indicating that the internal frame is iconified. */
  178. public final static String IS_ICON_PROPERTY = "icon";
  179. /**
  180. * Creates a non-resizable, non-closable, non-maximizable,
  181. * non-iconifiable <code>JInternalFrame</code> with no title.
  182. */
  183. public JInternalFrame() {
  184. this("", false, false, false, false);
  185. }
  186. /**
  187. * Creates a non-resizable, non-closable, non-maximizable,
  188. * non-iconifiable <code>JInternalFrame</code> with the specified title.
  189. * Note that passing in a <code>null</code> <code>title</code> results in
  190. * unspecified behavior and possibly an exception.
  191. *
  192. * @param title the non-<code>null</code> <code>String</code>
  193. * to display in the title bar
  194. */
  195. public JInternalFrame(String title) {
  196. this(title, false, false, false, false);
  197. }
  198. /**
  199. * Creates a non-closable, non-maximizable, non-iconifiable
  200. * <code>JInternalFrame</code> with the specified title
  201. * and resizability.
  202. *
  203. * @param title the <code>String</code> to display in the title bar
  204. * @param resizable if <code>true</code>, the internal frame can be resized
  205. */
  206. public JInternalFrame(String title, boolean resizable) {
  207. this(title, resizable, false, false, false);
  208. }
  209. /**
  210. * Creates a non-maximizable, non-iconifiable <code>JInternalFrame</code>
  211. * with the specified title, resizability, and
  212. * closability.
  213. *
  214. * @param title the <code>String</code> to display in the title bar
  215. * @param resizable if <code>true</code>, the internal frame can be resized
  216. * @param closable if <code>true</code>, the internal frame can be closed
  217. */
  218. public JInternalFrame(String title, boolean resizable, boolean closable) {
  219. this(title, resizable, closable, false, false);
  220. }
  221. /**
  222. * Creates a non-iconifiable <code>JInternalFrame</code>
  223. * with the specified title,
  224. * resizability, closability, and maximizability.
  225. *
  226. * @param title the <code>String</code> to display in the title bar
  227. * @param resizable if <code>true</code>, the internal frame can be resized
  228. * @param closable if <code>true</code>, the internal frame can be closed
  229. * @param maximizable if <code>true</code>, the internal frame can be maximized
  230. */
  231. public JInternalFrame(String title, boolean resizable, boolean closable,
  232. boolean maximizable) {
  233. this(title, resizable, closable, maximizable, false);
  234. }
  235. /**
  236. * Creates a <code>JInternalFrame</code> with the specified title,
  237. * resizability, closability, maximizability, and iconifiability.
  238. * All <code>JInternalFrame</code> constructors use this one.
  239. *
  240. * @param title the <code>String</code> to display in the title bar
  241. * @param resizable if <code>true</code>, the internal frame can be resized
  242. * @param closable if <code>true</code>, the internal frame can be closed
  243. * @param maximizable if <code>true</code>, the internal frame can be maximized
  244. * @param iconifiable if <code>true</code>, the internal frame can be iconified
  245. */
  246. public JInternalFrame(String title, boolean resizable, boolean closable,
  247. boolean maximizable, boolean iconifiable) {
  248. setRootPane(createRootPane());
  249. getGlassPane().setVisible(true);
  250. setLayout(new BorderLayout());
  251. this.title = title;
  252. this.resizable = resizable;
  253. this.closable = closable;
  254. this.maximizable = maximizable;
  255. isMaximum = false;
  256. this.iconable = iconifiable;
  257. isIcon = false;
  258. setVisible(false);
  259. setRootPaneCheckingEnabled(true);
  260. desktopIcon = new JDesktopIcon(this);
  261. updateUI();
  262. }
  263. /**
  264. * Called by the constructor to set up the <code>JRootPane</code>.
  265. * @return a new <code>JRootPane</code>
  266. * @see JRootPane
  267. */
  268. protected JRootPane createRootPane() {
  269. return new JRootPane();
  270. }
  271. /**
  272. * Returns the look-and-feel object that renders this component.
  273. *
  274. * @return the <code>InternalFrameUI</code> object that renders
  275. * this component
  276. */
  277. public InternalFrameUI getUI() {
  278. return (InternalFrameUI)ui;
  279. }
  280. /**
  281. * Sets the UI delegate for this <code>JInternalFrame</code>.
  282. * @param ui the UI delegate
  283. * @beaninfo
  284. * bound: true
  285. * hidden: true
  286. * attribute: visualUpdate true
  287. * description: The UI object that implements the Component's LookAndFeel.
  288. */
  289. public void setUI(InternalFrameUI ui) {
  290. boolean checkingEnabled = isRootPaneCheckingEnabled();
  291. try {
  292. setRootPaneCheckingEnabled(false);
  293. super.setUI(ui);
  294. }
  295. finally {
  296. setRootPaneCheckingEnabled(checkingEnabled);
  297. }
  298. }
  299. /**
  300. * Notification from the <code>UIManager</code> that the look and feel
  301. * has changed.
  302. * Replaces the current UI object with the latest version from the
  303. * <code>UIManager</code>.
  304. *
  305. * @see JComponent#updateUI
  306. */
  307. public void updateUI() {
  308. setUI((InternalFrameUI)UIManager.getUI(this));
  309. invalidate();
  310. if (desktopIcon != null) {
  311. desktopIcon.updateUIWhenHidden();
  312. }
  313. }
  314. /* This method is called if <code>updateUI</code> was called
  315. * on the associated
  316. * JDesktopIcon. It's necessary to avoid infinite recursion.
  317. */
  318. void updateUIWhenHidden() {
  319. setUI((InternalFrameUI)UIManager.getUI(this));
  320. invalidate();
  321. Component[] children = getComponents();
  322. if (children != null) {
  323. for(int i = 0; i < children.length; i++) {
  324. SwingUtilities.updateComponentTreeUI(children[i]);
  325. }
  326. }
  327. }
  328. /**
  329. * Returns the name of the look-and-feel
  330. * class that renders this component.
  331. *
  332. * @return the string "InternalFrameUI"
  333. *
  334. * @see JComponent#getUIClassID
  335. * @see UIDefaults#getUI
  336. *
  337. * @beaninfo
  338. * description: UIClassID
  339. */
  340. public String getUIClassID() {
  341. return uiClassID;
  342. }
  343. /**
  344. * Returns whether calls to <code>add</code> and
  345. * <code>setLayout</code> cause an exception to be thrown.
  346. *
  347. * @return <code>true</code> if <code>add</code> and <code>setLayout</code>
  348. * are checked
  349. * @see #addImpl
  350. * @see #setLayout
  351. * @see #setRootPaneCheckingEnabled
  352. */
  353. protected boolean isRootPaneCheckingEnabled() {
  354. return rootPaneCheckingEnabled;
  355. }
  356. /**
  357. * Determines whether calls to <code>add</code> and
  358. * <code>setLayout</code> cause an exception to be thrown.
  359. *
  360. * @param enabled a boolean value, <code>true</code> if checking is to be
  361. * enabled, which cause the exceptions to be thrown
  362. *
  363. * @see #addImpl
  364. * @see #setLayout
  365. * @see #isRootPaneCheckingEnabled
  366. */
  367. protected void setRootPaneCheckingEnabled(boolean enabled) {
  368. rootPaneCheckingEnabled = enabled;
  369. }
  370. /**
  371. * Creates and returns a runtime exception with a message like:
  372. * <pre>
  373. * "Do not use JFrame.add() use JFrame.getContentPane().add() instead"
  374. * </pre>
  375. *
  376. * @param op a <code>String</code> indicating the attempted operation;
  377. * in the example above, the operation string is "add"
  378. */
  379. private Error createRootPaneException(String op) {
  380. String type = getClass().getName();
  381. return new Error(
  382. "Do not use " + type + "." + op + "() use "
  383. + type + ".getContentPane()." + op + "() instead");
  384. }
  385. /**
  386. * Ensures that, by default, children cannot be added
  387. * directly to this component.
  388. * Instead, children must be added to its content pane.
  389. * For example:
  390. * <pre>
  391. *thisComponent.getContentPane().add(child)
  392. * </pre>
  393. * An attempt to add to directly to this component will cause a
  394. * runtime exception to be thrown. Subclasses can disable this
  395. * behavior.
  396. *
  397. * @param comp the <code>Component</code> to be added
  398. * @param constraints the object containing the constraints, if any
  399. * @param index the index
  400. * @see #setRootPaneCheckingEnabled
  401. * @exception Error if called with <code>isRootPaneChecking</code> <code>true</code>
  402. */
  403. protected void addImpl(Component comp, Object constraints, int index)
  404. {
  405. if(isRootPaneCheckingEnabled()) {
  406. throw createRootPaneException("add");
  407. }
  408. else {
  409. super.addImpl(comp, constraints, index);
  410. }
  411. }
  412. /**
  413. * Removes the specified component from this container.
  414. * @param comp the component to be removed
  415. * @see #add
  416. */
  417. public void remove(Component comp) {
  418. int oldCount = getComponentCount();
  419. super.remove(comp);
  420. if (oldCount == getComponentCount()) {
  421. // Client mistake, but we need to handle it to avoid a
  422. // common object leak in client applications.
  423. getContentPane().remove(comp);
  424. }
  425. }
  426. /**
  427. * Ensures that, by default, the layout of this component cannot be set.
  428. * Instead, the layout of its content pane should be set.
  429. * For example:
  430. * <pre>
  431. * thisComponent.getContentPane().setLayout(new GridLayout(1,2))
  432. * </pre>
  433. * An attempt to set the layout of this component will cause an
  434. * runtime exception to be thrown. Subclasses can disable this
  435. * behavior.
  436. *
  437. * @param manager the <code>LayoutManager</code>
  438. * @see #setRootPaneCheckingEnabled
  439. * @exception Error if called with <code>isRootPaneChecking</code> <code>true</code>
  440. */
  441. public void setLayout(LayoutManager manager) {
  442. if(isRootPaneCheckingEnabled()) {
  443. throw createRootPaneException("setLayout");
  444. }
  445. else {
  446. super.setLayout(manager);
  447. }
  448. }
  449. //////////////////////////////////////////////////////////////////////////
  450. /// Property Methods
  451. //////////////////////////////////////////////////////////////////////////
  452. /**
  453. * Returns the current <code>JMenuBar</code> for this
  454. * <code>JInternalFrame</code>, or <code>null</code>
  455. * if no menu bar has been set.
  456. * @return the current menu bar, or <code>null</code> if none has been set
  457. *
  458. * @deprecated As of Swing version 1.0.3,
  459. * replaced by <code>getJMenuBar()</code>.
  460. */
  461. public JMenuBar getMenuBar() {
  462. return getRootPane().getMenuBar();
  463. }
  464. /**
  465. * Returns the current <code>JMenuBar</code> for this
  466. * <code>JInternalFrame</code>, or <code>null</code>
  467. * if no menu bar has been set.
  468. *
  469. * @return the <code>JMenuBar</code> used by this internal frame
  470. * @see #setJMenuBar
  471. */
  472. public JMenuBar getJMenuBar() {
  473. return getRootPane().getJMenuBar();
  474. }
  475. /**
  476. * Sets the <code>menuBar</code> property for this <code>JInternalFrame</code>.
  477. *
  478. * @param m the <code>JMenuBar</code> to use in this internal frame
  479. * @see #getJMenuBar
  480. * @deprecated As of Swing version 1.0.3
  481. * replaced by <code>setJMenuBar(JMenuBar m)</code>.
  482. */
  483. public void setMenuBar(JMenuBar m) {
  484. JMenuBar oldValue = getMenuBar();
  485. getRootPane().setJMenuBar(m);
  486. firePropertyChange(MENU_BAR_PROPERTY, oldValue, m);
  487. }
  488. /**
  489. * Sets the <code>menuBar</code> property for this <code>JInternalFrame</code>.
  490. *
  491. * @param m the <code>JMenuBar</code> to use in this internal frame
  492. * @see #getJMenuBar
  493. * @beaninfo
  494. * bound: true
  495. * preferred: true
  496. * description: The menu bar for accessing pulldown menus
  497. * from this internal frame.
  498. */
  499. public void setJMenuBar(JMenuBar m){
  500. JMenuBar oldValue = getMenuBar();
  501. getRootPane().setJMenuBar(m);
  502. firePropertyChange(MENU_BAR_PROPERTY, oldValue, m);
  503. }
  504. // implements javax.swing.RootPaneContainer
  505. /**
  506. * Returns the content pane for this internal frame.
  507. * @return the content pane
  508. */
  509. public Container getContentPane() {
  510. return getRootPane().getContentPane();
  511. }
  512. /**
  513. * Sets this <code>JInternalFrame</code>'s <code>contentPane</code>
  514. * property.
  515. *
  516. * @param c the content pane for this internal frame
  517. *
  518. * @exception java.awt.IllegalComponentStateException (a runtime
  519. * exception) if the content pane parameter is <code>null</code>
  520. * @see RootPaneContainer#getContentPane
  521. * @beaninfo
  522. * bound: true
  523. * hidden: true
  524. * description: The client area of the internal frame where child
  525. * components are normally inserted.
  526. */
  527. public void setContentPane(Container c) {
  528. Container oldValue = getContentPane();
  529. getRootPane().setContentPane(c);
  530. firePropertyChange(CONTENT_PANE_PROPERTY, oldValue, c);
  531. }
  532. /**
  533. * Returns the layered pane for this internal frame.
  534. *
  535. * @return a <code>JLayeredPane</code> object
  536. * @see RootPaneContainer#setLayeredPane
  537. * @see RootPaneContainer#getLayeredPane
  538. */
  539. public JLayeredPane getLayeredPane() {
  540. return getRootPane().getLayeredPane();
  541. }
  542. /**
  543. * Sets this <code>JInternalFrame</code>'s
  544. * <code>layeredPane</code> property.
  545. *
  546. * @param layered the <code>JLayeredPane</code> for this internal frame
  547. *
  548. * @exception java.awt.IllegalComponentStateException (a runtime
  549. * exception) if the layered pane parameter is <code>null</code>
  550. * @see RootPaneContainer#setLayeredPane
  551. * @beaninfo
  552. * hidden: true
  553. * bound: true
  554. * description: The pane which holds the various desktop layers.
  555. */
  556. public void setLayeredPane(JLayeredPane layered) {
  557. JLayeredPane oldValue = getLayeredPane();
  558. getRootPane().setLayeredPane(layered);
  559. firePropertyChange(LAYERED_PANE_PROPERTY, oldValue, layered);
  560. }
  561. /**
  562. * Returns the glass pane for this internal frame.
  563. *
  564. * @return the glass pane
  565. * @see RootPaneContainer#setGlassPane
  566. */
  567. public Component getGlassPane() {
  568. return getRootPane().getGlassPane();
  569. }
  570. /**
  571. * Sets this <code>JInternalFrame</code>'s
  572. * <code>glassPane</code> property.
  573. *
  574. * @param glass the glass pane for this internal frame
  575. * @see RootPaneContainer#getGlassPane
  576. * @beaninfo
  577. * bound: true
  578. * hidden: true
  579. * description: A transparent pane used for menu rendering.
  580. */
  581. public void setGlassPane(Component glass) {
  582. Component oldValue = getGlassPane();
  583. getRootPane().setGlassPane(glass);
  584. firePropertyChange(GLASS_PANE_PROPERTY, oldValue, glass);
  585. }
  586. /**
  587. * Returns the <code>rootPane</code> object for this internal frame.
  588. *
  589. * @return the <code>rootPane</code> property
  590. * @see RootPaneContainer#getRootPane
  591. */
  592. public JRootPane getRootPane() {
  593. return rootPane;
  594. }
  595. /**
  596. * Sets the <code>rootPane</code> property
  597. * for this <code>JInternalFrame</code>.
  598. * This method is called by the constructor.
  599. *
  600. * @param root the new <code>JRootPane</code> object
  601. * @beaninfo
  602. * bound: true
  603. * hidden: true
  604. * description: The root pane used by this internal frame.
  605. */
  606. protected void setRootPane(JRootPane root) {
  607. if(rootPane != null) {
  608. remove(rootPane);
  609. }
  610. JRootPane oldValue = getRootPane();
  611. rootPane = root;
  612. if(rootPane != null) {
  613. boolean checkingEnabled = isRootPaneCheckingEnabled();
  614. try {
  615. setRootPaneCheckingEnabled(false);
  616. add(rootPane, BorderLayout.CENTER);
  617. }
  618. finally {
  619. setRootPaneCheckingEnabled(checkingEnabled);
  620. }
  621. }
  622. firePropertyChange(ROOT_PANE_PROPERTY, oldValue, root);
  623. }
  624. /**
  625. * Sets whether this <code>JInternalFrame</code> can be closed by
  626. * some user action.
  627. * @param b a boolean value, where <code>true</code> means this internal frame can be closed
  628. * @beaninfo
  629. * preferred: true
  630. * bound: true
  631. * description: Indicates whether this internal frame can be closed.
  632. */
  633. public void setClosable(boolean b) {
  634. Boolean oldValue = closable ? Boolean.TRUE : Boolean.FALSE;
  635. Boolean newValue = b ? Boolean.TRUE : Boolean.FALSE;
  636. closable = b;
  637. firePropertyChange("closable", oldValue, newValue);
  638. }
  639. /**
  640. * Returns whether this <code>JInternalFrame</code> can be closed by
  641. * some user action.
  642. * @return <code>true</code> if this internal frame can be closed
  643. */
  644. public boolean isClosable() {
  645. return closable;
  646. }
  647. /**
  648. * Returns whether this <code>JInternalFrame</code> is currently closed.
  649. * @return <code>true</code> if this internal frame is closed, <code>false</code> otherwise
  650. */
  651. public boolean isClosed() {
  652. return isClosed;
  653. }
  654. /**
  655. * Closes this internal frame if the argument is <code>true</code>.
  656. * Do not invoke this method with a <code>false</code> argument;
  657. * the result of invoking <code>setClosed(false)</code>
  658. * is unspecified.
  659. *
  660. * <p>
  661. *
  662. * If the internal frame is already closed,
  663. * this method does nothing and returns immediately.
  664. * Otherwise,
  665. * this method begins by firing
  666. * an <code>INTERNAL_FRAME_CLOSING</code> event.
  667. * Then this method sets the <code>closed</code> property to <code>true</code>
  668. * unless a listener vetoes the property change.
  669. * This method finishes by making the internal frame
  670. * invisible and unselected,
  671. * and then firing an <code>INTERNAL_FRAME_CLOSED</code> event.
  672. *
  673. * <p>
  674. *
  675. * <b>Note:</b>
  676. * To reuse an internal frame that has been closed,
  677. * you must add it to a container
  678. * (even if you never removed it from its previous container).
  679. * Typically, this container will be the <code>JDesktopPane</code>
  680. * that previously contained the internal frame.
  681. *
  682. * @param b must be <code>true</code>
  683. *
  684. * @exception PropertyVetoException when the attempt to set the
  685. * property is vetoed by the <code>JInternalFrame</code>
  686. *
  687. * @see #isClosed()
  688. * @see #setDefaultCloseOperation
  689. * @see #dispose
  690. * @see javax.swing.event.InternalFrameEvent#INTERNAL_FRAME_CLOSING
  691. *
  692. * @beaninfo
  693. * bound: true
  694. * constrained: true
  695. * description: Indicates whether this internal frame has been closed.
  696. */
  697. public void setClosed(boolean b) throws PropertyVetoException {
  698. if (isClosed == b) {
  699. return;
  700. }
  701. Boolean oldValue = isClosed ? Boolean.TRUE : Boolean.FALSE;
  702. Boolean newValue = b ? Boolean.TRUE : Boolean.FALSE;
  703. if (b) {
  704. fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSING);
  705. }
  706. fireVetoableChange(IS_CLOSED_PROPERTY, oldValue, newValue);
  707. isClosed = b;
  708. firePropertyChange(IS_CLOSED_PROPERTY, oldValue, newValue);
  709. if (isClosed) {
  710. dispose();
  711. } else if (!opened) {
  712. /* this bogus -- we haven't defined what
  713. setClosed(false) means. */
  714. // fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_OPENED);
  715. // opened = true;
  716. }
  717. }
  718. /**
  719. * Sets whether the <code>JInternalFrame</code> can be resized by some
  720. * user action.
  721. *
  722. * @param b a boolean, where <code>true</code> means this internal frame can be resized
  723. * @beaninfo
  724. * preferred: true
  725. * bound: true
  726. * description: Determines whether this internal frame can be resized
  727. * by the user.
  728. */
  729. public void setResizable(boolean b) {
  730. Boolean oldValue = resizable ? Boolean.TRUE : Boolean.FALSE;
  731. Boolean newValue = b ? Boolean.TRUE : Boolean.FALSE;
  732. resizable = b;
  733. firePropertyChange("resizable", oldValue, newValue);
  734. }
  735. /**
  736. * Returns whether the <code>JInternalFrame</code> can be resized
  737. * by some user action.
  738. *
  739. * @return <code>true</code> if this internal frame can be resized, <code>false</code> otherwise
  740. */
  741. public boolean isResizable() {
  742. // don't allow resizing when maximized.
  743. return isMaximum ? false : resizable;
  744. }
  745. /**
  746. * Sets the <code>iconable</code> property,
  747. * which must be <code>true</code>
  748. * for the user to be able to
  749. * make the <code>JInternalFrame</code> an icon.
  750. * Some look and feels might not implement iconification;
  751. * they will ignore this property.
  752. *
  753. * @param b a boolean, where <code>true</code> means this internal frame can be iconified
  754. * @beaninfo
  755. * preferred: true
  756. bound: true
  757. * description: Determines whether this internal frame can be iconified.
  758. */
  759. public void setIconifiable(boolean b) {
  760. Boolean oldValue = iconable ? Boolean.TRUE : Boolean.FALSE;
  761. Boolean newValue = b ? Boolean.TRUE : Boolean.FALSE;
  762. iconable = b;
  763. firePropertyChange("iconable", oldValue, newValue);
  764. }
  765. /**
  766. * Gets the <code>iconable</code> property,
  767. * which by default is <code>false</code>.
  768. *
  769. * @return the value of the <code>iconable</code> property.
  770. *
  771. * @see #setIconifiable
  772. */
  773. public boolean isIconifiable() {
  774. return iconable;
  775. }
  776. /**
  777. * Returns whether the <code>JInternalFrame</code> is currently iconified.
  778. *
  779. * @return <code>true</code> if this internal frame is iconified
  780. */
  781. public boolean isIcon() {
  782. return isIcon;
  783. }
  784. /**
  785. * Iconifies or de-iconifies this internal frame,
  786. * if the look and feel supports iconification.
  787. * If the internal frame's state changes to iconified,
  788. * this method fires an <code>INTERNAL_FRAME_ICONIFIED</code> event.
  789. * If the state changes to de-iconified,
  790. * an <code>INTERNAL_FRAME_DEICONIFIED</code> event is fired.
  791. *
  792. * @param b a boolean, where <code>true</code> means to iconify this internal frame and
  793. * <code>false</code> means to de-iconify it
  794. * @exception PropertyVetoException when the attempt to set the
  795. * property is vetoed by the <code>JInternalFrame</code>
  796. *
  797. * @see InternalFrameEvent#INTERNAL_FRAME_ICONIFIED
  798. * @see InternalFrameEvent#INTERNAL_FRAME_DEICONIFIED
  799. *
  800. * @beaninfo
  801. * bound: true
  802. * constrained: true
  803. * description: The image displayed when this internal frame is minimized.
  804. */
  805. public void setIcon(boolean b) throws PropertyVetoException {
  806. if (isIcon == b) {
  807. return;
  808. }
  809. /* If an internal frame is being iconified before it has a
  810. parent, (e.g., client wants it to start iconic), create the
  811. parent if possible so that we can place the icon in its
  812. proper place on the desktop. I am not sure the call to
  813. validate() is necessary, since we are not going to display
  814. this frame yet */
  815. firePropertyChange("ancestor", null, getParent());
  816. Boolean oldValue = isIcon ? Boolean.TRUE : Boolean.FALSE;
  817. Boolean newValue = b ? Boolean.TRUE : Boolean.FALSE;
  818. fireVetoableChange(IS_ICON_PROPERTY, oldValue, newValue);
  819. isIcon = b;
  820. firePropertyChange(IS_ICON_PROPERTY, oldValue, newValue);
  821. if (b)
  822. fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_ICONIFIED);
  823. else
  824. fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_DEICONIFIED);
  825. }
  826. /**
  827. * Sets the <code>maximizable</code> property,
  828. * which determines whether the <code>JInternalFrame</code>
  829. * can be maximized by
  830. * some user action.
  831. * Some look and feels might not support maximizing internal frames;
  832. * they will ignore this property.
  833. *
  834. * @param b <code>true</code> to specify that this internal frame should be maximizable; <code>false</code> to specify that it should not be
  835. * @beaninfo
  836. * bound: true
  837. * preferred: true
  838. * description: Determines whether this internal frame can be maximized.
  839. */
  840. public void setMaximizable(boolean b) {
  841. Boolean oldValue = maximizable ? Boolean.TRUE : Boolean.FALSE;
  842. Boolean newValue = b ? Boolean.TRUE : Boolean.FALSE;
  843. maximizable = b;
  844. firePropertyChange("maximizable", oldValue, newValue);
  845. }
  846. /**
  847. * Gets the value of the <code>maximizable</code> property.
  848. *
  849. * @return the value of the <code>maximizable</code> property
  850. * @see #setMaximizable
  851. */
  852. public boolean isMaximizable() {
  853. return maximizable;
  854. }
  855. /**
  856. * Returns whether the <code>JInternalFrame</code> is currently maximized.
  857. *
  858. * @return <code>true</code> if this internal frame is maximized, <code>false</code> otherwise
  859. */
  860. public boolean isMaximum() {
  861. return isMaximum;
  862. }
  863. /**
  864. * Maximizes and restores this internal frame. A maximized frame is resized to
  865. * fully fit the <code>JDesktopPane</code> area associated with the
  866. * <code>JInternalFrame</code>.
  867. * A restored frame's size is set to the <code>JInternalFrame</code>'s
  868. * actual size.
  869. *
  870. * @param b a boolean, where <code>true</code> maximizes this internal frame and <code>false</code>
  871. * restores it
  872. * @exception PropertyVetoException when the attempt to set the
  873. * property is vetoed by the <code>JInternalFrame</code>
  874. * @beaninfo
  875. * bound: true
  876. * constrained: true
  877. * description: Indicates whether this internal frame is maximized.
  878. */
  879. public void setMaximum(boolean b) throws PropertyVetoException {
  880. if (isMaximum == b) {
  881. return;
  882. }
  883. Boolean oldValue = isMaximum ? Boolean.TRUE : Boolean.FALSE;
  884. Boolean newValue = b ? Boolean.TRUE : Boolean.FALSE;
  885. fireVetoableChange(IS_MAXIMUM_PROPERTY, oldValue, newValue);
  886. /* setting isMaximum above the event firing means that
  887. property listeners that, for some reason, test it will
  888. get it wrong... See, for example, getNormalBounds() */
  889. isMaximum = b;
  890. firePropertyChange(IS_MAXIMUM_PROPERTY, oldValue, newValue);
  891. }
  892. /**
  893. * Returns the title of the <code>JInternalFrame</code>.
  894. *
  895. * @return a <code>String</code> containing this internal frame's title
  896. * @see #setTitle
  897. */
  898. public String getTitle() {
  899. return title;
  900. }
  901. /**
  902. * Sets the <code>JInternalFrame</code> title. <code>title</code>
  903. * may have a <code>null</code> value.
  904. * @see #getTitle
  905. *
  906. * @param title the <code>String</code> to display in the title bar
  907. * @beaninfo
  908. * preferred: true
  909. * bound: true
  910. * description: The text displayed in the title bar.
  911. */
  912. public void setTitle(String title) {
  913. String oldValue = this.title;
  914. this.title = title;
  915. firePropertyChange(TITLE_PROPERTY, oldValue, title);
  916. }
  917. /**
  918. * Selects or deselects the internal frame
  919. * if it's showing.
  920. * A <code>JInternalFrame</code> normally draws its title bar
  921. * differently if it is
  922. * the selected frame, which indicates to the user that this
  923. * internal frame has the focus.
  924. * When this method changes the state of the internal frame
  925. * from deselected to selected, it fires an
  926. * <code>InternalFrameEvent.INTERNAL_FRAME_ACTIVATED</code> event.
  927. * If the change is from selected to deselected,
  928. * an <code>InternalFrameEvent.INTERNAL_FRAME_DEACTIVATED</code> event
  929. * is fired.
  930. *
  931. * @param selected a boolean, where <code>true</code> means this internal frame
  932. * should become selected (currently active)
  933. * and <code>false</code> means it should become deselected
  934. * @exception PropertyVetoException when the attempt to set the
  935. * property is vetoed by the <code>JInternalFrame</code>
  936. *
  937. * @see #isShowing
  938. * @see InternalFrameEvent#INTERNAL_FRAME_ACTIVATED
  939. * @see InternalFrameEvent#INTERNAL_FRAME_DEACTIVATED
  940. *
  941. * @beaninfo
  942. * constrained: true
  943. * bound: true
  944. * description: Indicates whether this internal frame is currently
  945. * the active frame.
  946. */
  947. public void setSelected(boolean selected) throws PropertyVetoException {
  948. // The internal frame or the desktop icon must be showing to allow
  949. // selection. We may deselect even if neither is showing.
  950. if ((isSelected == selected) || (selected &&
  951. (isIcon ? !desktopIcon.isShowing() : !isShowing()))) {
  952. return;
  953. }
  954. Boolean oldValue = isSelected ? Boolean.TRUE : Boolean.FALSE;
  955. Boolean newValue = selected ? Boolean.TRUE : Boolean.FALSE;
  956. fireVetoableChange(IS_SELECTED_PROPERTY, oldValue, newValue);
  957. /* We don't want to leave focus in the previously selected
  958. frame, so we have to set it to *something* in case it
  959. doesn't get set in some other way (as if a user clicked on
  960. a component that doesn't request focus). If this call is
  961. happening because the user clicked on a component that will
  962. want focus, then it will get transfered there later.
  963. We test for parent.isShowing() above, because AWT throws a
  964. NPE if you try to request focus on a lightweight before its
  965. parent has been made visible */
  966. lastFocusOwner = null;
  967. if (selected) {
  968. restoreSubcomponentFocus();
  969. } else {
  970. getRootPane().setMostRecentFocusOwner(getFocusOwner());
  971. }
  972. isSelected = selected;
  973. firePropertyChange(IS_SELECTED_PROPERTY, oldValue, newValue);
  974. if (isSelected)
  975. fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_ACTIVATED);
  976. else
  977. fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_DEACTIVATED);
  978. lastFocusOwner = null;
  979. repaint();
  980. }
  981. /**
  982. * Returns whether the <code>JInternalFrame</code> is the
  983. * currently "selected" or active frame.
  984. *
  985. * @return <code>true</code> if this internal frame is currently selected (active)
  986. * @see #setSelected
  987. */
  988. public boolean isSelected() {
  989. return isSelected;
  990. }
  991. /**
  992. * Sets an image to be displayed in the titlebar of this internal frame (usually
  993. * in the top-left corner).
  994. * This image is not the <code>desktopIcon</code> object, which
  995. * is the image displayed in the <code>JDesktop</code> when
  996. * this internal frame is iconified.
  997. *
  998. * Passing <code>null</code> to this function is valid,
  999. * but the look and feel
  1000. * can choose the
  1001. * appropriate behavior for that situation, such as displaying no icon
  1002. * or a default icon for the look and feel.
  1003. *
  1004. * @param icon the <code>Icon</code> to display in the title bar
  1005. * @see #getFrameIcon
  1006. * @beaninfo
  1007. * bound: true
  1008. * description: The icon shown in the top-left corner of this internal frame.
  1009. */
  1010. public void setFrameIcon(Icon icon) {
  1011. Icon oldIcon = frameIcon;
  1012. frameIcon = icon;
  1013. firePropertyChange(FRAME_ICON_PROPERTY, oldIcon, icon);
  1014. }
  1015. /**
  1016. * Returns the image displayed in the title bar of this internal frame (usually
  1017. * in the top-left corner).
  1018. *
  1019. * @return the <code>Icon</code> displayed in the title bar
  1020. * @see #setFrameIcon
  1021. */
  1022. public Icon getFrameIcon() {
  1023. return frameIcon;
  1024. }
  1025. /**
  1026. * Convenience method that moves this component to position 0 if its
  1027. * parent is a <code>JLayeredPane</code>.
  1028. */
  1029. public void moveToFront() {
  1030. if(getParent() != null && getParent() instanceof JLayeredPane) {
  1031. JLayeredPane l = (JLayeredPane)getParent();
  1032. // Because move to front typically involves adding and removing
  1033. // Components, it will often times result in focus changes. We
  1034. // either install focus to the lastFocusOwner, or our desendant
  1035. // that has focus. We use the ivar for lastFocusOwner as in
  1036. // some cases requestFocus is async and won't have completed from
  1037. // the requestFocus call in setSelected so that getFocusOwner
  1038. // will return the wrong component.
  1039. Component focusOwner = (lastFocusOwner != null) ? lastFocusOwner :
  1040. KeyboardFocusManager.getCurrentKeyboardFocusManager().
  1041. getFocusOwner();
  1042. if (focusOwner != null &&
  1043. !SwingUtilities.isDescendingFrom(focusOwner, this)) {
  1044. focusOwner = null;
  1045. }
  1046. l.moveToFront(this);
  1047. if (focusOwner != null) {
  1048. focusOwner.requestFocus();
  1049. }
  1050. }
  1051. }
  1052. /**
  1053. * Convenience method that moves this component to position -1 if its
  1054. * parent is a <code>JLayeredPane</code>.
  1055. */
  1056. public void moveToBack() {
  1057. if(getParent() != null && getParent() instanceof JLayeredPane) {
  1058. JLayeredPane l = (JLayeredPane)getParent();
  1059. l.moveToBack(this);
  1060. }
  1061. }
  1062. /**
  1063. * Convenience method for setting the layer attribute of this component.
  1064. *
  1065. * @param layer an <code>Integer</code> object specifying this
  1066. * frame's desktop layer
  1067. * @see JLayeredPane
  1068. * @beaninfo
  1069. * expert: true
  1070. * description: Specifies what desktop layer is used.
  1071. */
  1072. public void setLayer(Integer layer) {
  1073. if(getParent() != null && getParent() instanceof JLayeredPane) {
  1074. // Normally we want to do this, as it causes the LayeredPane
  1075. // to draw properly.
  1076. JLayeredPane p = (JLayeredPane)getParent();
  1077. p.setLayer(this, layer.intValue(), p.getPosition(this));
  1078. } else {
  1079. // Try to do the right thing
  1080. JLayeredPane.putLayer(this, layer.intValue());
  1081. if(getParent() != null)
  1082. getParent().repaint(_bounds.x, _bounds.y,
  1083. _bounds.width, _bounds.height);
  1084. }
  1085. }
  1086. /**
  1087. * Convenience method for setting the layer attribute of this component.
  1088. * The method <code>setLayer(Integer)</code> should be used for
  1089. * layer values predefined in <code>JLayeredPane</code>.
  1090. * When using <code>setLayer(int)</code>, care must be taken not to
  1091. * accidentally clash with those values.
  1092. *
  1093. * @param layer an integer specifying this internal frame's desktop layer
  1094. *
  1095. * @since 1.3
  1096. *
  1097. * @see #setLayer(Integer)
  1098. * @see JLayeredPane
  1099. * @beaninfo
  1100. * expert: true
  1101. * description: Specifies what desktop layer is used.
  1102. */
  1103. public void setLayer(int layer) {
  1104. this.setLayer(new Integer(layer));
  1105. }
  1106. /**
  1107. * Convenience method for getting the layer attribute of this component.
  1108. *
  1109. * @return an <code>Integer</code> object specifying this
  1110. * frame's desktop layer
  1111. * @see JLayeredPane
  1112. */
  1113. public int getLayer() {
  1114. return JLayeredPane.getLayer(this);
  1115. }
  1116. /**
  1117. * Convenience method that searches the ancestor hierarchy for a
  1118. * <code>JDesktop</code> instance. If <code>JInternalFrame</code>
  1119. * finds none, the <code>desktopIcon</code> tree is searched.
  1120. *
  1121. * @return the <code>JDesktopPane</code> this internal frame belongs to,
  1122. * or <code>null</code> if none is found
  1123. */
  1124. public JDesktopPane getDesktopPane() {
  1125. Container p;
  1126. // Search upward for desktop
  1127. p = getParent();
  1128. while(p != null && !(p instanceof JDesktopPane))
  1129. p = p.getParent();
  1130. if(p == null) {
  1131. // search its icon parent for desktop
  1132. p = getDesktopIcon().getParent();
  1133. while(p != null && !(p instanceof JDesktopPane))
  1134. p = p.getParent();
  1135. }
  1136. return (JDesktopPane)p;
  1137. }
  1138. /**
  1139. * Sets the <code>JDesktopIcon</code> associated with this
  1140. * <code>JInternalFrame</code>.
  1141. *
  1142. * @param d the <code>JDesktopIcon</code> to display on the desktop
  1143. * @see #getDesktopIcon
  1144. * @beaninfo
  1145. * bound: true
  1146. * description: The icon shown when this internal frame is minimized.
  1147. */
  1148. public void setDesktopIcon(JDesktopIcon d) {
  1149. JDesktopIcon oldValue = getDesktopIcon();
  1150. desktopIcon = d;
  1151. firePropertyChange("desktopIcon", oldValue, d);
  1152. }
  1153. /**
  1154. * Returns the <code>JDesktopIcon</code> used when this
  1155. * <code>JInternalFrame</code> is iconified.
  1156. *
  1157. * @return the <code>JDesktopIcon</code> displayed on the desktop
  1158. * @see #setDesktopIcon
  1159. */
  1160. public JDesktopIcon getDesktopIcon() {
  1161. return desktopIcon;
  1162. }
  1163. /**
  1164. * If the <code>JInternalFrame</code> is not in maximized state, returns
  1165. * <code>getBounds()</code> otherwise, returns the bounds that the
  1166. * <code>JInternalFrame</code> would be restored to.
  1167. *
  1168. * @return a <code>Rectangle</code> containing the bounds of this
  1169. * frame when in the normal state
  1170. * @since 1.3
  1171. */
  1172. public Rectangle getNormalBounds() {
  1173. /* we used to test (!isMaximum) here, but since this
  1174. method is used by the property listener for the
  1175. IS_MAXIMUM_PROPERTY, it ended up getting the wrong
  1176. answer... Since normalBounds get set to null when the
  1177. frame is restored, this should work better */
  1178. if (normalBounds != null) {
  1179. return normalBounds;
  1180. } else {
  1181. return getBounds();
  1182. }
  1183. }
  1184. /**
  1185. * Sets the normal bounds for this internal frame, the bounds that
  1186. * this internal frame would be restored to from its maximized state.
  1187. * This method is intended for use only by desktop managers.
  1188. *
  1189. * @param r the bounds that this internal frame should be restored to
  1190. * @since 1.3
  1191. */
  1192. public void setNormalBounds(Rectangle r) {
  1193. normalBounds = r;
  1194. }
  1195. /**
  1196. * If this <code>JInternalFrame</code> is active,
  1197. * returns the child that has focus.
  1198. * Otherwise, returns <code>null</code>.
  1199. *
  1200. * @return the component with focus, or <code>null</code> if no children have focus
  1201. * @since 1.3
  1202. */
  1203. public Component getFocusOwner() {
  1204. if (isSelected()) {
  1205. Component focusOwner = KeyboardFocusManager.
  1206. getCurrentKeyboardFocusManager().getFocusOwner();
  1207. if (focusOwner != null && !SwingUtilities.
  1208. isDescendingFrom(focusOwner, this)) {
  1209. // Sanity check, may sure we don't return a bogus value here.
  1210. focusOwner = null;
  1211. }
  1212. return focusOwner;
  1213. }
  1214. return null;
  1215. }
  1216. /**
  1217. * Returns the child component of this <code>JInternalFrame</code>
  1218. * that will receive the
  1219. * focus when this <code>JInternalFrame</code> is selected.
  1220. * If this <code>JInternalFrame</code> is
  1221. * currently selected, this method returns the same component as
  1222. * the <code>getFocusOwner</code> method.
  1223. * If this <code>JInternalFrame</code> is not selected,
  1224. * then the child component that most recently requested focus will be
  1225. * returned. If no child component has ever requested focus, then this
  1226. * <code>JInternalFrame</code>'s initial focusable component is returned.
  1227. * If no such
  1228. * child exists, then this <code>JInternalFrame</code>'s default component
  1229. * to focus is returned.
  1230. *
  1231. * @return the child component that will receive focus when this
  1232. * <code>JInternalFrame</code> is selected
  1233. * @see #getFocusOwner
  1234. * @see #isSelected
  1235. * @since 1.4
  1236. */
  1237. public Component getMostRecentFocusOwner() {
  1238. if (isSelected()) {
  1239. return getFocusOwner();
  1240. }
  1241. Component mostRecentFocusOwner =
  1242. getRootPane().getMostRecentFocusOwner();
  1243. if (mostRecentFocusOwner != null) {
  1244. return mostRecentFocusOwner;
  1245. }
  1246. FocusTraversalPolicy policy = getFocusTraversalPolicy();
  1247. if (policy instanceof InternalFrameFocusTraversalPolicy) {
  1248. return ((InternalFrameFocusTraversalPolicy)policy).
  1249. getInitialComponent(this);
  1250. }
  1251. return policy.getDefaultComponent(this);
  1252. }
  1253. /**
  1254. * Requests the internal frame to restore focus to the
  1255. * last subcomponent that had focus. This is used by the UI when
  1256. * the user selected this internal frame --
  1257. * for example, by clicking on the title bar.
  1258. *
  1259. * @since 1.3
  1260. */
  1261. public void restoreSubcomponentFocus() {
  1262. lastFocusOwner = getMostRecentFocusOwner();
  1263. if (lastFocusOwner == null) {
  1264. // Make sure focus is restored somewhere, so that
  1265. // we don't leave a focused component in another frame while
  1266. // this frame is selected.
  1267. lastFocusOwner = getContentPane();
  1268. }
  1269. lastFocusOwner.requestFocus();
  1270. }
  1271. /*
  1272. * Creates a new <code>EventDispatchThread</code> to dispatch events
  1273. * from. This method returns when <code>stopModal</code> is invoked.
  1274. */
  1275. synchronized void startModal() {
  1276. /* Since all input will be blocked until this dialog is dismissed,
  1277. * make sure its parent containers are visible first (this component
  1278. * is tested below). This is necessary for JApplets, because
  1279. * because an applet normally isn't made visible until after its
  1280. * start() method returns -- if this method is called from start(),
  1281. * the applet will appear to hang while an invisible modal frame
  1282. * waits for input.
  1283. */
  1284. if (isVisible() && !isShowing()) {
  1285. Container parent = this.getParent();
  1286. while (parent != null) {
  1287. if (parent.isVisible() == false) {
  1288. parent.setVisible(true);
  1289. }
  1290. parent = parent.getParent();
  1291. }
  1292. }
  1293. try {
  1294. if (SwingUtilities.isEventDispatchThread()) {
  1295. EventQueue theQueue = getToolkit().getSystemEventQueue();
  1296. while (isVisible()) {
  1297. // This is essentially the body of EventDispatchThread
  1298. AWTEvent event = theQueue.getNextEvent();
  1299. Object src = event.getSource();
  1300. // can't call theQueue.dispatchEvent, so I pasted its body here
  1301. if (event instanceof ActiveEvent) {
  1302. ((ActiveEvent) event).dispatch();
  1303. } else if (src instanceof Component) {
  1304. ((Component) src).dispatchEvent(event);
  1305. } else if (src instanceof MenuComponent) {
  1306. ((MenuComponent) src).dispatchEvent(event);
  1307. } else {
  1308. System.err.println("unable to dispatch event: " + event);
  1309. }
  1310. }
  1311. } else
  1312. while (isVisible())
  1313. wait();
  1314. } catch(InterruptedException e){}
  1315. }
  1316. /*
  1317. * Stops the event dispatching loop created by a previous call to
  1318. * <code>startModal</code>.
  1319. */
  1320. synchronized void stopModal() {
  1321. notifyAll();
  1322. }
  1323. /**
  1324. * Moves and resizes this component. Unlike other components,
  1325. * this implementation also forces re-layout, so that frame
  1326. * decorations such as the title bar are always redisplayed.
  1327. *
  1328. * @param x an integer giving the component's new horizontal position
  1329. * measured in pixels from the left of its container
  1330. * @param y an integer giving the component's new vertical position,
  1331. * measured in pixels from the bottom of its container
  1332. * @param width an integer giving the component's new width in pixels
  1333. * @param height an integer giving the component's new height in pixels
  1334. */
  1335. public void reshape(int x, int y, int width, int height) {
  1336. super.reshape(x, y, width, height);
  1337. validate();
  1338. repaint();
  1339. }
  1340. ///////////////////////////
  1341. // Frame/Window equivalents
  1342. ///////////////////////////
  1343. /**
  1344. * Adds the specified listener to receive internal
  1345. * frame events from this internal frame.
  1346. *
  1347. * @param l the internal frame listener
  1348. */
  1349. public void addInternalFrameListener(InternalFrameListener l) { // remind: sync ??
  1350. listenerList.add(InternalFrameListener.class, l);
  1351. // remind: needed?
  1352. enableEvents(0); // turn on the newEventsOnly flag in Component.
  1353. }
  1354. /**
  1355. * Removes the specified internal frame listener so that it no longer
  1356. * receives internal frame events from this internal frame.
  1357. *
  1358. * @param l the internal frame listener
  1359. */
  1360. public void removeInternalFrameListener(InternalFrameListener l) { // remind: sync??
  1361. listenerList.remove(InternalFrameListener.class, l);
  1362. }
  1363. /**
  1364. * Returns an array of all the <code>InternalFrameListener</code>s added
  1365. * to this <code>JInternalFrame</code> with
  1366. * <code>addInternalFrameListener</code>.
  1367. *
  1368. * @return all of the <code>InternalFrameListener</code>s added or an empty
  1369. * array if no listeners have been added
  1370. * @since 1.4
  1371. *
  1372. * @see #addInternalFrameListener
  1373. */
  1374. public InternalFrameListener[] getInternalFrameListeners() {
  1375. return (InternalFrameListener[])listenerList.getListeners(
  1376. InternalFrameListener.class);
  1377. }
  1378. // remind: name ok? all one method ok? need to be synchronized?
  1379. /**
  1380. * Fires an internal frame event.
  1381. *
  1382. * @param id the type of the event being fired; one of the following:
  1383. * <ul>
  1384. * <li><code>InternalFrameEvent.INTERNAL_FRAME_OPENED</code>
  1385. * <li><code>InternalFrameEvent.INTERNAL_FRAME_CLOSING</code>
  1386. * <li><code>InternalFrameEvent.INTERNAL_FRAME_CLOSED</code>
  1387. * <li><code>InternalFrameEvent.INTERNAL_FRAME_ICONIFIED</code>
  1388. * <li><code>InternalFrameEvent.INTERNAL_FRAME_DEICONIFIED</code>
  1389. * <li><code>InternalFrameEvent.INTERNAL_FRAME_ACTIVATED</code>
  1390. * <li><code>InternalFrameEvent.INTERNAL_FRAME_DEACTIVATED</code>
  1391. * </ul>
  1392. * If the event type is not one of the above, nothing happens.
  1393. */
  1394. protected void fireInternalFrameEvent(int id){
  1395. Object[] listeners = listenerList.getListenerList();
  1396. InternalFrameEvent e = null;
  1397. for (int i = listeners.length -2; i >=0; i -= 2){
  1398. if (listeners[i] == InternalFrameListener.class){
  1399. if (e == null){
  1400. e = new InternalFrameEvent(this, id);
  1401. // System.out.println("InternalFrameEvent: " + e.paramString());
  1402. }
  1403. switch(e.getID()) {
  1404. case InternalFrameEvent.INTERNAL_FRAME_OPENED:
  1405. ((InternalFrameListener)listeners[i+1]).internalFrameOpened(e);
  1406. break;
  1407. case InternalFrameEvent.INTERNAL_FRAME_CLOSING:
  1408. ((InternalFrameListener)listeners[i+1]).internalFrameClosing(e);
  1409. break;
  1410. case InternalFrameEvent.INTERNAL_FRAME_CLOSED:
  1411. ((InternalFrameListener)listeners[i+1]).internalFrameClosed(e);
  1412. break;
  1413. case InternalFrameEvent.INTERNAL_FRAME_ICONIFIED:
  1414. ((InternalFrameListener)listeners[i+1]).internalFrameIconified(e);
  1415. break;
  1416. case InternalFrameEvent.INTERNAL_FRAME_DEICONIFIED:
  1417. ((InternalFrameListener)listeners[i+1]).internalFrameDeiconified(e);
  1418. break;
  1419. case InternalFrameEvent.INTERNAL_FRAME_ACTIVATED:
  1420. ((InternalFrameListener)listeners[i+1]).internalFrameActivated(e);
  1421. break;
  1422. case InternalFrameEvent.INTERNAL_FRAME_DEACTIVATED:
  1423. ((InternalFrameListener)listeners[i+1]).internalFrameDeactivated(e);
  1424. break;
  1425. default:
  1426. break;
  1427. }
  1428. }
  1429. }
  1430. /* we could do it off the event, but at the moment, that's not how
  1431. I'm implementing it */
  1432. // if (id == InternalFrameEvent.INTERNAL_FRAME_CLOSING) {
  1433. // doDefaultCloseAction();
  1434. // }
  1435. }
  1436. /**
  1437. * Fires an
  1438. * <code>INTERNAL_FRAME_CLOSING</code> event
  1439. * and then performs the action specified by
  1440. * the internal frame's default close operation.
  1441. * This method is typically invoked by the
  1442. * look-and-feel-implemented action handler
  1443. * for the internal frame's close button.
  1444. *
  1445. * @since 1.3
  1446. * @see #setDefaultCloseOperation
  1447. * @see javax.swing.event.InternalFrameEvent#INTERNAL_FRAME_CLOSING
  1448. */
  1449. public void doDefaultCloseAction() {
  1450. fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSING);
  1451. switch(defaultCloseOperation) {
  1452. case DO_NOTHING_ON_CLOSE:
  1453. break;
  1454. case HIDE_ON_CLOSE:
  1455. setVisible(false);
  1456. if (isSelected())
  1457. try {
  1458. setSelected(false);
  1459. } catch (PropertyVetoException pve) {}
  1460. /* should this activate the next frame? that's really
  1461. desktopmanager's policy... */
  1462. break;
  1463. case DISPOSE_ON_CLOSE:
  1464. try {
  1465. fireVetoableChange(IS_CLOSED_PROPERTY, Boolean.FALSE,
  1466. Boolean.TRUE);
  1467. isClosed = true;
  1468. firePropertyChange(IS_CLOSED_PROPERTY, Boolean.FALSE,
  1469. Boolean.TRUE);
  1470. dispose();
  1471. } catch (PropertyVetoException pve) {}
  1472. break;
  1473. default:
  1474. break;
  1475. }
  1476. }
  1477. /**
  1478. * Sets the operation that will happen by default when
  1479. * the user initiates a "close" on this internal frame.
  1480. * The possible choices are:
  1481. * <p>
  1482. * <dl>
  1483. * <dt><code>DO_NOTHING_ON_CLOSE</code>
  1484. * <dd> Do nothing.
  1485. * This requires the program to handle the operation
  1486. * in the <code>windowClosing</code> method
  1487. * of a registered <code>InternalFrameListener</code> object.
  1488. * <dt><code>HIDE_ON_CLOSE</code>
  1489. * <dd> Automatically make the internal frame invisible.
  1490. * <dt><code>DISPOSE_ON_CLOSE</code>
  1491. * <dd> Automatically dispose of the internal frame.
  1492. * </dl>
  1493. * <p>
  1494. * The default value is <code>DISPOSE_ON_CLOSE</code>.
  1495. * Before performing the specified close operation,
  1496. * the internal frame fires
  1497. * an <code>INTERNAL_FRAME_CLOSING</code> event.
  1498. *
  1499. * @param operation one of the following constants defined in
  1500. * <code>javax.swing.WindowConstants</code>
  1501. * (an interface implemented by
  1502. * <code>JInternalFrame</code>):
  1503. * <code>DO_NOTHING_ON_CLOSE</code>,
  1504. * <code>HIDE_ON_CLOSE</code>, or
  1505. * <code>DISPOSE_ON_CLOSE</code>
  1506. *
  1507. * @see #addInternalFrameListener
  1508. * @see #getDefaultCloseOperation
  1509. * @see #setVisible
  1510. * @see #dispose
  1511. * @see InternalFrameEvent#INTERNAL_FRAME_CLOSING
  1512. */
  1513. public void setDefaultCloseOperation(int operation) {
  1514. this.defaultCloseOperation = operation;
  1515. }
  1516. /**
  1517. * Returns the default operation that occurs when the user
  1518. * initiates a "close" on this internal frame.
  1519. * @return the operation that will occur when the user closes the internal
  1520. * frame
  1521. * @see #setDefaultCloseOperation
  1522. */
  1523. public int getDefaultCloseOperation() {
  1524. return defaultCloseOperation;
  1525. }
  1526. /**
  1527. * Causes subcomponents of this <code>JInternalFrame</code>
  1528. * to be laid out at their preferred size. Internal frames that are
  1529. * iconized or maximized are first restored and then packed. If the
  1530. * internal frame is unable to be restored its state is not changed
  1531. * and will not be packed.
  1532. *
  1533. * @see java.awt.Window#pack
  1534. */
  1535. public void pack() {
  1536. try {
  1537. if (isIcon()) {
  1538. setIcon(false);
  1539. } else if (isMaximum()) {
  1540. setMaximum(false);
  1541. }
  1542. } catch(PropertyVetoException e) {
  1543. return;
  1544. }
  1545. setSize(getPreferredSize());
  1546. validate();
  1547. }
  1548. /**
  1549. * If the internal frame is not visible,
  1550. * brings the internal frame to the front,
  1551. * makes it visible,
  1552. * and attempts to select it.
  1553. * The first time the internal frame is made visible,
  1554. * this method also fires an <code>INTERNAL_FRAME_OPENED</code> event.
  1555. * This method does nothing if the internal frame is already visible.
  1556. * Invoking this method
  1557. * has the same result as invoking
  1558. * <code>setVisible(true)</code>.
  1559. *
  1560. * @see #moveToFront
  1561. * @see #setSelected
  1562. * @see InternalFrameEvent#INTERNAL_FRAME_OPENED
  1563. * @see #setVisible
  1564. */
  1565. public void show() {
  1566. // bug 4312922
  1567. if (isVisible()) {
  1568. //match the behavior of setVisible(true): do nothing
  1569. return;
  1570. }
  1571. // bug 4149505
  1572. if (!opened) {
  1573. fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_OPENED);
  1574. opened = true;
  1575. }
  1576. /* icon default visibility is false; set it to true so that it shows
  1577. up when user iconifies frame */
  1578. getDesktopIcon().setVisible(true);
  1579. toFront();
  1580. super.show();
  1581. if (isIcon) {
  1582. return;
  1583. }
  1584. if (!isSelected()) {
  1585. try {
  1586. setSelected(true);
  1587. } catch (PropertyVetoException pve) {}
  1588. }
  1589. }
  1590. public void hide() {
  1591. if (isIcon()) {
  1592. getDesktopIcon().setVisible(false);
  1593. }
  1594. super.hide();
  1595. }
  1596. /**
  1597. * Makes this internal frame
  1598. * invisible, unselected, and closed.
  1599. * If the frame is not already closed,
  1600. * this method fires an
  1601. * <code>INTERNAL_FRAME_CLOSED</code> event.
  1602. * The results of invoking this method are similar to
  1603. * <code>setClosed(true)</code>,
  1604. * but <code>dispose</code> always succeeds in closing
  1605. * the internal frame and does not fire
  1606. * an <code>INTERNAL_FRAME_CLOSING</code> event.
  1607. *
  1608. * @see javax.swing.event.InternalFrameEvent#INTERNAL_FRAME_CLOSED
  1609. * @see #setVisible
  1610. * @see #setSelected
  1611. * @see #setClosed
  1612. */
  1613. public void dispose() {
  1614. if (isVisible()) {
  1615. setVisible(false);
  1616. }
  1617. if (isSelected()) {
  1618. try {
  1619. setSelected(false);
  1620. } catch (PropertyVetoException pve) {}
  1621. }
  1622. if (!isClosed) {
  1623. firePropertyChange(IS_CLOSED_PROPERTY, Boolean.FALSE, Boolean.TRUE);
  1624. isClosed = true;
  1625. }
  1626. fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSED);
  1627. }
  1628. /**
  1629. * Brings this internal frame to the front.
  1630. * Places this internal frame at the top of the stacking order
  1631. * and makes the corresponding adjustment to other visible internal
  1632. * frames.
  1633. *
  1634. * @see java.awt.Window#toFront
  1635. * @see #moveToFront
  1636. */
  1637. public void toFront() {
  1638. moveToFront();
  1639. }
  1640. /**
  1641. * Sends this internal frame to the back.
  1642. * Places this internal frame at the bottom of the stacking order
  1643. * and makes the corresponding adjustment to other visible
  1644. * internal frames.
  1645. *
  1646. * @see java.awt.Window#toBack
  1647. * @see #moveToBack
  1648. */
  1649. public void toBack() {
  1650. moveToBack();
  1651. }
  1652. /**
  1653. * Does nothing because <code>JInternalFrame</code>s must always be roots of a focus
  1654. * traversal cycle.
  1655. *
  1656. * @param focusCycleRoot this value is ignored
  1657. * @see #isFocusCycleRoot
  1658. * @see java.awt.Container#setFocusTraversalPolicy
  1659. * @see java.awt.Container#getFocusTraversalPolicy
  1660. * @since 1.4
  1661. */
  1662. public final void setFocusCycleRoot(boolean focusCycleRoot) {
  1663. }
  1664. /**
  1665. * Always returns <code>true</code> because all <code>JInternalFrame</code>s must be
  1666. * roots of a focus traversal cycle.
  1667. *
  1668. * @return <code>true</code>
  1669. * @see #setFocusCycleRoot
  1670. * @see java.awt.Container#setFocusTraversalPolicy
  1671. * @see java.awt.Container#getFocusTraversalPolicy
  1672. * @since 1.4
  1673. */
  1674. public final boolean isFocusCycleRoot() {
  1675. return true;
  1676. }
  1677. /**
  1678. * Always returns <code>null</code> because <code>JInternalFrame</code>s
  1679. * must always be roots of a focus
  1680. * traversal cycle.
  1681. *
  1682. * @return <code>null</code>
  1683. * @see java.awt.Container#isFocusCycleRoot()
  1684. * @since 1.4
  1685. */
  1686. public final Container getFocusCycleRootAncestor() {
  1687. return null;
  1688. }
  1689. /**
  1690. * Gets the warning string that is displayed with this internal frame.
  1691. * Since an internal frame is always secure (since it's fully
  1692. * contained within a window that might need a warning string)
  1693. * this method always returns <code>null</code>.
  1694. * @return <code>null</code>
  1695. * @see java.awt.Window#getWarningString
  1696. */
  1697. public final String getWarningString() {
  1698. return null;
  1699. }
  1700. /**
  1701. * See <code>readObject</code> and <code>writeObject</code>
  1702. * in <code>JComponent</code> for more
  1703. * information about serialization in Swing.
  1704. */
  1705. private void writeObject(ObjectOutputStream s) throws IOException {
  1706. s.defaultWriteObject();
  1707. if (getUIClassID().equals(uiClassID)) {
  1708. byte count = JComponent.getWriteObjCounter(this);
  1709. JComponent.setWriteObjCounter(this, --count);
  1710. if (count == 0 && ui != null) {
  1711. boolean old = isRootPaneCheckingEnabled();
  1712. try {
  1713. setRootPaneCheckingEnabled(false);
  1714. ui.installUI(this);
  1715. } finally {
  1716. setRootPaneCheckingEnabled(old);
  1717. }
  1718. }
  1719. }
  1720. }
  1721. /* Called from the JComponent's EnableSerializationFocusListener to
  1722. * do any Swing-specific pre-serialization configuration.
  1723. */
  1724. void compWriteObjectNotify() {
  1725. // need to disable rootpane checking for InternalFrame: 4172083
  1726. boolean old = isRootPaneCheckingEnabled();
  1727. try {
  1728. setRootPaneCheckingEnabled(false);
  1729. super.compWriteObjectNotify();
  1730. }
  1731. finally {
  1732. setRootPaneCheckingEnabled(old);
  1733. }
  1734. }
  1735. /**
  1736. * Returns a string representation of this <code>JInternalFrame</code>.
  1737. * This method
  1738. * is intended to be used only for debugging purposes, and the
  1739. * content and format of the returned string may vary between
  1740. * implementations. The returned string may be empty but may not
  1741. * be <code>null</code>.
  1742. *
  1743. * @return a string representation of this <code>JInternalFrame</code>
  1744. */
  1745. protected String paramString() {
  1746. String rootPaneString = (rootPane != null ?
  1747. rootPane.toString() : "");
  1748. String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ?
  1749. "true" : "false");
  1750. String closableString = (closable ? "true" : "false");
  1751. String isClosedString = (isClosed ? "true" : "false");
  1752. String maximizableString = (maximizable ? "true" : "false");
  1753. String isMaximumString = (isMaximum ? "true" : "false");
  1754. String iconableString = (iconable ? "true" : "false");
  1755. String isIconString = (isIcon ? "true" : "false");
  1756. String resizableString = (resizable ? "true" : "false");
  1757. String isSelectedString = (isSelected ? "true" : "false");
  1758. String frameIconString = (frameIcon != null ?
  1759. frameIcon.toString() : "");
  1760. String titleString = (title != null ?
  1761. title : "");
  1762. String desktopIconString = (desktopIcon != null ?
  1763. desktopIcon.toString() : "");
  1764. String openedString = (opened ? "true" : "false");
  1765. String defaultCloseOperationString;
  1766. if (defaultCloseOperation == HIDE_ON_CLOSE) {
  1767. defaultCloseOperationString = "HIDE_ON_CLOSE";
  1768. } else if (defaultCloseOperation == DISPOSE_ON_CLOSE) {
  1769. defaultCloseOperationString = "DISPOSE_ON_CLOSE";
  1770. } else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) {
  1771. defaultCloseOperationString = "DO_NOTHING_ON_CLOSE";
  1772. } else defaultCloseOperationString = "";
  1773. return super.paramString() +
  1774. ",closable=" + closableString +
  1775. ",defaultCloseOperation=" + defaultCloseOperationString +
  1776. ",desktopIcon=" + desktopIconString +
  1777. ",frameIcon=" + frameIconString +
  1778. ",iconable=" + iconableString +
  1779. ",isClosed=" + isClosedString +
  1780. ",isIcon=" + isIconString +
  1781. ",isMaximum=" + isMaximumString +
  1782. ",isSelected=" + isSelectedString +
  1783. ",maximizable=" + maximizableString +
  1784. ",opened=" + openedString +
  1785. ",resizable=" + resizableString +
  1786. ",rootPane=" + rootPaneString +
  1787. ",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString +
  1788. ",title=" + titleString;
  1789. }
  1790. // ======= begin optimized frame dragging defence code ==============
  1791. boolean isDragging = false;
  1792. boolean danger = false;
  1793. /**
  1794. * Overridden to allow optimized painting when the
  1795. * internal frame is being dragged.
  1796. */
  1797. protected void paintComponent(Graphics g) {
  1798. if (isDragging) {
  1799. // System.out.println("ouch");
  1800. danger = true;
  1801. }
  1802. super.paintComponent(g);
  1803. }
  1804. // ======= end optimized frame dragging defence code ==============
  1805. /////////////////
  1806. // Accessibility support
  1807. ////////////////
  1808. /**
  1809. * Gets the <code>AccessibleContext</code> associated with this
  1810. * <code>JInternalFrame</code>.
  1811. * For internal frames, the <code>AccessibleContext</code>
  1812. * takes the form of an
  1813. * <code>AccessibleJInternalFrame</code> object.
  1814. * A new <code>AccessibleJInternalFrame</code> instance is created if necessary.
  1815. *
  1816. * @return an <code>AccessibleJInternalFrame</code> that serves as the
  1817. * <code>AccessibleContext</code> of this
  1818. * <code>JInternalFrame</code>
  1819. * @see AccessibleJInternalFrame
  1820. */
  1821. public AccessibleContext getAccessibleContext() {
  1822. if (accessibleContext == null) {
  1823. accessibleContext = new AccessibleJInternalFrame();
  1824. }
  1825. return accessibleContext;
  1826. }
  1827. /**
  1828. * This class implements accessibility support for the
  1829. * <code>JInternalFrame</code> class. It provides an implementation of the
  1830. * Java Accessibility API appropriate to internal frame user-interface
  1831. * elements.
  1832. * <p>
  1833. * <strong>Warning:</strong>
  1834. * Serialized objects of this class will not be compatible with
  1835. * future Swing releases. The current serialization support is
  1836. * appropriate for short term storage or RMI between applications running
  1837. * the same version of Swing. As of 1.4, support for long term storage
  1838. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  1839. * has been added to the <code>java.beans</code> package.
  1840. * Please see {@link java.beans.XMLEncoder}.
  1841. */
  1842. protected class AccessibleJInternalFrame extends AccessibleJComponent
  1843. implements AccessibleValue {
  1844. /**
  1845. * Get the accessible name of this object.
  1846. *
  1847. * @return the localized name of the object -- can be <code>null</code> if this
  1848. * object does not have a name
  1849. * @see #setAccessibleName
  1850. */
  1851. public String getAccessibleName() {
  1852. if (accessibleName != null) {
  1853. return accessibleName;
  1854. } else {
  1855. return getTitle();
  1856. }
  1857. }
  1858. /**
  1859. * Get the role of this object.
  1860. *
  1861. * @return an instance of AccessibleRole describing the role of the
  1862. * object
  1863. * @see AccessibleRole
  1864. */
  1865. public AccessibleRole getAccessibleRole() {
  1866. return AccessibleRole.INTERNAL_FRAME;
  1867. }
  1868. /**
  1869. * Gets the AccessibleValue associated with this object. In the
  1870. * implementation of the Java Accessibility API for this class,
  1871. * returns this object, which is responsible for implementing the
  1872. * <code>AccessibleValue</code> interface on behalf of itself.
  1873. *
  1874. * @return this object
  1875. */
  1876. public AccessibleValue getAccessibleValue() {
  1877. return this;
  1878. }
  1879. //
  1880. // AccessibleValue methods
  1881. //
  1882. /**
  1883. * Get the value of this object as a Number.
  1884. *
  1885. * @return value of the object -- can be <code>null</code> if this object does not
  1886. * have a value
  1887. */
  1888. public Number getCurrentAccessibleValue() {
  1889. return new Integer(getLayer());
  1890. }
  1891. /**
  1892. * Set the value of this object as a Number.
  1893. *
  1894. * @return <code>true</code> if the value was set
  1895. */
  1896. public boolean setCurrentAccessibleValue(Number n) {
  1897. if (n instanceof Integer) {
  1898. setLayer((Integer) n);
  1899. return true;
  1900. } else {
  1901. return false;
  1902. }
  1903. }
  1904. /**
  1905. * Get the minimum value of this object as a Number.
  1906. *
  1907. * @return Minimum value of the object; <code>null</code> if this object does not
  1908. * have a minimum value
  1909. */
  1910. public Number getMinimumAccessibleValue() {
  1911. return new Integer(Integer.MIN_VALUE);
  1912. }
  1913. /**
  1914. * Get the maximum value of this object as a Number.
  1915. *
  1916. * @return Maximum value of the object; <code>null</code> if this object does not
  1917. * have a maximum value
  1918. */
  1919. public Number getMaximumAccessibleValue() {
  1920. return new Integer(Integer.MAX_VALUE);
  1921. }
  1922. } // AccessibleJInternalFrame
  1923. /**
  1924. * This component represents an iconified version of a
  1925. * <code>JInternalFrame</code>.
  1926. * This API should NOT BE USED by Swing applications, as it will go
  1927. * away in future versions of Swing as its functionality is moved into
  1928. * <code>JInternalFrame</code>. This class is public only so that
  1929. * UI objects can display a desktop icon. If an application
  1930. * wants to display a desktop icon, it should create a
  1931. * <code>JInternalFrame</code> instance and iconify it.
  1932. * <p>
  1933. * <strong>Warning:</strong>
  1934. * Serialized objects of this class will not be compatible with
  1935. * future Swing releases. The current serialization support is
  1936. * appropriate for short term storage or RMI between applications running
  1937. * the same version of Swing. As of 1.4, support for long term storage
  1938. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  1939. * has been added to the <code>java.beans</code> package.
  1940. * Please see {@link java.beans.XMLEncoder}.
  1941. *
  1942. * @author David Kloba
  1943. */
  1944. static public class JDesktopIcon extends JComponent implements Accessible
  1945. {
  1946. JInternalFrame internalFrame;
  1947. /**
  1948. * Creates an icon for an internal frame.
  1949. *
  1950. * @param f the <code>JInternalFrame</code>
  1951. * for which the icon is created
  1952. */
  1953. public JDesktopIcon(JInternalFrame f) {
  1954. setVisible(false);
  1955. setInternalFrame(f);
  1956. updateUI();
  1957. }
  1958. /**
  1959. * Returns the look-and-feel object that renders this component.
  1960. *
  1961. * @return the <code>DesktopIconUI</code> object that renders
  1962. * this component
  1963. */
  1964. public DesktopIconUI getUI() {
  1965. return (DesktopIconUI)ui;
  1966. }
  1967. /**
  1968. * Sets the look-and-feel object that renders this component.
  1969. *
  1970. * @param ui the <code>DesktopIconUI</code> look-and-feel object
  1971. * @see UIDefaults#getUI
  1972. */
  1973. public void setUI(DesktopIconUI ui) {
  1974. super.setUI(ui);
  1975. }
  1976. /**
  1977. * Returns the <code>JInternalFrame</code> that this
  1978. * <code>DesktopIcon</code> is associated with.
  1979. *
  1980. * @return the <code>JInternalFrame</code> with which this icon
  1981. * is associated
  1982. */
  1983. public JInternalFrame getInternalFrame() {
  1984. return internalFrame;
  1985. }
  1986. /**
  1987. * Sets the <code>JInternalFrame</code> with which this
  1988. * <code>DesktopIcon</code> is associated.
  1989. *
  1990. * @param f the <code>JInternalFrame</code> with which this icon
  1991. * is associated
  1992. */
  1993. public void setInternalFrame(JInternalFrame f) {
  1994. internalFrame = f;
  1995. }
  1996. /**
  1997. * Convenience method to ask the icon for the <code>Desktop</code>
  1998. * object it belongs to.
  1999. *
  2000. * @return the <code>JDesktopPane</code> that contains this
  2001. * icon's internal frame, or <code>null</code> if none found
  2002. */
  2003. public JDesktopPane getDesktopPane() {
  2004. if(getInternalFrame() != null)
  2005. return getInternalFrame().getDesktopPane();
  2006. return null;
  2007. }
  2008. /**
  2009. * Notification from the <code>UIManager</code> that the look and feel
  2010. * has changed.
  2011. * Replaces the current UI object with the latest version from the
  2012. * <code>UIManager</code>.
  2013. *
  2014. * @see JComponent#updateUI
  2015. */
  2016. public void updateUI() {
  2017. boolean hadUI = (ui != null);
  2018. setUI((DesktopIconUI)UIManager.getUI(this));
  2019. invalidate();
  2020. Dimension r = getPreferredSize();
  2021. setSize(r.width, r.height);
  2022. if (internalFrame != null && internalFrame.getUI() != null) { // don't do this if UI not created yet
  2023. SwingUtilities.updateComponentTreeUI(internalFrame);
  2024. }
  2025. }
  2026. /* This method is called if updateUI was called on the associated
  2027. * JInternalFrame. It's necessary to avoid infinite recursion.
  2028. */
  2029. void updateUIWhenHidden() {
  2030. /* Update this UI and any associated internal frame */
  2031. setUI((DesktopIconUI)UIManager.getUI(this));
  2032. Dimension r = getPreferredSize();
  2033. setSize(r.width, r.height);
  2034. invalidate();
  2035. Component[] children = getComponents();
  2036. if (children != null) {
  2037. for(int i = 0; i < children.length; i++) {
  2038. SwingUtilities.updateComponentTreeUI(children[i]);
  2039. }
  2040. }
  2041. }
  2042. /**
  2043. * Returns the name of the look-and-feel
  2044. * class that renders this component.
  2045. *
  2046. * @return the string "DesktopIconUI"
  2047. * @see JComponent#getUIClassID
  2048. * @see UIDefaults#getUI
  2049. */
  2050. public String getUIClassID() {
  2051. return "DesktopIconUI";
  2052. }
  2053. ////////////////
  2054. // Serialization support
  2055. ////////////////
  2056. private void writeObject(ObjectOutputStream s) throws IOException {
  2057. s.defaultWriteObject();
  2058. if (getUIClassID().equals("DesktopIconUI")) {
  2059. byte count = JComponent.getWriteObjCounter(this);
  2060. JComponent.setWriteObjCounter(this, --count);
  2061. if (count == 0 && ui != null) {
  2062. ui.installUI(this);
  2063. }
  2064. }
  2065. }
  2066. /////////////////
  2067. // Accessibility support
  2068. ////////////////
  2069. /**
  2070. * Gets the AccessibleContext associated with this JDesktopIcon.
  2071. * For desktop icons, the AccessibleContext takes the form of an
  2072. * AccessibleJDesktopIcon.
  2073. * A new AccessibleJDesktopIcon instance is created if necessary.
  2074. *
  2075. * @return an AccessibleJDesktopIcon that serves as the
  2076. * AccessibleContext of this JDesktopIcon
  2077. */
  2078. public AccessibleContext getAccessibleContext() {
  2079. if (accessibleContext == null) {
  2080. accessibleContext = new AccessibleJDesktopIcon();
  2081. }
  2082. return accessibleContext;
  2083. }
  2084. /**
  2085. * This class implements accessibility support for the
  2086. * <code>JInternalFrame.JDesktopIcon</code> class. It provides an
  2087. * implementation of the Java Accessibility API appropriate to
  2088. * desktop icon user-interface elements.
  2089. * <p>
  2090. * <strong>Warning:</strong>
  2091. * Serialized objects of this class will not be compatible with
  2092. * future Swing releases. The current serialization support is
  2093. * appropriate for short term storage or RMI between applications running
  2094. * the same version of Swing. As of 1.4, support for long term storage
  2095. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  2096. * has been added to the <code>java.beans</code> package.
  2097. * Please see {@link java.beans.XMLEncoder}.
  2098. */
  2099. protected class AccessibleJDesktopIcon extends AccessibleJComponent
  2100. implements AccessibleValue {
  2101. /**
  2102. * Gets the role of this object.
  2103. *
  2104. * @return an instance of AccessibleRole describing the role of the
  2105. * object
  2106. * @see AccessibleRole
  2107. */
  2108. public AccessibleRole getAccessibleRole() {
  2109. return AccessibleRole.DESKTOP_ICON;
  2110. }
  2111. /**
  2112. * Gets the AccessibleValue associated with this object. In the
  2113. * implementation of the Java Accessibility API for this class,
  2114. * returns this object, which is responsible for implementing the
  2115. * <code>AccessibleValue</code> interface on behalf of itself.
  2116. *
  2117. * @return this object
  2118. */
  2119. public AccessibleValue getAccessibleValue() {
  2120. return this;
  2121. }
  2122. //
  2123. // AccessibleValue methods
  2124. //
  2125. /**
  2126. * Gets the value of this object as a <code>Number</code>.
  2127. *
  2128. * @return value of the object -- can be <code>null</code> if this object does not
  2129. * have a value
  2130. */
  2131. public Number getCurrentAccessibleValue() {
  2132. AccessibleContext a = JDesktopIcon.this.getInternalFrame().getAccessibleContext();
  2133. AccessibleValue v = a.getAccessibleValue();
  2134. if (v != null) {
  2135. return v.getCurrentAccessibleValue();
  2136. } else {
  2137. return null;
  2138. }
  2139. }
  2140. /**
  2141. * Sets the value of this object as a <code>Number</code>.
  2142. *
  2143. * @return <code>true</code> if the value was set
  2144. */
  2145. public boolean setCurrentAccessibleValue(Number n) {
  2146. AccessibleContext a = JDesktopIcon.this.getInternalFrame().getAccessibleContext();
  2147. AccessibleValue v = a.getAccessibleValue();
  2148. if (v != null) {
  2149. return v.setCurrentAccessibleValue(n);
  2150. } else {
  2151. return false;
  2152. }
  2153. }
  2154. /**
  2155. * Gets the minimum value of this object as a <code>Number</code>.
  2156. *
  2157. * @return minimum value of the object; <code>null</code> if this object does not
  2158. * have a minimum value
  2159. */
  2160. public Number getMinimumAccessibleValue() {
  2161. AccessibleContext a = JDesktopIcon.this.getInternalFrame().getAccessibleContext();
  2162. if (a instanceof AccessibleValue) {
  2163. return ((AccessibleValue)a).getMinimumAccessibleValue();
  2164. } else {
  2165. return null;
  2166. }
  2167. }
  2168. /**
  2169. * Gets the maximum value of this object as a <code>Number</code>.
  2170. *
  2171. * @return maximum value of the object; <code>null</code> if this object does not
  2172. * have a maximum value
  2173. */
  2174. public Number getMaximumAccessibleValue() {
  2175. AccessibleContext a = JDesktopIcon.this.getInternalFrame().getAccessibleContext();
  2176. if (a instanceof AccessibleValue) {
  2177. return ((AccessibleValue)a).getMaximumAccessibleValue();
  2178. } else {
  2179. return null;
  2180. }
  2181. }
  2182. } // AccessibleJDesktopIcon
  2183. }
  2184. }