OSDN Git Service

2005-04-19 Roman Kennke <roman@kennke.org>
[pf3gnuchains/gcc-fork.git] / libjava / javax / swing / JDesktopPane.java
1 /* JDesktopPane.java --
2    Copyright (C) 2002, 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package javax.swing;
40
41 import java.awt.Component;
42 import java.beans.PropertyVetoException;
43
44 import javax.accessibility.Accessible;
45 import javax.accessibility.AccessibleContext;
46 import javax.accessibility.AccessibleRole;
47 import javax.swing.plaf.DesktopPaneUI;
48
49 /**
50  * JDesktopPane is a container (usually for JInternalFrames) that simulates a
51  * desktop. Typically, the user will create JInternalFrames and place thme in
52  * a JDesktopPane. The user can then interact with JInternalFrames like they
53  * usually would with JFrames. The actions (minimize, maximize, close, etc)
54  * are done by using a DesktopManager that is associated with the
55  * JDesktopPane.
56  */
57 public class JDesktopPane extends JLayeredPane implements Accessible
58 {
59   /** DOCUMENT ME! */
60   private static final long serialVersionUID = 766333777224038726L;
61
62   /**
63    * This specifies that when dragged, a JInternalFrame should be completely
64    * visible.
65    *
66    * @specnote final since 1.5.0.
67    */
68   public static final int LIVE_DRAG_MODE = 0;
69
70   /**
71    * This specifies that when dragged, a JInternalFrame should only be visible
72    * as an outline.
73    *
74    * @specnote final since 1.5.0.
75    */
76   public static final int OUTLINE_DRAG_MODE = 1;
77
78   /** The selected frame in the JDesktopPane. */
79   private transient JInternalFrame selectedFrame;
80
81   /** The JDesktopManager to use for acting on JInternalFrames. */
82   transient DesktopManager desktopManager;
83
84   /** The drag mode used by the JDesktopPane. */
85   private transient int dragMode = LIVE_DRAG_MODE;
86
87   /**
88    * AccessibleJDesktopPane
89    */
90   protected class AccessibleJDesktopPane extends AccessibleJComponent
91   {
92     /** DOCUMENT ME! */
93     private static final long serialVersionUID = 6079388927946077570L;
94
95     /**
96      * Constructor AccessibleJDesktopPane
97      */
98     protected AccessibleJDesktopPane()
99     {
100     }
101
102     /**
103      * getAccessibleRole
104      *
105      * @return AccessibleRole
106      */
107     public AccessibleRole getAccessibleRole()
108     {
109       return AccessibleRole.DESKTOP_PANE;
110     }
111   }
112
113   /**
114    * Creates a new JDesktopPane object.
115    */
116   public JDesktopPane()
117   {
118     setLayout(null);
119     updateUI();
120   }
121
122   /**
123    * This method returns the UI used with the JDesktopPane.
124    *
125    * @return The UI used with the JDesktopPane.
126    */
127   public DesktopPaneUI getUI()
128   {
129     return (DesktopPaneUI) ui;
130   }
131
132   /**
133    * This method sets the UI used with the JDesktopPane.
134    *
135    * @param ui The UI to use with the JDesktopPane.
136    */
137   public void setUI(DesktopPaneUI ui)
138   {
139     super.setUI(ui);
140   }
141
142   /**
143    * This method sets the drag mode to use with the JDesktopPane.
144    *
145    * @param mode The drag mode to use.
146    *
147    * @throws IllegalArgumentException If the drag mode given is not
148    *         LIVE_DRAG_MODE or OUTLINE_DRAG_MODE.
149    */
150   public void setDragMode(int mode)
151   {
152     if ((mode != LIVE_DRAG_MODE) && (mode != OUTLINE_DRAG_MODE))
153       throw new IllegalArgumentException("Drag mode not valid.");
154
155     // FIXME: Unsupported mode.
156     if (mode == OUTLINE_DRAG_MODE)
157       {
158         // throw new IllegalArgumentException("Outline drag modes are unsupported.");
159         mode = LIVE_DRAG_MODE;
160       }
161
162     dragMode = mode;
163   }
164
165   /**
166    * This method returns the drag mode used with the JDesktopPane.
167    *
168    * @return The drag mode used with the JDesktopPane.
169    */
170   public int getDragMode()
171   {
172     return dragMode;
173   }
174
175   /**
176    * This method returns the DesktopManager used with the JDesktopPane.
177    *
178    * @return The DesktopManager to use with the JDesktopPane.
179    */
180   public DesktopManager getDesktopManager()
181   {
182     return desktopManager;
183   }
184
185   /**
186    * This method sets the DesktopManager to use with the JDesktopPane.
187    *
188    * @param manager The DesktopManager to use with the JDesktopPane.
189    */
190   public void setDesktopManager(DesktopManager manager)
191   {
192     desktopManager = manager;
193   }
194
195   /**
196    * This method restores the UI used with the JDesktopPane to the default.
197    */
198   public void updateUI()
199   {
200     setUI((DesktopPaneUI) UIManager.getUI(this));
201     invalidate();
202   }
203
204   /**
205    * This method returns a String identifier that allows the UIManager to know
206    * which class will act as JDesktopPane's UI.
207    *
208    * @return A String identifier for the UI class to use.
209    */
210   public String getUIClassID()
211   {
212     return "DesktopPaneUI";
213   }
214
215   /**
216    * This method returns all JInternalFrames that are in the JDesktopPane.
217    *
218    * @return All JInternalFrames that are in the JDesktopPane.
219    */
220   public JInternalFrame[] getAllFrames()
221   {
222     return getFramesFromComponents(getComponents());
223   }
224
225   /**
226    * This method returns the currently selected frame in the JDesktopPane.
227    *
228    * @return The currently selected frame in the JDesktopPane.
229    */
230   public JInternalFrame getSelectedFrame()
231   {
232     return selectedFrame;
233   }
234
235   /**
236    * This method sets the selected frame in the JDesktopPane.
237    *
238    * @param frame The selected frame in the JDesktopPane.
239    */
240   public void setSelectedFrame(JInternalFrame frame)
241   {
242     if (selectedFrame != null)
243       {
244         try
245           {
246             selectedFrame.setSelected(false);
247           }
248         catch (PropertyVetoException e)
249           {
250           }
251       }
252     selectedFrame = null;
253
254     try
255       {
256         if (frame != null)
257           frame.setSelected(true);
258
259         selectedFrame = frame;
260       }
261     catch (PropertyVetoException e)
262       {
263       }
264   }
265
266   /**
267    * This method returns all the JInternalFrames in the given layer.
268    *
269    * @param layer The layer to grab frames in.
270    *
271    * @return All JInternalFrames in the given layer.
272    */
273   public JInternalFrame[] getAllFramesInLayer(int layer)
274   {
275     return getFramesFromComponents(getComponentsInLayer(layer));
276   }
277
278   /**
279    * This method always returns true to indicate that it is not transparent.
280    *
281    * @return true.
282    */
283   public boolean isOpaque()
284   {
285     return true;
286   }
287
288   /**
289    * This method returns a String that describes the JDesktopPane.
290    *
291    * @return A String that describes the JDesktopPane.
292    */
293   protected String paramString()
294   {
295     return "JDesktopPane";
296   }
297
298   /**
299    * This method returns all the JInternalFrames in the given Component array.
300    *
301    * @param components An array to search for JInternalFrames in.
302    *
303    * @return An array of JInternalFrames found in the Component array.
304    */
305   private static JInternalFrame[] getFramesFromComponents(Component[] components)
306   {
307     int count = 0;
308
309     for (int i = 0; i < components.length; i++)
310         if (components[i] instanceof JInternalFrame)
311           count++;
312           
313     JInternalFrame[] value = new JInternalFrame[count];
314     for (int i = 0, j = 0; i < components.length && j != count; i++)
315       if (components[i] instanceof JInternalFrame)
316         value[j++] = (JInternalFrame) components[i];
317     return value;
318   }
319
320   /**
321    * getAccessibleContext
322    *
323    * @return AccessibleContext
324    */
325   public AccessibleContext getAccessibleContext()
326   {
327     if (accessibleContext == null)
328       accessibleContext = new AccessibleJDesktopPane();
329
330     return accessibleContext;
331   }
332 }