| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivegui.component; |
| 16 | |
| 17 | import java.awt.Color; |
| 18 | import java.awt.GridBagConstraints; |
| 19 | import java.awt.GridBagLayout; |
| 20 | import java.awt.Insets; |
| 21 | |
| 22 | import javax.swing.Icon; |
| 23 | import javax.swing.JLabel; |
| 24 | import javax.swing.JSlider; |
| 25 | import javax.swing.JSpinner; |
| 26 | import javax.swing.SpinnerNumberModel; |
| 27 | import javax.swing.UIManager; |
| 28 | import javax.swing.colorchooser.AbstractColorChooserPanel; |
| 29 | import javax.swing.colorchooser.ColorSelectionModel; |
| 30 | import javax.swing.event.ChangeEvent; |
| 31 | import javax.swing.event.ChangeListener; |
| 32 | |
| 33 | /** |
| 34 | * Special ColorChooserPanel implementation that allows the user to set the |
| 35 | * alpha (transparency) value in addition to RGB. |
| 36 | * |
| 37 | * @author Jean-Francois Poilpret |
| 38 | */ |
| 39 | public class AlphaRGBChooserPanel extends AbstractColorChooserPanel |
| 40 | { |
| 41 | @Override public String getDisplayName() |
| 42 | { |
| 43 | return UIManager.getString("ColorChooser.rgbNameText"); |
| 44 | } |
| 45 | |
| 46 | @Override public int getMnemonic() |
| 47 | { |
| 48 | return getIntValue("ColorChooser.rgbMnemonic", -1); |
| 49 | } |
| 50 | |
| 51 | @Override public int getDisplayedMnemonicIndex() |
| 52 | { |
| 53 | return getIntValue("ColorChooser.rgbDisplayedMnemonicIndex", -1); |
| 54 | } |
| 55 | |
| 56 | @Override public Icon getSmallDisplayIcon() |
| 57 | { |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | @Override public Icon getLargeDisplayIcon() |
| 62 | { |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | @Override protected void buildChooser() |
| 67 | { |
| 68 | setName("color-chooser-rgb"); |
| 69 | Color color = getColorFromModel(); |
| 70 | |
| 71 | setLayout(new GridBagLayout()); |
| 72 | |
| 73 | // Build each color channel row: R,G,B,A |
| 74 | buildRow( RED, |
| 75 | RED_NAME, |
| 76 | "ColorChooser.rgbRedText", |
| 77 | "ColorChooser.rgbRedMnemonic", |
| 78 | color.getRed()); |
| 79 | buildRow( GREEN, |
| 80 | GREEN_NAME, |
| 81 | "ColorChooser.rgbGreenText", |
| 82 | "ColorChooser.rgbGreenMnemonic", |
| 83 | color.getGreen()); |
| 84 | buildRow( BLUE, |
| 85 | BLUE_NAME, |
| 86 | "ColorChooser.rgbBlueText", |
| 87 | "ColorChooser.rgbBlueMnemonic", |
| 88 | color.getBlue()); |
| 89 | buildRow( ALPHA, |
| 90 | ALPHA_NAME, |
| 91 | "Alpha", |
| 92 | 'A', |
| 93 | color.getAlpha()); |
| 94 | } |
| 95 | |
| 96 | protected void buildRow(int row, String name, String label, String mnemonic, int value) |
| 97 | { |
| 98 | buildRow(row, name, UIManager.getString(label), UIManager.getInt(mnemonic), value); |
| 99 | } |
| 100 | |
| 101 | // CSOFF: MagicNumberCheck |
| 102 | protected void buildRow(int row, String name, String label, int mnemonic, int value) |
| 103 | { |
| 104 | // Create the widgets |
| 105 | JLabel rowLabel = new JLabel(label); |
| 106 | rowLabel.setDisplayedMnemonic(mnemonic); |
| 107 | JSlider slider = new JSlider(JSlider.HORIZONTAL, MIN_VALUE, MAX_VALUE, value); |
| 108 | slider.setName("color-chooser-rgb-slider-" + name); |
| 109 | slider.setMajorTickSpacing(85); |
| 110 | slider.setMinorTickSpacing(17); |
| 111 | slider.setPaintTicks(true); |
| 112 | slider.setPaintLabels(true); |
| 113 | slider.addChangeListener(_listener); |
| 114 | slider.putClientProperty("JSlider.isFilled", Boolean.TRUE); |
| 115 | JSpinner spinner = new JSpinner( |
| 116 | new SpinnerNumberModel(value, MIN_VALUE, MAX_VALUE, 1)); |
| 117 | spinner.setName("color-chooser-rgb-spinner-" + name); |
| 118 | spinner.addChangeListener(_listener); |
| 119 | |
| 120 | _rgbaSliders[row] = slider; |
| 121 | _rgbaSpinners[row] = spinner; |
| 122 | |
| 123 | // Add the widgets to the panel |
| 124 | Insets insets = new Insets(2, 2, 2, 2); |
| 125 | GridBagConstraints constraints = new GridBagConstraints(0, row, 1, 1, |
| 126 | 0.0, 0.0, |
| 127 | GridBagConstraints.WEST, |
| 128 | GridBagConstraints.HORIZONTAL, |
| 129 | insets, |
| 130 | 0, 0); |
| 131 | add(rowLabel, constraints); |
| 132 | |
| 133 | constraints.gridx = 2; |
| 134 | add(spinner, constraints); |
| 135 | |
| 136 | constraints.gridx = 1; |
| 137 | constraints.weightx = 1.0; |
| 138 | add(slider, constraints); |
| 139 | } |
| 140 | // CSON: MagicNumberCheck |
| 141 | |
| 142 | @Override public void updateChooser() |
| 143 | { |
| 144 | if (!_isAdjusting) |
| 145 | { |
| 146 | _isAdjusting = true; |
| 147 | setColor(getColorFromModel()); |
| 148 | _isAdjusting = false; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | static protected int getIntValue(Object key, int defaultValue) |
| 153 | { |
| 154 | Object value = UIManager.get(key); |
| 155 | if (value instanceof Integer) |
| 156 | { |
| 157 | return ((Integer) value).intValue(); |
| 158 | } |
| 159 | if (value instanceof String) |
| 160 | { |
| 161 | // CSOFF: EmptyBlockCheck |
| 162 | try |
| 163 | { |
| 164 | return Integer.parseInt((String) value); |
| 165 | } |
| 166 | catch (NumberFormatException nfe) |
| 167 | { |
| 168 | // Intentionnally blank: use default value |
| 169 | } |
| 170 | // CSON: EmptyBlockCheck |
| 171 | } |
| 172 | return defaultValue; |
| 173 | } |
| 174 | |
| 175 | private class ValueListener implements ChangeListener |
| 176 | { |
| 177 | public void stateChanged(ChangeEvent e) |
| 178 | { |
| 179 | if (_isAdjusting) |
| 180 | { |
| 181 | return; |
| 182 | } |
| 183 | int red; |
| 184 | int green; |
| 185 | int blue; |
| 186 | int alpha; |
| 187 | if (e.getSource() instanceof JSlider) |
| 188 | { |
| 189 | red = _rgbaSliders[RED].getValue(); |
| 190 | green = _rgbaSliders[GREEN].getValue(); |
| 191 | blue = _rgbaSliders[BLUE].getValue(); |
| 192 | alpha = _rgbaSliders[ALPHA].getValue(); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | red = ((Integer) _rgbaSpinners[RED].getValue()).intValue(); |
| 197 | green = ((Integer) _rgbaSpinners[GREEN].getValue()).intValue(); |
| 198 | blue = ((Integer) _rgbaSpinners[BLUE].getValue()).intValue(); |
| 199 | alpha = ((Integer) _rgbaSpinners[ALPHA].getValue()).intValue(); |
| 200 | } |
| 201 | ColorSelectionModel model = getColorSelectionModel(); |
| 202 | if (model instanceof AlphaColorSelectionModel) |
| 203 | { |
| 204 | ((AlphaColorSelectionModel) model).setAlpha(alpha, false); |
| 205 | } |
| 206 | model.setSelectedColor(new Color(red, green, blue)); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | protected void setColor(Color newColor) |
| 211 | { |
| 212 | setColor(RED, newColor.getRed()); |
| 213 | setColor(GREEN, newColor.getGreen()); |
| 214 | setColor(BLUE, newColor.getBlue()); |
| 215 | setColor(ALPHA, newColor.getAlpha()); |
| 216 | } |
| 217 | |
| 218 | protected void setColor(int row, int value) |
| 219 | { |
| 220 | if (_rgbaSliders[row].getValue() != value) |
| 221 | { |
| 222 | _rgbaSliders[row].setValue(value); |
| 223 | } |
| 224 | if (((Integer) _rgbaSpinners[row].getValue()).intValue() != value) |
| 225 | { |
| 226 | _rgbaSpinners[row].setValue(new Integer(value)); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | protected final ChangeListener _listener = new ValueListener(); |
| 231 | protected final JSlider[] _rgbaSliders = new JSlider[NUM_CHANNELS]; |
| 232 | protected final JSpinner[] _rgbaSpinners = new JSpinner[NUM_CHANNELS]; |
| 233 | private boolean _isAdjusting = false; |
| 234 | |
| 235 | static final private String RED_NAME = "red"; |
| 236 | static final private String GREEN_NAME = "green"; |
| 237 | static final private String BLUE_NAME = "blue"; |
| 238 | static final private String ALPHA_NAME = "alpha"; |
| 239 | static final private int RED = 0; |
| 240 | static final private int GREEN = 1; |
| 241 | static final private int BLUE = 2; |
| 242 | static final private int ALPHA = 3; |
| 243 | static final private int NUM_CHANNELS = 4; |
| 244 | static final private int MIN_VALUE = 0; |
| 245 | static final private int MAX_VALUE = 255; |
| 246 | } |