KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JDesktopPane


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
8 package javax.swing;
9
10 import java.util.Vector JavaDoc;
11 import javax.swing.plaf.*;
12 import javax.accessibility.*;
13
14 import java.awt.Component JavaDoc;
15 import java.awt.Container JavaDoc;
16 import java.awt.DefaultFocusTraversalPolicy JavaDoc;
17 import java.awt.FocusTraversalPolicy JavaDoc;
18 import java.awt.Window JavaDoc;
19 import java.io.ObjectOutputStream JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 /**
24  * A container used to create a multiple-document interface or a virtual desktop.
25  * You create <code>JInternalFrame</code> objects and add them to the
26  * <code>JDesktopPane</code>. <code>JDesktopPane</code> extends
27  * <code>JLayeredPane</code> to manage the potentially overlapping internal
28  * frames. It also maintains a reference to an instance of
29  * <code>DesktopManager</code> that is set by the UI
30  * class for the current look and feel (L&F). Note that <code>JDesktopPane</code>
31  * does not support borders.
32  * <p>
33  * This class is normally used as the parent of <code>JInternalFrames</code>
34  * to provide a pluggable <code>DesktopManager</code> object to the
35  * <code>JInternalFrames</code>. The <code>installUI</code> of the
36  * L&F specific implementation is responsible for setting the
37  * <code>desktopManager</code> variable appropriately.
38  * When the parent of a <code>JInternalFrame</code> is a <code>JDesktopPane</code>,
39  * it should delegate most of its behavior to the <code>desktopManager</code>
40  * (closing, resizing, etc).
41  * <p>
42  * For further documentation and examples see
43  * <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html">How to Use Internal Frames</a>,
44  * a section in <em>The Java Tutorial</em>.
45  * <p>
46  * <strong>Warning:</strong>
47  * Serialized objects of this class will not be compatible with
48  * future Swing releases. The current serialization support is
49  * appropriate for short term storage or RMI between applications running
50  * the same version of Swing. As of 1.4, support for long term storage
51  * of all JavaBeans<sup><font size="-2">TM</font></sup>
52  * has been added to the <code>java.beans</code> package.
53  * Please see {@link java.beans.XMLEncoder}.
54  *
55  * @see JInternalFrame
56  * @see JInternalFrame.JDesktopIcon
57  * @see DesktopManager
58  *
59  * @version 1.51 12/19/03
60  * @author David Kloba
61  */

62 public class JDesktopPane extends JLayeredPane JavaDoc implements Accessible
63 {
64     /**
65      * @see #getUIClassID
66      * @see #readObject
67      */

68     private static final String JavaDoc uiClassID = "DesktopPaneUI";
69
70     transient DesktopManager JavaDoc desktopManager;
71
72     private transient JInternalFrame JavaDoc selectedFrame = null;
73
74     /**
75       * Indicates that the entire contents of the item being dragged
76       * should appear inside the desktop pane.
77       *
78       * @see #OUTLINE_DRAG_MODE
79       * @see #setDragMode
80       */

81     public static final int LIVE_DRAG_MODE = 0;
82
83     /**
84       * Indicates that an outline only of the item being dragged
85       * should appear inside the desktop pane.
86       *
87       * @see #LIVE_DRAG_MODE
88       * @see #setDragMode
89       */

90     public static final int OUTLINE_DRAG_MODE = 1;
91
92     private int dragMode = LIVE_DRAG_MODE;
93     private boolean dragModeSet = false;
94
95     /**
96      * Creates a new <code>JDesktopPane</code>.
97      */

98     public JDesktopPane() {
99         setFocusCycleRoot(true);
100
101         setFocusTraversalPolicy(new LayoutFocusTraversalPolicy JavaDoc() {
102             public Component JavaDoc getDefaultComponent(Container JavaDoc c) {
103                 JInternalFrame JavaDoc jifArray[] = getAllFrames();
104                 Component JavaDoc comp = null;
105                 for (int i = 0; i < jifArray.length; i++) {
106                     comp = jifArray[i].getFocusTraversalPolicy().getDefaultComponent(jifArray[i]);
107                     if (comp != null) {
108                         break;
109                     }
110                 }
111                 return comp;
112             }
113         });
114         updateUI();
115     }
116
117     /**
118      * Returns the L&F object that renders this component.
119      *
120      * @return the <code>DesktopPaneUI</code> object that
121      * renders this component
122      */

123     public DesktopPaneUI getUI() {
124         return (DesktopPaneUI)ui;
125     }
126
127     /**
128      * Sets the L&F object that renders this component.
129      *
130      * @param ui the DesktopPaneUI L&F object
131      * @see UIDefaults#getUI
132      * @beaninfo
133      * bound: true
134      * hidden: true
135      * attribute: visualUpdate true
136      * description: The UI object that implements the Component's LookAndFeel.
137      */

138     public void setUI(DesktopPaneUI ui) {
139         super.setUI(ui);
140     }
141
142     /**
143      * Sets the "dragging style" used by the desktop pane.
144      * You may want to change to one mode or another for
145      * performance or aesthetic reasons.
146      *
147      * @param dragMode the style of drag to use for items in the Desktop
148      *
149      * @see #LIVE_DRAG_MODE
150      * @see #OUTLINE_DRAG_MODE
151      *
152      * @beaninfo
153      * bound: true
154      * description: Dragging style for internal frame children.
155      * enum: LIVE_DRAG_MODE JDesktopPane.LIVE_DRAG_MODE
156      * OUTLINE_DRAG_MODE JDesktopPane.OUTLINE_DRAG_MODE
157      */

158     public void setDragMode(int dragMode) {
159         int oldDragMode = this.dragMode;
160         this.dragMode = dragMode;
161         firePropertyChange("dragMode", oldDragMode, this.dragMode);
162     dragModeSet = true;
163      }
164
165     /**
166      * Gets the current "dragging style" used by the desktop pane.
167      * @return either <code>Live_DRAG_MODE</code> or
168      * <code>OUTLINE_DRAG_MODE</code>
169      * @see #setDragMode
170      */

171      public int getDragMode() {
172          return dragMode;
173      }
174
175     /**
176      * Returns the <code>DesktopManger</code> that handles
177      * desktop-specific UI actions.
178      */

179     public DesktopManager JavaDoc getDesktopManager() {
180         return desktopManager;
181     }
182
183     /**
184      * Sets the <code>DesktopManger</code> that will handle
185      * desktop-specific UI actions.
186      *
187      * @param d the <code>DesktopManager</code> to use
188      *
189      * @beaninfo
190      * bound: true
191      * description: Desktop manager to handle the internal frames in the
192      * desktop pane.
193      */

194     public void setDesktopManager(DesktopManager JavaDoc d) {
195         DesktopManager JavaDoc oldValue = desktopManager;
196         desktopManager = d;
197         firePropertyChange("desktopManager", oldValue, desktopManager);
198     }
199
200     /**
201      * Notification from the <code>UIManager</code> that the L&F has changed.
202      * Replaces the current UI object with the latest version from the
203      * <code>UIManager</code>.
204      *
205      * @see JComponent#updateUI
206      */

207     public void updateUI() {
208         setUI((DesktopPaneUI)UIManager.getUI(this));
209     }
210
211
212     /**
213      * Returns the name of the L&F class that renders this component.
214      *
215      * @return the string "DesktopPaneUI"
216      * @see JComponent#getUIClassID
217      * @see UIDefaults#getUI
218      */

219     public String JavaDoc getUIClassID() {
220         return uiClassID;
221     }
222
223     /**
224      * Returns all <code>JInternalFrames</code> currently displayed in the
225      * desktop. Returns iconified frames as well as expanded frames.
226      *
227      * @return an array of <code>JInternalFrame</code> objects
228      */

229     public JInternalFrame JavaDoc[] getAllFrames() {
230         int i, count;
231         JInternalFrame JavaDoc[] results;
232         Vector JavaDoc vResults = new Vector JavaDoc(10);
233         Object JavaDoc next, tmp;
234
235         count = getComponentCount();
236         for(i = 0; i < count; i++) {
237             next = getComponent(i);
238             if(next instanceof JInternalFrame JavaDoc)
239                 vResults.addElement(next);
240             else if(next instanceof JInternalFrame.JDesktopIcon JavaDoc) {
241                 tmp = ((JInternalFrame.JDesktopIcon JavaDoc)next).getInternalFrame();
242                 if(tmp != null)
243                     vResults.addElement(tmp);
244             }
245         }
246
247         results = new JInternalFrame JavaDoc[vResults.size()];
248         vResults.copyInto(results);
249
250         return results;
251     }
252
253     /** Returns the currently active <code>JInternalFrame</code>
254       * in this <code>JDesktopPane</code>, or <code>null</code>
255       * if no <code>JInternalFrame</code> is currently active.
256       *
257       * @return the currently active <code>JInternalFrame</code> or
258       * <code>null</code>
259       * @since 1.3
260       */

261
262     public JInternalFrame JavaDoc getSelectedFrame() {
263       return selectedFrame;
264     }
265
266     /** Sets the currently active <code>JInternalFrame</code>
267      * in this <code>JDesktopPane</code>.
268      *
269      * @param f the internal frame that's currently selected
270      * @since 1.3
271      */

272
273     public void setSelectedFrame(JInternalFrame JavaDoc f) {
274       selectedFrame = f;
275     }
276
277     /**
278      * Returns all <code>JInternalFrames</code> currently displayed in the
279      * specified layer of the desktop. Returns iconified frames as well
280      * expanded frames.
281      *
282      * @param layer an int specifying the desktop layer
283      * @return an array of <code>JInternalFrame</code> objects
284      * @see JLayeredPane
285      */

286     public JInternalFrame JavaDoc[] getAllFramesInLayer(int layer) {
287         int i, count;
288         JInternalFrame JavaDoc[] results;
289         Vector JavaDoc vResults = new Vector JavaDoc(10);
290         Object JavaDoc next, tmp;
291
292         count = getComponentCount();
293         for(i = 0; i < count; i++) {
294             next = getComponent(i);
295             if(next instanceof JInternalFrame JavaDoc) {
296                 if(((JInternalFrame JavaDoc)next).getLayer() == layer)
297                     vResults.addElement(next);
298             } else if(next instanceof JInternalFrame.JDesktopIcon JavaDoc) {
299                 tmp = ((JInternalFrame.JDesktopIcon JavaDoc)next).getInternalFrame();
300                 if(tmp != null && ((JInternalFrame JavaDoc)tmp).getLayer() == layer)
301                     vResults.addElement(tmp);
302             }
303         }
304
305         results = new JInternalFrame JavaDoc[vResults.size()];
306         vResults.copyInto(results);
307
308         return results;
309     }
310
311
312     /**
313      * Returns true to indicate that this component paints every pixel
314      * in its range. (In other words, it does not have a transparent
315      * background or foreground.)
316      *
317      * @return true
318      * @see JComponent#isOpaque
319      */

320     public boolean isOpaque() {
321         return true;
322     }
323
324
325     /**
326      * See readObject() and writeObject() in JComponent for more
327      * information about serialization in Swing.
328      */

329     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
330         s.defaultWriteObject();
331         if (getUIClassID().equals(uiClassID)) {
332             byte count = JComponent.getWriteObjCounter(this);
333             JComponent.setWriteObjCounter(this, --count);
334             if (count == 0 && ui != null) {
335                 ui.installUI(this);
336             }
337         }
338     }
339
340     void setUIProperty(String JavaDoc propertyName, Object JavaDoc value) {
341         if (propertyName == "dragMode") {
342         if (!dragModeSet) {
343         setDragMode(((Integer JavaDoc)value).intValue());
344         dragModeSet = false;
345         }
346     } else {
347         super.setUIProperty(propertyName, value);
348     }
349     }
350
351     /**
352      * Returns a string representation of this <code>JDesktopPane</code>.
353      * This method is intended to be used only for debugging purposes, and the
354      * content and format of the returned string may vary between
355      * implementations. The returned string may be empty but may not
356      * be <code>null</code>.
357      *
358      * @return a string representation of this <code>JDesktopPane</code>
359      */

360     protected String JavaDoc paramString() {
361     String JavaDoc desktopManagerString = (desktopManager != null ?
362                        desktopManager.toString() : "");
363
364     return super.paramString() +
365     ",desktopManager=" + desktopManagerString;
366     }
367
368 /////////////////
369
// Accessibility support
370
////////////////
371

372     /**
373      * Gets the <code>AccessibleContext</code> associated with this
374      * <code>JDesktopPane</code>. For desktop panes, the
375      * <code>AccessibleContext</code> takes the form of an
376      * <code>AccessibleJDesktopPane</code>.
377      * A new <code>AccessibleJDesktopPane</code> instance is created if necessary.
378      *
379      * @return an <code>AccessibleJDesktopPane</code> that serves as the
380      * <code>AccessibleContext</code> of this <code>JDesktopPane</code>
381      */

382     public AccessibleContext getAccessibleContext() {
383         if (accessibleContext == null) {
384             accessibleContext = new AccessibleJDesktopPane();
385         }
386         return accessibleContext;
387     }
388
389     /**
390      * This class implements accessibility support for the
391      * <code>JDesktopPane</code> class. It provides an implementation of the
392      * Java Accessibility API appropriate to desktop pane user-interface
393      * elements.
394      * <p>
395      * <strong>Warning:</strong>
396      * Serialized objects of this class will not be compatible with
397      * future Swing releases. The current serialization support is
398      * appropriate for short term storage or RMI between applications running
399      * the same version of Swing. As of 1.4, support for long term storage
400      * of all JavaBeans<sup><font size="-2">TM</font></sup>
401      * has been added to the <code>java.beans</code> package.
402      * Please see {@link java.beans.XMLEncoder}.
403      */

404     protected class AccessibleJDesktopPane extends AccessibleJComponent {
405
406         /**
407          * Get the role of this object.
408          *
409          * @return an instance of AccessibleRole describing the role of the
410          * object
411          * @see AccessibleRole
412          */

413         public AccessibleRole getAccessibleRole() {
414             return AccessibleRole.DESKTOP_PANE;
415         }
416     }
417 }
418
419
Popular Tags