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