gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / pickercontroller / ColorPickerControllerImpl.java @ 1746
History | View | Annotate | Download (6.41 KB)
1 |
|
---|---|
2 |
package org.gvsig.tools.swing.impl.pickercontroller; |
3 |
|
4 |
import java.awt.Color; |
5 |
import java.awt.Font; |
6 |
import java.awt.event.ActionEvent; |
7 |
import java.awt.event.ActionListener; |
8 |
import java.net.URL; |
9 |
import javax.swing.ImageIcon; |
10 |
|
11 |
import javax.swing.JButton; |
12 |
import javax.swing.JColorChooser; |
13 |
import javax.swing.JSlider; |
14 |
import javax.swing.JTextField; |
15 |
import javax.swing.SwingUtilities; |
16 |
import javax.swing.event.ChangeEvent; |
17 |
import javax.swing.event.ChangeListener; |
18 |
|
19 |
import org.gvsig.tools.ToolsLocator; |
20 |
import org.gvsig.tools.dataTypes.CoercionException; |
21 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
22 |
import org.gvsig.tools.i18n.I18nManager; |
23 |
import org.gvsig.tools.swing.api.DataTypes; |
24 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
25 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
26 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
27 |
import org.gvsig.tools.swing.api.pickercontroller.ColorPickerController; |
28 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
29 |
import org.gvsig.tools.swing.icontheme.IconThemeManager; |
30 |
|
31 |
public class ColorPickerControllerImpl |
32 |
extends AbstractPickerController<Color> |
33 |
implements ColorPickerController
|
34 |
{ |
35 |
|
36 |
private final JTextField txtLabel; |
37 |
private final JButton btnShowDialog; |
38 |
private final JSlider sldAlpha; |
39 |
private Color color; |
40 |
private boolean allowNull; |
41 |
|
42 |
|
43 |
public static void selfRegister() { |
44 |
URL imageResource = DatePickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-color.png"); |
45 |
if (imageResource != null) { |
46 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
47 |
IconTheme theme = iconThemeManager.getCurrent(); |
48 |
ImageIcon icon = new ImageIcon(imageResource); |
49 |
theme.registerDefault("tools", "picker", "picker-color", icon, imageResource); |
50 |
} |
51 |
} |
52 |
|
53 |
public ColorPickerControllerImpl(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha, boolean allowNull) { |
54 |
this.txtLabel = txtLabel;
|
55 |
this.btnShowDialog = btnShowDialog;
|
56 |
this.sldAlpha = sldAlpha;
|
57 |
|
58 |
this.txtLabel.setEditable(false); |
59 |
|
60 |
this.btnShowDialog.setIcon(this.getIcon("picker-color")); |
61 |
|
62 |
// Remove the listeners to prevent strange behavior when you call this
|
63 |
// class repeatedly with the same button and slider.
|
64 |
for(ActionListener l : btnShowDialog.getActionListeners()) { |
65 |
btnShowDialog.removeActionListener(l); |
66 |
} |
67 |
for( ChangeListener l : sldAlpha.getChangeListeners()) { |
68 |
sldAlpha.removeChangeListener(l); |
69 |
} |
70 |
this.btnShowDialog.addActionListener(new ActionListener() { |
71 |
@Override
|
72 |
public void actionPerformed(ActionEvent e) { |
73 |
doShowDialog(); |
74 |
} |
75 |
}); |
76 |
if( this.sldAlpha != null ) { |
77 |
this.sldAlpha.setMinimum(0); |
78 |
this.sldAlpha.setMaximum(255); |
79 |
this.sldAlpha.addChangeListener(new ChangeListener() { |
80 |
@Override
|
81 |
public void stateChanged(ChangeEvent e) { |
82 |
doAlphaChanged(); |
83 |
} |
84 |
}); |
85 |
} |
86 |
Font font = this.txtLabel.getFont(); |
87 |
try {
|
88 |
font = new Font(Font.MONOSPACED, Font.PLAIN, font.getSize()); |
89 |
this.txtLabel.setFont(font);
|
90 |
} catch(Exception ex) { |
91 |
// If dont set a monospaced font ignore it.
|
92 |
font = null;
|
93 |
} |
94 |
this.allowNull = allowNull;
|
95 |
if( !this.allowNull ) { |
96 |
this.color = Color.BLACK; |
97 |
} |
98 |
} |
99 |
|
100 |
public ColorPickerControllerImpl(JTextField txtLabel, JButton btnShowDialog) { |
101 |
this(txtLabel, btnShowDialog, null, false); |
102 |
} |
103 |
|
104 |
public ColorPickerControllerImpl(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha) { |
105 |
this(txtLabel, btnShowDialog, sldAlpha, false); |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public boolean isEmpty() { |
110 |
return this.color==null; |
111 |
} |
112 |
|
113 |
private void doAlphaChanged() { |
114 |
if( !this.isEditable() ) { |
115 |
return;
|
116 |
} |
117 |
if( this.color == null ) { |
118 |
set(Color.BLACK);
|
119 |
} |
120 |
if( this.sldAlpha == null ) { |
121 |
set(new Color( |
122 |
color.getRed(), |
123 |
color.getGreen(), |
124 |
color.getBlue() |
125 |
)); |
126 |
} else {
|
127 |
int alpha = this.sldAlpha.getValue(); |
128 |
set(new Color( |
129 |
color.getRed(), |
130 |
color.getGreen(), |
131 |
color.getBlue(), |
132 |
alpha |
133 |
)); |
134 |
} |
135 |
} |
136 |
|
137 |
protected void doShowDialog() { |
138 |
if( !this.isEditable() ) { |
139 |
return;
|
140 |
} |
141 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
142 |
Color c = JColorChooser.showDialog( |
143 |
SwingUtilities.windowForComponent(this.btnShowDialog), |
144 |
i18n.getTranslation("choose_color"),
|
145 |
this.get()
|
146 |
); |
147 |
if( c == null ) { |
148 |
return;
|
149 |
} |
150 |
set(c); |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public void set(Color color) { |
155 |
if( color == null ) { |
156 |
if( allowNull ) {
|
157 |
this.color = null; |
158 |
return;
|
159 |
} |
160 |
color = Color.BLACK;
|
161 |
} |
162 |
ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
163 |
this.color = color;
|
164 |
String text = String.format("%02x %02x%02x%02x",color.getAlpha(),color.getRed(),color.getGreen(),color.getBlue()); |
165 |
this.txtLabel.setBackground(manager.opaqueColor(this.color)); |
166 |
this.txtLabel.setText(text);
|
167 |
if( this.sldAlpha != null ) { |
168 |
this.sldAlpha.setValue(this.color.getAlpha()); |
169 |
} |
170 |
this.txtLabel.invalidate();
|
171 |
} |
172 |
|
173 |
@Override
|
174 |
public Color get() { |
175 |
return this.color; |
176 |
} |
177 |
|
178 |
@Override
|
179 |
public void setEnabled(boolean enabled) { |
180 |
this.txtLabel.setEnabled(enabled);
|
181 |
this.btnShowDialog.setEnabled(enabled);
|
182 |
if( this.sldAlpha!=null ) { |
183 |
this.sldAlpha.setEnabled(enabled);
|
184 |
} |
185 |
} |
186 |
|
187 |
@Override
|
188 |
public boolean isEnabled() { |
189 |
return this.btnShowDialog.isEnabled(); |
190 |
} |
191 |
|
192 |
@Override
|
193 |
public void coerceAndSet(Object value) { |
194 |
try {
|
195 |
DataTypesManager.Coercion toColor = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.COLOR); |
196 |
this.set((Color) toColor.coerce(value)); |
197 |
} catch (CoercionException ex) {
|
198 |
throw new IllegalArgumentException("Can't converto to color", ex); |
199 |
} |
200 |
} |
201 |
|
202 |
|
203 |
} |