diff options
Diffstat (limited to 'libjava/classpath/javax/swing/plaf/basic/BasicSplitPaneDivider.java')
-rw-r--r-- | libjava/classpath/javax/swing/plaf/basic/BasicSplitPaneDivider.java | 412 |
1 files changed, 301 insertions, 111 deletions
diff --git a/libjava/classpath/javax/swing/plaf/basic/BasicSplitPaneDivider.java b/libjava/classpath/javax/swing/plaf/basic/BasicSplitPaneDivider.java index 06d32984efb..95468caa972 100644 --- a/libjava/classpath/javax/swing/plaf/basic/BasicSplitPaneDivider.java +++ b/libjava/classpath/javax/swing/plaf/basic/BasicSplitPaneDivider.java @@ -38,12 +38,15 @@ exception statement from your version. */ package javax.swing.plaf.basic; +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.LayoutManager; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; @@ -52,7 +55,7 @@ import java.beans.PropertyChangeListener; import javax.swing.JButton; import javax.swing.JSplitPane; -import javax.swing.SwingConstants; +import javax.swing.UIManager; import javax.swing.border.Border; /** @@ -72,6 +75,207 @@ public class BasicSplitPaneDivider extends Container implements PropertyChangeListener { /** + * The buttons used as one touch buttons. + */ + private class BasicOneTouchButton + extends JButton + { + /** + * Denotes a left button. + */ + static final int LEFT = 0; + + /** + * Denotes a right button. + */ + static final int RIGHT = 1; + + /** + * The x points for the arrow. + */ + private int[] xpoints; + + /** + * The y points for the arrow. + */ + private int[] ypoints; + + /** + * Either LEFT or RIGHT. + */ + private int direction; + + /** + * Creates a new instance. + * + * @param dir either LEFT or RIGHT + */ + BasicOneTouchButton(int dir) + { + direction = dir; + xpoints = new int[3]; + ypoints = new int[3]; + } + + /** + * Never allow borders. + */ + public void setBorder(Border b) + { + } + + /** + * Never allow focus traversal. + */ + public boolean isFocusTraversable() + { + return false; + } + + /** + * Paints the one touch button. + */ + public void paint(Graphics g) + { + if (splitPane != null) + { + // Fill background. + g.setColor(splitPane.getBackground()); + g.fillRect(0, 0, getWidth(), getHeight()); + + // Draw arrow. + int size; + if (direction == LEFT) + { + if (orientation == JSplitPane.VERTICAL_SPLIT) + { + size = Math.min(getHeight(), ONE_TOUCH_SIZE); + xpoints[0] = 0; + xpoints[1] = size / 2; + xpoints[2] = size; + ypoints[0] = size; + ypoints[1] = 0; + ypoints[2] = size; + } + else + { + size = Math.min(getWidth(), ONE_TOUCH_SIZE); + xpoints[0] = size; + xpoints[1] = 0; + xpoints[2] = size; + ypoints[0] = 0; + ypoints[1] = size / 2; + ypoints[2] = size; + } + } + else + { + if (orientation == JSplitPane.VERTICAL_SPLIT) + { + size = Math.min(getHeight(), ONE_TOUCH_SIZE); + xpoints[0] = 0; + xpoints[1] = size / 2; + xpoints[2] = size; + ypoints[0] = 0; + ypoints[1] = size; + ypoints[2] = 0; + } + else + { + size = Math.min(getWidth(), ONE_TOUCH_SIZE); + xpoints[0] = 0; + xpoints[1] = size; + xpoints[2] = 0; + ypoints[0] = 0; + ypoints[1] = size / 2; + ypoints[2] = size; + } + } + g.setColor(Color.BLACK); + g.fillPolygon(xpoints, ypoints, 3); + } + } + } + + /** + * Listens for actions on the one touch buttons. + */ + private class OneTouchAction + implements ActionListener + { + + public void actionPerformed(ActionEvent ev) + { + Insets insets = splitPane.getInsets(); + int lastLoc = splitPane.getLastDividerLocation(); + int currentLoc = splitPaneUI.getDividerLocation(splitPane); + int newLoc; + + if (ev.getSource() == leftButton) + { + if (orientation == JSplitPane.VERTICAL_SPLIT) + { + if (currentLoc + >= splitPane.getHeight() - insets.bottom - getHeight()) + { + newLoc = Math.min(splitPane.getMaximumDividerLocation(), + lastLoc); + } + else + { + newLoc = insets.top; + } + } + else + { + if (currentLoc + >= splitPane.getWidth() - insets.right - getWidth()) + { + newLoc = Math.min(splitPane.getMaximumDividerLocation(), + lastLoc); + } + else + { + newLoc = insets.left; + } + } + } + else + { + if (orientation == JSplitPane.VERTICAL_SPLIT) + { + if (currentLoc == insets.top) + { + newLoc = Math.min(splitPane.getMaximumDividerLocation(), + lastLoc); + } + else + { + newLoc = splitPane.getHeight() - insets.top - getHeight(); + } + } + else + { + if (currentLoc == insets.left) + { + newLoc = Math.min(splitPane.getMaximumDividerLocation(), + lastLoc); + } + else + { + newLoc = splitPane.getWidth() - insets.left - getWidth(); + } + } + } + if (currentLoc != newLoc) + { + splitPane.setDividerLocation(newLoc); + splitPane.setLastDividerLocation(currentLoc); + } + } + } + + /** * Determined using the <code>serialver</code> tool of Apple/Sun JDK 1.3.1 * on MacOS X 10.1.5. */ @@ -161,6 +365,14 @@ public class BasicSplitPaneDivider extends Container transient int currentDividerLocation = 1; /** + * Indicates if the ont touch buttons are laid out centered or at the + * top/left. + * + * Package private to avoid accessor method. + */ + boolean centerOneTouchButtons; + + /** * Constructs a new divider. * * @param ui the UI delegate of the enclosing <code>JSplitPane</code>. @@ -170,6 +382,8 @@ public class BasicSplitPaneDivider extends Container setLayout(new DividerLayout()); setBasicSplitPaneUI(ui); setDividerSize(splitPane.getDividerSize()); + centerOneTouchButtons = + UIManager.getBoolean("SplitPane.centerOneTouchButtons"); } /** @@ -202,7 +416,8 @@ public class BasicSplitPaneDivider extends Container addMouseMotionListener(mouseHandler); hiddenDivider = splitPaneUI.getNonContinuousLayoutDivider(); orientation = splitPane.getOrientation(); - oneTouchExpandableChanged(); + if (splitPane.isOneTouchExpandable()) + oneTouchExpandableChanged(); } } @@ -293,7 +508,12 @@ public class BasicSplitPaneDivider extends Container */ public Dimension getPreferredSize() { - return getLayout().preferredLayoutSize(this); + Dimension d; + if (orientation == JSplitPane.HORIZONTAL_SPLIT) + d = new Dimension(getDividerSize(), 1); + else + d = new Dimension(1, getDividerSize()); + return d; } /** @@ -320,11 +540,9 @@ public class BasicSplitPaneDivider extends Container else if (e.getPropertyName().equals(JSplitPane.ORIENTATION_PROPERTY)) { orientation = splitPane.getOrientation(); - if (splitPane.isOneTouchExpandable()) - { - layout(); - repaint(); - } + invalidate(); + if (splitPane != null) + splitPane.revalidate(); } else if (e.getPropertyName().equals(JSplitPane.DIVIDER_SIZE_PROPERTY)) dividerSize = splitPane.getDividerSize(); @@ -345,11 +563,6 @@ public class BasicSplitPaneDivider extends Container dividerSize = getSize(); border.paintBorder(this, g, 0, 0, dividerSize.width, dividerSize.height); } - if (splitPane.isOneTouchExpandable()) - { - ((BasicArrowButton) rightButton).paint(g); - ((BasicArrowButton) leftButton).paint(g); - } } /** @@ -361,31 +574,23 @@ public class BasicSplitPaneDivider extends Container if (splitPane.isOneTouchExpandable()) { leftButton = createLeftOneTouchButton(); - rightButton = createRightOneTouchButton(); - add(leftButton); - add(rightButton); + if (leftButton != null) + leftButton.addActionListener(new OneTouchAction()); - leftButton.addMouseListener(mouseHandler); - rightButton.addMouseListener(mouseHandler); + rightButton = createRightOneTouchButton(); + if (rightButton != null) + rightButton.addActionListener(new OneTouchAction()); - // Set it to 1. - currentDividerLocation = 1; - } - else - { + // Only add them when both are non-null. if (leftButton != null && rightButton != null) - { - leftButton.removeMouseListener(mouseHandler); - rightButton.removeMouseListener(mouseHandler); - - remove(leftButton); - remove(rightButton); - leftButton = null; - rightButton = null; + { + add(leftButton); + add(rightButton); } } - layout(); - repaint(); + invalidate(); + if (splitPane != null) + splitPane.revalidate(); } /** @@ -396,12 +601,9 @@ public class BasicSplitPaneDivider extends Container */ protected JButton createLeftOneTouchButton() { - int dir = SwingConstants.WEST; - if (orientation == JSplitPane.VERTICAL_SPLIT) - dir = SwingConstants.NORTH; - JButton button = new BasicArrowButton(dir); - button.setBorder(null); - + JButton button = new BasicOneTouchButton(BasicOneTouchButton.LEFT); + button.setMinimumSize(new Dimension(ONE_TOUCH_SIZE, ONE_TOUCH_SIZE)); + button.setRequestFocusEnabled(false); return button; } @@ -413,11 +615,9 @@ public class BasicSplitPaneDivider extends Container */ protected JButton createRightOneTouchButton() { - int dir = SwingConstants.EAST; - if (orientation == JSplitPane.VERTICAL_SPLIT) - dir = SwingConstants.SOUTH; - JButton button = new BasicArrowButton(dir); - button.setBorder(null); + JButton button = new BasicOneTouchButton(BasicOneTouchButton.RIGHT); + button.setMinimumSize(new Dimension(ONE_TOUCH_SIZE, ONE_TOUCH_SIZE)); + button.setRequestFocusEnabled(false); return button; } @@ -521,25 +721,6 @@ public class BasicSplitPaneDivider extends Container */ public void mousePressed(MouseEvent e) { - if (splitPane.isOneTouchExpandable()) - { - if (e.getSource() == leftButton) - { - currentDividerLocation--; - if (currentDividerLocation < 0) - currentDividerLocation = 0; - moveDividerTo(currentDividerLocation); - return; - } - else if (e.getSource() == rightButton) - { - currentDividerLocation++; - if (currentDividerLocation > 2) - currentDividerLocation = 2; - moveDividerTo(currentDividerLocation); - return; - } - } isDragging = true; currentDividerLocation = 1; if (orientation == JSplitPane.HORIZONTAL_SPLIT) @@ -797,10 +978,64 @@ public class BasicSplitPaneDivider extends Container */ public void layoutContainer(Container c) { - if (splitPane.isOneTouchExpandable()) + if (leftButton != null && rightButton != null + && c == BasicSplitPaneDivider.this) { - changeButtonOrientation(); - positionButtons(); + if (splitPane.isOneTouchExpandable()) + { + Insets insets = getInsets(); + if (orientation == JSplitPane.HORIZONTAL_SPLIT) + { + int size = getWidth() - insets.left - insets.right; + size = Math.max(size, 0); + size = Math.min(size, ONE_TOUCH_SIZE); + int x, y; + if (centerOneTouchButtons) + { + y = insets.top; + x = (getWidth() - size) / 2; + } + else + { + x = insets.left; + y = 0; + } + + leftButton.setBounds(x, y + ONE_TOUCH_OFFSET, size, + size * 2); + rightButton.setBounds(x, y + ONE_TOUCH_OFFSET + + ONE_TOUCH_SIZE * 2, size, size * 2); + } + else + { + int size = getHeight() - insets.top - insets.bottom; + size = Math.max(size, 0); + size = Math.min(size, ONE_TOUCH_SIZE); + int x, y; + if (centerOneTouchButtons) + { + x = insets.left; + y = (getHeight() - size) / 2; + } + else + { + x = 0; + y = insets.top; + } + leftButton.setBounds(x + ONE_TOUCH_OFFSET, y, size * 2, + size); + rightButton.setBounds(x + ONE_TOUCH_OFFSET + + ONE_TOUCH_SIZE * 2, y, size * 2, + size); + } + } + else + { + // The JDK sets this bounds for disabled one touch buttons, so + // do we. + leftButton.setBounds(-5, -5, 1, 1); + rightButton.setBounds(-5, -5, 1, 1); + } } } @@ -838,50 +1073,5 @@ public class BasicSplitPaneDivider extends Container // Do nothing. } - /** - * This method changes the button orientation when the orientation of the - * SplitPane changes. - */ - private void changeButtonOrientation() - { - if (orientation == JSplitPane.HORIZONTAL_SPLIT) - { - ((BasicArrowButton) rightButton).setDirection(SwingConstants.EAST); - ((BasicArrowButton) leftButton).setDirection(SwingConstants.WEST); - } - else - { - ((BasicArrowButton) rightButton).setDirection(SwingConstants.SOUTH); - ((BasicArrowButton) leftButton).setDirection(SwingConstants.NORTH); - } - } - - /** - * This method sizes and positions the buttons. - */ - private void positionButtons() - { - int w = 0; - int h = 0; - if (orientation == JSplitPane.HORIZONTAL_SPLIT) - { - rightButton.setLocation(ONE_TOUCH_OFFSET, ONE_TOUCH_OFFSET); - leftButton.setLocation(ONE_TOUCH_OFFSET, - ONE_TOUCH_OFFSET + 2 * ONE_TOUCH_SIZE); - w = dividerSize - 2 * ONE_TOUCH_OFFSET; - h = 2 * ONE_TOUCH_SIZE; - } - else - { - leftButton.setLocation(ONE_TOUCH_OFFSET, ONE_TOUCH_OFFSET); - rightButton.setLocation(ONE_TOUCH_OFFSET + 2 * ONE_TOUCH_SIZE, - ONE_TOUCH_OFFSET); - h = dividerSize - 2 * ONE_TOUCH_OFFSET; - w = 2 * ONE_TOUCH_SIZE; - } - Dimension dims = new Dimension(w, h); - leftButton.setSize(dims); - rightButton.setSize(dims); - } } } |