AquaSplitPaneDividerUI.java revision 4632
325N/A/*
325N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.apple.laf;
325N/A
325N/Aimport java.awt.*;
325N/Aimport java.beans.PropertyChangeEvent;
325N/A
325N/Aimport javax.swing.*;
325N/Aimport javax.swing.border.Border;
325N/Aimport javax.swing.plaf.basic.BasicSplitPaneDivider;
325N/A
325N/Aimport apple.laf.*;
325N/Aimport apple.laf.JRSUIConstants.State;
325N/A
325N/Aimport com.apple.laf.AquaUtils.LazyKeyedSingleton;
325N/Aimport com.apple.laf.AquaUtils.RecyclableSingleton;
325N/Aimport com.apple.laf.AquaUtils.RecyclableSingletonFromDefaultConstructor;
325N/A
325N/Apublic class AquaSplitPaneDividerUI extends BasicSplitPaneDivider {
325N/A final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIStateFactory.getSplitPaneDivider());
325N/A
325N/A public AquaSplitPaneDividerUI(final AquaSplitPaneUI ui) {
325N/A super(ui);
325N/A setLayout(new AquaSplitPaneDividerUI.DividerLayout());
325N/A }
325N/A
325N/A /**
325N/A * Property change event, presumably from the JSplitPane, will message
325N/A * updateOrientation if necessary.
325N/A */
325N/A public void propertyChange(final PropertyChangeEvent e) {
325N/A if (e.getSource() == splitPane) {
325N/A final String propName = e.getPropertyName();
325N/A if ("enabled".equals(propName)) {
325N/A final boolean enabled = splitPane.isEnabled();
325N/A if (leftButton != null) leftButton.setEnabled(enabled);
325N/A if (rightButton != null) rightButton.setEnabled(enabled);
325N/A } else if (JSplitPane.ORIENTATION_PROPERTY.equals(propName)) {
325N/A // need to regenerate the buttons, since we bake the orientation into them
325N/A if (rightButton != null) {
325N/A remove(rightButton); rightButton = null;
325N/A }
325N/A if (leftButton != null) {
325N/A remove(leftButton); leftButton = null;
325N/A }
325N/A oneTouchExpandableChanged();
325N/A }
325N/A }
325N/A super.propertyChange(e);
325N/A }
325N/A
325N/A public int getMaxDividerSize() {
325N/A return 10;
325N/A }
325N/A
325N/A /**
325N/A * Paints the divider.
325N/A */
325N/A public void paint(final Graphics g) {
325N/A final Dimension size = getSize();
325N/A int x = 0;
325N/A int y = 0;
325N/A
325N/A final boolean horizontal = splitPane.getOrientation() == SwingConstants.HORIZONTAL;
325N/A //System.err.println("Size = " + size + " orientation horiz = " + horizontal);
325N/A // size determines orientation
325N/A final int maxSize = getMaxDividerSize();
325N/A boolean doPaint = true;
325N/A if (horizontal) {
325N/A if (size.height > maxSize) {
325N/A final int diff = size.height - maxSize;
325N/A y = diff / 2;
325N/A size.height = maxSize;
325N/A }
325N/A if (size.height < 4) doPaint = false;
325N/A } else {
325N/A if (size.width > maxSize) {
325N/A final int diff = size.width - maxSize;
325N/A x = diff / 2;
325N/A size.width = maxSize;
325N/A }
325N/A if (size.width < 4) doPaint = false;
325N/A }
325N/A
325N/A if (doPaint) {
325N/A painter.state.set(getState());
325N/A painter.paint(g, splitPane, x, y, size.width, size.height);
325N/A }
325N/A
325N/A super.paint(g); // Ends up at Container.paint, which paints our JButton children
325N/A }
325N/A
325N/A protected State getState() {
325N/A return splitPane.isEnabled() ? State.ACTIVE : State.DISABLED;
325N/A }
325N/A
325N/A protected JButton createLeftOneTouchButton() {
325N/A return createButtonForDirection(getDirection(true));
325N/A }
325N/A
325N/A protected JButton createRightOneTouchButton() {
325N/A return createButtonForDirection(getDirection(false));
325N/A }
325N/A
325N/A static final LazyKeyedSingleton<Integer, Image> directionArrows = new LazyKeyedSingleton<Integer, Image>() {
325N/A protected Image getInstance(final Integer direction) {
325N/A final Image arrowImage = AquaImageFactory.getArrowImageForDirection(direction);
325N/A final int h = (arrowImage.getHeight(null) * 5) / 7;
325N/A final int w = (arrowImage.getWidth(null) * 5) / 7;
325N/A return AquaUtils.generateLightenedImage(arrowImage.getScaledInstance(w, h, Image.SCALE_SMOOTH), 50);
325N/A }
325N/A };
325N/A
325N/A // separate static, because the divider needs to be serializable
325N/A // see <rdar://problem/7590946> JSplitPane is not serializable when using Aqua look and feel
325N/A static JButton createButtonForDirection(final int direction) {
325N/A final JButton button = new JButton(new ImageIcon(directionArrows.get(Integer.valueOf(direction))));
325N/A button.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
325N/A button.setFocusPainted(false);
325N/A button.setRequestFocusEnabled(false);
button.setFocusable(false);
button.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
return button;
}
int getDirection(final boolean isLeft) {
if (splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
return isLeft ? SwingConstants.WEST : SwingConstants.EAST;
}
return isLeft ? SwingConstants.NORTH : SwingConstants.SOUTH;
}
static final int kMaxPopupArrowSize = 9;
protected class DividerLayout extends BasicSplitPaneDivider.DividerLayout {
public void layoutContainer(final Container c) {
final int maxSize = getMaxDividerSize();
final Dimension size = getSize();
if (leftButton == null || rightButton == null || c != AquaSplitPaneDividerUI.this) return;
if (!splitPane.isOneTouchExpandable()) {
leftButton.setBounds(-5, -5, 1, 1);
rightButton.setBounds(-5, -5, 1, 1);
return;
}
final int blockSize = Math.min(getDividerSize(), kMaxPopupArrowSize); // make it 1 less than divider, or kMaxPopupArrowSize
// put them at the right or the bottom
if (orientation == JSplitPane.VERTICAL_SPLIT) {
int yPosition = 0;
if (size.height > maxSize) {
final int diff = size.height - maxSize;
yPosition = diff / 2;
}
int xPosition = kMaxPopupArrowSize + ONE_TOUCH_OFFSET;
rightButton.setBounds(xPosition, yPosition, kMaxPopupArrowSize, blockSize);
xPosition -= (kMaxPopupArrowSize + ONE_TOUCH_OFFSET);
leftButton.setBounds(xPosition, yPosition, kMaxPopupArrowSize, blockSize);
} else {
int xPosition = 0;
if (size.width > maxSize) {
final int diff = size.width - maxSize;
xPosition = diff / 2;
}
int yPosition = kMaxPopupArrowSize + ONE_TOUCH_OFFSET;
rightButton.setBounds(xPosition, yPosition, blockSize, kMaxPopupArrowSize);
yPosition -= (kMaxPopupArrowSize + ONE_TOUCH_OFFSET);
leftButton.setBounds(xPosition, yPosition, blockSize, kMaxPopupArrowSize);
}
}
}
public static Border getHorizontalSplitDividerGradientVariant() {
return HorizontalSplitDividerGradientPainter.instance();
}
static class HorizontalSplitDividerGradientPainter implements Border {
private static final RecyclableSingleton<HorizontalSplitDividerGradientPainter> instance = new RecyclableSingletonFromDefaultConstructor<HorizontalSplitDividerGradientPainter>(HorizontalSplitDividerGradientPainter.class);
static HorizontalSplitDividerGradientPainter instance() {
return instance.get();
}
final Color startColor = Color.white;
final Color endColor = new Color(217, 217, 217);
final Color borderLines = Color.lightGray;
public Insets getBorderInsets(final Component c) {
return new Insets(0, 0, 0, 0);
}
public boolean isBorderOpaque() {
return true;
}
public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
if (!(g instanceof Graphics2D)) return;
final Graphics2D g2d = (Graphics2D)g;
final Color oldColor = g2d.getColor();
g2d.setPaint(new GradientPaint(0, 0, startColor, 0, height, endColor));
g2d.fillRect(x, y, width, height);
g2d.setColor(borderLines);
g2d.drawLine(x, y, x + width, y);
g2d.drawLine(x, y + height - 1, x + width, y + height - 1);
g2d.setColor(oldColor);
}
}
}