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 / DefaultColorChooserController.java @ 2165
History | View | Annotate | Download (4.9 KB)
1 |
|
---|---|
2 |
package org.gvsig.tools.swing.impl; |
3 |
|
4 |
import java.awt.Color; |
5 |
import java.awt.Font; |
6 |
import java.awt.event.ActionEvent; |
7 |
import java.awt.event.ActionListener; |
8 |
|
9 |
import javax.swing.JButton; |
10 |
import javax.swing.JColorChooser; |
11 |
import javax.swing.JSlider; |
12 |
import javax.swing.SwingUtilities; |
13 |
import javax.swing.event.ChangeEvent; |
14 |
import javax.swing.event.ChangeListener; |
15 |
import javax.swing.text.JTextComponent; |
16 |
|
17 |
import org.gvsig.tools.ToolsLocator; |
18 |
import org.gvsig.tools.i18n.I18nManager; |
19 |
import org.gvsig.tools.swing.api.ColorChooserController; |
20 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
21 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
22 |
|
23 |
public class DefaultColorChooserController implements ColorChooserController { |
24 |
|
25 |
private final JTextComponent txtLabel; |
26 |
private final JButton btnShowDialog; |
27 |
private final JSlider sldAlpha; |
28 |
private Color color; |
29 |
private boolean allowNull; |
30 |
private ChangeListener changeListener; |
31 |
|
32 |
public DefaultColorChooserController(JTextComponent txtLabel, JButton btnShowDialog, JSlider sldAlpha, boolean allowNull) { |
33 |
this.txtLabel = txtLabel;
|
34 |
this.btnShowDialog = btnShowDialog;
|
35 |
this.sldAlpha = sldAlpha;
|
36 |
|
37 |
// Remove the listeners to prevent strange behavior when you call this
|
38 |
// class repeatedly with the same button and slider.
|
39 |
for(ActionListener l : btnShowDialog.getActionListeners()) { |
40 |
btnShowDialog.removeActionListener(l); |
41 |
} |
42 |
for( ChangeListener l : sldAlpha.getChangeListeners()) { |
43 |
sldAlpha.removeChangeListener(l); |
44 |
} |
45 |
this.btnShowDialog.addActionListener((ActionEvent e) -> { |
46 |
doShowDialog(); |
47 |
}); |
48 |
if( this.sldAlpha != null ) { |
49 |
this.sldAlpha.setMinimum(0); |
50 |
this.sldAlpha.setMaximum(255); |
51 |
this.sldAlpha.addChangeListener((ChangeEvent e) -> { |
52 |
doAlphaChanged(); |
53 |
}); |
54 |
} |
55 |
Font font = this.txtLabel.getFont(); |
56 |
try {
|
57 |
font = new Font(Font.MONOSPACED, Font.PLAIN, font.getSize()); |
58 |
this.txtLabel.setFont(font);
|
59 |
} catch(Exception ex) { |
60 |
// If dont set a monospaced font ignore it.
|
61 |
font = null;
|
62 |
} |
63 |
this.allowNull = allowNull;
|
64 |
if( !this.allowNull ) { |
65 |
this.color = Color.BLACK; |
66 |
} |
67 |
} |
68 |
|
69 |
public DefaultColorChooserController(JTextComponent txtLabel, JButton btnShowDialog) { |
70 |
this(txtLabel, btnShowDialog, null, false); |
71 |
} |
72 |
|
73 |
public DefaultColorChooserController(JTextComponent txtLabel, JButton btnShowDialog, JSlider sldAlpha) { |
74 |
this(txtLabel, btnShowDialog, sldAlpha, false); |
75 |
} |
76 |
|
77 |
private void doAlphaChanged() { |
78 |
if( this.color == null ) { |
79 |
set(Color.BLACK);
|
80 |
} |
81 |
int alpha = 255; |
82 |
if( this.sldAlpha == null ) { |
83 |
set(new Color( |
84 |
color.getRed(), |
85 |
color.getGreen(), |
86 |
color.getBlue() |
87 |
)); |
88 |
} else {
|
89 |
alpha = this.sldAlpha.getValue();
|
90 |
set(new Color( |
91 |
color.getRed(), |
92 |
color.getGreen(), |
93 |
color.getBlue(), |
94 |
alpha |
95 |
)); |
96 |
} |
97 |
} |
98 |
|
99 |
@Override
|
100 |
public void addChangeListener(ChangeListener changeListener) { |
101 |
this.changeListener = changeListener;
|
102 |
} |
103 |
|
104 |
protected void fireChanged() { |
105 |
if( this.changeListener != null ) { |
106 |
this.changeListener.stateChanged(null); |
107 |
} |
108 |
} |
109 |
|
110 |
protected void doShowDialog() { |
111 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
112 |
Color c = JColorChooser.showDialog( |
113 |
SwingUtilities.windowForComponent(this.btnShowDialog), |
114 |
i18n.getTranslation("choose_color"),
|
115 |
this.get()
|
116 |
); |
117 |
if( c == null ) { |
118 |
return;
|
119 |
} |
120 |
set(c); |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public void set(Color color) { |
125 |
if( color == null ) { |
126 |
if( allowNull ) {
|
127 |
this.color = null; |
128 |
return;
|
129 |
} |
130 |
color = Color.BLACK;
|
131 |
} |
132 |
ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
133 |
this.color = color;
|
134 |
String text = String.format("%02x %02x%02x%02x",color.getAlpha(),color.getRed(),color.getGreen(),color.getBlue()); |
135 |
this.txtLabel.setBackground(manager.opaqueColor(this.color)); |
136 |
this.txtLabel.setText(text);
|
137 |
if( this.sldAlpha != null ) { |
138 |
this.sldAlpha.setValue(this.color.getAlpha()); |
139 |
} |
140 |
this.txtLabel.invalidate();
|
141 |
} |
142 |
|
143 |
@Override
|
144 |
public Color get() { |
145 |
return this.color; |
146 |
} |
147 |
|
148 |
@Override
|
149 |
public void setEnabled(boolean enabled) { |
150 |
this.txtLabel.setEditable(enabled);
|
151 |
this.btnShowDialog.setEnabled(enabled);
|
152 |
if( this.sldAlpha!=null ) { |
153 |
this.sldAlpha.setEnabled(enabled);
|
154 |
} |
155 |
} |
156 |
|
157 |
@Override
|
158 |
public boolean isEnabled() { |
159 |
return this.btnShowDialog.isEnabled(); |
160 |
} |
161 |
|
162 |
|
163 |
} |