View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.gui;
21  
22  import java.awt.Color;
23  import java.awt.Component;
24  import java.awt.Graphics;
25  import java.awt.event.ActionEvent;
26  import java.util.Hashtable;
27  import java.util.Map;
28  import javax.swing.Action;
29  import javax.swing.Box;
30  import javax.swing.BoxLayout;
31  import javax.swing.DefaultListCellRenderer;
32  import javax.swing.Icon;
33  import javax.swing.JColorChooser;
34  import javax.swing.JComboBox;
35  import javax.swing.JList;
36  import javax.swing.JPanel;
37  import org.xnap.commons.gui.action.AbstractXNapAction;
38  import org.xnap.commons.i18n.I18n;
39  import org.xnap.commons.i18n.I18nFactory;
40  
41  /***
42   * Provides a panel that displays a {@link JComboBox} with predefined colors and
43   * a button that can launch a {@link JColorChooser}.
44   * 
45   * @author Steffen Pingel
46   */
47  public class ColorChooserPanel extends JPanel {
48  
49  	private static final I18n i18n = I18nFactory.getI18n(ColorChooserPanel.class);
50  	
51  	private Color customColor = new Color(254, 254, 254);
52  	private JComboBox colorComboBox;
53  	private Map<Color, String> namesByColor = new Hashtable<Color, String>();
54  	private ColorChooserAction colorAction;
55  	private Component dialogParent;
56  	
57  	/***
58  	 * Creates a panel with a list of default colors and selects the specified
59  	 * color.
60  	 * 
61  	 * @param selectedColor the color to select
62  	 */
63  	public ColorChooserPanel(Color selectedColor)
64  	{
65  		initialize();
66  		
67  		setSelectedColor(selectedColor);
68  	}
69  
70  	/***
71  	 * Creates a panel with a list of default colors.
72  	 */
73  	public ColorChooserPanel()
74  	{
75  		initialize();
76  	}
77  
78  	private void initialize()
79  	{
80  		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
81  		colorComboBox = new JComboBox();
82  		colorComboBox.setRenderer(new ColorCellRenderer());
83  		add(colorComboBox);
84  		add(Box.createHorizontalStrut(4));
85  			
86  		namesByColor.put(Color.black, i18n.tr("Black"));
87  		namesByColor.put(Color.darkGray, i18n.tr("DarkGray"));
88  		namesByColor.put(Color.gray, i18n.tr("Gray"));
89  		namesByColor.put(Color.lightGray, i18n.tr("LightGray"));
90  		namesByColor.put(Color.blue, i18n.tr("Blue"));
91  		namesByColor.put(Color.cyan, i18n.tr("Cyan"));
92  		namesByColor.put(Color.magenta, i18n.tr("Magenta"));
93  		namesByColor.put(Color.pink, i18n.tr("Pink"));
94  		namesByColor.put(Color.red, i18n.tr("Red"));
95  		namesByColor.put(Color.orange, i18n.tr("Orange"));
96  		namesByColor.put(Color.green, i18n.tr("Green"));
97  		namesByColor.put(Color.yellow, i18n.tr("Yellow"));
98  		namesByColor.put(Color.white, i18n.tr("White"));
99  	
100 		for (Color color : namesByColor.keySet()) {
101 			colorComboBox.addItem(color);
102 		}
103 		
104 		colorComboBox.addItem(customColor);
105 		colorAction = new ColorChooserAction();
106 		add(Builder.createIconButton(colorAction));
107 	}
108 
109 	/***
110 	 * Returns the currently selected color.
111 	 * 
112 	 * @return the selected color
113 	 */
114 	public Color getSelectedColor()
115 	{
116 		return (Color)colorComboBox.getSelectedItem();
117 	}
118 
119 	/***
120 	 * Selects the specified color in the list.
121 	 * 
122 	 * @param newValue the color to select; must not be null
123 	 * @throws if newValue == null
124 	 */
125 	public void setSelectedColor(Color newValue)
126 	{
127 		if (newValue == null) {
128 			throw new IllegalArgumentException();
129 		}
130 		
131 		if (namesByColor.get(newValue) != null) {
132 			colorComboBox.setSelectedItem(newValue);
133 		}
134 		else {
135 			colorComboBox.removeItem(customColor);
136 			customColor = newValue;
137 			colorComboBox.addItem(customColor);
138 			colorComboBox.setSelectedItem(customColor);
139 		}
140 	}
141 
142 	/***
143 	 * Returns the parent of the file chooser dialog.
144 	 * 
145 	 * @return the parent of the file chooser dialog
146 	 */
147 	public Component getDialogParent() 
148 	{
149 		return dialogParent;
150 	}
151 
152 	/***
153 	 * Sets the parent of the color chooser dialog that is used internally.
154 	 * <p>
155 	 * See {@link JColorChooser#showDialog(java.awt.Component, java.lang.String, java.awt.Color)}
156 	 * for details on dialog parents.
157 	 */
158 	public void setDialogParent(Component dialogParent)
159 	{
160 		this.dialogParent = dialogParent;
161 	}
162 
163 	protected class ColorChooserAction extends AbstractXNapAction {
164 
165 		public ColorChooserAction()
166 		{
167 			putValue(Action.SHORT_DESCRIPTION, 
168 					i18n.tr("Opens color selection dialog"));
169 			putValue(ICON_FILENAME, "palette_color.png");
170 		}
171 
172 		public void actionPerformed(ActionEvent event)
173 		{
174 			Color c = JColorChooser.showDialog(getDialogParent(), 
175 					i18n.tr("Choose color"), getSelectedColor());
176 			if (c != null) {
177 				setSelectedColor(c);
178 			}
179 		}
180 	}
181 
182 	protected class ColorCellRenderer extends DefaultListCellRenderer {
183 
184 		public ColorIcon icon = new ColorIcon(Color.white);
185 
186 		public Component getListCellRendererComponent(JList list, Object val,
187 				int idx, boolean isSel, boolean hasFocus)
188 		{
189 			super.getListCellRendererComponent(list, val, idx, isSel, hasFocus);
190 			String colorName = (String)namesByColor.get(val);
191 			if (colorName == null) {
192 				colorName = i18n.tr("Custom");
193 			}
194 			setText(colorName);
195 			icon.setColor((Color)val);
196 			setIcon(icon);
197 			return this;
198 		}
199 	}
200 
201 	protected class ColorIcon implements Icon {
202 
203 		private int height = 15;
204 		private int width = 15;
205 		private Color color;
206 
207 		public ColorIcon(Color color)
208 		{
209 			this.color = color;
210 		}
211 
212 		public int getIconHeight()
213 		{
214 			return height;
215 		}
216 
217 		public int getIconWidth()
218 		{
219 			return width;
220 		}
221 
222 		public void setColor(Color newValue)
223 		{
224 			this.color = newValue;
225 		}
226 
227 		public void paintIcon(Component c, Graphics g, int x, int y)
228 		{
229 			g.setColor(Color.black);
230 			g.drawRect(x, y, width, height);
231 			g.setColor(color);
232 			g.fillRect(x + 1, y + 1, width - 1, height - 1);
233 		}
234 	}
235 }