2422 lines · 84.0 KB · Java
RawDownload
/* * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.swing; import java.awt.*; import java.beans.PropertyVetoException;import java.beans.PropertyChangeEvent; import javax.swing.event.InternalFrameEvent;import javax.swing.event.InternalFrameListener;import javax.swing.plaf.*; import javax.accessibility.*; import java.io.ObjectOutputStream;import java.io.IOException;import java.lang.StringBuilder;import java.beans.PropertyChangeListener;import sun.awt.AppContext;import sun.swing.SwingUtilities2;  /** * A lightweight object that provides many of the features of * a native frame, including dragging, closing, becoming an icon, * resizing, title display, and support for a menu bar. * For task-oriented documentation and examples of using internal frames, * see <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html" target="_top">How to Use Internal Frames</a>, * a section in <em>The Java Tutorial</em>. * * <p> * * Generally, * you add <code>JInternalFrame</code>s to a <code>JDesktopPane</code>. The UI * delegates the look-and-feel-specific actions to the * <code>DesktopManager</code> * object maintained by the <code>JDesktopPane</code>. * <p> * The <code>JInternalFrame</code> content pane * is where you add child components. * As a conveniance <code>add</code> and its variants, <code>remove</code> and * <code>setLayout</code> have been overridden to forward to the * <code>contentPane</code> as necessary. This means you can write: * <pre> *       internalFrame.add(child); * </pre> * And the child will be added to the contentPane. * The content pane is actually managed by an instance of * <code>JRootPane</code>, * which also manages a layout pane, glass pane, and * optional menu bar for the internal frame. Please see the * <code>JRootPane</code> * documentation for a complete description of these components. * Refer to {@link javax.swing.RootPaneContainer} * for details on adding, removing and setting the <code>LayoutManager</code> * of a <code>JInternalFrame</code>. * <p> * <strong>Warning:</strong> Swing is not thread safe. For more * information see <a * href="package-summary.html#threading">Swing's Threading * Policy</a>. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing.  As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @see InternalFrameEvent * @see JDesktopPane * @see DesktopManager * @see JInternalFrame.JDesktopIcon * @see JRootPane * @see javax.swing.RootPaneContainer * * @author David Kloba * @author Rich Schiavi * @beaninfo *      attribute: isContainer true *      attribute: containerDelegate getContentPane *      description: A frame container which is contained within *                   another window. */public class JInternalFrame extends JComponent implements        Accessible, WindowConstants,        RootPaneContainer{    /**     * @see #getUIClassID     * @see #readObject     */    private static final String uiClassID = "InternalFrameUI";     /**     * The <code>JRootPane</code> instance that manages the     * content pane     * and optional menu bar for this internal frame, as well as the     * glass pane.     *     * @see JRootPane     * @see RootPaneContainer     */    protected JRootPane rootPane;     /**     * If true then calls to <code>add</code> and <code>setLayout</code>     * will be forwarded to the <code>contentPane</code>. This is initially     * false, but is set to true when the <code>JInternalFrame</code> is     * constructed.     *     * @see #isRootPaneCheckingEnabled     * @see #setRootPaneCheckingEnabled     * @see javax.swing.RootPaneContainer     */    protected boolean rootPaneCheckingEnabled = false;     /** The frame can be closed. */    protected boolean closable;    /** The frame has been closed. */    protected boolean isClosed;    /** The frame can be expanded to the size of the desktop pane. */    protected boolean maximizable;    /**     * The frame has been expanded to its maximum size.     * @see #maximizable     */    protected boolean isMaximum;    /**     * The frame can "iconified" (shrunk down and displayed as     * an icon-image).     * @see JInternalFrame.JDesktopIcon     * @see #setIconifiable     */    protected boolean iconable;    /**     * The frame has been iconified.     * @see #isIcon()     */    protected boolean isIcon;    /** The frame's size can be changed. */    protected boolean resizable;    /** The frame is currently selected. */    protected boolean isSelected;    /** The icon shown in the top-left corner of this internal frame. */    protected Icon frameIcon;    /** The title displayed in this internal frame's title bar. */    protected String  title;    /**     * The icon that is displayed when this internal frame is iconified.     * @see #iconable     */    protected JDesktopIcon desktopIcon;     private Cursor lastCursor;     private boolean opened;     private Rectangle normalBounds = null;     private int defaultCloseOperation = DISPOSE_ON_CLOSE;     /**     * Contains the Component that focus is to go when     * <code>restoreSubcomponentFocus</code> is invoked, that is,     * <code>restoreSubcomponentFocus</code> sets this to the value returned     * from <code>getMostRecentFocusOwner</code>.     */    private Component lastFocusOwner;     /** Bound property name. */    public final static String CONTENT_PANE_PROPERTY = "contentPane";    /** Bound property name. */    public final static String MENU_BAR_PROPERTY = "JMenuBar";    /** Bound property name. */    public final static String TITLE_PROPERTY = "title";    /** Bound property name. */    public final static String LAYERED_PANE_PROPERTY = "layeredPane";    /** Bound property name. */    public final static String ROOT_PANE_PROPERTY = "rootPane";    /** Bound property name. */    public final static String GLASS_PANE_PROPERTY = "glassPane";    /** Bound property name. */    public final static String FRAME_ICON_PROPERTY = "frameIcon";     /**     * Constrained property name indicated that this frame has     * selected status.     */    public final static String IS_SELECTED_PROPERTY = "selected";    /** Constrained property name indicating that the internal frame is closed. */    public final static String IS_CLOSED_PROPERTY = "closed";    /** Constrained property name indicating that the internal frame is maximized. */    public final static String IS_MAXIMUM_PROPERTY = "maximum";    /** Constrained property name indicating that the internal frame is iconified. */    public final static String IS_ICON_PROPERTY = "icon";     private static final Object PROPERTY_CHANGE_LISTENER_KEY =        new StringBuilder("InternalFramePropertyChangeListener");     private static void addPropertyChangeListenerIfNecessary() {        if (AppContext.getAppContext().get(PROPERTY_CHANGE_LISTENER_KEY) ==            null) {            PropertyChangeListener focusListener =                new FocusPropertyChangeListener();             AppContext.getAppContext().put(PROPERTY_CHANGE_LISTENER_KEY,                focusListener);             KeyboardFocusManager.getCurrentKeyboardFocusManager().                addPropertyChangeListener(focusListener);        }    }     private static class FocusPropertyChangeListener implements        PropertyChangeListener {        public void propertyChange(PropertyChangeEvent e) {            if (e.getPropertyName() == "permanentFocusOwner") {                updateLastFocusOwner((Component)e.getNewValue());            }        }    }     private static void updateLastFocusOwner(Component component) {        if (component != null) {            Component parent = component;            while (parent != null && !(parent instanceof Window)) {                if (parent instanceof JInternalFrame) {                    // Update lastFocusOwner for parent.                    ((JInternalFrame)parent).setLastFocusOwner(component);                }                parent = parent.getParent();            }        }    }     /**     * Creates a non-resizable, non-closable, non-maximizable,     * non-iconifiable <code>JInternalFrame</code> with no title.     */    public JInternalFrame() {        this("", false, false, false, false);    }     /**     * Creates a non-resizable, non-closable, non-maximizable,     * non-iconifiable <code>JInternalFrame</code> with the specified title.     * Note that passing in a <code>null</code> <code>title</code> results in     * unspecified behavior and possibly an exception.     *     * @param title  the non-<code>null</code> <code>String</code>     *     to display in the title bar     */    public JInternalFrame(String title) {        this(title, false, false, false, false);    }     /**     * Creates a non-closable, non-maximizable, non-iconifiable     * <code>JInternalFrame</code> with the specified title     * and resizability.     *     * @param title      the <code>String</code> to display in the title bar     * @param resizable  if <code>true</code>, the internal frame can be resized     */    public JInternalFrame(String title, boolean resizable) {        this(title, resizable, false, false, false);    }     /**     * Creates a non-maximizable, non-iconifiable <code>JInternalFrame</code>     * with the specified title, resizability, and     * closability.     *     * @param title      the <code>String</code> to display in the title bar     * @param resizable  if <code>true</code>, the internal frame can be resized     * @param closable   if <code>true</code>, the internal frame can be closed     */    public JInternalFrame(String title, boolean resizable, boolean closable) {        this(title, resizable, closable, false, false);    }     /**     * Creates a non-iconifiable <code>JInternalFrame</code>     * with the specified title,     * resizability, closability, and maximizability.     *     * @param title       the <code>String</code> to display in the title bar     * @param resizable   if <code>true</code>, the internal frame can be resized     * @param closable    if <code>true</code>, the internal frame can be closed     * @param maximizable if <code>true</code>, the internal frame can be maximized     */    public JInternalFrame(String title, boolean resizable, boolean closable,                          boolean maximizable) {        this(title, resizable, closable, maximizable, false);    }     /**     * Creates a <code>JInternalFrame</code> with the specified title,     * resizability, closability, maximizability, and iconifiability.     * All <code>JInternalFrame</code> constructors use this one.     *     * @param title       the <code>String</code> to display in the title bar     * @param resizable   if <code>true</code>, the internal frame can be resized     * @param closable    if <code>true</code>, the internal frame can be closed     * @param maximizable if <code>true</code>, the internal frame can be maximized     * @param iconifiable if <code>true</code>, the internal frame can be iconified     */    public JInternalFrame(String title, boolean resizable, boolean closable,                                boolean maximizable, boolean iconifiable) {         setRootPane(createRootPane());        setLayout(new BorderLayout());        this.title = title;        this.resizable = resizable;        this.closable = closable;        this.maximizable = maximizable;        isMaximum = false;        this.iconable = iconifiable;        isIcon = false;        setVisible(false);        setRootPaneCheckingEnabled(true);        desktopIcon = new JDesktopIcon(this);        updateUI();        sun.awt.SunToolkit.checkAndSetPolicy(this, true);        addPropertyChangeListenerIfNecessary();    }     /**     * Called by the constructor to set up the <code>JRootPane</code>.     * @return  a new <code>JRootPane</code>     * @see JRootPane     */    protected JRootPane createRootPane() {        return new JRootPane();    }     /**     * Returns the look-and-feel object that renders this component.     *     * @return the <code>InternalFrameUI</code> object that renders     *          this component     */    public InternalFrameUI getUI() {        return (InternalFrameUI)ui;    }     /**     * Sets the UI delegate for this <code>JInternalFrame</code>.     * @param ui  the UI delegate     * @beaninfo     *        bound: true     *       hidden: true     *    attribute: visualUpdate true     *  description: The UI object that implements the Component's LookAndFeel.     */    public void setUI(InternalFrameUI ui) {        boolean checkingEnabled = isRootPaneCheckingEnabled();        try {            setRootPaneCheckingEnabled(false);            super.setUI(ui);        }        finally {            setRootPaneCheckingEnabled(checkingEnabled);        }    }     /**     * Notification from the <code>UIManager</code> that the look and feel     * has changed.     * Replaces the current UI object with the latest version from the     * <code>UIManager</code>.     *     * @see JComponent#updateUI     */    public void updateUI() {        setUI((InternalFrameUI)UIManager.getUI(this));        invalidate();        if (desktopIcon != null) {            desktopIcon.updateUIWhenHidden();        }    }     /* This method is called if <code>updateUI</code> was called     * on the associated     * JDesktopIcon.  It's necessary to avoid infinite recursion.     */    void updateUIWhenHidden() {        setUI((InternalFrameUI)UIManager.getUI(this));        invalidate();        Component[] children = getComponents();        if (children != null) {            for (Component child : children) {                SwingUtilities.updateComponentTreeUI(child);            }        }    }      /**     * Returns the name of the look-and-feel     * class that renders this component.     *     * @return the string "InternalFrameUI"     *     * @see JComponent#getUIClassID     * @see UIDefaults#getUI     *     * @beaninfo     *     description: UIClassID     */    public String getUIClassID() {        return uiClassID;    }     /**     * Returns whether calls to <code>add</code> and     * <code>setLayout</code> are forwarded to the <code>contentPane</code>.     *     * @return true if <code>add</code> and <code>setLayout</code>     *         are fowarded; false otherwise     *     * @see #addImpl     * @see #setLayout     * @see #setRootPaneCheckingEnabled     * @see javax.swing.RootPaneContainer     */    protected boolean isRootPaneCheckingEnabled() {        return rootPaneCheckingEnabled;    }     /**     * Sets whether calls to <code>add</code> and     * <code>setLayout</code> are forwarded to the <code>contentPane</code>.     *     * @param enabled  true if <code>add</code> and <code>setLayout</code>     *        are forwarded, false if they should operate directly on the     *        <code>JInternalFrame</code>.     *     * @see #addImpl     * @see #setLayout     * @see #isRootPaneCheckingEnabled     * @see javax.swing.RootPaneContainer     * @beaninfo     *      hidden: true     * description: Whether the add and setLayout methods are forwarded     */    protected void setRootPaneCheckingEnabled(boolean enabled) {        rootPaneCheckingEnabled = enabled;    }     /**     * Adds the specified child <code>Component</code>.     * This method is overridden to conditionally forward calls to the     * <code>contentPane</code>.     * By default, children are added to the <code>contentPane</code> instead     * of the frame, refer to {@link javax.swing.RootPaneContainer} for     * details.     *     * @param comp the component to be enhanced     * @param constraints the constraints to be respected     * @param index the index     * @exception IllegalArgumentException if <code>index</code> is invalid     * @exception IllegalArgumentException if adding the container's parent     *                  to itself     * @exception IllegalArgumentException if adding a window to a container     *     * @see #setRootPaneCheckingEnabled     * @see javax.swing.RootPaneContainer     */    protected void addImpl(Component comp, Object constraints, int index) {        if(isRootPaneCheckingEnabled()) {            getContentPane().add(comp, constraints, index);

Lines 1–500 of 2422.