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 @ 2509
History | View | Annotate | Download (6.43 KB)
1 | 1746 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.tools.swing.impl.pickercontroller; |
||
3 | |||
4 | import java.awt.Color; |
||
5 | 2434 | jjdelcerro | import java.awt.Cursor; |
6 | 1746 | jjdelcerro | import java.awt.Font; |
7 | import java.awt.event.ActionEvent; |
||
8 | import java.awt.event.ActionListener; |
||
9 | import java.net.URL; |
||
10 | import javax.swing.ImageIcon; |
||
11 | |||
12 | import javax.swing.JButton; |
||
13 | import javax.swing.JColorChooser; |
||
14 | import javax.swing.JSlider; |
||
15 | import javax.swing.SwingUtilities; |
||
16 | import javax.swing.event.ChangeEvent; |
||
17 | import javax.swing.event.ChangeListener; |
||
18 | 2165 | jjdelcerro | import javax.swing.text.JTextComponent; |
19 | 2509 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
20 | 1746 | jjdelcerro | |
21 | import org.gvsig.tools.ToolsLocator; |
||
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 | 2165 | jjdelcerro | private final JTextComponent txtLabel; |
37 | 1746 | jjdelcerro | 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 | 2165 | jjdelcerro | public ColorPickerControllerImpl(JTextComponent txtLabel, JButton btnShowDialog, JSlider sldAlpha, boolean allowNull) { |
54 | 2509 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
55 | 1746 | jjdelcerro | this.txtLabel = txtLabel;
|
56 | this.btnShowDialog = btnShowDialog;
|
||
57 | this.sldAlpha = sldAlpha;
|
||
58 | |||
59 | this.txtLabel.setEditable(false); |
||
60 | |||
61 | 2434 | jjdelcerro | this.btnShowDialog.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
62 | 1746 | jjdelcerro | this.btnShowDialog.setIcon(this.getIcon("picker-color")); |
63 | 2328 | jjdelcerro | if( this.btnShowDialog.getText().equals("...") ) { |
64 | this.btnShowDialog.setText(""); |
||
65 | } |
||
66 | 2509 | jjdelcerro | if( StringUtils.isBlank(this.btnShowDialog.getToolTipText()) ) { |
67 | this.btnShowDialog.setToolTipText(i18n.getTranslation("_Color_picker")); |
||
68 | } |
||
69 | 1746 | jjdelcerro | |
70 | // Remove the listeners to prevent strange behavior when you call this
|
||
71 | // class repeatedly with the same button and slider.
|
||
72 | for(ActionListener l : btnShowDialog.getActionListeners()) { |
||
73 | btnShowDialog.removeActionListener(l); |
||
74 | } |
||
75 | 1879 | omartinez | if( this.sldAlpha != null ) { |
76 | for( ChangeListener l : sldAlpha.getChangeListeners()) { |
||
77 | sldAlpha.removeChangeListener(l); |
||
78 | } |
||
79 | 1746 | jjdelcerro | } |
80 | 2165 | jjdelcerro | this.btnShowDialog.addActionListener((ActionEvent e) -> { |
81 | doShowDialog(); |
||
82 | 1746 | jjdelcerro | }); |
83 | if( this.sldAlpha != null ) { |
||
84 | this.sldAlpha.setMinimum(0); |
||
85 | this.sldAlpha.setMaximum(255); |
||
86 | 2165 | jjdelcerro | this.sldAlpha.addChangeListener((ChangeEvent e) -> { |
87 | doAlphaChanged(); |
||
88 | 1746 | jjdelcerro | }); |
89 | } |
||
90 | Font font = this.txtLabel.getFont(); |
||
91 | try {
|
||
92 | font = new Font(Font.MONOSPACED, Font.PLAIN, font.getSize()); |
||
93 | this.txtLabel.setFont(font);
|
||
94 | } catch(Exception ex) { |
||
95 | // If dont set a monospaced font ignore it.
|
||
96 | font = null;
|
||
97 | } |
||
98 | this.allowNull = allowNull;
|
||
99 | if( !this.allowNull ) { |
||
100 | this.color = Color.BLACK; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | 2165 | jjdelcerro | public ColorPickerControllerImpl(JTextComponent txtLabel, JButton btnShowDialog) { |
105 | 1746 | jjdelcerro | this(txtLabel, btnShowDialog, null, false); |
106 | } |
||
107 | |||
108 | 2165 | jjdelcerro | public ColorPickerControllerImpl(JTextComponent txtLabel, JButton btnShowDialog, JSlider sldAlpha) { |
109 | 1746 | jjdelcerro | this(txtLabel, btnShowDialog, sldAlpha, false); |
110 | } |
||
111 | |||
112 | @Override
|
||
113 | public boolean isEmpty() { |
||
114 | return this.color==null; |
||
115 | } |
||
116 | |||
117 | private void doAlphaChanged() { |
||
118 | if( !this.isEditable() ) { |
||
119 | return;
|
||
120 | } |
||
121 | if( this.color == null ) { |
||
122 | set(Color.BLACK);
|
||
123 | } |
||
124 | if( this.sldAlpha == null ) { |
||
125 | set(new Color( |
||
126 | color.getRed(), |
||
127 | color.getGreen(), |
||
128 | color.getBlue() |
||
129 | )); |
||
130 | } else {
|
||
131 | int alpha = this.sldAlpha.getValue(); |
||
132 | set(new Color( |
||
133 | color.getRed(), |
||
134 | color.getGreen(), |
||
135 | color.getBlue(), |
||
136 | alpha |
||
137 | )); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | protected void doShowDialog() { |
||
142 | if( !this.isEditable() ) { |
||
143 | return;
|
||
144 | } |
||
145 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
146 | Color c = JColorChooser.showDialog( |
||
147 | SwingUtilities.windowForComponent(this.btnShowDialog), |
||
148 | i18n.getTranslation("choose_color"),
|
||
149 | this.get()
|
||
150 | ); |
||
151 | if( c == null ) { |
||
152 | return;
|
||
153 | } |
||
154 | set(c); |
||
155 | } |
||
156 | |||
157 | @Override
|
||
158 | public void set(Color color) { |
||
159 | if( color == null ) { |
||
160 | if( allowNull ) {
|
||
161 | this.color = null; |
||
162 | return;
|
||
163 | } |
||
164 | color = Color.BLACK;
|
||
165 | } |
||
166 | ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
||
167 | this.color = color;
|
||
168 | String text = String.format("%02x %02x%02x%02x",color.getAlpha(),color.getRed(),color.getGreen(),color.getBlue()); |
||
169 | this.txtLabel.setBackground(manager.opaqueColor(this.color)); |
||
170 | this.txtLabel.setText(text);
|
||
171 | if( this.sldAlpha != null ) { |
||
172 | this.sldAlpha.setValue(this.color.getAlpha()); |
||
173 | } |
||
174 | this.txtLabel.invalidate();
|
||
175 | } |
||
176 | |||
177 | @Override
|
||
178 | public Color get() { |
||
179 | return this.color; |
||
180 | } |
||
181 | |||
182 | @Override
|
||
183 | public void setEnabled(boolean enabled) { |
||
184 | this.txtLabel.setEnabled(enabled);
|
||
185 | this.btnShowDialog.setEnabled(enabled);
|
||
186 | if( this.sldAlpha!=null ) { |
||
187 | this.sldAlpha.setEnabled(enabled);
|
||
188 | } |
||
189 | } |
||
190 | |||
191 | @Override
|
||
192 | public boolean isEnabled() { |
||
193 | return this.btnShowDialog.isEnabled(); |
||
194 | } |
||
195 | |||
196 | @Override
|
||
197 | public void coerceAndSet(Object value) { |
||
198 | 2080 | jjdelcerro | this.set((Color) this.coerce(DataTypes.COLOR, value, null)); |
199 | 1746 | jjdelcerro | } |
200 | |||
201 | |||
202 | } |