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