1 /* 2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.apple.laf; 27 28 import java.awt.*; 29 import java.beans.PropertyChangeEvent; 30 31 import javax.swing.*; 32 import javax.swing.border.Border; 33 import javax.swing.plaf.basic.BasicSplitPaneDivider; 34 35 import apple.laf.*; 36 import apple.laf.JRSUIConstants.State; 37 38 import com.apple.laf.AquaUtils.LazyKeyedSingleton; 39 import com.apple.laf.AquaUtils.RecyclableSingleton; 40 import com.apple.laf.AquaUtils.RecyclableSingletonFromDefaultConstructor; 41 42 public class AquaSplitPaneDividerUI extends BasicSplitPaneDivider { 43 final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIStateFactory.getSplitPaneDivider()); 44 45 public AquaSplitPaneDividerUI(final AquaSplitPaneUI ui) { 46 super(ui); 47 setLayout(new AquaSplitPaneDividerUI.DividerLayout()); 48 } 49 50 /** 51 * Property change event, presumably from the JSplitPane, will message 52 * updateOrientation if necessary. 53 */ 54 public void propertyChange(final PropertyChangeEvent e) { 55 if (e.getSource() == splitPane) { 56 final String propName = e.getPropertyName(); 57 if ("enabled".equals(propName)) { 58 final boolean enabled = splitPane.isEnabled(); 59 if (leftButton != null) leftButton.setEnabled(enabled); 60 if (rightButton != null) rightButton.setEnabled(enabled); 61 } else if (JSplitPane.ORIENTATION_PROPERTY.equals(propName)) { 62 // need to regenerate the buttons, since we bake the orientation into them 63 if (rightButton != null) { 64 remove(rightButton); rightButton = null; 65 } 66 if (leftButton != null) { 67 remove(leftButton); leftButton = null; 68 } 69 oneTouchExpandableChanged(); 70 } 71 } 72 super.propertyChange(e); 73 } 74 75 public int getMaxDividerSize() { 76 return 10; 77 } 78 79 /** 80 * Paints the divider. 81 */ 82 public void paint(final Graphics g) { 83 final Dimension size = getSize(); 84 int x = 0; 85 int y = 0; 86 87 final boolean horizontal = splitPane.getOrientation() == SwingConstants.HORIZONTAL; 88 //System.err.println("Size = " + size + " orientation horiz = " + horizontal); 89 // size determines orientation 90 final int maxSize = getMaxDividerSize(); 91 boolean doPaint = true; 92 if (horizontal) { 93 if (size.height > maxSize) { 94 final int diff = size.height - maxSize; 95 y = diff / 2; 96 size.height = maxSize; 97 } 98 if (size.height < 4) doPaint = false; 99 } else { 100 if (size.width > maxSize) { 101 final int diff = size.width - maxSize; 102 x = diff / 2; 103 size.width = maxSize; 104 } 105 if (size.width < 4) doPaint = false; 106 } 107 108 if (doPaint) { 109 painter.state.set(getState()); 110 painter.paint(g, splitPane, x, y, size.width, size.height); 111 } 112 113 super.paint(g); // Ends up at Container.paint, which paints our JButton children 114 } 115 116 protected State getState() { 117 return splitPane.isEnabled() ? State.ACTIVE : State.DISABLED; 118 } 119 120 protected JButton createLeftOneTouchButton() { 121 return createButtonForDirection(getDirection(true)); 122 } 123 124 protected JButton createRightOneTouchButton() { 125 return createButtonForDirection(getDirection(false)); 126 } 127 128 static final LazyKeyedSingleton<Integer, Image> directionArrows = new LazyKeyedSingleton<Integer, Image>() { 129 protected Image getInstance(final Integer direction) { 130 final Image arrowImage = AquaImageFactory.getArrowImageForDirection(direction); 131 final int h = (arrowImage.getHeight(null) * 5) / 7; 132 final int w = (arrowImage.getWidth(null) * 5) / 7; 133 return AquaUtils.generateLightenedImage(arrowImage.getScaledInstance(w, h, Image.SCALE_SMOOTH), 50); 134 } 135 }; 136 137 // separate static, because the divider needs to be serializable 138 // see <rdar://problem/7590946> JSplitPane is not serializable when using Aqua look and feel 139 static JButton createButtonForDirection(final int direction) { 140 final JButton button = new JButton(new ImageIcon(directionArrows.get(Integer.valueOf(direction)))); 141 button.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 142 button.setFocusPainted(false); 143 button.setRequestFocusEnabled(false); 144 button.setFocusable(false); 145 button.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1)); 146 return button; 147 } 148 149 int getDirection(final boolean isLeft) { 150 if (splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) { 151 return isLeft ? SwingConstants.WEST : SwingConstants.EAST; 152 } 153 154 return isLeft ? SwingConstants.NORTH : SwingConstants.SOUTH; 155 } 156 157 static final int kMaxPopupArrowSize = 9; 158 protected class DividerLayout extends BasicSplitPaneDivider.DividerLayout { 159 public void layoutContainer(final Container c) { 160 final int maxSize = getMaxDividerSize(); 161 final Dimension size = getSize(); 162 163 if (leftButton == null || rightButton == null || c != AquaSplitPaneDividerUI.this) return; 164 165 if (!splitPane.isOneTouchExpandable()) { 166 leftButton.setBounds(-5, -5, 1, 1); 167 rightButton.setBounds(-5, -5, 1, 1); 168 return; 169 } 170 171 final int blockSize = Math.min(getDividerSize(), kMaxPopupArrowSize); // make it 1 less than divider, or kMaxPopupArrowSize 172 173 // put them at the right or the bottom 174 if (orientation == JSplitPane.VERTICAL_SPLIT) { 175 int yPosition = 0; 176 if (size.height > maxSize) { 177 final int diff = size.height - maxSize; 178 yPosition = diff / 2; 179 } 180 int xPosition = kMaxPopupArrowSize + ONE_TOUCH_OFFSET; 181 182 rightButton.setBounds(xPosition, yPosition, kMaxPopupArrowSize, blockSize); 183 184 xPosition -= (kMaxPopupArrowSize + ONE_TOUCH_OFFSET); 185 leftButton.setBounds(xPosition, yPosition, kMaxPopupArrowSize, blockSize); 186 } else { 187 int xPosition = 0; 188 if (size.width > maxSize) { 189 final int diff = size.width - maxSize; 190 xPosition = diff / 2; 191 } 192 int yPosition = kMaxPopupArrowSize + ONE_TOUCH_OFFSET; 193 194 rightButton.setBounds(xPosition, yPosition, blockSize, kMaxPopupArrowSize); 195 196 yPosition -= (kMaxPopupArrowSize + ONE_TOUCH_OFFSET); 197 leftButton.setBounds(xPosition, yPosition, blockSize, kMaxPopupArrowSize); 198 } 199 } 200 } 201 202 public static Border getHorizontalSplitDividerGradientVariant() { 203 return HorizontalSplitDividerGradientPainter.instance(); 204 } 205 206 static class HorizontalSplitDividerGradientPainter implements Border { 207 private static final RecyclableSingleton<HorizontalSplitDividerGradientPainter> instance = new RecyclableSingletonFromDefaultConstructor<HorizontalSplitDividerGradientPainter>(HorizontalSplitDividerGradientPainter.class); 208 static HorizontalSplitDividerGradientPainter instance() { 209 return instance.get(); 210 } 211 212 final Color startColor = Color.white; 213 final Color endColor = new Color(217, 217, 217); 214 final Color borderLines = Color.lightGray; 215 216 public Insets getBorderInsets(final Component c) { 217 return new Insets(0, 0, 0, 0); 218 } 219 220 public boolean isBorderOpaque() { 221 return true; 222 } 223 224 public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int width, final int height) { 225 if (!(g instanceof Graphics2D)) return; 226 227 final Graphics2D g2d = (Graphics2D)g; 228 final Color oldColor = g2d.getColor(); 229 230 g2d.setPaint(new GradientPaint(0, 0, startColor, 0, height, endColor)); 231 g2d.fillRect(x, y, width, height); 232 g2d.setColor(borderLines); 233 g2d.drawLine(x, y, x + width, y); 234 g2d.drawLine(x, y + height - 1, x + width, y + height - 1); 235 236 g2d.setColor(oldColor); 237 } 238 } 239 }