blob: a3610e0ee2ef9d7c64c74c0f627de2bd3c6571f6 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1996-2007 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.beans.editors;
27
28import java.awt.*;
29import java.beans.*;
30
31public class ColorEditor extends Panel implements PropertyEditor {
32 public ColorEditor() {
33 setLayout(null);
34
35 ourWidth = hPad;
36
37 // Create a sample color block bordered in black
38 Panel p = new Panel();
39 p.setLayout(null);
40 p.setBackground(Color.black);
41 sample = new Canvas();
42 p.add(sample);
43 sample.reshape(2, 2, sampleWidth, sampleHeight);
44 add(p);
45 p.reshape(ourWidth, 2, sampleWidth+4, sampleHeight+4);
46 ourWidth += sampleWidth + 4 + hPad;
47
48 text = new TextField("", 14);
49 add(text);
50 text.reshape(ourWidth,0,100,30);
51 ourWidth += 100 + hPad;
52
53 choser = new Choice();
54 int active = 0;
55 for (int i = 0; i < colorNames.length; i++) {
56 choser.addItem(colorNames[i]);
57 }
58 add(choser);
59 choser.reshape(ourWidth,0,100,30);
60 ourWidth += 100 + hPad;
61
62 resize(ourWidth,40);
63 }
64
65 public void setValue(Object o) {
66 Color c = (Color)o;
67 changeColor(c);
68 }
69
70 public Dimension preferredSize() {
71 return new Dimension(ourWidth, 40);
72 }
73
74 public boolean keyUp(Event e, int key) {
75 if (e.target == text) {
76 try {
77 setAsText(text.getText());
78 } catch (IllegalArgumentException ex) {
79 // Quietly ignore.
80 }
81 }
82 return (false);
83 }
84
85 public void setAsText(String s) throws java.lang.IllegalArgumentException {
86 if (s == null) {
87 changeColor(null);
88 return;
89 }
90 int c1 = s.indexOf(',');
91 int c2 = s.indexOf(',', c1+1);
92 if (c1 < 0 || c2 < 0) {
93 // Invalid string.
94 throw new IllegalArgumentException(s);
95 }
96 try {
97 int r = Integer.parseInt(s.substring(0,c1));
98 int g = Integer.parseInt(s.substring(c1+1, c2));
99 int b = Integer.parseInt(s.substring(c2+1));
100 Color c = new Color(r,g,b);
101 changeColor(c);
102 } catch (Exception ex) {
103 throw new IllegalArgumentException(s);
104 }
105
106 }
107
108 public boolean action(Event e, Object arg) {
109 if (e.target == choser) {
110 changeColor(colors[choser.getSelectedIndex()]);
111 }
112 return false;
113 }
114
115 public String getJavaInitializationString() {
116 return (this.color != null)
117 ? "new java.awt.Color(" + this.color.getRGB() + ",true)"
118 : "null";
119 }
120
121
122 private void changeColor(Color c) {
123
124 if (c == null) {
125 this.color = null;
126 this.text.setText("");
127 return;
128 }
129
130 color = c;
131
132 text.setText("" + c.getRed() + "," + c.getGreen() + "," + c.getBlue());
133
134 int active = 0;
135 for (int i = 0; i < colorNames.length; i++) {
136 if (color.equals(colors[i])) {
137 active = i;
138 }
139 }
140 choser.select(active);
141
142 sample.setBackground(color);
143 sample.repaint();
144
145 support.firePropertyChange("", null, null);
146 }
147
148 public Object getValue() {
149 return color;
150 }
151
152 public boolean isPaintable() {
153 return true;
154 }
155
156 public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) {
157 Color oldColor = gfx.getColor();
158 gfx.setColor(Color.black);
159 gfx.drawRect(box.x, box.y, box.width-3, box.height-3);
160 gfx.setColor(color);
161 gfx.fillRect(box.x+1, box.y+1, box.width-4, box.height-4);
162 gfx.setColor(oldColor);
163 }
164
165 public String getAsText() {
166 return (this.color != null)
167 ? this.color.getRed() + "," + this.color.getGreen() + "," + this.color.getBlue()
168 : null;
169 }
170
171 public String[] getTags() {
172 return null;
173 }
174
175 public java.awt.Component getCustomEditor() {
176 return this;
177 }
178
179 public boolean supportsCustomEditor() {
180 return true;
181 }
182
183 public void addPropertyChangeListener(PropertyChangeListener l) {
184 support.addPropertyChangeListener(l);
185 }
186
187 public void removePropertyChangeListener(PropertyChangeListener l) {
188 support.removePropertyChangeListener(l);
189 }
190
191
192 private String colorNames[] = { " ", "white", "lightGray", "gray", "darkGray",
193 "black", "red", "pink", "orange",
194 "yellow", "green", "magenta", "cyan",
195 "blue"};
196 private Color colors[] = { null, Color.white, Color.lightGray, Color.gray, Color.darkGray,
197 Color.black, Color.red, Color.pink, Color.orange,
198 Color.yellow, Color.green, Color.magenta, Color.cyan,
199 Color.blue};
200
201 private Canvas sample;
202 private int sampleHeight = 20;
203 private int sampleWidth = 40;
204 private int hPad = 5;
205 private int ourWidth;
206
207 private Color color;
208 private TextField text;
209 private Choice choser;
210
211 private PropertyChangeSupport support = new PropertyChangeSupport(this);
212}