1. /*
  2. * @(#)JDesktopPane.java 1.46 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.util.Vector;
  9. import javax.swing.plaf.*;
  10. import javax.accessibility.*;
  11. import java.awt.Component;
  12. import java.awt.Container;
  13. import java.awt.DefaultFocusTraversalPolicy;
  14. import java.awt.FocusTraversalPolicy;
  15. import java.awt.Window;
  16. import java.io.ObjectOutputStream;
  17. import java.io.ObjectInputStream;
  18. import java.io.IOException;
  19. /**
  20. * A container used to create a multiple-document interface or a virtual desktop.
  21. * You create <code>JInternalFrame</code> objects and add them to the
  22. * <code>JDesktopPane</code>. <code>JDesktopPane</code> extends
  23. * <code>JLayeredPane</code> to manage the potentially overlapping internal
  24. * frames. It also maintains a reference to an instance of
  25. * <code>DesktopManager</code> that is set by the UI
  26. * class for the current look and feel (L&F). Note that <code>JDesktopPane</code>
  27. * does not support borders.
  28. * <p>
  29. * This class is normally used as the parent of <code>JInternalFrames</code>
  30. * to provide a pluggable <code>DesktopManager</code> object to the
  31. * <code>JInternalFrames</code>. The <code>installUI</code> of the
  32. * L&F specific implementation is responsible for setting the
  33. * <code>desktopManager</code> variable appropriately.
  34. * When the parent of a <code>JInternalFrame</code> is a <code>JDesktopPane</code>,
  35. * it should delegate most of its behavior to the <code>desktopManager</code>
  36. * (closing, resizing, etc).
  37. * <p>
  38. * For the keyboard keys used by this component in the standard look and
  39. * feel (L&F) renditions, see the
  40. * <a href="doc-files/Key-Index.html#JDesktopPane"><code>JDesktopPane</code> key assignments</a>.
  41. * For further documentation and examples see
  42. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html">How to Use Internal Frames</a>,
  43. * a section in <em>The Java Tutorial</em>.
  44. * <p>
  45. * <strong>Warning:</strong>
  46. * Serialized objects of this class will not be compatible with
  47. * future Swing releases. The current serialization support is
  48. * appropriate for short term storage or RMI between applications running
  49. * the same version of Swing. As of 1.4, support for long term storage
  50. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  51. * has been added to the <code>java.beans</code> package.
  52. * Please see {@link java.beans.XMLEncoder}.
  53. *
  54. * @see JInternalFrame
  55. * @see JInternalFrame.JDesktopIcon
  56. * @see DesktopManager
  57. *
  58. * @version 1.46 01/23/03
  59. * @author David Kloba
  60. */
  61. public class JDesktopPane extends JLayeredPane implements Accessible
  62. {
  63. /**
  64. * @see #getUIClassID
  65. * @see #readObject
  66. */
  67. private static final String uiClassID = "DesktopPaneUI";
  68. transient DesktopManager desktopManager;
  69. private transient JInternalFrame selectedFrame = null;
  70. /**
  71. * Indicates that the entire contents of the item being dragged
  72. * should appear inside the desktop pane.
  73. *
  74. * @see #OUTLINE_DRAG_MODE
  75. * @see #setDragMode
  76. */
  77. public static int LIVE_DRAG_MODE = 0;
  78. /**
  79. * Indicates that an outline only of the item being dragged
  80. * should appear inside the desktop pane.
  81. *
  82. * @see #LIVE_DRAG_MODE
  83. * @see #setDragMode
  84. */
  85. public static int OUTLINE_DRAG_MODE = 1;
  86. private int dragMode = LIVE_DRAG_MODE;
  87. /**
  88. * Creates a new <code>JDesktopPane</code>.
  89. */
  90. public JDesktopPane() {
  91. setFocusCycleRoot(true);
  92. setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
  93. public Component getDefaultComponent(Container c) {
  94. JInternalFrame jifArray[] = getAllFrames();
  95. Component comp = null;
  96. for (int i = 0; i < jifArray.length; i++) {
  97. comp = jifArray[i].getFocusTraversalPolicy().getDefaultComponent(jifArray[i]);
  98. if (comp != null) {
  99. break;
  100. }
  101. }
  102. return comp;
  103. }
  104. });
  105. updateUI();
  106. }
  107. /**
  108. * Returns the L&F object that renders this component.
  109. *
  110. * @return the <code>DesktopPaneUI</code> object that
  111. * renders this component
  112. */
  113. public DesktopPaneUI getUI() {
  114. return (DesktopPaneUI)ui;
  115. }
  116. /**
  117. * Sets the L&F object that renders this component.
  118. *
  119. * @param ui the DesktopPaneUI L&F object
  120. * @see UIDefaults#getUI
  121. * @beaninfo
  122. * bound: true
  123. * hidden: true
  124. * attribute: visualUpdate true
  125. * description: The UI object that implements the Component's LookAndFeel.
  126. */
  127. public void setUI(DesktopPaneUI ui) {
  128. super.setUI(ui);
  129. }
  130. /**
  131. * Sets the "dragging style" used by the desktop pane.
  132. * You may want to change to one mode or another for
  133. * performance or aesthetic reasons.
  134. *
  135. * @param dragMode the style of drag to use for items in the Desktop
  136. *
  137. * @see #LIVE_DRAG_MODE
  138. * @see #OUTLINE_DRAG_MODE
  139. *
  140. * @beaninfo
  141. * bound: true
  142. * description: Dragging style for internal frame children.
  143. * enum: LIVE_DRAG_MODE JDesktopPane.LIVE_DRAG_MODE
  144. * OUTLINE_DRAG_MODE JDesktopPane.OUTLINE_DRAG_MODE
  145. */
  146. public void setDragMode(int dragMode) {
  147. /* if (!(dragMode == LIVE_DRAG_MODE || dragMode == OUTLINE_DRAG_MODE)) {
  148. throw new IllegalArgumentException("Not a valid drag mode");
  149. }*/
  150. firePropertyChange("dragMode", this.dragMode, dragMode);
  151. this.dragMode = dragMode;
  152. }
  153. /**
  154. * Gets the current "dragging style" used by the desktop pane.
  155. * @return either <code>Live_DRAG_MODE</code> or
  156. * <code>OUTLINE_DRAG_MODE</code>
  157. * @see #setDragMode
  158. */
  159. public int getDragMode() {
  160. return dragMode;
  161. }
  162. /**
  163. * Returns the <code>DesktopManger</code> that handles
  164. * desktop-specific UI actions.
  165. */
  166. public DesktopManager getDesktopManager() {
  167. return desktopManager;
  168. }
  169. /**
  170. * Sets the <code>DesktopManger</code> that will handle
  171. * desktop-specific UI actions.
  172. *
  173. * @param d the <code>DesktopManager</code> to use
  174. */
  175. public void setDesktopManager(DesktopManager d) {
  176. desktopManager = d;
  177. }
  178. /**
  179. * Notification from the <code>UIManager</code> that the L&F has changed.
  180. * Replaces the current UI object with the latest version from the
  181. * <code>UIManager</code>.
  182. *
  183. * @see JComponent#updateUI
  184. */
  185. public void updateUI() {
  186. setUI((DesktopPaneUI)UIManager.getUI(this));
  187. }
  188. /**
  189. * Returns the name of the L&F class that renders this component.
  190. *
  191. * @return the string "DesktopPaneUI"
  192. * @see JComponent#getUIClassID
  193. * @see UIDefaults#getUI
  194. */
  195. public String getUIClassID() {
  196. return uiClassID;
  197. }
  198. /**
  199. * Returns all <code>JInternalFrames</code> currently displayed in the
  200. * desktop. Returns iconified frames as well as expanded frames.
  201. *
  202. * @return an array of <code>JInternalFrame</code> objects
  203. */
  204. public JInternalFrame[] getAllFrames() {
  205. int i, count;
  206. JInternalFrame[] results;
  207. Vector vResults = new Vector(10);
  208. Object next, tmp;
  209. count = getComponentCount();
  210. for(i = 0; i < count; i++) {
  211. next = getComponent(i);
  212. if(next instanceof JInternalFrame)
  213. vResults.addElement(next);
  214. else if(next instanceof JInternalFrame.JDesktopIcon) {
  215. tmp = ((JInternalFrame.JDesktopIcon)next).getInternalFrame();
  216. if(tmp != null)
  217. vResults.addElement(tmp);
  218. }
  219. }
  220. results = new JInternalFrame[vResults.size()];
  221. vResults.copyInto(results);
  222. return results;
  223. }
  224. /** Returns the currently active <code>JInternalFrame</code>
  225. * in this <code>JDesktopPane</code>, or <code>null</code>
  226. * if no <code>JInternalFrame</code> is currently active.
  227. *
  228. * @return the currently active <code>JInternalFrame</code> or
  229. * <code>null</code>
  230. * @since 1.3
  231. */
  232. public JInternalFrame getSelectedFrame() {
  233. return selectedFrame;
  234. }
  235. /** Sets the currently active <code>JInternalFrame</code>
  236. * in this <code>JDesktopPane</code>.
  237. *
  238. * @param f the internal frame that's currently selected
  239. * @since 1.3
  240. */
  241. public void setSelectedFrame(JInternalFrame f) {
  242. selectedFrame = f;
  243. }
  244. /**
  245. * Returns all <code>JInternalFrames</code> currently displayed in the
  246. * specified layer of the desktop. Returns iconified frames as well
  247. * expanded frames.
  248. *
  249. * @param layer an int specifying the desktop layer
  250. * @return an array of <code>JInternalFrame</code> objects
  251. * @see JLayeredPane
  252. */
  253. public JInternalFrame[] getAllFramesInLayer(int layer) {
  254. int i, count;
  255. JInternalFrame[] results;
  256. Vector vResults = new Vector(10);
  257. Object next, tmp;
  258. count = getComponentCount();
  259. for(i = 0; i < count; i++) {
  260. next = getComponent(i);
  261. if(next instanceof JInternalFrame) {
  262. if(((JInternalFrame)next).getLayer() == layer)
  263. vResults.addElement(next);
  264. } else if(next instanceof JInternalFrame.JDesktopIcon) {
  265. tmp = ((JInternalFrame.JDesktopIcon)next).getInternalFrame();
  266. if(tmp != null && ((JInternalFrame)tmp).getLayer() == layer)
  267. vResults.addElement(tmp);
  268. }
  269. }
  270. results = new JInternalFrame[vResults.size()];
  271. vResults.copyInto(results);
  272. return results;
  273. }
  274. /**
  275. * Returns true to indicate that this component paints every pixel
  276. * in its range. (In other words, it does not have a transparent
  277. * background or foreground.)
  278. *
  279. * @return true
  280. * @see JComponent#isOpaque
  281. */
  282. public boolean isOpaque() {
  283. return true;
  284. }
  285. /**
  286. * See readObject() and writeObject() in JComponent for more
  287. * information about serialization in Swing.
  288. */
  289. private void writeObject(ObjectOutputStream s) throws IOException {
  290. s.defaultWriteObject();
  291. if (getUIClassID().equals(uiClassID)) {
  292. byte count = JComponent.getWriteObjCounter(this);
  293. JComponent.setWriteObjCounter(this, --count);
  294. if (count == 0 && ui != null) {
  295. ui.installUI(this);
  296. }
  297. }
  298. }
  299. /**
  300. * Returns a string representation of this <code>JDesktopPane</code>.
  301. * This method is intended to be used only for debugging purposes, and the
  302. * content and format of the returned string may vary between
  303. * implementations. The returned string may be empty but may not
  304. * be <code>null</code>.
  305. *
  306. * @return a string representation of this <code>JDesktopPane</code>
  307. */
  308. protected String paramString() {
  309. String desktopManagerString = (desktopManager != null ?
  310. desktopManager.toString() : "");
  311. return super.paramString() +
  312. ",desktopManager=" + desktopManagerString;
  313. }
  314. /////////////////
  315. // Accessibility support
  316. ////////////////
  317. /**
  318. * Gets the <code>AccessibleContext</code> associated with this
  319. * <code>JDesktopPane</code>. For desktop panes, the
  320. * <code>AccessibleContext</code> takes the form of an
  321. * <code>AccessibleJDesktopPane</code>.
  322. * A new <code>AccessibleJDesktopPane</code> instance is created if necessary.
  323. *
  324. * @return an <code>AccessibleJDesktopPane</code> that serves as the
  325. * <code>AccessibleContext</code> of this <code>JDesktopPane</code>
  326. */
  327. public AccessibleContext getAccessibleContext() {
  328. if (accessibleContext == null) {
  329. accessibleContext = new AccessibleJDesktopPane();
  330. }
  331. return accessibleContext;
  332. }
  333. /**
  334. * This class implements accessibility support for the
  335. * <code>JDesktopPane</code> class. It provides an implementation of the
  336. * Java Accessibility API appropriate to desktop pane user-interface
  337. * elements.
  338. * <p>
  339. * <strong>Warning:</strong>
  340. * Serialized objects of this class will not be compatible with
  341. * future Swing releases. The current serialization support is
  342. * appropriate for short term storage or RMI between applications running
  343. * the same version of Swing. As of 1.4, support for long term storage
  344. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  345. * has been added to the <code>java.beans</code> package.
  346. * Please see {@link java.beans.XMLEncoder}.
  347. */
  348. protected class AccessibleJDesktopPane extends AccessibleJComponent {
  349. /**
  350. * Get the role of this object.
  351. *
  352. * @return an instance of AccessibleRole describing the role of the
  353. * object
  354. * @see AccessibleRole
  355. */
  356. public AccessibleRole getAccessibleRole() {
  357. return AccessibleRole.DESKTOP_PANE;
  358. }
  359. }
  360. }