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