001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package test.check;
015:
016: import java.awt.*;
017: import java.awt.event.ActionEvent;
018: import java.awt.event.ActionListener;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import javax.swing.*;
023:
024: import org.jvnet.substance.SubstanceBorder;
025:
026: /**
027: *
028: * @author Administrator
029: */
030: public class ColorComboBox extends JComboBox {
031:
032: public static final String PROP_COLOR = "color"; // NOI18N
033:
034: public static final Value CUSTOM_COLOR = new Value(loc("Custom"),
035: null); // NOI18N
036:
037: private static Map<Color, String> colorMap = new HashMap<Color, String>();
038: static {
039: colorMap.put(Color.BLACK, loc("Black")); // NOI18N
040: colorMap.put(Color.BLUE, loc("Blue")); // NOI18N
041: colorMap.put(Color.CYAN, loc("Cyan")); // NOI18N
042: colorMap.put(Color.DARK_GRAY, loc("Dark_Gray")); // NOI18N
043: colorMap.put(Color.GRAY, loc("Gray")); // NOI18N
044: colorMap.put(Color.GREEN, loc("Green")); // NOI18N
045: colorMap.put(Color.LIGHT_GRAY, loc("Light_Gray")); // NOI18N
046: colorMap.put(Color.MAGENTA, loc("Magenta")); // NOI18N
047: colorMap.put(Color.ORANGE, loc("Orange")); // NOI18N
048: colorMap.put(Color.PINK, loc("Pink")); // NOI18N
049: colorMap.put(Color.RED, loc("Red")); // NOI18N
050: colorMap.put(Color.WHITE, loc("White")); // NOI18N
051: colorMap.put(Color.YELLOW, loc("Yellow")); // NOI18N
052: }
053:
054: private static Object[] content = new Object[] {
055: new Value(Color.BLACK), new Value(Color.BLUE),
056: new Value(Color.CYAN), new Value(Color.DARK_GRAY),
057: new Value(Color.GRAY), new Value(Color.GREEN),
058: new Value(Color.LIGHT_GRAY), new Value(Color.MAGENTA),
059: new Value(Color.ORANGE), new Value(Color.PINK),
060: new Value(Color.RED), new Value(Color.WHITE),
061: new Value(Color.YELLOW), CUSTOM_COLOR };
062:
063: private Color lastColor;
064:
065: /** Creates a new instance of ColorChooser */
066: public ColorComboBox() {
067: super (content);
068: setRenderer(new Renderer());
069: setEditable(true);
070: setEditor(new Renderer());
071: setSelectedItem(new Value(null, null));
072: addActionListener(new ActionListener() {
073: public void actionPerformed(ActionEvent ev) {
074: if (getSelectedItem() == CUSTOM_COLOR) {
075: Color c = JColorChooser.showDialog(SwingUtilities
076: .getAncestorOfClass(Dialog.class,
077: ColorComboBox.this ),
078: loc("SelectColor"), lastColor);
079: if (c != null)
080: setColor(c);
081: } else {
082: lastColor = ((Value) getSelectedItem()).color;
083: }
084: ColorComboBox.this .firePropertyChange(PROP_COLOR, null,
085: null);
086: }
087: });
088: }
089:
090: public void setInheritedColor(Color color) {
091: Object[] ncontent = new Object[content.length];
092: System.arraycopy(content, 0, ncontent, 0, content.length);
093: if (color != null)
094: ncontent[content.length - 1] = new Value(
095: loc("CTL_Inherited_Color"), color // NOI18N
096: );
097: else
098: ncontent[content.length - 1] = new Value(
099: loc("CTL_None_Color"), null // NOI18N
100: );
101: setModel(new DefaultComboBoxModel(ncontent));
102: }
103:
104: public void setColor(Color color) {
105: if (color == null) {
106: setSelectedIndex(content.length - 1);
107: lastColor = ((Value) getItemAt(content.length - 1)).color;
108: } else {
109: setSelectedItem(new Value(color));
110: lastColor = color;
111: }
112: }
113:
114: public Color getColor() {
115: if (getSelectedIndex() == (content.length - 1))
116: return null;
117: return ((Value) getSelectedItem()).color;
118: }
119:
120: private static String loc(String key) {
121: return key;
122: }
123:
124: // innerclasses ............................................................
125:
126: public static class Value {
127: String text;
128:
129: Color color;
130:
131: Value(Color color) {
132: this .color = color;
133: text = (String) colorMap.get(color);
134: if (text != null)
135: return;
136: StringBuffer sb = new StringBuffer();
137: sb.append('[').append(color.getRed()).append(',').append(
138: color.getGreen()).append(',').append(
139: color.getBlue()).append(']');
140: text = sb.toString();
141: }
142:
143: Value(String text, Color color) {
144: this .text = text;
145: this .color = color;
146: }
147: }
148:
149: private class Renderer extends JComponent implements
150: ListCellRenderer, ComboBoxEditor {
151:
152: private int SIZE = 9;
153:
154: private Value value;
155:
156: Renderer() {
157: setPreferredSize(new Dimension(50, getFontMetrics(
158: ColorComboBox.this .getFont()).getHeight() + 2));
159: setOpaque(true);
160: setFocusable(true);
161: setBorder(new SubstanceBorder());
162: }
163:
164: public void paint(Graphics g) {
165: Color oldColor = g.getColor();
166: Dimension size = getSize();
167: if (isFocusOwner())
168: g.setColor(SystemColor.textHighlight);
169: else
170: g.setColor(getBackground());
171: g.fillRect(0, 0, size.width, size.height);
172: int i = (size.height - SIZE) / 2;
173: if (value.color != null) {
174: g.setColor(Color.black);
175: g.drawRect(i, i, SIZE, SIZE);
176: g.setColor(value.color);
177: g.fillRect(i + 1, i + 1, SIZE - 1, SIZE - 1);
178: }
179: if (value.text != null) {
180: if (isFocusOwner())
181: g.setColor(SystemColor.textHighlightText);
182: else
183: g.setColor(getForeground());
184: if (value.color != null)
185: g.drawString(value.text, i + SIZE + 5, i + SIZE);
186: else
187: g.drawString(value.text, 5, i + SIZE);
188: }
189: g.setColor(oldColor);
190: }
191:
192: public void setEnabled(boolean enabled) {
193: setBackground(enabled ? SystemColor.text
194: : SystemColor.control);
195: super .setEnabled(enabled);
196: }
197:
198: public Component getListCellRendererComponent(JList list,
199: Object value, int index, boolean isSelected,
200: boolean cellHasFocus) {
201: this .value = (Value) value;
202: setEnabled(list.isEnabled());
203: setBackground(isSelected ? SystemColor.textHighlight
204: : SystemColor.text);
205: setForeground(isSelected ? SystemColor.textHighlightText
206: : SystemColor.textText);
207: return this ;
208: }
209:
210: public Component getEditorComponent() {
211: setEnabled(ColorComboBox.this .isEnabled());
212: setBackground(ColorComboBox.this .isFocusOwner() ? SystemColor.textHighlight
213: : SystemColor.text);
214: setForeground(ColorComboBox.this .isFocusOwner() ? SystemColor.textHighlightText
215: : SystemColor.textText);
216: return this ;
217: }
218:
219: public void setItem(Object anObject) {
220: this .value = (Value) anObject;
221: }
222:
223: public Object getItem() {
224: return value;
225: }
226:
227: public void selectAll() {
228: }
229:
230: public void addActionListener(ActionListener l) {
231: }
232:
233: public void removeActionListener(ActionListener l) {
234: }
235: }
236: }
|