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 @ 1942

History | View | Annotate | Download (13.2 KB)

1

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

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

    
44

    
45
public class DefaultAggregateLegendEditor extends DefaultAggregateLegendEditorView implements AggregateLegendEditor {
46

    
47
    private static final long serialVersionUID = 4307714097793669675L;
48

    
49
    private final Class<? extends AggregateLegend> legendClass;
50
    private AggregateLegend legend;
51
    private FLayer layer;
52
    private ILabelStyle style;
53
    private ColorChooserController outlineColor;
54
    private ColorChooserController fillColor;
55
    private ColorChooserController textColor;
56
    private Font font;
57

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

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

    
193
    @Override
194
    public ILegend getLegend() {
195

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

    
218
    private void doChangeMode() {
219
        if( this.rdbBasic.isSelected() ) {
220
            this.spnSymbolSize.setEnabled(true);
221
            this.fillColor.setEnabled(true);
222
            this.outlineColor.setEnabled(true);
223
            this.textColor.setEnabled(true);
224
            this.txtTextFont.setEnabled(true);
225
            this.chkShowBounds.setEnabled(true);
226
            
227
            this.btnStyle.setEnabled(false);
228
        } else {
229
            this.spnSymbolSize.setEnabled(false);
230
            this.fillColor.setEnabled(false);
231
            this.outlineColor.setEnabled(false);
232
            this.textColor.setEnabled(false);
233
            this.txtTextFont.setEnabled(false);
234
            this.chkShowBounds.setEnabled(false);
235
            
236
            this.btnStyle.setEnabled(true);
237
        }
238
    }
239
    
240
    private void doOperationChanged() {
241
        Operation operation = (Operation) this.cboOperation.getSelectedItem();
242
        if( operation==null ) {
243
            this.cboOperationAttribute.setEnabled(false);
244
            this.txtOperationValue.setEnabled(false);
245
            this.lblOperationDescription.setText("");
246
            return;
247
        }
248
        this.txtOperationValue.setEnabled(operation.isAditionalValueRequiered());
249
        this.cboOperationAttribute.setEnabled(operation.isAttributeRequiered());
250
        if( operation.isAttributeRequiered() && this.cboOperationAttribute.getSelectedIndex()<0 ) {
251
            this.cboOperationAttribute.setSelectedIndex(0);
252
        }
253
        //this.lblOperationDescription.setText(operation.getDescription());
254
        this.cboOperation.setToolTipText(operation.getDescription());
255
    }
256
    
257
    private void showFontChooser() {
258
        I18nManager i18n = ToolsLocator.getI18nManager();
259
        WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
260
        
261
        final FontChooserPanel fontChooser = new FontChooserPanel(font);
262
//        fontChooser.setPreferredSize(new Dimension(500,350));
263
        final Dialog dialog = winmgr.createDialog(
264
            fontChooser,
265
            i18n.getTranslation("_Select_font"),
266
            null, 
267
            WindowManager_v2.BUTTONS_OK_CANCEL
268
        );
269
        dialog.addActionListener(new ActionListener() {
270
            @Override
271
            public void actionPerformed(ActionEvent e) {
272
                if( dialog.getAction() == WindowManager_v2.BUTTONS_OK ) {
273
                    font = fontChooser.getSelectedFont();
274
                    txtTextFont.setText(getFontLabel(font));
275
                }
276
            }
277
        });
278
        dialog.show(WindowManager.MODE.DIALOG);
279
    }
280

    
281
    private String getFontLabel(Font font) {
282
        StringBuilder b = new StringBuilder();
283
        b.append(font.getFontName());
284
        b.append(", ");
285
        b.append(font.getSize());
286
        return b.toString();
287
    }
288
    
289
    @Override
290
    public String getDescription() {
291
        I18nManager i18n = ToolsLocator.getI18nManager();
292
        return i18n.getTranslation( "_Aggregate_legend_description");
293
    }
294

    
295
    @Override
296
    public ImageIcon getIcon() {
297
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
298
        return theme.get("legend-overview-vectorial-aggregated");
299
    }
300

    
301
    @Override
302
        public Class getParentClass() {
303
                return Features.class;
304
        }
305

    
306
    @Override
307
    public String getTitle() {
308
        I18nManager i18n = ToolsLocator.getI18nManager();
309
        return i18n.getTranslation( "_Aggregate_legend");
310
    }
311

    
312
    @Override
313
    public JPanel getPanel() {
314
        return this;
315
    }
316

    
317
    @Override
318
    public Class getLegendClass() {
319
        return this.legendClass;
320
    }
321

    
322
    @Override
323
    public boolean isSuitableFor(FLayer layer) {
324
        return true;
325
    }
326

    
327
    private void doSelectLabelStyle() {
328
        I18nManager i18n = ToolsLocator.getI18nManager();
329
        WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
330
        final StyleSelector styleSelector = new StyleSelector(this.style, Geometry.TYPES.POINT);
331
        styleSelector.setPreferredSize(new Dimension(600, 350));
332
        winmanager.showWindow(
333
            styleSelector,
334
            i18n.getTranslation("_Select_style"),
335
            WindowManager.MODE.DIALOG
336
        );
337
        ILabelStyle x = (ILabelStyle) styleSelector.getSelectedObject();
338
        if( x != null ) {
339
            this.style = x;
340
        }
341
    }
342
}