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 @ 2339
History | View | Annotate | Download (4.9 KB)
1 | 1610 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.tools.swing.impl; |
||
3 | |||
4 | import java.awt.Color; |
||
5 | 1632 | jjdelcerro | import java.awt.Font; |
6 | 1610 | jjdelcerro | import java.awt.event.ActionEvent; |
7 | import java.awt.event.ActionListener; |
||
8 | 1623 | fdiaz | |
9 | 1610 | jjdelcerro | 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 | 2165 | jjdelcerro | import javax.swing.text.JTextComponent; |
16 | 1623 | fdiaz | |
17 | 1610 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
18 | import org.gvsig.tools.i18n.I18nManager; |
||
19 | import org.gvsig.tools.swing.api.ColorChooserController; |
||
20 | 1632 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
21 | import org.gvsig.tools.swing.api.ToolsSwingManager; |
||
22 | 1610 | jjdelcerro | |
23 | public class DefaultColorChooserController implements ColorChooserController { |
||
24 | |||
25 | 2165 | jjdelcerro | private final JTextComponent txtLabel; |
26 | 1610 | jjdelcerro | private final JButton btnShowDialog; |
27 | private final JSlider sldAlpha; |
||
28 | private Color color; |
||
29 | private boolean allowNull; |
||
30 | private ChangeListener changeListener; |
||
31 | |||
32 | 2165 | jjdelcerro | public DefaultColorChooserController(JTextComponent txtLabel, JButton btnShowDialog, JSlider sldAlpha, boolean allowNull) { |
33 | 1610 | jjdelcerro | this.txtLabel = txtLabel;
|
34 | this.btnShowDialog = btnShowDialog;
|
||
35 | this.sldAlpha = sldAlpha;
|
||
36 | 1623 | fdiaz | |
37 | // Remove the listeners to prevent strange behavior when you call this
|
||
38 | 1615 | jjdelcerro | // class repeatedly with the same button and slider.
|
39 | 1614 | jjdelcerro | for(ActionListener l : btnShowDialog.getActionListeners()) { |
40 | btnShowDialog.removeActionListener(l); |
||
41 | 1623 | fdiaz | } |
42 | 1614 | jjdelcerro | for( ChangeListener l : sldAlpha.getChangeListeners()) { |
43 | sldAlpha.removeChangeListener(l); |
||
44 | 1623 | fdiaz | } |
45 | 2165 | jjdelcerro | this.btnShowDialog.addActionListener((ActionEvent e) -> { |
46 | doShowDialog(); |
||
47 | 1610 | jjdelcerro | }); |
48 | if( this.sldAlpha != null ) { |
||
49 | 1632 | jjdelcerro | this.sldAlpha.setMinimum(0); |
50 | this.sldAlpha.setMaximum(255); |
||
51 | 2165 | jjdelcerro | this.sldAlpha.addChangeListener((ChangeEvent e) -> { |
52 | doAlphaChanged(); |
||
53 | 1610 | jjdelcerro | }); |
54 | } |
||
55 | 1632 | jjdelcerro | 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 | 1610 | jjdelcerro | this.allowNull = allowNull;
|
64 | if( !this.allowNull ) { |
||
65 | this.color = Color.BLACK; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | 2165 | jjdelcerro | public DefaultColorChooserController(JTextComponent txtLabel, JButton btnShowDialog) { |
70 | 1610 | jjdelcerro | this(txtLabel, btnShowDialog, null, false); |
71 | } |
||
72 | |||
73 | 2165 | jjdelcerro | public DefaultColorChooserController(JTextComponent txtLabel, JButton btnShowDialog, JSlider sldAlpha) { |
74 | 1610 | jjdelcerro | this(txtLabel, btnShowDialog, sldAlpha, false); |
75 | } |
||
76 | |||
77 | private void doAlphaChanged() { |
||
78 | if( this.color == null ) { |
||
79 | set(Color.BLACK);
|
||
80 | } |
||
81 | 1614 | jjdelcerro | int alpha = 255; |
82 | 1610 | jjdelcerro | if( this.sldAlpha == null ) { |
83 | 1623 | fdiaz | set(new Color( |
84 | 1610 | jjdelcerro | color.getRed(), |
85 | color.getGreen(), |
||
86 | color.getBlue() |
||
87 | 1623 | fdiaz | )); |
88 | 1610 | jjdelcerro | } else {
|
89 | 1614 | jjdelcerro | alpha = this.sldAlpha.getValue();
|
90 | 1623 | fdiaz | set(new Color( |
91 | 1610 | jjdelcerro | color.getRed(), |
92 | color.getGreen(), |
||
93 | color.getBlue(), |
||
94 | 1614 | jjdelcerro | alpha |
95 | 1623 | fdiaz | )); |
96 | 1610 | jjdelcerro | } |
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 | 1632 | jjdelcerro | ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
133 | 1610 | jjdelcerro | this.color = color;
|
134 | 1623 | fdiaz | String text = String.format("%02x %02x%02x%02x",color.getAlpha(),color.getRed(),color.getGreen(),color.getBlue()); |
135 | 1632 | jjdelcerro | this.txtLabel.setBackground(manager.opaqueColor(this.color)); |
136 | 1623 | fdiaz | this.txtLabel.setText(text);
|
137 | 1610 | jjdelcerro | if( this.sldAlpha != null ) { |
138 | this.sldAlpha.setValue(this.color.getAlpha()); |
||
139 | } |
||
140 | 1623 | fdiaz | this.txtLabel.invalidate();
|
141 | 1610 | jjdelcerro | } |
142 | |||
143 | @Override
|
||
144 | public Color get() { |
||
145 | return this.color; |
||
146 | } |
||
147 | |||
148 | 1614 | jjdelcerro | @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 | 1623 | fdiaz | |
163 | 1610 | jjdelcerro | } |