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