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