Statistics
| Revision:

root / org.gvsig.legend.aggregate / trunk / org.gvsig.legend.aggregate / org.gvsig.legend.aggregate.swing / org.gvsig.legend.aggregate.swing.impl / src / main / java / org / gvsig / legend / aggregate / swing / impl / DefaultAggregateLegendEditor.java @ 1850

History | View | Annotate | Download (11.9 KB)

1

    
2
package org.gvsig.legend.aggregate.swing.impl;
3

    
4
import java.awt.Dimension;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
import java.awt.event.ItemEvent;
8
import java.awt.event.ItemListener;
9
import java.util.Collection;
10
import javax.swing.AbstractButton;
11
import javax.swing.DefaultComboBoxModel;
12
import javax.swing.ImageIcon;
13
import javax.swing.JLabel;
14
import javax.swing.JPanel;
15
import javax.swing.SwingUtilities;
16
import javax.swing.event.ChangeEvent;
17
import javax.swing.event.ChangeListener;
18
import org.gvsig.app.gui.styling.StyleSelector;
19
import org.gvsig.app.project.documents.view.legend.gui.Features;
20
import org.gvsig.fmap.dal.exception.DataException;
21
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
22
import org.gvsig.fmap.dal.feature.FeatureStore;
23
import org.gvsig.fmap.dal.feature.FeatureType;
24
import org.gvsig.fmap.geom.Geometry;
25
import org.gvsig.fmap.mapcontext.layers.FLayer;
26
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
27
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
28
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILabelStyle;
29
import org.gvsig.legend.aggregate.lib.api.AggregateLegend;
30
import org.gvsig.legend.aggregate.lib.api.AggregateLegendLocator;
31
import org.gvsig.legend.aggregate.lib.api.Operation;
32
import org.gvsig.legend.aggregate.swing.api.AggregateLegendEditor;
33
import org.gvsig.tools.ToolsLocator;
34
import org.gvsig.tools.i18n.I18nManager;
35
import org.gvsig.tools.swing.api.ColorChooserController;
36
import org.gvsig.tools.swing.api.ToolsSwingLocator;
37
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
38
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
39
import org.gvsig.tools.swing.icontheme.IconTheme;
40

    
41

    
42
public class DefaultAggregateLegendEditor extends DefaultAggregateLegendEditorView implements AggregateLegendEditor {
43

    
44
    private static final long serialVersionUID = 4307714097793669675L;
45

    
46
    private final Class<? extends AggregateLegend> legendClass;
47
    private AggregateLegend legend;
48
    private FLayer layer;
49
    private ILabelStyle style;
50
    private ColorChooserController outlineColor;
51
    private ColorChooserController fillColor;
52
    private ColorChooserController textColor;
53

    
54
    public DefaultAggregateLegendEditor() {
55
        this.legendClass = AggregateLegendLocator.getAggregateLegendManager().getLegendClass();
56
        this.initComponents();
57
    }
58
    
59
    private void initComponents() {
60
        this.btnStyle.addActionListener(new ActionListener() {
61
            @Override
62
            public void actionPerformed(ActionEvent e) {
63
                doSelectLabelStyle();
64
            }
65
        });
66
        this.outlineColor = ToolsSwingLocator.getToolsSwingManager().createColorChooserController(
67
            txtOutlineColor, btnOutlineColor, sldOutlineColorAlpha
68
        );
69
        this.fillColor = ToolsSwingLocator.getToolsSwingManager().createColorChooserController(
70
            txtFillColor, btnFillColor, sldFillColorAlpha
71
        );
72
        this.textColor = ToolsSwingLocator.getToolsSwingManager().createColorChooserController(
73
            txtTextColor, btnTextColor, sldTextColorAlpha
74
        );
75
        Collection<Operation> operations = AggregateLegendLocator.getAggregateLegendManager().getOperations();
76
        DefaultComboBoxModel<Operation> operationsModel = new DefaultComboBoxModel<>();
77
        for( Operation operation : operations ) {
78
            operationsModel.addElement(operation);
79
        }        
80
        this.cboOperation.setModel(operationsModel);
81
        this.rdbBasic.addChangeListener(new ChangeListener() {
82
            @Override
83
            public void stateChanged(ChangeEvent e) {
84
                doChangeMode();
85
            }
86
        });
87
        this.rdbStyled.addChangeListener(new ChangeListener() {
88
            @Override
89
            public void stateChanged(ChangeEvent e) {
90
                doChangeMode();
91
            }
92
        });
93
        this.btnTextFont.addActionListener(new ActionListener() {
94
            @Override
95
            public void actionPerformed(ActionEvent e) {
96
                showFontChooser();
97
            }
98
        });
99
        this.cboOperation.addItemListener(new ItemListener() {
100
            @Override
101
            public void itemStateChanged(ItemEvent e) {
102
                // Para evitar cuelgues en depuracion
103
                SwingUtilities.invokeLater(new Runnable() {
104
                    @Override
105
                    public void run() {
106
                        doOperationChanged();
107
                    }
108
                });
109
            }
110
        });
111
        this.translateAll();
112
    }
113
    
114
    private void translate(AbstractButton component) {
115
        I18nManager i18n = ToolsLocator.getI18nManager();
116
        component.setText(i18n.getTranslation(component.getText()));
117
    }
118
    
119
    private void translate(JLabel component) {
120
        I18nManager i18n = ToolsLocator.getI18nManager();
121
        component.setText(i18n.getTranslation(component.getText()));
122
    }
123
    
124
    private void translateAll() {
125
        this.translate(this.rdbBasic);
126
        this.translate(this.lblSymbolSize);        
127
        this.translate(this.lblFillColor);
128
        this.translate(this.lblFillColorAlpha);
129
        this.translate(this.lblOutlineColor);
130
        this.translate(this.lblOutlineColorAlpha);
131
        this.translate(this.lblTextColor);
132
        this.translate(this.lblTextColorAlpha);
133
        this.translate(this.lblTextFont);
134
        this.translate(this.lblShowBounds);
135
        this.translate(this.rdbStyled);
136
        this.translate(this.lblStyle);
137
        this.translate(this.lblOperation);
138
        this.translate(this.lblOperationAttribute);
139
        this.translate(this.lblOperationValue);
140
    }
141

    
142
    @Override
143
    public void setData(FLayer layer, ILegend legend) {
144
        this.layer = layer;
145
        if( this.getLegendClass().isInstance(legend) ) {
146
            this.legend = (AggregateLegend) legend;
147
        } else {
148
            this.legend = AggregateLegendLocator.getAggregateLegendManager().createAggregateLegend();
149
        }
150
        this.spnSymbolSize.setValue(this.legend.getSymbolSize());
151
        this.outlineColor.set(this.legend.getOutlineColor());
152
        this.fillColor.set(this.legend.getFillColor());
153
        this.textColor.set(this.legend.getTextColor());
154
        this.chkShowBounds.setSelected(this.legend.isShowBounds());
155
        this.txtTextFont.setText(this.legend.getFont().getFontName());
156
        if( this.legend.getLabelStyle()!=null ) {
157
            this.txtStyle.setText(this.legend.getLabelStyle().getDescription());
158
        }
159
        Operation operation = this.legend.getOperation();
160
        this.txtOperationValue.setText(operation.getAditionalValue()==null? "":operation.getAditionalValue());
161
        if( this.layer instanceof FLyrVect ) {
162
            FeatureStore store = ((FLyrVect)this.layer).getFeatureStore();
163
            try {
164
                FeatureType type = store.getDefaultFeatureType();
165
                DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
166
                for( FeatureAttributeDescriptor attr : type ) {
167
                    model.addElement(attr.getName());
168
                }
169
                this.cboOperationAttribute.setModel(model);
170
            } catch (DataException ex) {
171
                this.cboOperationAttribute.removeAllItems();
172
            }
173
            this.cboOperationAttribute.setSelectedIndex(0);
174
            this.cboOperationAttribute.setSelectedItem(operation.getAttributeName());
175
        }
176
        for( int i=0; i<this.cboOperation.getItemCount(); i++ ) {
177
            Operation x = (Operation) this.cboOperation.getItemAt(i);
178
            if( x.getName().equalsIgnoreCase(this.legend.getOperation().getName()) ) {
179
                this.cboOperation.setSelectedIndex(i);
180
                doOperationChanged();
181
                break;
182
            }
183
        }
184
        this.rdbBasic.setSelected(!this.legend.isUseStyle());
185
        this.rdbStyled.setSelected(this.legend.isUseStyle());
186
    }
187

    
188
    @Override
189
    public ILegend getLegend() {
190

    
191
        legend.setUseStyle(this.rdbStyled.isSelected());
192
        legend.setSymbolSize((int) this.spnSymbolSize.getValue());
193
        legend.setOutlineColor(this.outlineColor.get());
194
        legend.setFillColor(this.fillColor.get());
195
        legend.setTextColor(this.textColor.get());
196
        legend.setShowBounds(this.chkShowBounds.isSelected());
197
        legend.setLabelStyle(this.style);
198
        Operation operation = (Operation) this.cboOperation.getSelectedItem();
199
        if( operation!=null ) {
200
            operation = operation.clone();
201
            if( operation.isAditionalValueRequiered() ) {
202
                operation.setAditionalValue(this.txtOperationValue.getText());
203
            }
204
            if( operation.isAttributeRequiered() ) {
205
                operation.setAttributeName((String) this.cboOperationAttribute.getSelectedItem());
206
            }
207
            this.legend.setOperation(operation);
208
        }
209
        return legend;
210
    }
211

    
212
    private void doChangeMode() {
213
        if( this.rdbBasic.isSelected() ) {
214
            this.spnSymbolSize.setEnabled(true);
215
            this.fillColor.setEnabled(true);
216
            this.outlineColor.setEnabled(true);
217
            this.textColor.setEnabled(true);
218
            this.txtTextFont.setEnabled(true);
219
            this.chkShowBounds.setEnabled(true);
220
            
221
            this.btnStyle.setEnabled(false);
222
        } else {
223
            this.spnSymbolSize.setEnabled(false);
224
            this.fillColor.setEnabled(false);
225
            this.outlineColor.setEnabled(false);
226
            this.textColor.setEnabled(false);
227
            this.txtTextFont.setEnabled(false);
228
            this.chkShowBounds.setEnabled(false);
229
            
230
            this.btnStyle.setEnabled(true);
231
        }
232
    }
233
    
234
    private void doOperationChanged() {
235
        Operation operation = (Operation) this.cboOperation.getSelectedItem();
236
        if( operation==null ) {
237
            this.cboOperationAttribute.setEnabled(false);
238
            this.txtOperationValue.setEnabled(false);
239
            this.lblOperationDescription.setText("");
240
            return;
241
        }
242
        this.txtOperationValue.setEnabled(operation.isAditionalValueRequiered());
243
        this.cboOperationAttribute.setEnabled(operation.isAttributeRequiered());
244
        if( operation.isAttributeRequiered() && this.cboOperationAttribute.getSelectedIndex()<0 ) {
245
            this.cboOperationAttribute.setSelectedIndex(0);
246
        }
247
        //this.lblOperationDescription.setText(operation.getDescription());
248
        this.cboOperation.setToolTipText(operation.getDescription());
249
    }
250
    
251
    private void showFontChooser() {
252
        
253
    }
254
    
255
    @Override
256
    public String getDescription() {
257
        I18nManager i18n = ToolsLocator.getI18nManager();
258
        return i18n.getTranslation( "_Aggregate_legend_description");
259
    }
260

    
261
    @Override
262
    public ImageIcon getIcon() {
263
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
264
        return theme.get("legend-overview-vectorial-aggregated");
265
    }
266

    
267
    @Override
268
        public Class getParentClass() {
269
                return Features.class;
270
        }
271

    
272
    @Override
273
    public String getTitle() {
274
        I18nManager i18n = ToolsLocator.getI18nManager();
275
        return i18n.getTranslation( "_Aggregate_legend");
276
    }
277

    
278
    @Override
279
    public JPanel getPanel() {
280
        return this;
281
    }
282

    
283
    @Override
284
    public Class getLegendClass() {
285
        return this.legendClass;
286
    }
287

    
288
    @Override
289
    public boolean isSuitableFor(FLayer layer) {
290
        return true;
291
    }
292

    
293
    private void doSelectLabelStyle() {
294
        I18nManager i18n = ToolsLocator.getI18nManager();
295
        WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
296
        final StyleSelector styleSelector = new StyleSelector(this.style, Geometry.TYPES.POINT);
297
        styleSelector.setPreferredSize(new Dimension(600, 350));
298
        winmanager.showWindow(
299
            styleSelector,
300
            i18n.getTranslation("_Select_style"),
301
            WindowManager.MODE.DIALOG
302
        );
303
        ILabelStyle x = (ILabelStyle) styleSelector.getSelectedObject();
304
        if( x != null ) {
305
            this.style = x;
306
        }
307
    }
308
}