KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > base > ColorComboBox


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.gui.base;
17
18 import java.awt.Color JavaDoc;
19 import java.awt.event.ItemEvent JavaDoc;
20 import java.awt.event.ItemListener JavaDoc;
21
22 import javax.swing.ComboBoxModel JavaDoc;
23 import javax.swing.JColorChooser JavaDoc;
24 import javax.swing.JComboBox JavaDoc;
25
26
27 /**
28  * A JComboBox that displays Colors.
29  * @author redsolo
30  */

31
32 public class ColorComboBox extends JComboBox JavaDoc implements ItemListener JavaDoc {
33     private boolean codeSelectionUpdate = false;
34
35     /**
36  * Constructs a color combobo.x
37  */

38     public ColorComboBox() {
39         super();
40
41         // Add the default colors items.
42
addItem(new ColorItem(Color.black, "None"));
43         addItem(new ColorItem(Color.blue, "Blue"));
44         addItem(new ColorItem(Color.gray, "Gray"));
45         addItem(new ColorItem(Color.green, "Green"));
46         addItem(new ColorItem(Color.red, "Red"));
47         addItem(new ColorItem(Color.yellow, "Yellow"));
48         addItem(new ColorItem(Color.black, "Custom"));
49
50         setRenderer(new ColorItemRenderer());
51
52         addItemListener(this);
53     }
54
55     /**
56  * Selects the combobox item with the specified color name.
57  * @param name the name of the color to select.
58  */

59     public void setSelectedColor(String JavaDoc name) {
60         codeSelectionUpdate = true;
61
62         ComboBoxModel JavaDoc model = getModel();
63
64         if (name == null) {
65             setSelectedIndex(0);
66         } else {
67             for (int i = 0; i < model.getSize(); i++) {
68                 ColorItem object = (ColorItem) model.getElementAt(i);
69
70                 if (object.getName().equalsIgnoreCase(name)) {
71                     setSelectedIndex(i);
72
73                     break;
74                 }
75             }
76         }
77
78         codeSelectionUpdate = false;
79     }
80
81     /**
82  * Sets the color for the Custom color item.
83  * @param color the new color for the Custom color.
84  */

85     public void setCustomColor(Color JavaDoc color) {
86         ColorItem item = (ColorItem) getModel().getElementAt(getModel().getSize() -
87                 1);
88         item.setColor(color);
89         repaint();
90     }
91
92     /**
93  * Sets the color for the Custom color item.
94  * @param rgb the new color, in rgb value, for the Custom color.
95  */

96     public void setCustomColor(int rgb) {
97         setCustomColor(ColorFactory.getColor(rgb));
98     }
99
100     /**
101  * Returns the selected coloritem.
102  * @return the selected coloritem.
103  */

104     public ColorItem getSelectedColorItem() {
105         return (ColorItem) getSelectedItem();
106     }
107
108     /** {@inheritDoc} */
109     public void itemStateChanged(ItemEvent JavaDoc e) {
110         if ((!codeSelectionUpdate) &&
111                 (e.getStateChange() == ItemEvent.SELECTED)) {
112             ColorItem item = (ColorItem) getSelectedItem();
113
114             if (item.getName().equalsIgnoreCase("custom")) {
115                 Color JavaDoc newColor = JColorChooser.showDialog(null,
116                         "Choose Background Color", item.getColor());
117
118                 item.setColor(newColor);
119                 repaint();
120             }
121         }
122     }
123 }
124
Popular Tags