1 /* 2 * Copyright (c) 1997, 2014, 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.Collection; 31 import java.util.Iterator; 32 import javax.swing.plaf.*; 33 import javax.accessibility.*; 34 35 import java.awt.Component; 36 import java.awt.Container; 37 import java.awt.DefaultFocusTraversalPolicy; 38 import java.awt.FocusTraversalPolicy; 39 import java.awt.Window; 40 import java.io.ObjectOutputStream; 41 import java.io.ObjectInputStream; 42 import java.io.IOException; 43 import java.beans.PropertyVetoException; 44 import java.util.Set; 45 import java.util.TreeSet; 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://docs.oracle.com/javase/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™ 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 * @since 1.2 89 */ 90 @SuppressWarnings("serial") // Same-version serialization only 91 public class JDesktopPane extends JLayeredPane implements Accessible 92 { 93 /** 94 * @see #getUIClassID 95 * @see #readObject 96 */ 97 private static final String uiClassID = "DesktopPaneUI"; 98 99 transient DesktopManager desktopManager; 100 101 private transient JInternalFrame selectedFrame = null; 102 103 /** 104 * Indicates that the entire contents of the item being dragged 105 * should appear inside the desktop pane. 106 * 107 * @see #OUTLINE_DRAG_MODE 108 * @see #setDragMode 109 */ 110 public static final int LIVE_DRAG_MODE = 0; 111 112 /** 113 * Indicates that an outline only of the item being dragged 114 * should appear inside the desktop pane. 115 * 116 * @see #LIVE_DRAG_MODE 117 * @see #setDragMode 118 */ 119 public static final int OUTLINE_DRAG_MODE = 1; 120 121 private int dragMode = LIVE_DRAG_MODE; 122 private boolean dragModeSet = false; 123 private transient List<JInternalFrame> framesCache; 124 private boolean componentOrderCheckingEnabled = true; 125 private boolean componentOrderChanged = false; 126 127 /** 128 * Creates a new <code>JDesktopPane</code>. 129 */ 130 public JDesktopPane() { 131 setUIProperty("opaque", Boolean.TRUE); 132 setFocusCycleRoot(true); 133 134 setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() { 135 public Component getDefaultComponent(Container c) { 136 JInternalFrame jifArray[] = getAllFrames(); 137 Component comp = null; 138 for (JInternalFrame jif : jifArray) { 139 comp = jif.getFocusTraversalPolicy().getDefaultComponent(jif); 140 if (comp != null) { 141 break; 142 } 143 } 144 return comp; 145 } 146 }); 147 updateUI(); 148 } 149 150 /** 151 * Returns the L&F object that renders this component. 152 * 153 * @return the <code>DesktopPaneUI</code> object that 154 * renders this component 155 */ 156 public DesktopPaneUI getUI() { 157 return (DesktopPaneUI)ui; 158 } 159 160 /** 161 * Sets the L&F object that renders this component. 162 * 163 * @param ui the DesktopPaneUI L&F object 164 * @see UIDefaults#getUI 165 * @beaninfo 166 * bound: true 167 * hidden: true 168 * attribute: visualUpdate true 169 * description: The UI object that implements the Component's LookAndFeel. 170 */ 171 public void setUI(DesktopPaneUI ui) { 172 super.setUI(ui); 173 } 174 175 /** 176 * Sets the "dragging style" used by the desktop pane. 177 * You may want to change to one mode or another for 178 * performance or aesthetic reasons. 179 * 180 * @param dragMode the style of drag to use for items in the Desktop 181 * 182 * @see #LIVE_DRAG_MODE 183 * @see #OUTLINE_DRAG_MODE 184 * 185 * @beaninfo 186 * bound: true 187 * description: Dragging style for internal frame children. 188 * enum: LIVE_DRAG_MODE JDesktopPane.LIVE_DRAG_MODE 189 * OUTLINE_DRAG_MODE JDesktopPane.OUTLINE_DRAG_MODE 190 * @since 1.3 191 */ 192 public void setDragMode(int dragMode) { 193 int oldDragMode = this.dragMode; 194 this.dragMode = dragMode; 195 firePropertyChange("dragMode", oldDragMode, this.dragMode); 196 dragModeSet = true; 197 } 198 199 /** 200 * Gets the current "dragging style" used by the desktop pane. 201 * @return either <code>Live_DRAG_MODE</code> or 202 * <code>OUTLINE_DRAG_MODE</code> 203 * @see #setDragMode 204 * @since 1.3 205 */ 206 public int getDragMode() { 207 return dragMode; 208 } 209 210 /** 211 * Returns the {@code DesktopManger} that handles 212 * desktop-specific UI actions. 213 * 214 * @return the {@code DesktopManger} that handles desktop-specific 215 * UI actions 216 */ 217 public DesktopManager getDesktopManager() { 218 return desktopManager; 219 } 220 221 /** 222 * Sets the <code>DesktopManger</code> that will handle 223 * desktop-specific UI actions. This may be overridden by 224 * {@code LookAndFeel}. 225 * 226 * @param d the <code>DesktopManager</code> to use 227 * 228 * @beaninfo 229 * bound: true 230 * description: Desktop manager to handle the internal frames in the 231 * desktop pane. 232 */ 233 public void setDesktopManager(DesktopManager d) { 234 DesktopManager oldValue = desktopManager; 235 desktopManager = d; 236 firePropertyChange("desktopManager", oldValue, desktopManager); 237 } 238 239 /** 240 * Notification from the <code>UIManager</code> that the L&F has changed. 241 * Replaces the current UI object with the latest version from the 242 * <code>UIManager</code>. 243 * 244 * @see JComponent#updateUI 245 */ 246 public void updateUI() { 247 setUI((DesktopPaneUI)UIManager.getUI(this)); 248 } 249 250 251 /** 252 * Returns the name of the L&F class that renders this component. 253 * 254 * @return the string "DesktopPaneUI" 255 * @see JComponent#getUIClassID 256 * @see UIDefaults#getUI 257 */ 258 public String getUIClassID() { 259 return uiClassID; 260 } 261 262 /** 263 * Returns all <code>JInternalFrames</code> currently displayed in the 264 * desktop. Returns iconified frames as well as expanded frames. 265 * 266 * @return an array of <code>JInternalFrame</code> objects 267 */ 268 public JInternalFrame[] getAllFrames() { 269 return getAllFrames(this).toArray(new JInternalFrame[0]); 270 } 271 272 private static Collection<JInternalFrame> getAllFrames(Container parent) { 273 int i, count; 274 Collection<JInternalFrame> results = new ArrayList<JInternalFrame>(); 275 count = parent.getComponentCount(); 276 for (i = 0; i < count; i++) { 277 Component next = parent.getComponent(i); 278 if (next instanceof JInternalFrame) { 279 results.add((JInternalFrame) next); 280 } else if (next instanceof JInternalFrame.JDesktopIcon) { 281 JInternalFrame tmp = ((JInternalFrame.JDesktopIcon) next).getInternalFrame(); 282 if (tmp != null) { 283 results.add(tmp); 284 } 285 } else if (next instanceof Container) { 286 results.addAll(getAllFrames((Container) next)); 287 } 288 } 289 return results; 290 } 291 292 /** Returns the currently active <code>JInternalFrame</code> 293 * in this <code>JDesktopPane</code>, or <code>null</code> 294 * if no <code>JInternalFrame</code> is currently active. 295 * 296 * @return the currently active <code>JInternalFrame</code> or 297 * <code>null</code> 298 * @since 1.3 299 */ 300 301 public JInternalFrame getSelectedFrame() { 302 return selectedFrame; 303 } 304 305 /** Sets the currently active <code>JInternalFrame</code> 306 * in this <code>JDesktopPane</code>. This method is used to bridge 307 * the package gap between JDesktopPane and the platform implementation 308 * code and should not be called directly. To visually select the frame 309 * the client must call JInternalFrame.setSelected(true) to activate 310 * the frame. 311 * @see JInternalFrame#setSelected(boolean) 312 * 313 * @param f the internal frame that's currently selected 314 * @since 1.3 315 */ 316 317 public void setSelectedFrame(JInternalFrame f) { 318 selectedFrame = f; 319 } 320 321 /** 322 * Returns all <code>JInternalFrames</code> currently displayed in the 323 * specified layer of the desktop. Returns iconified frames as well 324 * expanded frames. 325 * 326 * @param layer an int specifying the desktop layer 327 * @return an array of <code>JInternalFrame</code> objects 328 * @see JLayeredPane 329 */ 330 public JInternalFrame[] getAllFramesInLayer(int layer) { 331 Collection<JInternalFrame> allFrames = getAllFrames(this); 332 Iterator<JInternalFrame> iterator = allFrames.iterator(); 333 while (iterator.hasNext()) { 334 if (iterator.next().getLayer() != layer) { 335 iterator.remove(); 336 } 337 } 338 return allFrames.toArray(new JInternalFrame[0]); 339 } 340 341 private List<JInternalFrame> getFrames() { 342 Component c; 343 Set<ComponentPosition> set = new TreeSet<ComponentPosition>(); 344 for (int i = 0; i < getComponentCount(); i++) { 345 c = getComponent(i); 346 if (c instanceof JInternalFrame) { 347 set.add(new ComponentPosition((JInternalFrame)c, getLayer(c), 348 i)); 349 } 350 else if (c instanceof JInternalFrame.JDesktopIcon) { 351 c = ((JInternalFrame.JDesktopIcon)c).getInternalFrame(); 352 set.add(new ComponentPosition((JInternalFrame)c, getLayer(c), 353 i)); 354 } 355 } 356 List<JInternalFrame> frames = new ArrayList<JInternalFrame>( 357 set.size()); 358 for (ComponentPosition position : set) { 359 frames.add(position.component); 360 } 361 return frames; 362 } 363 364 private static class ComponentPosition implements 365 Comparable<ComponentPosition> { 366 private final JInternalFrame component; 367 private final int layer; 368 private final int zOrder; 369 370 ComponentPosition(JInternalFrame component, int layer, int zOrder) { 371 this.component = component; 372 this.layer = layer; 373 this.zOrder = zOrder; 374 } 375 376 public int compareTo(ComponentPosition o) { 377 int delta = o.layer - layer; 378 if (delta == 0) { 379 return zOrder - o.zOrder; 380 } 381 return delta; 382 } 383 } 384 385 private JInternalFrame getNextFrame(JInternalFrame f, boolean forward) { 386 verifyFramesCache(); 387 if (f == null) { 388 return getTopInternalFrame(); 389 } 390 int i = framesCache.indexOf(f); 391 if (i == -1 || framesCache.size() == 1) { 392 /* error */ 393 return null; 394 } 395 if (forward) { 396 // navigate to the next frame 397 if (++i == framesCache.size()) { 398 /* wrap */ 399 i = 0; 400 } 401 } 402 else { 403 // navigate to the previous frame 404 if (--i == -1) { 405 /* wrap */ 406 i = framesCache.size() - 1; 407 } 408 } 409 return framesCache.get(i); 410 } 411 412 JInternalFrame getNextFrame(JInternalFrame f) { 413 return getNextFrame(f, true); 414 } 415 416 private JInternalFrame getTopInternalFrame() { 417 if (framesCache.size() == 0) { 418 return null; 419 } 420 return framesCache.get(0); 421 } 422 423 private void updateFramesCache() { 424 framesCache = getFrames(); 425 } 426 427 private void verifyFramesCache() { 428 // If framesCache is dirty, then recreate it. 429 if (componentOrderChanged) { 430 componentOrderChanged = false; 431 updateFramesCache(); 432 } 433 } 434 435 /** 436 * {@inheritDoc} 437 */ 438 @Override 439 public void remove(Component comp) { 440 super.remove(comp); 441 updateFramesCache(); 442 } 443 444 /** 445 * Selects the next <code>JInternalFrame</code> in this desktop pane. 446 * 447 * @param forward a boolean indicating which direction to select in; 448 * <code>true</code> for forward, <code>false</code> for 449 * backward 450 * @return the JInternalFrame that was selected or <code>null</code> 451 * if nothing was selected 452 * @since 1.6 453 */ 454 public JInternalFrame selectFrame(boolean forward) { 455 JInternalFrame selectedFrame = getSelectedFrame(); 456 JInternalFrame frameToSelect = getNextFrame(selectedFrame, forward); 457 if (frameToSelect == null) { 458 return null; 459 } 460 // Maintain navigation traversal order until an 461 // external stack change, such as a click on a frame. 462 setComponentOrderCheckingEnabled(false); 463 if (forward && selectedFrame != null) { 464 selectedFrame.moveToBack(); // For Windows MDI fidelity. 465 } 466 try { frameToSelect.setSelected(true); 467 } catch (PropertyVetoException pve) {} 468 setComponentOrderCheckingEnabled(true); 469 return frameToSelect; 470 } 471 472 /* 473 * Sets whether component order checking is enabled. 474 * @param enable a boolean value, where <code>true</code> means 475 * a change in component order will cause a change in the keyboard 476 * navigation order. 477 * @since 1.6 478 */ 479 void setComponentOrderCheckingEnabled(boolean enable) { 480 componentOrderCheckingEnabled = enable; 481 } 482 483 /** 484 * {@inheritDoc} 485 * @since 1.6 486 */ 487 protected void addImpl(Component comp, Object constraints, int index) { 488 super.addImpl(comp, constraints, index); 489 if (componentOrderCheckingEnabled) { 490 if (comp instanceof JInternalFrame || 491 comp instanceof JInternalFrame.JDesktopIcon) { 492 componentOrderChanged = true; 493 } 494 } 495 } 496 497 /** 498 * {@inheritDoc} 499 * @since 1.6 500 */ 501 public void remove(int index) { 502 if (componentOrderCheckingEnabled) { 503 Component comp = getComponent(index); 504 if (comp instanceof JInternalFrame || 505 comp instanceof JInternalFrame.JDesktopIcon) { 506 componentOrderChanged = true; 507 } 508 } 509 super.remove(index); 510 } 511 512 /** 513 * {@inheritDoc} 514 * @since 1.6 515 */ 516 public void removeAll() { 517 if (componentOrderCheckingEnabled) { 518 int count = getComponentCount(); 519 for (int i = 0; i < count; i++) { 520 Component comp = getComponent(i); 521 if (comp instanceof JInternalFrame || 522 comp instanceof JInternalFrame.JDesktopIcon) { 523 componentOrderChanged = true; 524 break; 525 } 526 } 527 } 528 super.removeAll(); 529 } 530 531 /** 532 * {@inheritDoc} 533 * @since 1.6 534 */ 535 public void setComponentZOrder(Component comp, int index) { 536 super.setComponentZOrder(comp, index); 537 if (componentOrderCheckingEnabled) { 538 if (comp instanceof JInternalFrame || 539 comp instanceof JInternalFrame.JDesktopIcon) { 540 componentOrderChanged = true; 541 } 542 } 543 } 544 545 /** 546 * See readObject() and writeObject() in JComponent for more 547 * information about serialization in Swing. 548 */ 549 private void writeObject(ObjectOutputStream s) throws IOException { 550 s.defaultWriteObject(); 551 if (getUIClassID().equals(uiClassID)) { 552 byte count = JComponent.getWriteObjCounter(this); 553 JComponent.setWriteObjCounter(this, --count); 554 if (count == 0 && ui != null) { 555 ui.installUI(this); 556 } 557 } 558 } 559 560 void setUIProperty(String propertyName, Object value) { 561 if (propertyName == "dragMode") { 562 if (!dragModeSet) { 563 setDragMode(((Integer)value).intValue()); 564 dragModeSet = false; 565 } 566 } else { 567 super.setUIProperty(propertyName, value); 568 } 569 } 570 571 /** 572 * Returns a string representation of this <code>JDesktopPane</code>. 573 * This method is intended to be used only for debugging purposes, and the 574 * content and format of the returned string may vary between 575 * implementations. The returned string may be empty but may not 576 * be <code>null</code>. 577 * 578 * @return a string representation of this <code>JDesktopPane</code> 579 */ 580 protected String paramString() { 581 String desktopManagerString = (desktopManager != null ? 582 desktopManager.toString() : ""); 583 584 return super.paramString() + 585 ",desktopManager=" + desktopManagerString; 586 } 587 588 ///////////////// 589 // Accessibility support 590 //////////////// 591 592 /** 593 * Gets the <code>AccessibleContext</code> associated with this 594 * <code>JDesktopPane</code>. For desktop panes, the 595 * <code>AccessibleContext</code> takes the form of an 596 * <code>AccessibleJDesktopPane</code>. 597 * A new <code>AccessibleJDesktopPane</code> instance is created if necessary. 598 * 599 * @return an <code>AccessibleJDesktopPane</code> that serves as the 600 * <code>AccessibleContext</code> of this <code>JDesktopPane</code> 601 */ 602 public AccessibleContext getAccessibleContext() { 603 if (accessibleContext == null) { 604 accessibleContext = new AccessibleJDesktopPane(); 605 } 606 return accessibleContext; 607 } 608 609 /** 610 * This class implements accessibility support for the 611 * <code>JDesktopPane</code> class. It provides an implementation of the 612 * Java Accessibility API appropriate to desktop pane user-interface 613 * elements. 614 * <p> 615 * <strong>Warning:</strong> 616 * Serialized objects of this class will not be compatible with 617 * future Swing releases. The current serialization support is 618 * appropriate for short term storage or RMI between applications running 619 * the same version of Swing. As of 1.4, support for long term storage 620 * of all JavaBeans™ 621 * has been added to the <code>java.beans</code> package. 622 * Please see {@link java.beans.XMLEncoder}. 623 */ 624 @SuppressWarnings("serial") // Same-version serialization only 625 protected class AccessibleJDesktopPane extends AccessibleJComponent { 626 627 /** 628 * Get the role of this object. 629 * 630 * @return an instance of AccessibleRole describing the role of the 631 * object 632 * @see AccessibleRole 633 */ 634 public AccessibleRole getAccessibleRole() { 635 return AccessibleRole.DESKTOP_PANE; 636 } 637 } 638 }