001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.ui;
027:
028: import java.awt.Color;
029: import java.awt.Component;
030: import java.awt.Dimension;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033:
034: import java.util.Vector;
035:
036: import javax.swing.AbstractListModel;
037: import javax.swing.BorderFactory;
038: import javax.swing.ComboBoxModel;
039: import javax.swing.DefaultListCellRenderer;
040: import javax.swing.JColorChooser;
041: import javax.swing.JComboBox;
042: import javax.swing.JList;
043: import javax.swing.event.ChangeEvent;
044: import javax.swing.event.ChangeListener;
045:
046: /**
047: *
048: *
049: * @author $author$
050: * @version $Revision: 1.13 $
051: */
052: public class ColorComboBox extends JComboBox {
053: /**
054: * Creates a new ColorComboBox object.
055: */
056: public ColorComboBox() {
057: this (null);
058: }
059:
060: /**
061: * Creates a new ColorComboBox object.
062: *
063: * @param color
064: */
065: public ColorComboBox(Color color) {
066: super (new ColorComboModel());
067: setColor(color);
068: setRenderer(new ColorRenderer());
069: addActionListener(new ActionListener() {
070: public void actionPerformed(ActionEvent evt) {
071: if (getSelectedItem() == null) {
072: chooseCustomColor();
073: } else {
074: fireChangeEvent();
075: }
076: }
077: });
078: }
079:
080: /**
081: *
082: */
083: protected void fireChangeEvent() {
084: ChangeEvent evt = new ChangeEvent(this );
085: ChangeListener[] l = (ChangeListener[]) listenerList
086: .getListeners(ChangeListener.class);
087:
088: for (int i = (l.length - 1); i >= 0; i--) {
089: l[i].stateChanged(evt);
090: }
091: }
092:
093: /**
094: *
095: *
096: * @param l
097: */
098: public void addChangeListener(ChangeListener l) {
099: listenerList.add(ChangeListener.class, l);
100: }
101:
102: /**
103: *
104: *
105: * @param l
106: */
107: public void removeChangeListener(ChangeListener l) {
108: listenerList.remove(ChangeListener.class, l);
109: }
110:
111: private void chooseCustomColor() {
112: Color c = JColorChooser.showDialog(this , "Custom Color",
113: Color.black);
114:
115: if (c != null) {
116: setColor(c);
117: fireChangeEvent();
118: }
119: }
120:
121: /**
122: *
123: *
124: * @param c
125: */
126: public void setColor(Color c) {
127: for (int i = 0; i < (getModel().getSize() - 1); i++) {
128: Color z = (Color) getModel().getElementAt(i);
129:
130: if (z.equals(c)) {
131: setSelectedIndex(i);
132:
133: return;
134: }
135: }
136:
137: if (c != null) {
138: ((ColorComboModel) getModel()).addColor(c);
139: }
140: }
141:
142: /**
143: *
144: *
145: * @return
146: */
147: public Color getColor() {
148: return (Color) getSelectedItem();
149: }
150:
151: // Supporting classes
152: static class ColorComboModel extends AbstractListModel implements
153: ComboBoxModel {
154: private Vector colors = new Vector();
155: private Object selected;
156:
157: ColorComboModel() {
158: colors = new Vector();
159:
160: // Add the initial colors
161: colors.addElement(Color.black);
162: colors.addElement(Color.white);
163: colors.addElement(Color.red.darker());
164: colors.addElement(Color.red);
165: colors.addElement(Color.orange.darker());
166: colors.addElement(Color.orange);
167: colors.addElement(Color.yellow.darker());
168: colors.addElement(Color.yellow);
169: colors.addElement(Color.green.darker());
170: colors.addElement(Color.green);
171: colors.addElement(Color.blue.darker());
172: colors.addElement(Color.blue);
173: colors.addElement(Color.cyan.darker());
174: colors.addElement(Color.cyan);
175: colors.addElement(Color.magenta.darker());
176: colors.addElement(Color.magenta);
177: colors.addElement(Color.pink.darker());
178: colors.addElement(Color.pink);
179: colors.addElement(Color.lightGray);
180: colors.addElement(Color.gray);
181: colors.addElement(Color.darkGray);
182:
183: // Black is initialy selected
184: selected = colors.elementAt(0);
185: }
186:
187: public int getSize() {
188: return colors.size() + 1;
189: }
190:
191: public Object getElementAt(int i) {
192: if (i == colors.size()) {
193: return null;
194: } else {
195: return colors.elementAt(i);
196: }
197: }
198:
199: public void setSelectedItem(Object sel) {
200: selected = sel;
201: }
202:
203: public Object getSelectedItem() {
204: return selected;
205: }
206:
207: public void addColor(Color c) {
208: int idx = colors.size();
209: colors.addElement(c);
210: selected = c;
211: fireIntervalAdded(this , idx, idx);
212: }
213: }
214:
215: class ColorRenderer extends DefaultListCellRenderer {
216: private ColorIcon icon;
217:
218: ColorRenderer() {
219: icon = new ColorIcon(Color.black, new Dimension(10, 10),
220: Color.black);
221: setBorder(BorderFactory.createEmptyBorder(0, 16, 0, 0));
222: }
223:
224: public Component getListCellRendererComponent(JList list,
225: Object value, int index, boolean isSelected,
226: boolean cellHasFocus) {
227: super .getListCellRendererComponent(list, value, index,
228: isSelected, cellHasFocus);
229:
230: Color c = (Color) value;
231:
232: // If the value is null. Then this signifies custom color
233: if (c == null) {
234: setIcon(javax.swing.plaf.basic.BasicIconFactory
235: .getCheckBoxIcon());
236: setText("Custom ....");
237: } else {
238: // Set up the icon
239: icon.setColor(c);
240: setIcon(icon);
241:
242: // Set the text. If the color is a well known one with a name, render
243: // the name. Otherwise use the RGB values
244: String s = "#" + c.getRed() + "," + c.getGreen() + ","
245: + c.getBlue();
246:
247: if (c.equals(Color.black)) {
248: s = "Black";
249: } else if (c.equals(Color.white)) {
250: s = "White";
251: } else if (c.equals(Color.red)) {
252: s = "Red";
253: } else if (c.equals(Color.orange)) {
254: s = "Orange";
255: } else if (c.equals(Color.yellow)) {
256: s = "Yellow";
257: } else if (c.equals(Color.green)) {
258: s = "Green";
259: } else if (c.equals(Color.blue)) {
260: s = "Blue";
261: } else if (c.equals(Color.cyan)) {
262: s = "Cyan";
263: } else if (c.equals(Color.magenta)) {
264: s = "Magenta";
265: } else if (c.equals(Color.pink)) {
266: s = "Pink";
267: } else if (c.equals(Color.lightGray)) {
268: s = "Light Gray";
269: } else if (c.equals(Color.gray)) {
270: s = "Gray";
271: } else if (c.equals(Color.darkGray)) {
272: s = "Dark Gray";
273: }
274:
275: setText(s);
276: }
277:
278: //
279: return this;
280: }
281: }
282: }
|