📄 basicsplitpaneui.java
字号:
/* BasicSplitPaneUI.java -- Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.plaf.basic;import java.awt.Canvas;import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Insets;import java.awt.LayoutManager2;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusAdapter;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import javax.swing.JComponent;import javax.swing.JSplitPane;import javax.swing.KeyStroke;import javax.swing.LookAndFeel;import javax.swing.UIManager;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.SplitPaneUI;/** * This is the Basic Look and Feel implementation of the SplitPaneUI class. */public class BasicSplitPaneUI extends SplitPaneUI{ /** * This Layout Manager controls the position and size of the components when * the JSplitPane's orientation is HORIZONTAL_SPLIT. * * @specnote Apparently this class was intended to be protected, * but was made public by a compiler bug and is now * public for compatibility. */ public class BasicHorizontalLayoutManager implements LayoutManager2 { // 3 components at a time. // LEFT/TOP = 0 // RIGHT/BOTTOM = 1 // DIVIDER = 2 /** * This array contains the components in the JSplitPane. The left/top * component is at index 0, the right/bottom is at 1, and the divider is * at 2. */ protected Component[] components = new Component[3]; // These are the _current_ widths of the associated component. /** * This array contains the current width (for HORIZONTAL_SPLIT) or height * (for VERTICAL_SPLIT) of the components. The indices are the same as * for components. */ protected int[] sizes = new int[3]; /** * This method adds the component given to the JSplitPane. The position of * the component is given by the constraints object. * * @param comp The Component to add. * @param constraints The constraints that bind the object. */ public void addLayoutComponent(Component comp, Object constraints) { addLayoutComponent((String) constraints, comp); } /** * This method is called to add a Component to the JSplitPane. The * placement string determines where the Component will be placed. The * string should be one of LEFT, RIGHT, TOP, BOTTOM or null (signals that * the component is the divider). * * @param place The placement of the Component. * @param component The Component to add. * * @throws IllegalArgumentException DOCUMENT ME! */ public void addLayoutComponent(String place, Component component) { int i = 0; if (place == null) i = 2; else if (place.equals(JSplitPane.TOP) || place.equals(JSplitPane.LEFT)) i = 0; else if (place.equals(JSplitPane.BOTTOM) || place.equals(JSplitPane.RIGHT)) i = 1; else throw new IllegalArgumentException("Illegal placement in JSplitPane"); components[i] = component; resetSizeAt(i); splitPane.revalidate(); splitPane.repaint(); } /** * This method returns the width of the JSplitPane minus the insets. * * @param containerSize The Dimensions of the JSplitPane. * @param insets The Insets of the JSplitPane. * * @return The width of the JSplitPane minus the insets. */ protected int getAvailableSize(Dimension containerSize, Insets insets) { return containerSize.width - insets.left - insets.right; } /** * This method returns the given insets left value. If the given inset is * null, then 0 is returned. * * @param insets The Insets to use with the JSplitPane. * * @return The inset's left value. */ protected int getInitialLocation(Insets insets) { if (insets != null) return insets.left; return 0; } /** * This specifies how a component is aligned with respect to other * components in the x fdirection. * * @param target The container. * * @return The component's alignment. */ public float getLayoutAlignmentX(Container target) { return target.getAlignmentX(); } /** * This specifies how a component is aligned with respect to other * components in the y direction. * * @param target The container. * * @return The component's alignment. */ public float getLayoutAlignmentY(Container target) { return target.getAlignmentY(); } /** * This method returns the preferred width of the component. * * @param c The component to measure. * * @return The preferred width of the component. */ protected int getPreferredSizeOfComponent(Component c) { Dimension dims = c.getPreferredSize(); if (dims != null) return dims.width; return 0; } /** * This method returns the current width of the component. * * @param c The component to measure. * * @return The width of the component. */ protected int getSizeOfComponent(Component c) { return c.getWidth(); } /** * This method returns the sizes array. * * @return The sizes array. */ protected int[] getSizes() { return sizes; } /** * This method invalidates the layout. It does nothing. * * @param c The container to invalidate. */ public void invalidateLayout(Container c) { // DO NOTHING } /** * This method lays out the components in the container. * * @param container The container to lay out. */ public void layoutContainer(Container container) { if (container instanceof JSplitPane) { JSplitPane split = (JSplitPane) container; distributeExtraSpace(); Insets insets = split.getInsets(); int width = getInitialLocation(insets); Dimension dims = split.getSize(); for (int i = 0; i < components.length; i += 2) { if (components[i] == null) continue; setComponentToSize(components[i], sizes[i], width, insets, dims); width += sizes[i]; } if (components[1] != null) { setComponentToSize(components[1], sizes[1], width, insets, dims); width += sizes[1]; } } } /** * This method returns the maximum size for the container given the * components. It returns a new Dimension object that has width and * height equal to Integer.MAX_VALUE. * * @param target The container to measure. * * @return The maximum size. */ public Dimension maximumLayoutSize(Container target) { return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); } /** * This method returns the container's minimum size. The minimum width is * the sum of all the component's minimum widths. The minimum height is * the maximum of all the components' minimum heights. * * @param target The container to measure. * * @return The minimum size. */ public Dimension minimumLayoutSize(Container target) { if (target instanceof JSplitPane) { JSplitPane split = (JSplitPane) target; Insets insets = target.getInsets(); int height = 0; int width = 0; for (int i = 0; i < components.length; i++) { if (components[i] == null) continue; Dimension dims = components[i].getMinimumSize(); if (dims != null) { width += dims.width; height = Math.max(height, dims.height); } } return new Dimension(width, height); } return null; } /** * This method returns the container's preferred size. The preferred width * is the sum of all the component's preferred widths. The preferred * height is the maximum of all the components' preferred heights. * * @param target The container to measure. * * @return The preferred size. */ public Dimension preferredLayoutSize(Container target) { if (target instanceof JSplitPane) { JSplitPane split = (JSplitPane) target; Insets insets = target.getInsets(); int height = 0; int width = 0; for (int i = 0; i < components.length; i++) { if (components[i] == null) continue; Dimension dims = components[i].getPreferredSize(); if (dims != null) { width += dims.width; if (!(components[i] instanceof BasicSplitPaneDivider)) height = Math.max(height, dims.height); } } return new Dimension(width, height); } return null; } /** * This method removes the component from the layout. * * @param component The component to remove from the layout. */ public void removeLayoutComponent(Component component) { for (int i = 0; i < components.length; i++) { if (component == components[i]) { components[i] = null; sizes[i] = 0; } } } /** * This method resets the size of Component to the preferred size. * * @param index The index of the component to reset. */ protected void resetSizeAt(int index) { if (components[index] != null) sizes[index] = getPreferredSizeOfComponent(components[index]); } /** * This method resets the sizes of all the components. */ public void resetToPreferredSizes() { for (int i = 0; i < components.length; i++) resetSizeAt(i); } /** * This methods sets the bounds of the given component. The width is the * size. The height is the container size minus the top and bottom * inset. The x coordinate is the location given. The y coordinate is * the top inset. * * @param c The component to set. * @param size The width of the component. * @param location The x coordinate. * @param insets The insets to use. * @param containerSize The height of the container. */ protected void setComponentToSize(Component c, int size, int location, Insets insets, Dimension containerSize) { int w = size; int h = containerSize.height - insets.top - insets.bottom; int x = location; int y = insets.top; c.setBounds(x, y, w, h); } /** * This method stores the given int array as the new sizes array. * * @param newSizes The array to use as sizes. */ protected void setSizes(int[] newSizes) { sizes = newSizes; } /** * This method determines the size of each component. It should be called * when a new Layout Manager is created for an existing JSplitPane. */ protected void updateComponents() { Component left = splitPane.getLeftComponent(); Component right = splitPane.getRightComponent(); if (left != null) { components[0] = left; resetSizeAt(0); } if (right != null) { components[1] = right; resetSizeAt(1); } components[2] = divider; resetSizeAt(2); } /** * This method resizes the left and right components to fit inside the * JSplitPane when there is extra space. */ void distributeExtraSpace() { int availSize = getAvailableSize(splitPane.getSize(), splitPane.getInsets()); int[] newSizes = new int[3]; double weight = splitPane.getResizeWeight(); int oldLen = sizes[0] + sizes[1]; // dividers don't change size. availSize -= sizes[2] + oldLen; int rightAlloc = (int) (availSize * (1 - weight)); int leftAlloc = availSize - rightAlloc; sizes[0] += leftAlloc; sizes[1] += rightAlloc; } /** * This method returns the minimum width of the component at the given * index. * * @param index The index to check. * * @return The minimum width. */ int minimumSizeOfComponent(int index) { Dimension dims = components[index].getMinimumSize(); if (dims != null) return dims.width; else return 0; } } //end BasicHorizontalLayoutManager /** * This class is the Layout Manager for the JSplitPane when the orientation * is VERTICAL_SPLIT. * * @specnote Apparently this class was intended to be protected, * but was made public by a compiler bug and is now * public for compatibility. */ public class BasicVerticalLayoutManager extends BasicHorizontalLayoutManager { /** * This method returns the height of the container minus the top and * bottom inset. * * @param containerSize The size of the container. * @param insets The insets of the container. * * @return The height minus top and bottom inset. */ protected int getAvailableSize(Dimension containerSize, Insets insets) { return containerSize.height - insets.top - insets.bottom; } /** * This method returns the top inset. * * @param insets The Insets to use. * * @return The top inset. */ protected int getInitialLocation(Insets insets) { return insets.top; } /** * This method returns the preferred height of the component. * * @param c The component to measure. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -