| 1 | /* MetalSplitPaneDivider.java |
| 2 | Copyright (C) 2005, 2006, Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GNU Classpath. |
| 5 | |
| 6 | GNU Classpath is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2, or (at your option) |
| 9 | any later version. |
| 10 | |
| 11 | GNU Classpath is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the |
| 18 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 | 02110-1301 USA. |
| 20 | |
| 21 | Linking this library statically or dynamically with other modules is |
| 22 | making a combined work based on this library. Thus, the terms and |
| 23 | conditions of the GNU General Public License cover the whole |
| 24 | combination. |
| 25 | |
| 26 | As a special exception, the copyright holders of this library give you |
| 27 | permission to link this library with independent modules to produce an |
| 28 | executable, regardless of the license terms of these independent |
| 29 | modules, and to copy and distribute the resulting executable under |
| 30 | terms of your choice, provided that you also meet, for each linked |
| 31 | independent module, the terms and conditions of the license of that |
| 32 | module. An independent module is a module which is not derived from |
| 33 | or based on this library. If you modify this library, you may extend |
| 34 | this exception to your version of the library, but you are not |
| 35 | obligated to do so. If you do not wish to do so, delete this |
| 36 | exception statement from your version. */ |
| 37 | |
| 38 | package javax.swing.plaf.metal; |
| 39 | |
| 40 | import java.awt.Color; |
| 41 | import java.awt.Dimension; |
| 42 | import java.awt.Graphics; |
| 43 | import java.awt.Insets; |
| 44 | |
| 45 | import javax.swing.JButton; |
| 46 | import javax.swing.JSplitPane; |
| 47 | import javax.swing.UIManager; |
| 48 | import javax.swing.border.Border; |
| 49 | import javax.swing.plaf.basic.BasicSplitPaneDivider; |
| 50 | |
| 51 | /** |
| 52 | * The divider that is used by the {@link MetalSplitPaneUI}. |
| 53 | * |
| 54 | * @author Roman Kennke (roman@kennke.org) |
| 55 | */ |
| 56 | class MetalSplitPaneDivider extends BasicSplitPaneDivider |
| 57 | { |
| 58 | /** |
| 59 | * The button pixel data, as indices into the colors array below. |
| 60 | * This is the version for 'left' buttons. |
| 61 | * |
| 62 | * This is slightly different from the icon in Sun's version, it is |
| 63 | * one pixel smaller and is more consistent with BUTTON_SPRITE_R. |
| 64 | */ |
| 65 | static final byte[][] BUTTON_SPRITE_L = {{ 0, 0, 0, 2, 0, 0, 0, 0 }, |
| 66 | { 0, 0, 2, 1, 1, 0, 0, 0 }, |
| 67 | { 0, 2, 1, 1, 1, 1, 0, 0 }, |
| 68 | { 2, 1, 1, 1, 1, 1, 1, 0 }, |
| 69 | { 0, 3, 3, 3, 3, 3, 3, 3 }}; |
| 70 | |
| 71 | /** |
| 72 | * The button pixel data, as indices into the colors array below. |
| 73 | * This is the version for 'right' buttons. |
| 74 | */ |
| 75 | static final byte[][] BUTTON_SPRITE_R = {{ 2, 2, 2, 2, 2, 2, 2, 2 }, |
| 76 | { 0, 1, 1, 1, 1, 1, 1, 3 }, |
| 77 | { 0, 0, 1, 1, 1, 1, 3, 0 }, |
| 78 | { 0, 0, 0, 1, 1, 3, 0, 0 }, |
| 79 | { 0, 0, 0, 0, 3, 0, 0, 0 }}; |
| 80 | |
| 81 | private class MetalOneTouchButton |
| 82 | extends JButton |
| 83 | { |
| 84 | /** |
| 85 | * Denotes a left button. |
| 86 | */ |
| 87 | static final int LEFT = 0; |
| 88 | |
| 89 | /** |
| 90 | * Denotes a right button. |
| 91 | */ |
| 92 | static final int RIGHT = 1; |
| 93 | |
| 94 | /** |
| 95 | * The colors for the button sprite. |
| 96 | */ |
| 97 | private Color[] colors; |
| 98 | |
| 99 | /** |
| 100 | * Either LEFT or RIGHT. |
| 101 | */ |
| 102 | private int direction; |
| 103 | |
| 104 | /** |
| 105 | * Creates a new instance. |
| 106 | * |
| 107 | * @param dir either LEFT or RIGHT |
| 108 | */ |
| 109 | MetalOneTouchButton(int dir) |
| 110 | { |
| 111 | direction = dir; |
| 112 | colors = new Color[4]; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Never allow borders. |
| 117 | */ |
| 118 | public void setBorder(Border b) |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Never allow focus traversal. |
| 124 | */ |
| 125 | public boolean isFocusTraversable() |
| 126 | { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Paints the one touch button. |
| 132 | */ |
| 133 | public void paint(Graphics g) |
| 134 | { |
| 135 | if (splitPane != null) |
| 136 | { |
| 137 | // Update colors here to reflect dynamic changes to the theme. |
| 138 | colors[0] = getBackground(); |
| 139 | colors[1] = MetalLookAndFeel.getPrimaryControlDarkShadow(); |
| 140 | colors[2] = MetalLookAndFeel.getPrimaryControlInfo(); |
| 141 | colors[3] = MetalLookAndFeel.getPrimaryControlHighlight(); |
| 142 | |
| 143 | // Fill background. |
| 144 | g.setColor(getBackground()); |
| 145 | g.fillRect(0, 0, getWidth(), getHeight()); |
| 146 | |
| 147 | // Pressed buttons have slightly different color mapping. |
| 148 | if (getModel().isPressed()) |
| 149 | colors[1] = colors[2]; |
| 150 | |
| 151 | byte[][] sprite; |
| 152 | if (direction == LEFT) |
| 153 | sprite = BUTTON_SPRITE_L; |
| 154 | else |
| 155 | sprite = BUTTON_SPRITE_R; |
| 156 | |
| 157 | if (orientation == JSplitPane.VERTICAL_SPLIT) |
| 158 | { |
| 159 | // Draw the sprite as it is. |
| 160 | for (int y = 0; y < sprite.length; y++) |
| 161 | { |
| 162 | byte[] line = sprite[y]; |
| 163 | for (int x = 0; x < line.length; x++) |
| 164 | { |
| 165 | int c = line[x]; |
| 166 | if (c != 0) |
| 167 | { |
| 168 | g.setColor(colors[c]); |
| 169 | g.fillRect(x + 1, y + 1, 1, 1); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | // Draw the sprite with swapped X and Y axis. |
| 177 | for (int y = 0; y < sprite.length; y++) |
| 178 | { |
| 179 | byte[] line = sprite[y]; |
| 180 | for (int x = 0; x < line.length; x++) |
| 181 | { |
| 182 | int c = line[x]; |
| 183 | if (c != 0) |
| 184 | { |
| 185 | g.setColor(colors[c]); |
| 186 | g.fillRect(y + 1, x + 1, 1, 1); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** The dark color in the pattern. */ |
| 196 | Color dark; |
| 197 | |
| 198 | /** The light color in the pattern. */ |
| 199 | Color light; |
| 200 | |
| 201 | /** The JSplitPane the divider is on. */ |
| 202 | JSplitPane splitPane; |
| 203 | |
| 204 | /** The split pane orientation. */ |
| 205 | int orientation; |
| 206 | |
| 207 | /** |
| 208 | * Creates a new instance of <code>MetalSplitPaneDivider</code>. |
| 209 | * |
| 210 | * @param ui the <code>MetalSplitPaneUI</code> that uses this divider |
| 211 | */ |
| 212 | public MetalSplitPaneDivider(MetalSplitPaneUI ui, Color light, Color dark) |
| 213 | { |
| 214 | super(ui); |
| 215 | this.splitPane = super.splitPane; |
| 216 | this.orientation = super.orientation; |
| 217 | this.light = light; |
| 218 | this.dark = dark; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Paints the divider. |
| 223 | * |
| 224 | * @param g the <code>Graphics</code> context to use for painting |
| 225 | */ |
| 226 | public void paint(Graphics g) |
| 227 | { |
| 228 | Dimension s = getSize(); |
| 229 | |
| 230 | if (splitPane.hasFocus()) |
| 231 | { |
| 232 | g.setColor(UIManager.getColor("SplitPane.dividerFocusColor")); |
| 233 | g.fillRect(0, 0, s.width, s.height); |
| 234 | } |
| 235 | |
| 236 | // Paint border if one exists. |
| 237 | Border border = getBorder(); |
| 238 | if (border != null) |
| 239 | border.paintBorder(this, g, 0, 0, s.width, s.height); |
| 240 | |
| 241 | Insets i = getInsets(); |
| 242 | MetalUtils.fillMetalPattern(splitPane, g, i.left + 2, i.top + 2, |
| 243 | s.width - i.left - i.right - 4, |
| 244 | s.height - i.top - i.bottom - 4, |
| 245 | light, dark); |
| 246 | super.paint(g); |
| 247 | } |
| 248 | |
| 249 | protected JButton createLeftOneTouchButton() |
| 250 | { |
| 251 | JButton b = new MetalOneTouchButton(MetalOneTouchButton.LEFT); |
| 252 | b.setMinimumSize(new Dimension(ONE_TOUCH_SIZE, ONE_TOUCH_SIZE)); |
| 253 | b.setRequestFocusEnabled(false); |
| 254 | return b; |
| 255 | } |
| 256 | |
| 257 | protected JButton createRightOneTouchButton() |
| 258 | { |
| 259 | JButton b = new MetalOneTouchButton(MetalOneTouchButton.RIGHT); |
| 260 | b.setMinimumSize(new Dimension(ONE_TOUCH_SIZE, ONE_TOUCH_SIZE)); |
| 261 | b.setRequestFocusEnabled(false); |
| 262 | return b; |
| 263 | } |
| 264 | } |