1 /* 2 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.swing; 27 28 import java.util.List; 29 import java.util.ArrayList; 30 import java.util.Vector; 31 import javax.swing.plaf.*; 32 import javax.accessibility.*; 33 34 import java.awt.Component; 35 import java.awt.Container; 36 import java.awt.DefaultFocusTraversalPolicy; 37 import java.awt.FocusTraversalPolicy; 38 import java.awt.Window; 39 import java.io.ObjectOutputStream; 40 import java.io.ObjectInputStream; 41 import java.io.IOException; 42 import java.beans.PropertyVetoException; 43 import java.util.Set; 44 import java.util.TreeSet; 45 46 /** 47 * A container used to create a multiple-document interface or a virtual desktop. 48 * You create <code>JInternalFrame</code> objects and add them to the 49 * <code>JDesktopPane</code>. <code>JDesktopPane</code> extends 50 * <code>JLayeredPane</code> to manage the potentially overlapping internal 51 * frames. It also maintains a reference to an instance of 52 * <code>DesktopManager</code> that is set by the UI 53 * class for the current look and feel (L&F). Note that <code>JDesktopPane</code> 54 * does not support borders. 55 * <p> 56 * This class is normally used as the parent of <code>JInternalFrames</code> 57 * to provide a pluggable <code>DesktopManager</code> object to the 58 * <code>JInternalFrames</code>. The <code>installUI</code> of the 59 * L&F specific implementation is responsible for setting the 60 * <code>desktopManager</code> variable appropriately. 61 * When the parent of a <code>JInternalFrame</code> is a <code>JDesktopPane</code>, 62 * it should delegate most of its behavior to the <code>desktopManager</code> 63 * (closing, resizing, etc). 64 * <p> 65 * For further documentation and examples see 66 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html">How to Use Internal Frames</a>, 67 * a section in <em>The Java Tutorial</em>. 68 * <p> 69 * <strong>Warning:</strong> Swing is not thread safe. For more 70 * information see <a 71 * href="package-summary.html#threading">Swing's Threading 72 * Policy</a>. 73 * <p> 74 * <strong>Warning:</strong> 75 * Serialized objects of this class will not be compatible with 76 * future Swing releases. The current serialization support is 77 * appropriate for short term storage or RMI between applications running 78 * the same version of Swing. As of 1.4, support for long term storage 79 * of all JavaBeans<sup><font size="-2">TM</font></sup> 80 * has been added to the <code>java.beans</code> package. 81 * Please see {@link java.beans.XMLEncoder}. 82 * 83 * @see JInternalFrame 84 * @see JInternalFrame.JDesktopIcon 85 * @see DesktopManager 86 * 87 * @author David Kloba 88 */ 89 public class JDesktopPane extends JLayeredPane implements Accessible 90 { 91 /** 92 * @see #getUIClassID 93 * @see #readObject 94 */ 95 private static final String uiClassID = "DesktopPaneUI"; 96 97 transient DesktopManager desktopManager; 98 99 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 (JInternalFrame jif : jifArray) { 137 comp = jif.getFocusTraversalPolicy().getDefaultComponent(jif); 138 if (comp != null) { 139 break; 140 } 141 } 142 return comp; 143 } 144 }); 145 updateUI(); 146 } 147 148 /** 149 * Returns the L&F object that renders this component. 150 * 151 * @return the <code>DesktopPaneUI</code> object that 152 * renders this component 153 */ 154 public DesktopPaneUI getUI() { 155 return (DesktopPaneUI)ui; 156 } 157 158 /** 159 * Sets the L&F object that renders this component. 160 * 161 * @param ui the DesktopPaneUI L&F object 162 * @see UIDefaults#getUI 163 * @beaninfo 164 * bound: true 165 * hidden: true 166 * attribute: visualUpdate true 167 * description: The UI object that implements the Component's LookAndFeel. 168 */ 169 public void setUI(DesktopPaneUI ui) { 170 super.setUI(ui); 171 } 172 173 /** 174 * Sets the "dragging style" used by the desktop pane. 175 * You may want to change to one mode or another for 176 * performance or aesthetic reasons. 177 * 178 * @param dragMode the style of drag to use for items in the Desktop 179 * 180 * @see #LIVE_DRAG_MODE 181 * @see #OUTLINE_DRAG_MODE 182 * 183 * @beaninfo 184 * bound: true 185 * description: Dragging style for internal frame children. 186 * enum: LIVE_DRAG_MODE JDesktopPane.LIVE_DRAG_MODE 187 * OUTLINE_DRAG_MODE JDesktopPane.OUTLINE_DRAG_MODE 188 * @since 1.3 189 */ 190 public void setDragMode(int dragMode) { 191 int oldDragMode = this.dragMode; 192 this.dragMode = dragMode; 193 firePropertyChange("dragMode", oldDragMode, this.dragMode); 194 dragModeSet = true; 195 } 196 197 /** 198 * Gets the current "dragging style" used by the desktop pane. 199 * @return either <code>Live_DRAG_MODE</code> or 200 * <code>OUTLINE_DRAG_MODE</code> 201 * @see #setDragMode 202 * @since 1.3 203 */ 204 public int getDragMode() { 205 return dragMode; 206 } 207 208 /** 209 * Returns the <code>DesktopManger</code> that handles 210 * desktop-specific UI actions. 211 */ 212 public DesktopManager getDesktopManager() { 213 return desktopManager; 214 } 215 216 /** 217 * Sets the <code>DesktopManger</code> that will handle 218 * desktop-specific UI actions. 219 * 220 * @param d the <code>DesktopManager</code> to use 221 * 222 * @beaninfo 223 * bound: true 224 * description: Desktop manager to handle the internal frames in the 225 * desktop pane. 226 */ 227 public void setDesktopManager(DesktopManager d) { 228 DesktopManager oldValue = desktopManager; 229 desktopManager = d; 230 firePropertyChange("desktopManager", oldValue, desktopManager); 231 } 232 233 /** 234 * Notification from the <code>UIManager</code> that the L&F has changed. 235 * Replaces the current UI object with the latest version from the 236 * <code>UIManager</code>. 237 * 238 * @see JComponent#updateUI 239 */ 240 public void updateUI() { 241 setUI((DesktopPaneUI)UIManager.getUI(this)); 242 } 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<JInternalFrame> vResults = new Vector<JInternalFrame>(10); 266 267 count = getComponentCount(); 268 for(i = 0; i < count; i++) { 269 Component next = getComponent(i); 270 if(next instanceof JInternalFrame) 271 vResults.addElement((JInternalFrame) next); 272 else if(next instanceof JInternalFrame.JDesktopIcon) { 273 JInternalFrame tmp = ((JInternalFrame.JDesktopIcon)next).getInternalFrame(); 274 if(tmp != null) 275 vResults.addElement(tmp); 276 } 277 } 278 279 results = new JInternalFrame[vResults.size()]; 280 vResults.copyInto(results); 281 282 return results; 283 } 284 285 /** Returns the currently active <code>JInternalFrame</code> 286 * in this <code>JDesktopPane</code>, or <code>null</code> 287 * if no <code>JInternalFrame</code> is currently active. 288 * 289 * @return the currently active <code>JInternalFrame</code> or 290 * <code>null</code> 291 * @since 1.3 292 */ 293 294 public JInternalFrame getSelectedFrame() { 295 return selectedFrame; 296 } 297 298 /** Sets the currently active <code>JInternalFrame</code> 299 * in this <code>JDesktopPane</code>. This method is used to bridge 300 * the package gap between JDesktopPane and the platform implementation 301 * code and should not be called directly. To visually select the frame 302 * the client must call JInternalFrame.setSelected(true) to activate 303 * the frame. 304 * @see JInternalFrame#setSelected(boolean) 305 * 306 * @param f the internal frame that's currently selected 307 * @since 1.3 308 */ 309 310 public void setSelectedFrame(JInternalFrame f) { 311 selectedFrame = f; 312 } 313 314 /** 315 * Returns all <code>JInternalFrames</code> currently displayed in the 316 * specified layer of the desktop. Returns iconified frames as well 317 * expanded frames. 318 * 319 * @param layer an int specifying the desktop layer 320 * @return an array of <code>JInternalFrame</code> objects 321 * @see JLayeredPane 322 */ 323 public JInternalFrame[] getAllFramesInLayer(int layer) { 324 int i, count; 325 JInternalFrame[] results; 326 Vector<JInternalFrame> vResults = new Vector<JInternalFrame>(10); 327 328 count = getComponentCount(); 329 for(i = 0; i < count; i++) { 330 Component next = getComponent(i); 331 if(next instanceof JInternalFrame) { 332 if(((JInternalFrame)next).getLayer() == layer) 333 vResults.addElement((JInternalFrame) next); 334 } else if(next instanceof JInternalFrame.JDesktopIcon) { 335 JInternalFrame tmp = ((JInternalFrame.JDesktopIcon)next).getInternalFrame(); 336 if(tmp != null && tmp.getLayer() == layer) 337 vResults.addElement(tmp); 338 } 339 } 340 341 results = new JInternalFrame[vResults.size()]; 342 vResults.copyInto(results); 343 344 return results; 345 } 346 347 private List<JInternalFrame> getFrames() { 348 Component c; 349 Set<ComponentPosition> set = new TreeSet<ComponentPosition>(); 350 for (int i = 0; i < getComponentCount(); i++) { 351 c = getComponent(i); 352 if (c instanceof JInternalFrame) { 353 set.add(new ComponentPosition((JInternalFrame)c, getLayer(c), 354 i)); 355 } 356 else if (c instanceof JInternalFrame.JDesktopIcon) { 357 c = ((JInternalFrame.JDesktopIcon)c).getInternalFrame(); 358 set.add(new ComponentPosition((JInternalFrame)c, getLayer(c), 359 i)); 360 } 361 } 362 List<JInternalFrame> frames = new ArrayList<JInternalFrame>( 363 set.size()); 364 for (ComponentPosition position : set) { 365 frames.add(position.component); 366 } 367 return frames; 368 } 369 370 private static class ComponentPosition implements 371 Comparable<ComponentPosition> { 372 private final JInternalFrame component; 373 private final int layer; 374 private final int zOrder; 375 376 ComponentPosition(JInternalFrame component, int layer, int zOrder) { 377 this.component = component; 378 this.layer = layer; 379 this.zOrder = zOrder; 380 } 381 382 public int compareTo(ComponentPosition o) { 383 int delta = o.layer - layer; 384 if (delta == 0) { 385 return zOrder - o.zOrder; 386 } 387 return delta; 388 } 389 } 390 391 private JInternalFrame getNextFrame(JInternalFrame f, boolean forward) { 392 verifyFramesCache(); 393 if (f == null) { 394 return getTopInternalFrame(); 395 } 396 int i = framesCache.indexOf(f); 397 if (i == -1 || framesCache.size() == 1) { 398 /* error */ 399 return null; 400 } 401 if (forward) { 402 // navigate to the next frame 403 if (++i == framesCache.size()) { 404 /* wrap */ 405 i = 0; 406 } 407 } 408 else { 409 // navigate to the previous frame 410 if (--i == -1) { 411 /* wrap */ 412 i = framesCache.size() - 1; 413 } 414 } 415 return framesCache.get(i); 416 } 417 418 JInternalFrame getNextFrame(JInternalFrame f) { 419 return getNextFrame(f, true); 420 } 421 422 private JInternalFrame getTopInternalFrame() { 423 if (framesCache.size() == 0) { 424 return null; 425 } 426 return framesCache.get(0); 427 } 428 429 private void updateFramesCache() { 430 framesCache = getFrames(); 431 } 432 433 private void verifyFramesCache() { 434 // If framesCache is dirty, then recreate it. 435 if (componentOrderChanged) { 436 componentOrderChanged = false; 437 updateFramesCache(); 438 } 439 } 440 441 /** 442 * Selects the next <code>JInternalFrame</code> in this desktop pane. 443 * 444 * @param forward a boolean indicating which direction to select in; 445 * <code>true</code> for forward, <code>false</code> for 446 * backward 447 * @return the JInternalFrame that was selected or <code>null</code> 448 * if nothing was selected 449 * @since 1.6 450 */ 451 public JInternalFrame selectFrame(boolean forward) { 452 JInternalFrame selectedFrame = getSelectedFrame(); 453 JInternalFrame frameToSelect = getNextFrame(selectedFrame, forward); 454 if (frameToSelect == null) { 455 return null; 456 } 457 // Maintain navigation traversal order until an 458 // external stack change, such as a click on a frame. 459 setComponentOrderCheckingEnabled(false); 460 if (forward && selectedFrame != null) { 461 selectedFrame.moveToBack(); // For Windows MDI fidelity. 462 } 463 try { frameToSelect.setSelected(true); 464 } catch (PropertyVetoException pve) {} 465 setComponentOrderCheckingEnabled(true); 466 return frameToSelect; 467 } 468 469 /* 470 * Sets whether component order checking is enabled. 471 * @param enable a boolean value, where <code>true</code> means 472 * a change in component order will cause a change in the keyboard 473 * navigation order. 474 * @since 1.6 475 */ 476 void setComponentOrderCheckingEnabled(boolean enable) { 477 componentOrderCheckingEnabled = enable; 478 } 479 480 /** 481 * {@inheritDoc} 482 * @since 1.6 483 */ 484 protected void addImpl(Component comp, Object constraints, int index) { 485 super.addImpl(comp, constraints, index); 486 if (componentOrderCheckingEnabled) { 487 if (comp instanceof JInternalFrame || 488 comp instanceof JInternalFrame.JDesktopIcon) { 489 componentOrderChanged = true; 490 } 491 } 492 } 493 494 /** 495 * {@inheritDoc} 496 * @since 1.6 497 */ 498 public void remove(int index) { 499 if (componentOrderCheckingEnabled) { 500 Component comp = getComponent(index); 501 if (comp instanceof JInternalFrame || 502 comp instanceof JInternalFrame.JDesktopIcon) { 503 componentOrderChanged = true; 504 } 505 } 506 super.remove(index); 507 } 508 509 /** 510 * {@inheritDoc} 511 * @since 1.6 512 */ 513 public void removeAll() { 514 if (componentOrderCheckingEnabled) { 515 int count = getComponentCount(); 516 for (int i = 0; i < count; i++) { 517 Component comp = getComponent(i); 518 if (comp instanceof JInternalFrame || 519 comp instanceof JInternalFrame.JDesktopIcon) { 520 componentOrderChanged = true; 521 break; 522 } 523 } 524 } 525 super.removeAll(); 526 } 527 528 /** 529 * {@inheritDoc} 530 * @since 1.6 531 */ 532 public void setComponentZOrder(Component comp, int index) { 533 super.setComponentZOrder(comp, index); 534 if (componentOrderCheckingEnabled) { 535 if (comp instanceof JInternalFrame || 536 comp instanceof JInternalFrame.JDesktopIcon) { 537 componentOrderChanged = true; 538 } 539 } 540 } 541 542 /** 543 * See readObject() and writeObject() in JComponent for more 544 * information about serialization in Swing. 545 */ 546 private void writeObject(ObjectOutputStream s) throws IOException { 547 s.defaultWriteObject(); 548 if (getUIClassID().equals(uiClassID)) { 549 byte count = JComponent.getWriteObjCounter(this); 550 JComponent.setWriteObjCounter(this, --count); 551 if (count == 0 && ui != null) { 552 ui.installUI(this); 553 } 554 } 555 } 556 557 void setUIProperty(String propertyName, Object value) { 558 if (propertyName == "dragMode") { 559 if (!dragModeSet) { 560 setDragMode(((Integer)value).intValue()); 561 dragModeSet = false; 562 } 563 } else { 564 super.setUIProperty(propertyName, value); 565 } 566 } 567 568 /** 569 * Returns a string representation of this <code>JDesktopPane</code>. 570 * This method is intended to be used only for debugging purposes, and the 571 * content and format of the returned string may vary between 572 * implementations. The returned string may be empty but may not 573 * be <code>null</code>. 574 * 575 * @return a string representation of this <code>JDesktopPane</code> 576 */ 577 protected String paramString() { 578 String desktopManagerString = (desktopManager != null ? 579 desktopManager.toString() : ""); 580 581 return super.paramString() + 582 ",desktopManager=" + desktopManagerString; 583 } 584 585 ///////////////// 586 // Accessibility support 587 //////////////// 588 589 /** 590 * Gets the <code>AccessibleContext</code> associated with this 591 * <code>JDesktopPane</code>. For desktop panes, the 592 * <code>AccessibleContext</code> takes the form of an 593 * <code>AccessibleJDesktopPane</code>. 594 * A new <code>AccessibleJDesktopPane</code> instance is created if necessary. 595 * 596 * @return an <code>AccessibleJDesktopPane</code> that serves as the 597 * <code>AccessibleContext</code> of this <code>JDesktopPane</code> 598 */ 599 public AccessibleContext getAccessibleContext() { 600 if (accessibleContext == null) { 601 accessibleContext = new AccessibleJDesktopPane(); 602 } 603 return accessibleContext; 604 } 605 606 /** 607 * This class implements accessibility support for the 608 * <code>JDesktopPane</code> class. It provides an implementation of the 609 * Java Accessibility API appropriate to desktop pane user-interface 610 * elements. 611 * <p> 612 * <strong>Warning:</strong> 613 * Serialized objects of this class will not be compatible with 614 * future Swing releases. The current serialization support is 615 * appropriate for short term storage or RMI between applications running 616 * the same version of Swing. As of 1.4, support for long term storage 617 * of all JavaBeans<sup><font size="-2">TM</font></sup> 618 * has been added to the <code>java.beans</code> package. 619 * Please see {@link java.beans.XMLEncoder}. 620 */ 621 protected class AccessibleJDesktopPane extends AccessibleJComponent { 622 623 /** 624 * Get the role of this object. 625 * 626 * @return an instance of AccessibleRole describing the role of the 627 * object 628 * @see AccessibleRole 629 */ 630 public AccessibleRole getAccessibleRole() { 631 return AccessibleRole.DESKTOP_PANE; 632 } 633 } 634 }