001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing;
027
028 import java.util.List;
029 import java.util.ArrayList;
030 import java.util.Vector;
031 import javax.swing.plaf.*;
032 import javax.accessibility.*;
033
034 import java.awt.Component;
035 import java.awt.Container;
036 import java.awt.DefaultFocusTraversalPolicy;
037 import java.awt.FocusTraversalPolicy;
038 import java.awt.Window;
039 import java.io.ObjectOutputStream;
040 import java.io.ObjectInputStream;
041 import java.io.IOException;
042 import java.beans.PropertyVetoException;
043 import java.util.Set;
044 import java.util.TreeSet;
045
046 /**
047 * A container used to create a multiple-document interface or a virtual desktop.
048 * You create <code>JInternalFrame</code> objects and add them to the
049 * <code>JDesktopPane</code>. <code>JDesktopPane</code> extends
050 * <code>JLayeredPane</code> to manage the potentially overlapping internal
051 * frames. It also maintains a reference to an instance of
052 * <code>DesktopManager</code> that is set by the UI
053 * class for the current look and feel (L&F). Note that <code>JDesktopPane</code>
054 * does not support borders.
055 * <p>
056 * This class is normally used as the parent of <code>JInternalFrames</code>
057 * to provide a pluggable <code>DesktopManager</code> object to the
058 * <code>JInternalFrames</code>. The <code>installUI</code> of the
059 * L&F specific implementation is responsible for setting the
060 * <code>desktopManager</code> variable appropriately.
061 * When the parent of a <code>JInternalFrame</code> is a <code>JDesktopPane</code>,
062 * it should delegate most of its behavior to the <code>desktopManager</code>
063 * (closing, resizing, etc).
064 * <p>
065 * For further documentation and examples see
066 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html">How to Use Internal Frames</a>,
067 * a section in <em>The Java Tutorial</em>.
068 * <p>
069 * <strong>Warning:</strong> Swing is not thread safe. For more
070 * information see <a
071 * href="package-summary.html#threading">Swing's Threading
072 * Policy</a>.
073 * <p>
074 * <strong>Warning:</strong>
075 * Serialized objects of this class will not be compatible with
076 * future Swing releases. The current serialization support is
077 * appropriate for short term storage or RMI between applications running
078 * the same version of Swing. As of 1.4, support for long term storage
079 * of all JavaBeans<sup><font size="-2">TM</font></sup>
080 * has been added to the <code>java.beans</code> package.
081 * Please see {@link java.beans.XMLEncoder}.
082 *
083 * @see JInternalFrame
084 * @see JInternalFrame.JDesktopIcon
085 * @see DesktopManager
086 *
087 * @version 1.64 05/05/07
088 * @author David Kloba
089 */
090 public class JDesktopPane extends JLayeredPane implements Accessible {
091 /**
092 * @see #getUIClassID
093 * @see #readObject
094 */
095 private static final String uiClassID = "DesktopPaneUI";
096
097 transient DesktopManager desktopManager;
098
099 private transient JInternalFrame selectedFrame = null;
100
101 /**
102 * Indicates that the entire contents of the item being dragged
103 * should appear inside the desktop pane.
104 *
105 * @see #OUTLINE_DRAG_MODE
106 * @see #setDragMode
107 */
108 public static final int LIVE_DRAG_MODE = 0;
109
110 /**
111 * Indicates that an outline only of the item being dragged
112 * should appear inside the desktop pane.
113 *
114 * @see #LIVE_DRAG_MODE
115 * @see #setDragMode
116 */
117 public static final int OUTLINE_DRAG_MODE = 1;
118
119 private int dragMode = LIVE_DRAG_MODE;
120 private boolean dragModeSet = false;
121 private transient List<JInternalFrame> framesCache;
122 private boolean componentOrderCheckingEnabled = true;
123 private boolean componentOrderChanged = false;
124
125 /**
126 * Creates a new <code>JDesktopPane</code>.
127 */
128 public JDesktopPane() {
129 setUIProperty("opaque", Boolean.TRUE);
130 setFocusCycleRoot(true);
131
132 setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
133 public Component getDefaultComponent(Container c) {
134 JInternalFrame jifArray[] = getAllFrames();
135 Component comp = null;
136 for (int i = 0; i < jifArray.length; i++) {
137 comp = jifArray[i].getFocusTraversalPolicy()
138 .getDefaultComponent(jifArray[i]);
139 if (comp != null) {
140 break;
141 }
142 }
143 return comp;
144 }
145 });
146 updateUI();
147 }
148
149 /**
150 * Returns the L&F object that renders this component.
151 *
152 * @return the <code>DesktopPaneUI</code> object that
153 * renders this component
154 */
155 public DesktopPaneUI getUI() {
156 return (DesktopPaneUI) ui;
157 }
158
159 /**
160 * Sets the L&F object that renders this component.
161 *
162 * @param ui the DesktopPaneUI L&F object
163 * @see UIDefaults#getUI
164 * @beaninfo
165 * bound: true
166 * hidden: true
167 * attribute: visualUpdate true
168 * description: The UI object that implements the Component's LookAndFeel.
169 */
170 public void setUI(DesktopPaneUI ui) {
171 super .setUI(ui);
172 }
173
174 /**
175 * Sets the "dragging style" used by the desktop pane.
176 * You may want to change to one mode or another for
177 * performance or aesthetic reasons.
178 *
179 * @param dragMode the style of drag to use for items in the Desktop
180 *
181 * @see #LIVE_DRAG_MODE
182 * @see #OUTLINE_DRAG_MODE
183 *
184 * @beaninfo
185 * bound: true
186 * description: Dragging style for internal frame children.
187 * enum: LIVE_DRAG_MODE JDesktopPane.LIVE_DRAG_MODE
188 * OUTLINE_DRAG_MODE JDesktopPane.OUTLINE_DRAG_MODE
189 * @since 1.3
190 */
191 public void setDragMode(int dragMode) {
192 int oldDragMode = this .dragMode;
193 this .dragMode = dragMode;
194 firePropertyChange("dragMode", oldDragMode, this .dragMode);
195 dragModeSet = true;
196 }
197
198 /**
199 * Gets the current "dragging style" used by the desktop pane.
200 * @return either <code>Live_DRAG_MODE</code> or
201 * <code>OUTLINE_DRAG_MODE</code>
202 * @see #setDragMode
203 * @since 1.3
204 */
205 public int getDragMode() {
206 return dragMode;
207 }
208
209 /**
210 * Returns the <code>DesktopManger</code> that handles
211 * desktop-specific UI actions.
212 */
213 public DesktopManager getDesktopManager() {
214 return desktopManager;
215 }
216
217 /**
218 * Sets the <code>DesktopManger</code> that will handle
219 * desktop-specific UI actions.
220 *
221 * @param d the <code>DesktopManager</code> to use
222 *
223 * @beaninfo
224 * bound: true
225 * description: Desktop manager to handle the internal frames in the
226 * desktop pane.
227 */
228 public void setDesktopManager(DesktopManager d) {
229 DesktopManager oldValue = desktopManager;
230 desktopManager = d;
231 firePropertyChange("desktopManager", oldValue, desktopManager);
232 }
233
234 /**
235 * Notification from the <code>UIManager</code> that the L&F has changed.
236 * Replaces the current UI object with the latest version from the
237 * <code>UIManager</code>.
238 *
239 * @see JComponent#updateUI
240 */
241 public void updateUI() {
242 setUI((DesktopPaneUI) UIManager.getUI(this ));
243 }
244
245 /**
246 * Returns the name of the L&F class that renders this component.
247 *
248 * @return the string "DesktopPaneUI"
249 * @see JComponent#getUIClassID
250 * @see UIDefaults#getUI
251 */
252 public String getUIClassID() {
253 return uiClassID;
254 }
255
256 /**
257 * Returns all <code>JInternalFrames</code> currently displayed in the
258 * desktop. Returns iconified frames as well as expanded frames.
259 *
260 * @return an array of <code>JInternalFrame</code> objects
261 */
262 public JInternalFrame[] getAllFrames() {
263 int i, count;
264 JInternalFrame[] results;
265 Vector vResults = new Vector(10);
266 Object next, tmp;
267
268 count = getComponentCount();
269 for (i = 0; i < count; i++) {
270 next = getComponent(i);
271 if (next instanceof JInternalFrame)
272 vResults.addElement(next);
273 else if (next instanceof JInternalFrame.JDesktopIcon) {
274 tmp = ((JInternalFrame.JDesktopIcon) next)
275 .getInternalFrame();
276 if (tmp != null)
277 vResults.addElement(tmp);
278 }
279 }
280
281 results = new JInternalFrame[vResults.size()];
282 vResults.copyInto(results);
283
284 return results;
285 }
286
287 /** Returns the currently active <code>JInternalFrame</code>
288 * in this <code>JDesktopPane</code>, or <code>null</code>
289 * if no <code>JInternalFrame</code> is currently active.
290 *
291 * @return the currently active <code>JInternalFrame</code> or
292 * <code>null</code>
293 * @since 1.3
294 */
295
296 public JInternalFrame getSelectedFrame() {
297 return selectedFrame;
298 }
299
300 /** Sets the currently active <code>JInternalFrame</code>
301 * in this <code>JDesktopPane</code>. This method is used to bridge
302 * the package gap between JDesktopPane and the platform implementation
303 * code and should not be called directly. To visually select the frame
304 * the client must call JInternalFrame.setSelected(true) to activate
305 * the frame.
306 * @see JInternalFrame#setSelected(boolean)
307 *
308 * @param f the internal frame that's currently selected
309 * @since 1.3
310 */
311
312 public void setSelectedFrame(JInternalFrame f) {
313 selectedFrame = f;
314 }
315
316 /**
317 * Returns all <code>JInternalFrames</code> currently displayed in the
318 * specified layer of the desktop. Returns iconified frames as well
319 * expanded frames.
320 *
321 * @param layer an int specifying the desktop layer
322 * @return an array of <code>JInternalFrame</code> objects
323 * @see JLayeredPane
324 */
325 public JInternalFrame[] getAllFramesInLayer(int layer) {
326 int i, count;
327 JInternalFrame[] results;
328 Vector vResults = new Vector(10);
329 Object next, tmp;
330
331 count = getComponentCount();
332 for (i = 0; i < count; i++) {
333 next = getComponent(i);
334 if (next instanceof JInternalFrame) {
335 if (((JInternalFrame) next).getLayer() == layer)
336 vResults.addElement(next);
337 } else if (next instanceof JInternalFrame.JDesktopIcon) {
338 tmp = ((JInternalFrame.JDesktopIcon) next)
339 .getInternalFrame();
340 if (tmp != null
341 && ((JInternalFrame) tmp).getLayer() == layer)
342 vResults.addElement(tmp);
343 }
344 }
345
346 results = new JInternalFrame[vResults.size()];
347 vResults.copyInto(results);
348
349 return results;
350 }
351
352 private List<JInternalFrame> getFrames() {
353 Component c;
354 Set<ComponentPosition> set = new TreeSet<ComponentPosition>();
355 for (int i = 0; i < getComponentCount(); i++) {
356 c = getComponent(i);
357 if (c instanceof JInternalFrame) {
358 set.add(new ComponentPosition((JInternalFrame) c,
359 getLayer(c), i));
360 } else if (c instanceof JInternalFrame.JDesktopIcon) {
361 c = ((JInternalFrame.JDesktopIcon) c)
362 .getInternalFrame();
363 set.add(new ComponentPosition((JInternalFrame) c,
364 getLayer(c), i));
365 }
366 }
367 List<JInternalFrame> frames = new ArrayList<JInternalFrame>(set
368 .size());
369 for (ComponentPosition position : set) {
370 frames.add(position.component);
371 }
372 return frames;
373 }
374
375 private static class ComponentPosition implements
376 Comparable<ComponentPosition> {
377 private final JInternalFrame component;
378 private final int layer;
379 private final int zOrder;
380
381 ComponentPosition(JInternalFrame component, int layer,
382 int zOrder) {
383 this .component = component;
384 this .layer = layer;
385 this .zOrder = zOrder;
386 }
387
388 public int compareTo(ComponentPosition o) {
389 int delta = o.layer - layer;
390 if (delta == 0) {
391 return zOrder - o.zOrder;
392 }
393 return delta;
394 }
395 }
396
397 private JInternalFrame getNextFrame(JInternalFrame f,
398 boolean forward) {
399 verifyFramesCache();
400 if (f == null) {
401 return getTopInternalFrame();
402 }
403 int i = framesCache.indexOf(f);
404 if (i == -1 || framesCache.size() == 1) {
405 /* error */
406 return null;
407 }
408 if (forward) {
409 // navigate to the next frame
410 if (++i == framesCache.size()) {
411 /* wrap */
412 i = 0;
413 }
414 } else {
415 // navigate to the previous frame
416 if (--i == -1) {
417 /* wrap */
418 i = framesCache.size() - 1;
419 }
420 }
421 return framesCache.get(i);
422 }
423
424 JInternalFrame getNextFrame(JInternalFrame f) {
425 return getNextFrame(f, true);
426 }
427
428 private JInternalFrame getTopInternalFrame() {
429 if (framesCache.size() == 0) {
430 return null;
431 }
432 return framesCache.get(0);
433 }
434
435 private void updateFramesCache() {
436 framesCache = getFrames();
437 }
438
439 private void verifyFramesCache() {
440 // If framesCache is dirty, then recreate it.
441 if (componentOrderChanged) {
442 componentOrderChanged = false;
443 updateFramesCache();
444 }
445 }
446
447 /**
448 * Selects the next <code>JInternalFrame</code> in this desktop pane.
449 *
450 * @param forward a boolean indicating which direction to select in;
451 * <code>true</code> for forward, <code>false</code> for
452 * backward
453 * @return the JInternalFrame that was selected or <code>null</code>
454 * if nothing was selected
455 * @since 1.6
456 */
457 public JInternalFrame selectFrame(boolean forward) {
458 JInternalFrame selectedFrame = getSelectedFrame();
459 JInternalFrame frameToSelect = getNextFrame(selectedFrame,
460 forward);
461 if (frameToSelect == null) {
462 return null;
463 }
464 // Maintain navigation traversal order until an
465 // external stack change, such as a click on a frame.
466 setComponentOrderCheckingEnabled(false);
467 if (forward && selectedFrame != null) {
468 selectedFrame.moveToBack(); // For Windows MDI fidelity.
469 }
470 try {
471 frameToSelect.setSelected(true);
472 } catch (PropertyVetoException pve) {
473 }
474 setComponentOrderCheckingEnabled(true);
475 return frameToSelect;
476 }
477
478 /*
479 * Sets whether component order checking is enabled.
480 * @param enable a boolean value, where <code>true</code> means
481 * a change in component order will cause a change in the keyboard
482 * navigation order.
483 * @since 1.6
484 */
485 void setComponentOrderCheckingEnabled(boolean enable) {
486 componentOrderCheckingEnabled = enable;
487 }
488
489 /**
490 * {@inheritDoc}
491 * @since 1.6
492 */
493 protected void addImpl(Component comp, Object constraints, int index) {
494 super .addImpl(comp, constraints, index);
495 if (componentOrderCheckingEnabled) {
496 if (comp instanceof JInternalFrame
497 || comp instanceof JInternalFrame.JDesktopIcon) {
498 componentOrderChanged = true;
499 }
500 }
501 }
502
503 /**
504 * {@inheritDoc}
505 * @since 1.6
506 */
507 public void remove(int index) {
508 if (componentOrderCheckingEnabled) {
509 Component comp = getComponent(index);
510 if (comp instanceof JInternalFrame
511 || comp instanceof JInternalFrame.JDesktopIcon) {
512 componentOrderChanged = true;
513 }
514 }
515 super .remove(index);
516 }
517
518 /**
519 * {@inheritDoc}
520 * @since 1.6
521 */
522 public void removeAll() {
523 if (componentOrderCheckingEnabled) {
524 int count = getComponentCount();
525 for (int i = 0; i < count; i++) {
526 Component comp = getComponent(i);
527 if (comp instanceof JInternalFrame
528 || comp instanceof JInternalFrame.JDesktopIcon) {
529 componentOrderChanged = true;
530 break;
531 }
532 }
533 }
534 super .removeAll();
535 }
536
537 /**
538 * {@inheritDoc}
539 * @since 1.6
540 */
541 public void setComponentZOrder(Component comp, int index) {
542 super .setComponentZOrder(comp, index);
543 if (componentOrderCheckingEnabled) {
544 if (comp instanceof JInternalFrame
545 || comp instanceof JInternalFrame.JDesktopIcon) {
546 componentOrderChanged = true;
547 }
548 }
549 }
550
551 /**
552 * See readObject() and writeObject() in JComponent for more
553 * information about serialization in Swing.
554 */
555 private void writeObject(ObjectOutputStream s) throws IOException {
556 s.defaultWriteObject();
557 if (getUIClassID().equals(uiClassID)) {
558 byte count = JComponent.getWriteObjCounter(this );
559 JComponent.setWriteObjCounter(this , --count);
560 if (count == 0 && ui != null) {
561 ui.installUI(this );
562 }
563 }
564 }
565
566 void setUIProperty(String propertyName, Object value) {
567 if (propertyName == "dragMode") {
568 if (!dragModeSet) {
569 setDragMode(((Integer) value).intValue());
570 dragModeSet = false;
571 }
572 } else {
573 super .setUIProperty(propertyName, value);
574 }
575 }
576
577 /**
578 * Returns a string representation of this <code>JDesktopPane</code>.
579 * This method is intended to be used only for debugging purposes, and the
580 * content and format of the returned string may vary between
581 * implementations. The returned string may be empty but may not
582 * be <code>null</code>.
583 *
584 * @return a string representation of this <code>JDesktopPane</code>
585 */
586 protected String paramString() {
587 String desktopManagerString = (desktopManager != null ? desktopManager
588 .toString()
589 : "");
590
591 return super .paramString() + ",desktopManager="
592 + desktopManagerString;
593 }
594
595 /////////////////
596 // Accessibility support
597 ////////////////
598
599 /**
600 * Gets the <code>AccessibleContext</code> associated with this
601 * <code>JDesktopPane</code>. For desktop panes, the
602 * <code>AccessibleContext</code> takes the form of an
603 * <code>AccessibleJDesktopPane</code>.
604 * A new <code>AccessibleJDesktopPane</code> instance is created if necessary.
605 *
606 * @return an <code>AccessibleJDesktopPane</code> that serves as the
607 * <code>AccessibleContext</code> of this <code>JDesktopPane</code>
608 */
609 public AccessibleContext getAccessibleContext() {
610 if (accessibleContext == null) {
611 accessibleContext = new AccessibleJDesktopPane();
612 }
613 return accessibleContext;
614 }
615
616 /**
617 * This class implements accessibility support for the
618 * <code>JDesktopPane</code> class. It provides an implementation of the
619 * Java Accessibility API appropriate to desktop pane user-interface
620 * elements.
621 * <p>
622 * <strong>Warning:</strong>
623 * Serialized objects of this class will not be compatible with
624 * future Swing releases. The current serialization support is
625 * appropriate for short term storage or RMI between applications running
626 * the same version of Swing. As of 1.4, support for long term storage
627 * of all JavaBeans<sup><font size="-2">TM</font></sup>
628 * has been added to the <code>java.beans</code> package.
629 * Please see {@link java.beans.XMLEncoder}.
630 */
631 protected class AccessibleJDesktopPane extends AccessibleJComponent {
632
633 /**
634 * Get the role of this object.
635 *
636 * @return an instance of AccessibleRole describing the role of the
637 * object
638 * @see AccessibleRole
639 */
640 public AccessibleRole getAccessibleRole() {
641 return AccessibleRole.DESKTOP_PANE;
642 }
643 }
644 }
|