2246 lines · 76.4 KB · Java
RawDownload
/* * Copyright (c) 1997, 2011, 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.plaf.basic;  import sun.swing.DefaultLookup;import sun.swing.UIAction;import javax.swing.*;import javax.swing.border.Border;import java.awt.*;import java.awt.event.*;import java.awt.peer.ComponentPeer;import java.awt.peer.LightweightPeer;import java.beans.*;import java.util.*;import javax.swing.plaf.SplitPaneUI;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.UIResource;import sun.swing.SwingUtilities2;  /** * A Basic L&F implementation of the SplitPaneUI. * * @author Scott Violet * @author Steve Wilson * @author Ralph Kar */public class BasicSplitPaneUI extends SplitPaneUI{    /**     * The divider used for non-continuous layout is added to the split pane     * with this object.     */    protected static final String NON_CONTINUOUS_DIVIDER =        "nonContinuousDivider";      /**     * How far (relative) the divider does move when it is moved around by     * the cursor keys on the keyboard.     */    protected static int KEYBOARD_DIVIDER_MOVE_OFFSET = 3;      /**     * JSplitPane instance this instance is providing     * the look and feel for.     */    protected JSplitPane splitPane;      /**     * LayoutManager that is created and placed into the split pane.     */    protected BasicHorizontalLayoutManager layoutManager;      /**     * Instance of the divider for this JSplitPane.     */    protected BasicSplitPaneDivider divider;      /**     * Instance of the PropertyChangeListener for this JSplitPane.     */    protected PropertyChangeListener propertyChangeListener;      /**     * Instance of the FocusListener for this JSplitPane.     */    protected FocusListener focusListener;     private Handler handler;      /**     * Keys to use for forward focus traversal when the JComponent is     * managing focus.     */    private Set<KeyStroke> managingFocusForwardTraversalKeys;     /**     * Keys to use for backward focus traversal when the JComponent is     * managing focus.     */    private Set<KeyStroke> managingFocusBackwardTraversalKeys;      /**     * The size of the divider while the dragging session is valid.     */    protected int dividerSize;      /**     * Instance for the shadow of the divider when non continuous layout     * is being used.     */    protected Component nonContinuousLayoutDivider;      /**     * Set to true in startDragging if any of the children     * (not including the nonContinuousLayoutDivider) are heavy weights.     */    protected boolean draggingHW;      /**     * Location of the divider when the dragging session began.     */    protected int beginDragDividerLocation;      /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected KeyStroke upKey;    /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected KeyStroke downKey;    /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected KeyStroke leftKey;    /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected KeyStroke rightKey;    /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected KeyStroke homeKey;    /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected KeyStroke endKey;    /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected KeyStroke dividerResizeToggleKey;     /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected ActionListener keyboardUpLeftListener;    /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected ActionListener keyboardDownRightListener;    /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected ActionListener keyboardHomeListener;    /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected ActionListener keyboardEndListener;    /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected ActionListener keyboardResizeToggleListener;      // Private data of the instance    private int         orientation;    private int         lastDragLocation;    private boolean     continuousLayout;    private boolean     dividerKeyboardResize;    private boolean     dividerLocationIsSet;  // needed for tracking                                               // the first occurrence of                                               // setDividerLocation()    private Color dividerDraggingColor;    private boolean rememberPaneSizes;     // Indicates wether the one of splitpane sides is expanded    private boolean keepHidden = false;     /** Indicates that we have painted once. */    // This is used by the LayoutManager to determine when it should use    // the divider location provided by the JSplitPane. This is used as there    // is no way to determine when the layout process has completed.    boolean             painted;    /** If true, setDividerLocation does nothing. */    boolean             ignoreDividerLocationChange;      /**     * Creates a new BasicSplitPaneUI instance     */    public static ComponentUI createUI(JComponent x) {        return new BasicSplitPaneUI();    }     static void loadActionMap(LazyActionMap map) {        map.put(new Actions(Actions.NEGATIVE_INCREMENT));        map.put(new Actions(Actions.POSITIVE_INCREMENT));        map.put(new Actions(Actions.SELECT_MIN));        map.put(new Actions(Actions.SELECT_MAX));        map.put(new Actions(Actions.START_RESIZE));        map.put(new Actions(Actions.TOGGLE_FOCUS));        map.put(new Actions(Actions.FOCUS_OUT_FORWARD));        map.put(new Actions(Actions.FOCUS_OUT_BACKWARD));    }       /**     * Installs the UI.     */    public void installUI(JComponent c) {        splitPane = (JSplitPane) c;        dividerLocationIsSet = false;        dividerKeyboardResize = false;        keepHidden = false;        installDefaults();        installListeners();        installKeyboardActions();        setLastDragLocation(-1);    }      /**     * Installs the UI defaults.     */    protected void installDefaults(){        LookAndFeel.installBorder(splitPane, "SplitPane.border");        LookAndFeel.installColors(splitPane, "SplitPane.background",                                  "SplitPane.foreground");        LookAndFeel.installProperty(splitPane, "opaque", Boolean.TRUE);         if (divider == null) divider = createDefaultDivider();        divider.setBasicSplitPaneUI(this);         Border    b = divider.getBorder();         if (b == null || !(b instanceof UIResource)) {            divider.setBorder(UIManager.getBorder("SplitPaneDivider.border"));        }         dividerDraggingColor = UIManager.getColor("SplitPaneDivider.draggingColor");         setOrientation(splitPane.getOrientation());         // note: don't rename this temp variable to dividerSize        // since it will conflict with "this.dividerSize" field        Integer temp = (Integer)UIManager.get("SplitPane.dividerSize");        LookAndFeel.installProperty(splitPane, "dividerSize", temp == null? 10: temp);         divider.setDividerSize(splitPane.getDividerSize());        dividerSize = divider.getDividerSize();        splitPane.add(divider, JSplitPane.DIVIDER);         setContinuousLayout(splitPane.isContinuousLayout());         resetLayoutManager();         /* Install the nonContinuousLayoutDivider here to avoid having to        add/remove everything later. */        if(nonContinuousLayoutDivider == null) {            setNonContinuousLayoutDivider(                                createDefaultNonContinuousLayoutDivider(),                                true);        } else {            setNonContinuousLayoutDivider(nonContinuousLayoutDivider, true);        }         // focus forward traversal key        if (managingFocusForwardTraversalKeys==null) {            managingFocusForwardTraversalKeys = new HashSet<KeyStroke>();            managingFocusForwardTraversalKeys.add(                KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));        }        splitPane.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,                                        managingFocusForwardTraversalKeys);        // focus backward traversal key        if (managingFocusBackwardTraversalKeys==null) {            managingFocusBackwardTraversalKeys = new HashSet<KeyStroke>();            managingFocusBackwardTraversalKeys.add(                KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));        }        splitPane.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,                                        managingFocusBackwardTraversalKeys);    }      /**     * Installs the event listeners for the UI.     */    protected void installListeners() {        if ((propertyChangeListener = createPropertyChangeListener()) !=            null) {            splitPane.addPropertyChangeListener(propertyChangeListener);        }         if ((focusListener = createFocusListener()) != null) {            splitPane.addFocusListener(focusListener);        }    }      /**     * Installs the keyboard actions for the UI.     */    protected void installKeyboardActions() {        InputMap km = getInputMap(JComponent.                                  WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);         SwingUtilities.replaceUIInputMap(splitPane, JComponent.                                       WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,                                       km);        LazyActionMap.installLazyActionMap(splitPane, BasicSplitPaneUI.class,                                           "SplitPane.actionMap");    }     InputMap getInputMap(int condition) {        if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {            return (InputMap)DefaultLookup.get(splitPane, this,                                               "SplitPane.ancestorInputMap");        }        return null;    }     /**     * Uninstalls the UI.     */    public void uninstallUI(JComponent c) {        uninstallKeyboardActions();        uninstallListeners();        uninstallDefaults();        dividerLocationIsSet = false;        dividerKeyboardResize = false;        splitPane = null;    }      /**     * Uninstalls the UI defaults.     */    protected void uninstallDefaults() {        if(splitPane.getLayout() == layoutManager) {            splitPane.setLayout(null);        }         if(nonContinuousLayoutDivider != null) {            splitPane.remove(nonContinuousLayoutDivider);        }         LookAndFeel.uninstallBorder(splitPane);         Border    b = divider.getBorder();         if (b instanceof UIResource) {            divider.setBorder(null);        }         splitPane.remove(divider);        divider.setBasicSplitPaneUI(null);        layoutManager = null;        divider = null;        nonContinuousLayoutDivider = null;         setNonContinuousLayoutDivider(null);         // sets the focus forward and backward traversal keys to null        // to restore the defaults        splitPane.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);        splitPane.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);    }      /**     * Uninstalls the event listeners for the UI.     */    protected void uninstallListeners() {        if (propertyChangeListener != null) {            splitPane.removePropertyChangeListener(propertyChangeListener);            propertyChangeListener = null;        }        if (focusListener != null) {            splitPane.removeFocusListener(focusListener);            focusListener = null;        }         keyboardUpLeftListener = null;        keyboardDownRightListener = null;        keyboardHomeListener = null;        keyboardEndListener = null;        keyboardResizeToggleListener = null;        handler = null;    }      /**     * Uninstalls the keyboard actions for the UI.     */    protected void uninstallKeyboardActions() {        SwingUtilities.replaceUIActionMap(splitPane, null);        SwingUtilities.replaceUIInputMap(splitPane, JComponent.                                      WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,

Lines 1–500 of 2246.