1. /*
  2. * @(#)JDesktopPane.java 1.27 01/11/29
  3. *
  4. * Copyright 2002 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.io.ObjectOutputStream;
  12. import java.io.ObjectInputStream;
  13. import java.io.IOException;
  14. /**
  15. * A container used to create a multiple-document interface or a virtual desktop.
  16. * You create JInternalFrame objects and add them to the JDesktopPane.
  17. * JDesktopPane extends JLayeredPane to manage the potentially overlapping internal frames. It also
  18. * maintains a reference to an instance of DesktopManager that is set by the UI
  19. * class for the current Look and Feel (L&F).
  20. * <p>
  21. * This class is normally used as the parent of JInternalFrames to provide a
  22. * pluggable DesktopManager object to the JInternalFrames. The installUI of the
  23. * L&F specific implementation is responsible for setting the desktopManager
  24. * variable appropriately. When the parent of a JInternalFrame is a JDesktopPane,
  25. * it should delegate most of its behavior to the desktopManager (closing, resizing,
  26. * etc).
  27. * <p>
  28. * For the keyboard keys used by this component in the standard Look and
  29. * Feel (L&F) renditions, see the
  30. * <a href="doc-files/Key-Index.html#JDesktopPane">JDesktopPane</a> key assignments.
  31. * <p>
  32. * <strong>Warning:</strong>
  33. * Serialized objects of this class will not be compatible with
  34. * future Swing releases. The current serialization support is appropriate
  35. * for short term storage or RMI between applications running the same
  36. * version of Swing. A future release of Swing will provide support for
  37. * long term persistence.
  38. *
  39. * @see JInternalFrame
  40. * @see JInternalFrame.JDesktopIcon
  41. * @see DesktopManager
  42. *
  43. * @version 1.27 11/29/01
  44. * @author David Kloba
  45. */
  46. public class JDesktopPane extends JLayeredPane implements Accessible
  47. {
  48. /**
  49. * @see #getUIClassID
  50. * @see #readObject
  51. */
  52. private static final String uiClassID = "DesktopPaneUI";
  53. transient DesktopManager desktopManager;
  54. /**
  55. * Creates a new JDesktopPane.
  56. */
  57. public JDesktopPane() {
  58. updateUI();
  59. }
  60. /**
  61. * Returns the L&F object that renders this component.
  62. *
  63. * @return the DesktopPaneUI object that renders this component
  64. */
  65. public DesktopPaneUI getUI() {
  66. return (DesktopPaneUI)ui;
  67. }
  68. /**
  69. * Sets the L&F object that renders this component.
  70. *
  71. * @param ui the DesktopPaneUI L&F object
  72. * @see UIDefaults#getUI
  73. */
  74. public void setUI(DesktopPaneUI ui) {
  75. super.setUI(ui);
  76. }
  77. /**
  78. * Returns the DesktopManger that handles desktop-specific UI actions.
  79. *
  80. * @param d the DesktopManager currently in use
  81. */
  82. public DesktopManager getDesktopManager() {
  83. return desktopManager;
  84. }
  85. /**
  86. * Sets the DesktopManger that will handle desktop-specific UI actions.
  87. *
  88. * @param d the DesktopManager to use
  89. */
  90. public void setDesktopManager(DesktopManager d) {
  91. desktopManager = d;
  92. }
  93. /**
  94. * Notification from the UIManager that the L&F has changed.
  95. * Replaces the current UI object with the latest version from the
  96. * UIManager.
  97. *
  98. * @see JComponent#updateUI
  99. */
  100. public void updateUI() {
  101. setUI((DesktopPaneUI)UIManager.getUI(this));
  102. }
  103. /**
  104. * Returns the name of the L&F class that renders this component.
  105. *
  106. * @return "DesktopPaneUI"
  107. * @see JComponent#getUIClassID
  108. * @see UIDefaults#getUI
  109. */
  110. public String getUIClassID() {
  111. return uiClassID;
  112. }
  113. /**
  114. * Returns all JInternalFrames currently displayed in the
  115. * desktop. Returns iconified frames as well as expanded frames.
  116. *
  117. * @return an array of JInternalFrame objects
  118. */
  119. public JInternalFrame[] getAllFrames() {
  120. int i, count;
  121. JInternalFrame[] results;
  122. Vector vResults = new Vector(10);
  123. Object next, tmp;
  124. count = getComponentCount();
  125. for(i = 0; i < count; i++) {
  126. next = getComponent(i);
  127. if(next instanceof JInternalFrame)
  128. vResults.addElement(next);
  129. else if(next instanceof JInternalFrame.JDesktopIcon) {
  130. tmp = ((JInternalFrame.JDesktopIcon)next).getInternalFrame();
  131. if(tmp != null)
  132. vResults.addElement(tmp);
  133. }
  134. }
  135. results = new JInternalFrame[vResults.size()];
  136. vResults.copyInto(results);
  137. return results;
  138. }
  139. /**
  140. * Returns all JInternalFrames currently displayed in the
  141. * specified layer of the desktop. Returns iconified frames as well
  142. * expanded frames.
  143. *
  144. * @param layer an int specifying the desktop layer
  145. * @return an array of JInternalFrame objects
  146. * @see JLayeredPane
  147. */
  148. public JInternalFrame[] getAllFramesInLayer(int layer) {
  149. int i, count;
  150. JInternalFrame[] results;
  151. Vector vResults = new Vector(10);
  152. Object next, tmp;
  153. count = getComponentCount();
  154. for(i = 0; i < count; i++) {
  155. next = getComponent(i);
  156. if(next instanceof JInternalFrame)
  157. if(((JInternalFrame)next).getLayer() == layer)
  158. vResults.addElement(next);
  159. else if(next instanceof JInternalFrame.JDesktopIcon) {
  160. tmp = ((JInternalFrame.JDesktopIcon)next).getInternalFrame();
  161. if(tmp != null && ((JInternalFrame)tmp).getLayer() == layer)
  162. vResults.addElement(tmp);
  163. }
  164. }
  165. results = new JInternalFrame[vResults.size()];
  166. vResults.copyInto(results);
  167. return results;
  168. }
  169. /**
  170. * Returns true to indicate that this component paints every pixel
  171. * in its range. (In other words, it does not have a transparent
  172. * background or foreground.)
  173. *
  174. * @return true
  175. * @see JComponent#isOpaque
  176. */
  177. public boolean isOpaque() {
  178. return true;
  179. }
  180. /**
  181. * See readObject() and writeObject() in JComponent for more
  182. * information about serialization in Swing.
  183. */
  184. private void writeObject(ObjectOutputStream s) throws IOException {
  185. s.defaultWriteObject();
  186. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  187. ui.installUI(this);
  188. }
  189. }
  190. /**
  191. * Returns a string representation of this JDesktopPane. This method
  192. * is intended to be used only for debugging purposes, and the
  193. * content and format of the returned string may vary between
  194. * implementations. The returned string may be empty but may not
  195. * be <code>null</code>.
  196. *
  197. * @return a string representation of this JDesktopPane.
  198. */
  199. protected String paramString() {
  200. String desktopManagerString = (desktopManager != null ?
  201. desktopManager.toString() : "");
  202. return super.paramString() +
  203. ",desktopManager=" + desktopManagerString;
  204. }
  205. /////////////////
  206. // Accessibility support
  207. ////////////////
  208. /**
  209. * Get the AccessibleContext associated with this JComponent
  210. *
  211. * @return the AccessibleContext of this JComponent
  212. */
  213. public AccessibleContext getAccessibleContext() {
  214. if (accessibleContext == null) {
  215. accessibleContext = new AccessibleJDesktopPane();
  216. }
  217. return accessibleContext;
  218. }
  219. /**
  220. * The class used to obtain the accessible role for this object.
  221. * <p>
  222. * <strong>Warning:</strong>
  223. * Serialized objects of this class will not be compatible with
  224. * future Swing releases. The current serialization support is appropriate
  225. * for short term storage or RMI between applications running the same
  226. * version of Swing. A future release of Swing will provide support for
  227. * long term persistence.
  228. */
  229. protected class AccessibleJDesktopPane extends AccessibleJComponent {
  230. /**
  231. * Get the role of this object.
  232. *
  233. * @return an instance of AccessibleRole describing the role of the
  234. * object
  235. * @see AccessibleRole
  236. */
  237. public AccessibleRole getAccessibleRole() {
  238. return AccessibleRole.DESKTOP_PANE;
  239. }
  240. }
  241. }