root / org.gvsig.legend.heatmap / trunk / org.gvsig.legend.heatmap / org.gvsig.legend.heatmap.swing / org.gvsig.legend.heatmap.swing.impl / src / main / java / org / gvsig / legend / heatmap / swing / impl / DefaultHeatmapLegendEditor.java @ 1853
History | View | Annotate | Download (9.98 KB)
1 |
|
---|---|
2 |
package org.gvsig.legend.heatmap.swing.impl; |
3 |
|
4 |
import java.awt.Color; |
5 |
import javax.swing.AbstractButton; |
6 |
import javax.swing.ImageIcon; |
7 |
import javax.swing.JFormattedTextField; |
8 |
import javax.swing.JLabel; |
9 |
import javax.swing.JPanel; |
10 |
import javax.swing.event.ChangeEvent; |
11 |
import javax.swing.event.ChangeListener; |
12 |
import org.apache.commons.lang3.StringUtils; |
13 |
import org.gvsig.app.gui.styling.ColorSchemeSelector; |
14 |
import org.gvsig.app.project.documents.view.legend.gui.Features; |
15 |
import org.gvsig.fmap.dal.exception.DataException; |
16 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
17 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
18 |
import org.gvsig.fmap.dal.feature.FeatureType; |
19 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
20 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
21 |
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
22 |
import org.gvsig.legend.heatmap.lib.api.HeatmapLegend; |
23 |
import org.gvsig.legend.heatmap.lib.api.HeatmapLegendLocator; |
24 |
import org.gvsig.legend.heatmap.swing.api.HeatmapLegendEditor; |
25 |
import org.gvsig.tools.ToolsLocator; |
26 |
import org.gvsig.tools.i18n.I18nManager; |
27 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
28 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
29 |
import org.gvsig.tools.swing.api.ColorChooserController; |
30 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
31 |
|
32 |
|
33 |
public class DefaultHeatmapLegendEditor extends DefaultHeatmapLegendEditorView implements HeatmapLegendEditor { |
34 |
|
35 |
private static final long serialVersionUID = 4307714097793669675L; |
36 |
|
37 |
private HeatmapLegend legend;
|
38 |
private FLayer layer;
|
39 |
private final Class<? extends HeatmapLegend> legendClass; |
40 |
private ColorChooserController hotColorChooser;
|
41 |
private ColorChooserController coldColorChooser;
|
42 |
private ColorSchemeSelector colorSchemaSelector;
|
43 |
|
44 |
public DefaultHeatmapLegendEditor() {
|
45 |
this.legendClass = HeatmapLegendLocator.getHeatmapLegendManager().getLegendClass();
|
46 |
initComponents(); |
47 |
} |
48 |
|
49 |
private void initComponents(){ |
50 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
51 |
this.hotColorChooser = toolsSwingManager.createColorChooserController(txtHotColorLabel, btnHotColor, sldHotColorAlpha);
|
52 |
this.coldColorChooser = toolsSwingManager.createColorChooserController(txtColdColorLabel, btnColdColor, sldColdColorAlpha);
|
53 |
this.colorSchemaSelector = new ColorSchemeSelector(cboColorTable, false); |
54 |
this.rdbUseColorRamp.addChangeListener(new ChangeListener() { |
55 |
@Override
|
56 |
public void stateChanged(ChangeEvent e) { |
57 |
useColorModeChanged(); |
58 |
} |
59 |
}); |
60 |
this.chkUseField.addChangeListener(new ChangeListener() { |
61 |
@Override
|
62 |
public void stateChanged(ChangeEvent e) { |
63 |
useFieldChanged(); |
64 |
} |
65 |
}); |
66 |
this.chkColorTableUseAlpha.addChangeListener(new ChangeListener() { |
67 |
@Override
|
68 |
public void stateChanged(ChangeEvent e) { |
69 |
useAlphaInColorTableChanged(); |
70 |
} |
71 |
}); |
72 |
translateAll(); |
73 |
} |
74 |
|
75 |
private void translateAll() { |
76 |
this.translate(this.lblDistance); |
77 |
this.translate(this.lblPixels); |
78 |
this.translate(this.rdbUseColorRamp); |
79 |
this.translate(this.lblNumberOfColors); |
80 |
this.translate(this.lblHotColor); |
81 |
this.translate(this.lblHotColorAlpha); |
82 |
this.translate(this.lblColdColor); |
83 |
this.translate(this.lblColdColorAlpha); |
84 |
this.translate(this.rdbUseColorTable); |
85 |
this.translate(this.lblColorTable); |
86 |
this.translate(this.chkColorTableUseAlpha); |
87 |
this.translate(this.lblHotColorTransparency); |
88 |
this.translate(this.lblColdColorTransparency); |
89 |
this.translate(this.chkUseField); |
90 |
} |
91 |
|
92 |
private void translate(AbstractButton component) { |
93 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
94 |
component.setText(i18n.getTranslation(component.getText())); |
95 |
} |
96 |
|
97 |
private void translate(JLabel component) { |
98 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
99 |
component.setText(i18n.getTranslation(component.getText())); |
100 |
} |
101 |
|
102 |
@Override
|
103 |
public void setData(FLayer layer, ILegend legend) { |
104 |
if( layer == this.layer && legend == this.legend ) { |
105 |
return;
|
106 |
} |
107 |
this.layer = layer;
|
108 |
if( this.getLegendClass().isInstance(legend) ) { |
109 |
this.legend = (HeatmapLegend) legend;
|
110 |
} else {
|
111 |
this.legend = HeatmapLegendLocator.getHeatmapLegendManager().create();
|
112 |
} |
113 |
this.spnDistance.setValue(this.legend.getDistance()); |
114 |
if( this.legend.useRamp() ) { |
115 |
this.rdbUseColorRamp.setSelected(true); |
116 |
} else {
|
117 |
this.rdbUseColorTable.setSelected(true); |
118 |
} |
119 |
if( this.legend.useAlphaInColorTable() ) { |
120 |
this.chkColorTableUseAlpha.setSelected(true); |
121 |
this.coldColorChooser.set(this.legend.getRampColdColor()); |
122 |
this.hotColorChooser.set(this.legend.getRampHotColor()); |
123 |
this.spnNumberOfColors.setValue(this.legend.getRampNumColors()); |
124 |
|
125 |
} else {
|
126 |
this.chkColorTableUseAlpha.setSelected(false); |
127 |
} |
128 |
this.sldColorTableColdAlpha.setValue(this.legend.getColorTableColdColorAlpha()); |
129 |
this.sldColorTableHotAlpha.setValue(this.legend.getColorTableHotColorAlpha()); |
130 |
|
131 |
|
132 |
this.chkUseField.setEnabled(false); |
133 |
this.chkUseField.setSelected(false); |
134 |
this.cboFields.setEnabled(false); |
135 |
this.cboFields.removeAllItems();
|
136 |
if( layer instanceof FLyrVect ) { |
137 |
FeatureStore store = ((FLyrVect) layer).getFeatureStore(); |
138 |
if( store != null ) { |
139 |
try {
|
140 |
FeatureType ftype = store.getDefaultFeatureType(); |
141 |
for( FeatureAttributeDescriptor attr : ftype ) {
|
142 |
this.cboFields.addItem(attr.getName());
|
143 |
} |
144 |
if( this.cboFields.getItemCount()>0 ) { |
145 |
this.chkUseField.setEnabled(true); |
146 |
if( !StringUtils.isEmpty(this.legend.getFieldName()) ) { |
147 |
this.chkUseField.setSelected(true); |
148 |
this.cboFields.setSelectedItem(this.legend.getFieldName()); |
149 |
this.cboFields.setEnabled(true); |
150 |
} |
151 |
} |
152 |
} catch (Exception ex) { |
153 |
|
154 |
} |
155 |
} |
156 |
} |
157 |
} |
158 |
|
159 |
private void useColorModeChanged() { |
160 |
if( this.rdbUseColorRamp.isSelected() ) { |
161 |
this.hotColorChooser.setEnabled(true); |
162 |
this.coldColorChooser.setEnabled(true); |
163 |
this.spnNumberOfColors.setEnabled(true); |
164 |
this.cboColorTable.setEnabled(false); |
165 |
this.chkColorTableUseAlpha.setEnabled(false); |
166 |
this.sldColorTableColdAlpha.setEnabled(false); |
167 |
this.sldColorTableHotAlpha.setEnabled(false); |
168 |
this.spnNumberOfColors.setValue(this.legend.getRampNumColors()); |
169 |
this.coldColorChooser.set(this.legend.getRampColdColor()); |
170 |
this.hotColorChooser.set(this.legend.getRampHotColor()); |
171 |
} else {
|
172 |
this.hotColorChooser.setEnabled(false); |
173 |
this.coldColorChooser.setEnabled(false); |
174 |
this.spnNumberOfColors.setEnabled(false); |
175 |
this.cboColorTable.setEnabled(true); |
176 |
this.chkColorTableUseAlpha.setEnabled(true); |
177 |
this.sldColorTableColdAlpha.setEnabled(true); |
178 |
this.sldColorTableHotAlpha.setEnabled(true); |
179 |
this.colorSchemaSelector.setSelectedColors(this.legend.getSourceColorTable()); |
180 |
} |
181 |
} |
182 |
|
183 |
private void useFieldChanged() { |
184 |
this.cboFields.setEnabled(this.chkUseField.isSelected()); |
185 |
} |
186 |
|
187 |
private void useAlphaInColorTableChanged() { |
188 |
boolean x = this.chkColorTableUseAlpha.isSelected(); |
189 |
this.sldColorTableColdAlpha.setEnabled(x);
|
190 |
this.sldColorTableHotAlpha.setEnabled(x);
|
191 |
} |
192 |
|
193 |
@Override
|
194 |
public ILegend getLegend() {
|
195 |
legend.setDistance((int) spnDistance.getValue());
|
196 |
if( this.rdbUseColorRamp.isSelected() ) { |
197 |
legend.setColorTable( |
198 |
(int) spnNumberOfColors.getValue(),
|
199 |
coldColorChooser.get(), |
200 |
hotColorChooser.get() |
201 |
); |
202 |
} else {
|
203 |
legend.setColorTable( this.colorSchemaSelector.getSelectedColors());
|
204 |
} |
205 |
if( this.chkUseField.isSelected() ) { |
206 |
String fieldName = (String) this.cboFields.getSelectedItem(); |
207 |
if( fieldName != null ) { |
208 |
this.legend.setFieldName(fieldName);
|
209 |
} |
210 |
} |
211 |
this.legend.setColorTableColdColorAlpha(this.sldColorTableColdAlpha.getValue()); |
212 |
this.legend.setColorTableHotColorAlpha(this.sldColorTableHotAlpha.getValue()); |
213 |
this.legend.setUseAlphaInColorTable(this.chkColorTableUseAlpha.isSelected()); |
214 |
|
215 |
return legend;
|
216 |
} |
217 |
|
218 |
@Override
|
219 |
public String getDescription() { |
220 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
221 |
return i18n.getTranslation( "_Heatmap_legend_description"); |
222 |
} |
223 |
|
224 |
@Override
|
225 |
public ImageIcon getIcon() { |
226 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
227 |
return theme.get("legend-overview-vectorial-heatmap"); |
228 |
} |
229 |
|
230 |
@Override
|
231 |
public Class getParentClass() { |
232 |
return Features.class;
|
233 |
} |
234 |
|
235 |
@Override
|
236 |
public String getTitle() { |
237 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
238 |
return i18n.getTranslation( "_Heatmap_legend"); |
239 |
} |
240 |
|
241 |
@Override
|
242 |
public JPanel getPanel() { |
243 |
return this; |
244 |
} |
245 |
|
246 |
@Override
|
247 |
public Class getLegendClass() { |
248 |
return this.legendClass; |
249 |
} |
250 |
|
251 |
@Override
|
252 |
public boolean isSuitableFor(FLayer layer) { |
253 |
return true; |
254 |
} |
255 |
|
256 |
private int getIntValue(JFormattedTextField text, int defaultValue) { |
257 |
String s = text.getText();
|
258 |
if( StringUtils.isEmpty(s) ) {
|
259 |
return defaultValue;
|
260 |
} |
261 |
int value = defaultValue;
|
262 |
try {
|
263 |
value = Integer.parseInt(s);
|
264 |
} catch(Throwable th) { |
265 |
|
266 |
} |
267 |
return value;
|
268 |
} |
269 |
|
270 |
} |