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