1 /* 2 * Copyright (c) 2008, 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 javax.swing.colorchooser; 27 28 import java.awt.Color; 29 import java.awt.ContainerOrderFocusTraversalPolicy; 30 import java.awt.GridBagConstraints; 31 import java.awt.GridBagLayout; 32 import java.awt.Insets; 33 import java.awt.event.ActionEvent; 34 import java.awt.event.ActionListener; 35 import javax.swing.ButtonGroup; 36 import javax.swing.JLabel; 37 import javax.swing.JPanel; 38 import javax.swing.JRadioButton; 39 import javax.swing.border.EmptyBorder; 40 import javax.swing.JSpinner.DefaultEditor; 41 42 final class ColorPanel extends JPanel implements ActionListener { 43 44 private final SlidingSpinner[] spinners = new SlidingSpinner[5]; 45 private final float[] values = new float[this.spinners.length]; 46 47 private final ColorModel model; 48 private Color color; 49 private int x = 1; 50 private int y = 2; 51 private int z; 52 53 ColorPanel(ColorModel model) { 54 super(new GridBagLayout()); 55 56 GridBagConstraints gbc = new GridBagConstraints(); 57 gbc.fill = GridBagConstraints.HORIZONTAL; 58 59 gbc.gridx = 1; 60 ButtonGroup group = new ButtonGroup(); 61 EmptyBorder border = null; 62 for (int i = 0; i < this.spinners.length; i++) { 63 if (i < 3) { 64 JRadioButton button = new JRadioButton(); 65 if (i == 0) { 66 Insets insets = button.getInsets(); 67 insets.left = button.getPreferredSize().width; 68 border = new EmptyBorder(insets); 69 button.setSelected(true); 70 gbc.insets.top = 5; 71 } 72 add(button, gbc); 73 group.add(button); 74 button.setActionCommand(Integer.toString(i)); 75 button.addActionListener(this); 76 this.spinners[i] = new SlidingSpinner(this, button); 77 } 78 else { 79 JLabel label = new JLabel(); 80 add(label, gbc); 81 label.setBorder(border); 82 label.setFocusable(false); 83 this.spinners[i] = new SlidingSpinner(this, label); 84 } 85 } 86 gbc.gridx = 2; 87 gbc.weightx = 1.0; 88 gbc.insets.top = 0; 89 gbc.insets.left = 5; 90 for (SlidingSpinner spinner : this.spinners) { 91 add(spinner.getSlider(), gbc); 92 gbc.insets.top = 5; 93 } 94 gbc.gridx = 3; 95 gbc.weightx = 0.0; 96 gbc.insets.top = 0; 97 for (SlidingSpinner spinner : this.spinners) { 98 add(spinner.getSpinner(), gbc); 99 gbc.insets.top = 5; 100 } 101 setFocusTraversalPolicy(new ContainerOrderFocusTraversalPolicy()); 102 setFocusTraversalPolicyProvider(true); 103 setFocusable(false); 104 105 this.model = model; 106 } 107 108 public void actionPerformed(ActionEvent event) { 109 try { 110 this.z = Integer.parseInt(event.getActionCommand()); 111 this.y = (this.z != 2) ? 2 : 1; 112 this.x = (this.z != 0) ? 0 : 1; 113 getParent().repaint(); 114 } 115 catch (NumberFormatException exception) { 116 } 117 } 118 119 void buildPanel() { 120 int count = this.model.getCount(); 121 this.spinners[4].setVisible(count > 4); 122 for (int i = 0; i < count; i++) { 123 String text = this.model.getLabel(this, i); 124 Object object = this.spinners[i].getLabel(); 125 if (object instanceof JRadioButton) { 126 JRadioButton button = (JRadioButton) object; 127 button.setText(text); 128 button.getAccessibleContext().setAccessibleDescription(text); 129 } 130 else if (object instanceof JLabel) { 131 JLabel label = (JLabel) object; 132 label.setText(text); 133 } 134 this.spinners[i].setRange(this.model.getMinimum(i), this.model.getMaximum(i)); 135 this.spinners[i].setValue(this.values[i]); 136 this.spinners[i].getSlider().getAccessibleContext().setAccessibleName(text); 137 this.spinners[i].getSpinner().getAccessibleContext().setAccessibleName(text); 138 DefaultEditor editor = (DefaultEditor) this.spinners[i].getSpinner().getEditor(); 139 editor.getTextField().getAccessibleContext().setAccessibleName(text); 140 this.spinners[i].getSlider().getAccessibleContext().setAccessibleDescription(text); 141 this.spinners[i].getSpinner().getAccessibleContext().setAccessibleDescription(text); 142 editor.getTextField().getAccessibleContext().setAccessibleDescription(text); 143 } 144 } 145 146 void colorChanged() { 147 this.color = new Color(getColor(0), true); 148 Object parent = getParent(); 149 if (parent instanceof ColorChooserPanel) { 150 ColorChooserPanel chooser = (ColorChooserPanel) parent; 151 chooser.setSelectedColor(this.color); 152 chooser.repaint(); 153 } 154 } 155 156 float getValueX() { 157 return this.spinners[this.x].getValue(); 158 } 159 160 float getValueY() { 161 return 1.0f - this.spinners[this.y].getValue(); 162 } 163 164 float getValueZ() { 165 return 1.0f - this.spinners[this.z].getValue(); 166 } 167 168 void setValue(float z) { 169 this.spinners[this.z].setValue(1.0f - z); 170 colorChanged(); 171 } 172 173 void setValue(float x, float y) { 174 this.spinners[this.x].setValue(x); 175 this.spinners[this.y].setValue(1.0f - y); 176 colorChanged(); 177 } 178 179 int getColor(float z) { 180 setDefaultValue(this.x); 181 setDefaultValue(this.y); 182 this.values[this.z] = 1.0f - z; 183 return getColor(3); 184 } 185 186 int getColor(float x, float y) { 187 this.values[this.x] = x; 188 this.values[this.y] = 1.0f - y; 189 setValue(this.z); 190 return getColor(3); 191 } 192 193 void setColor(Color color) { 194 if (!color.equals(this.color)) { 195 this.color = color; 196 this.model.setColor(color.getRGB(), this.values); 197 for (int i = 0; i < this.model.getCount(); i++) { 198 this.spinners[i].setValue(this.values[i]); 199 } 200 } 201 } 202 203 private int getColor(int index) { 204 while (index < this.model.getCount()) { 205 setValue(index++); 206 } 207 return this.model.getColor(this.values); 208 } 209 210 private void setValue(int index) { 211 this.values[index] = this.spinners[index].getValue(); 212 } 213 214 private void setDefaultValue(int index) { 215 float value = this.model.getDefault(index); 216 this.values[index] = (value < 0.0f) 217 ? this.spinners[index].getValue() 218 : value; 219 } 220 }