0N/A/*
6101N/A * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing;
0N/A
0N/Aimport java.util.List;
0N/Aimport java.util.ArrayList;
4989N/Aimport java.util.Collection;
4989N/Aimport java.util.Iterator;
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.accessibility.*;
0N/A
0N/Aimport java.awt.Component;
0N/Aimport java.awt.Container;
0N/Aimport java.awt.DefaultFocusTraversalPolicy;
0N/Aimport java.awt.FocusTraversalPolicy;
0N/Aimport java.awt.Window;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.beans.PropertyVetoException;
0N/Aimport java.util.Set;
0N/Aimport java.util.TreeSet;
0N/A/**
0N/A * A container used to create a multiple-document interface or a virtual desktop.
0N/A * You create <code>JInternalFrame</code> objects and add them to the
0N/A * <code>JDesktopPane</code>. <code>JDesktopPane</code> extends
0N/A * <code>JLayeredPane</code> to manage the potentially overlapping internal
0N/A * frames. It also maintains a reference to an instance of
0N/A * <code>DesktopManager</code> that is set by the UI
0N/A * class for the current look and feel (L&F). Note that <code>JDesktopPane</code>
0N/A * does not support borders.
0N/A * <p>
0N/A * This class is normally used as the parent of <code>JInternalFrames</code>
0N/A * to provide a pluggable <code>DesktopManager</code> object to the
0N/A * <code>JInternalFrames</code>. The <code>installUI</code> of the
0N/A * L&F specific implementation is responsible for setting the
0N/A * <code>desktopManager</code> variable appropriately.
0N/A * When the parent of a <code>JInternalFrame</code> is a <code>JDesktopPane</code>,
0N/A * it should delegate most of its behavior to the <code>desktopManager</code>
0N/A * (closing, resizing, etc).
0N/A * <p>
0N/A * For further documentation and examples see
0N/A * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html">How to Use Internal Frames</a>,
0N/A * a section in <em>The Java Tutorial</em>.
0N/A * <p>
0N/A * <strong>Warning:</strong> Swing is not thread safe. For more
0N/A * information see <a
0N/A * href="package-summary.html#threading">Swing's Threading
0N/A * Policy</a>.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @see JInternalFrame
0N/A * @see JInternalFrame.JDesktopIcon
0N/A * @see DesktopManager
0N/A *
0N/A * @author David Kloba
0N/A */
0N/Apublic class JDesktopPane extends JLayeredPane implements Accessible
0N/A{
0N/A /**
0N/A * @see #getUIClassID
0N/A * @see #readObject
0N/A */
0N/A private static final String uiClassID = "DesktopPaneUI";
0N/A
0N/A transient DesktopManager desktopManager;
0N/A
0N/A private transient JInternalFrame selectedFrame = null;
0N/A
0N/A /**
0N/A * Indicates that the entire contents of the item being dragged
0N/A * should appear inside the desktop pane.
0N/A *
0N/A * @see #OUTLINE_DRAG_MODE
0N/A * @see #setDragMode
0N/A */
0N/A public static final int LIVE_DRAG_MODE = 0;
0N/A
0N/A /**
0N/A * Indicates that an outline only of the item being dragged
0N/A * should appear inside the desktop pane.
0N/A *
0N/A * @see #LIVE_DRAG_MODE
0N/A * @see #setDragMode
0N/A */
0N/A public static final int OUTLINE_DRAG_MODE = 1;
0N/A
0N/A private int dragMode = LIVE_DRAG_MODE;
0N/A private boolean dragModeSet = false;
0N/A private transient List<JInternalFrame> framesCache;
0N/A private boolean componentOrderCheckingEnabled = true;
0N/A private boolean componentOrderChanged = false;
0N/A
0N/A /**
0N/A * Creates a new <code>JDesktopPane</code>.
0N/A */
0N/A public JDesktopPane() {
0N/A setUIProperty("opaque", Boolean.TRUE);
0N/A setFocusCycleRoot(true);
0N/A
0N/A setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
0N/A public Component getDefaultComponent(Container c) {
0N/A JInternalFrame jifArray[] = getAllFrames();
0N/A Component comp = null;
625N/A for (JInternalFrame jif : jifArray) {
625N/A comp = jif.getFocusTraversalPolicy().getDefaultComponent(jif);
0N/A if (comp != null) {
0N/A break;
0N/A }
0N/A }
0N/A return comp;
0N/A }
0N/A });
0N/A updateUI();
0N/A }
0N/A
0N/A /**
0N/A * Returns the L&F object that renders this component.
0N/A *
0N/A * @return the <code>DesktopPaneUI</code> object that
0N/A * renders this component
0N/A */
0N/A public DesktopPaneUI getUI() {
0N/A return (DesktopPaneUI)ui;
0N/A }
0N/A
0N/A /**
0N/A * Sets the L&F object that renders this component.
0N/A *
0N/A * @param ui the DesktopPaneUI L&F object
0N/A * @see UIDefaults#getUI
0N/A * @beaninfo
0N/A * bound: true
0N/A * hidden: true
0N/A * attribute: visualUpdate true
0N/A * description: The UI object that implements the Component's LookAndFeel.
0N/A */
0N/A public void setUI(DesktopPaneUI ui) {
0N/A super.setUI(ui);
0N/A }
0N/A
0N/A /**
0N/A * Sets the "dragging style" used by the desktop pane.
0N/A * You may want to change to one mode or another for
0N/A * performance or aesthetic reasons.
0N/A *
0N/A * @param dragMode the style of drag to use for items in the Desktop
0N/A *
0N/A * @see #LIVE_DRAG_MODE
0N/A * @see #OUTLINE_DRAG_MODE
0N/A *
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: Dragging style for internal frame children.
0N/A * enum: LIVE_DRAG_MODE JDesktopPane.LIVE_DRAG_MODE
0N/A * OUTLINE_DRAG_MODE JDesktopPane.OUTLINE_DRAG_MODE
0N/A * @since 1.3
0N/A */
0N/A public void setDragMode(int dragMode) {
0N/A int oldDragMode = this.dragMode;
0N/A this.dragMode = dragMode;
0N/A firePropertyChange("dragMode", oldDragMode, this.dragMode);
0N/A dragModeSet = true;
0N/A }
0N/A
0N/A /**
0N/A * Gets the current "dragging style" used by the desktop pane.
0N/A * @return either <code>Live_DRAG_MODE</code> or
0N/A * <code>OUTLINE_DRAG_MODE</code>
0N/A * @see #setDragMode
0N/A * @since 1.3
0N/A */
0N/A public int getDragMode() {
0N/A return dragMode;
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>DesktopManger</code> that handles
0N/A * desktop-specific UI actions.
0N/A */
0N/A public DesktopManager getDesktopManager() {
0N/A return desktopManager;
0N/A }
0N/A
0N/A /**
0N/A * Sets the <code>DesktopManger</code> that will handle
2784N/A * desktop-specific UI actions. This may be overridden by
2784N/A * {@code LookAndFeel}.
0N/A *
0N/A * @param d the <code>DesktopManager</code> to use
0N/A *
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: Desktop manager to handle the internal frames in the
0N/A * desktop pane.
0N/A */
0N/A public void setDesktopManager(DesktopManager d) {
0N/A DesktopManager oldValue = desktopManager;
0N/A desktopManager = d;
0N/A firePropertyChange("desktopManager", oldValue, desktopManager);
0N/A }
0N/A
0N/A /**
0N/A * Notification from the <code>UIManager</code> that the L&F has changed.
0N/A * Replaces the current UI object with the latest version from the
0N/A * <code>UIManager</code>.
0N/A *
0N/A * @see JComponent#updateUI
0N/A */
0N/A public void updateUI() {
0N/A setUI((DesktopPaneUI)UIManager.getUI(this));
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the name of the L&F class that renders this component.
0N/A *
0N/A * @return the string "DesktopPaneUI"
0N/A * @see JComponent#getUIClassID
0N/A * @see UIDefaults#getUI
0N/A */
0N/A public String getUIClassID() {
0N/A return uiClassID;
0N/A }
0N/A
0N/A /**
0N/A * Returns all <code>JInternalFrames</code> currently displayed in the
0N/A * desktop. Returns iconified frames as well as expanded frames.
0N/A *
0N/A * @return an array of <code>JInternalFrame</code> objects
0N/A */
0N/A public JInternalFrame[] getAllFrames() {
4989N/A return getAllFrames(this).toArray(new JInternalFrame[0]);
4989N/A }
0N/A
4989N/A private static Collection<JInternalFrame> getAllFrames(Container parent) {
4989N/A int i, count;
4989N/A Collection<JInternalFrame> results = new ArrayList<JInternalFrame>();
4989N/A count = parent.getComponentCount();
4989N/A for (i = 0; i < count; i++) {
4989N/A Component next = parent.getComponent(i);
4989N/A if (next instanceof JInternalFrame) {
4989N/A results.add((JInternalFrame) next);
4989N/A } else if (next instanceof JInternalFrame.JDesktopIcon) {
4989N/A JInternalFrame tmp = ((JInternalFrame.JDesktopIcon) next).getInternalFrame();
4989N/A if (tmp != null) {
4989N/A results.add(tmp);
4989N/A }
4989N/A } else if (next instanceof Container) {
4989N/A results.addAll(getAllFrames((Container) next));
0N/A }
0N/A }
0N/A return results;
0N/A }
0N/A
0N/A /** Returns the currently active <code>JInternalFrame</code>
0N/A * in this <code>JDesktopPane</code>, or <code>null</code>
0N/A * if no <code>JInternalFrame</code> is currently active.
0N/A *
0N/A * @return the currently active <code>JInternalFrame</code> or
0N/A * <code>null</code>
0N/A * @since 1.3
0N/A */
0N/A
0N/A public JInternalFrame getSelectedFrame() {
0N/A return selectedFrame;
0N/A }
0N/A
0N/A /** Sets the currently active <code>JInternalFrame</code>
0N/A * in this <code>JDesktopPane</code>. This method is used to bridge
0N/A * the package gap between JDesktopPane and the platform implementation
0N/A * code and should not be called directly. To visually select the frame
0N/A * the client must call JInternalFrame.setSelected(true) to activate
0N/A * the frame.
0N/A * @see JInternalFrame#setSelected(boolean)
0N/A *
0N/A * @param f the internal frame that's currently selected
0N/A * @since 1.3
0N/A */
0N/A
0N/A public void setSelectedFrame(JInternalFrame f) {
0N/A selectedFrame = f;
0N/A }
0N/A
0N/A /**
0N/A * Returns all <code>JInternalFrames</code> currently displayed in the
0N/A * specified layer of the desktop. Returns iconified frames as well
0N/A * expanded frames.
0N/A *
0N/A * @param layer an int specifying the desktop layer
0N/A * @return an array of <code>JInternalFrame</code> objects
0N/A * @see JLayeredPane
0N/A */
0N/A public JInternalFrame[] getAllFramesInLayer(int layer) {
4989N/A Collection<JInternalFrame> allFrames = getAllFrames(this);
4989N/A Iterator<JInternalFrame> iterator = allFrames.iterator();
4989N/A while (iterator.hasNext()) {
4989N/A if (iterator.next().getLayer() != layer) {
4989N/A iterator.remove();
0N/A }
0N/A }
4989N/A return allFrames.toArray(new JInternalFrame[0]);
0N/A }
0N/A
0N/A private List<JInternalFrame> getFrames() {
0N/A Component c;
0N/A Set<ComponentPosition> set = new TreeSet<ComponentPosition>();
0N/A for (int i = 0; i < getComponentCount(); i++) {
0N/A c = getComponent(i);
0N/A if (c instanceof JInternalFrame) {
0N/A set.add(new ComponentPosition((JInternalFrame)c, getLayer(c),
0N/A i));
0N/A }
0N/A else if (c instanceof JInternalFrame.JDesktopIcon) {
0N/A c = ((JInternalFrame.JDesktopIcon)c).getInternalFrame();
0N/A set.add(new ComponentPosition((JInternalFrame)c, getLayer(c),
0N/A i));
0N/A }
0N/A }
0N/A List<JInternalFrame> frames = new ArrayList<JInternalFrame>(
0N/A set.size());
0N/A for (ComponentPosition position : set) {
0N/A frames.add(position.component);
0N/A }
0N/A return frames;
0N/A }
0N/A
0N/A private static class ComponentPosition implements
0N/A Comparable<ComponentPosition> {
0N/A private final JInternalFrame component;
0N/A private final int layer;
0N/A private final int zOrder;
0N/A
0N/A ComponentPosition(JInternalFrame component, int layer, int zOrder) {
0N/A this.component = component;
0N/A this.layer = layer;
0N/A this.zOrder = zOrder;
0N/A }
0N/A
0N/A public int compareTo(ComponentPosition o) {
0N/A int delta = o.layer - layer;
0N/A if (delta == 0) {
0N/A return zOrder - o.zOrder;
0N/A }
0N/A return delta;
0N/A }
0N/A }
0N/A
0N/A private JInternalFrame getNextFrame(JInternalFrame f, boolean forward) {
0N/A verifyFramesCache();
0N/A if (f == null) {
0N/A return getTopInternalFrame();
0N/A }
0N/A int i = framesCache.indexOf(f);
0N/A if (i == -1 || framesCache.size() == 1) {
0N/A /* error */
0N/A return null;
0N/A }
0N/A if (forward) {
0N/A // navigate to the next frame
0N/A if (++i == framesCache.size()) {
0N/A /* wrap */
0N/A i = 0;
0N/A }
0N/A }
0N/A else {
0N/A // navigate to the previous frame
0N/A if (--i == -1) {
0N/A /* wrap */
0N/A i = framesCache.size() - 1;
0N/A }
0N/A }
0N/A return framesCache.get(i);
0N/A }
0N/A
0N/A JInternalFrame getNextFrame(JInternalFrame f) {
0N/A return getNextFrame(f, true);
0N/A }
0N/A
0N/A private JInternalFrame getTopInternalFrame() {
0N/A if (framesCache.size() == 0) {
0N/A return null;
0N/A }
0N/A return framesCache.get(0);
0N/A }
0N/A
0N/A private void updateFramesCache() {
0N/A framesCache = getFrames();
0N/A }
0N/A
0N/A private void verifyFramesCache() {
0N/A // If framesCache is dirty, then recreate it.
0N/A if (componentOrderChanged) {
0N/A componentOrderChanged = false;
0N/A updateFramesCache();
0N/A }
0N/A }
0N/A
0N/A /**
6101N/A * {@inheritDoc}
6101N/A */
6101N/A @Override
6101N/A public void remove(Component comp) {
6101N/A super.remove(comp);
6101N/A updateFramesCache();
6101N/A }
6101N/A
6101N/A /**
0N/A * Selects the next <code>JInternalFrame</code> in this desktop pane.
0N/A *
0N/A * @param forward a boolean indicating which direction to select in;
0N/A * <code>true</code> for forward, <code>false</code> for
0N/A * backward
0N/A * @return the JInternalFrame that was selected or <code>null</code>
0N/A * if nothing was selected
0N/A * @since 1.6
0N/A */
0N/A public JInternalFrame selectFrame(boolean forward) {
0N/A JInternalFrame selectedFrame = getSelectedFrame();
0N/A JInternalFrame frameToSelect = getNextFrame(selectedFrame, forward);
0N/A if (frameToSelect == null) {
0N/A return null;
0N/A }
0N/A // Maintain navigation traversal order until an
0N/A // external stack change, such as a click on a frame.
0N/A setComponentOrderCheckingEnabled(false);
0N/A if (forward && selectedFrame != null) {
0N/A selectedFrame.moveToBack(); // For Windows MDI fidelity.
0N/A }
0N/A try { frameToSelect.setSelected(true);
0N/A } catch (PropertyVetoException pve) {}
0N/A setComponentOrderCheckingEnabled(true);
0N/A return frameToSelect;
0N/A }
0N/A
0N/A /*
0N/A * Sets whether component order checking is enabled.
0N/A * @param enable a boolean value, where <code>true</code> means
0N/A * a change in component order will cause a change in the keyboard
0N/A * navigation order.
0N/A * @since 1.6
0N/A */
0N/A void setComponentOrderCheckingEnabled(boolean enable) {
0N/A componentOrderCheckingEnabled = enable;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.6
0N/A */
0N/A protected void addImpl(Component comp, Object constraints, int index) {
0N/A super.addImpl(comp, constraints, index);
0N/A if (componentOrderCheckingEnabled) {
0N/A if (comp instanceof JInternalFrame ||
0N/A comp instanceof JInternalFrame.JDesktopIcon) {
0N/A componentOrderChanged = true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.6
0N/A */
0N/A public void remove(int index) {
0N/A if (componentOrderCheckingEnabled) {
0N/A Component comp = getComponent(index);
0N/A if (comp instanceof JInternalFrame ||
0N/A comp instanceof JInternalFrame.JDesktopIcon) {
0N/A componentOrderChanged = true;
0N/A }
0N/A }
0N/A super.remove(index);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.6
0N/A */
0N/A public void removeAll() {
0N/A if (componentOrderCheckingEnabled) {
0N/A int count = getComponentCount();
0N/A for (int i = 0; i < count; i++) {
0N/A Component comp = getComponent(i);
0N/A if (comp instanceof JInternalFrame ||
0N/A comp instanceof JInternalFrame.JDesktopIcon) {
0N/A componentOrderChanged = true;
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A super.removeAll();
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.6
0N/A */
0N/A public void setComponentZOrder(Component comp, int index) {
0N/A super.setComponentZOrder(comp, index);
0N/A if (componentOrderCheckingEnabled) {
0N/A if (comp instanceof JInternalFrame ||
0N/A comp instanceof JInternalFrame.JDesktopIcon) {
0N/A componentOrderChanged = true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * See readObject() and writeObject() in JComponent for more
0N/A * information about serialization in Swing.
0N/A */
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A s.defaultWriteObject();
0N/A if (getUIClassID().equals(uiClassID)) {
0N/A byte count = JComponent.getWriteObjCounter(this);
0N/A JComponent.setWriteObjCounter(this, --count);
0N/A if (count == 0 && ui != null) {
0N/A ui.installUI(this);
0N/A }
0N/A }
0N/A }
0N/A
0N/A void setUIProperty(String propertyName, Object value) {
0N/A if (propertyName == "dragMode") {
0N/A if (!dragModeSet) {
0N/A setDragMode(((Integer)value).intValue());
0N/A dragModeSet = false;
0N/A }
0N/A } else {
0N/A super.setUIProperty(propertyName, value);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this <code>JDesktopPane</code>.
0N/A * This method is intended to be used only for debugging purposes, and the
0N/A * content and format of the returned string may vary between
0N/A * implementations. The returned string may be empty but may not
0N/A * be <code>null</code>.
0N/A *
0N/A * @return a string representation of this <code>JDesktopPane</code>
0N/A */
0N/A protected String paramString() {
0N/A String desktopManagerString = (desktopManager != null ?
0N/A desktopManager.toString() : "");
0N/A
0N/A return super.paramString() +
0N/A ",desktopManager=" + desktopManagerString;
0N/A }
0N/A
0N/A/////////////////
0N/A// Accessibility support
0N/A////////////////
0N/A
0N/A /**
0N/A * Gets the <code>AccessibleContext</code> associated with this
0N/A * <code>JDesktopPane</code>. For desktop panes, the
0N/A * <code>AccessibleContext</code> takes the form of an
0N/A * <code>AccessibleJDesktopPane</code>.
0N/A * A new <code>AccessibleJDesktopPane</code> instance is created if necessary.
0N/A *
0N/A * @return an <code>AccessibleJDesktopPane</code> that serves as the
0N/A * <code>AccessibleContext</code> of this <code>JDesktopPane</code>
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleJDesktopPane();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * <code>JDesktopPane</code> class. It provides an implementation of the
0N/A * Java Accessibility API appropriate to desktop pane user-interface
0N/A * elements.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A protected class AccessibleJDesktopPane extends AccessibleJComponent {
0N/A
0N/A /**
0N/A * Get the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the
0N/A * object
0N/A * @see AccessibleRole
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.DESKTOP_PANE;
0N/A }
0N/A }
0N/A}