Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / gui / layerproperties / QuantityByCategory.java @ 20768

History | View | Annotate | Download (10.1 KB)

1
package org.gvsig.symbology.gui.layerproperties;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.GridLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
import java.sql.Types;
8
import java.util.ArrayList;
9

    
10
import javax.swing.BorderFactory;
11
import javax.swing.DefaultComboBoxModel;
12
import javax.swing.ImageIcon;
13
import javax.swing.JComponent;
14
import javax.swing.JPanel;
15

    
16
import org.gvsig.gui.beans.AcceptCancelPanel;
17
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
18
import org.gvsig.gui.beans.swing.JButton;
19
import org.gvsig.symbology.fmap.rendering.GraduatedSymbolLegend;
20
import org.gvsig.symbology.fmap.rendering.QuantityByCategoryLegend;
21
import org.gvsig.symbology.fmap.symbols.PictureFillSymbol;
22

    
23
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
24
import com.hardcode.gdbms.engine.data.DataSource;
25
import com.iver.andami.PluginServices;
26
import com.iver.andami.messages.NotificationManager;
27
import com.iver.andami.ui.mdiManager.IWindow;
28
import com.iver.andami.ui.mdiManager.WindowInfo;
29
import com.iver.cit.gvsig.fmap.core.FShape;
30
import com.iver.cit.gvsig.fmap.layers.FLayer;
31
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
32
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
33
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
34
import com.iver.cit.gvsig.fmap.rendering.ILegend;
35
import com.iver.cit.gvsig.fmap.rendering.VectorialIntervalLegend;
36
import com.iver.cit.gvsig.project.documents.view.legend.gui.ILegendPanel;
37
import com.iver.cit.gvsig.project.documents.view.legend.gui.MultipleAttributes;
38
import com.iver.cit.gvsig.project.documents.view.legend.gui.VectorialInterval;
39
import com.iver.utiles.swing.JComboBox;
40

    
41
public class QuantityByCategory extends JPanel implements ILegendPanel, ActionListener {
42

    
43
        private PictureFillSymbol previewSymbol;
44
        private JPanel pnlNorth;
45
        private GridBagLayoutPanel pnlFields;
46
        private JPanel pnlColorAndSymbol;
47
        private JComboBox cmbColorField;
48
        private JComboBox cmbGraduatedSymbolField;
49
        private JButton btnColor;
50
        private JButton btnSymbol;
51
        private QuantityByCategoryLegend legend;
52
        private FLayer layer;
53

    
54
        public QuantityByCategory() {
55
                super();
56
                initialize();
57
        }
58

    
59
        private void initialize() {
60
                this.setLayout(new BorderLayout());
61
        this.setSize(490, 300);
62
        this.add(getPnlNorth(), BorderLayout.NORTH);
63
        }
64

    
65
        private JPanel getPnlNorth() {
66
                if (pnlNorth == null) {
67
                        pnlNorth = new JPanel(new GridLayout(1, 2));
68
                        pnlNorth.add(getPnlFields());
69
                        pnlNorth.add(getPnlColorAndSymbol());
70
                }
71

    
72
                return pnlNorth;
73
        }
74

    
75
        private JPanel getPnlColorAndSymbol() {
76
                if (pnlColorAndSymbol == null) {
77
                        pnlColorAndSymbol = new JPanel();
78
                        pnlColorAndSymbol.setBorder(
79
                                        BorderFactory.createTitledBorder(
80
                                                        null, PluginServices.getText(this, "variation_by")));
81
                        pnlColorAndSymbol.add(getBtnColor());
82
                        pnlColorAndSymbol.add(getBtnSymbol());
83
                }
84

    
85
                return pnlColorAndSymbol;
86
        }
87

    
88
        private JButton getBtnSymbol() {
89
                if (btnSymbol == null) {
90
                        btnSymbol = new JButton(PluginServices.getText(this, "symbol"));
91
                        btnSymbol.addActionListener(this);
92
                }
93
                return btnSymbol;
94
        }
95

    
96
        private JButton getBtnColor() {
97
                if (btnColor == null) {
98
                        btnColor = new JButton(PluginServices.getText(this, "color_ramp"));
99
                        btnColor.addActionListener(this);
100
                }
101

    
102
                return btnColor;
103
        }
104

    
105
        private GridBagLayoutPanel getPnlFields() {
106
                if (pnlFields == null) {
107
                        pnlFields = new GridBagLayoutPanel();
108
                        pnlFields.setBorder(
109
                                        BorderFactory.createTitledBorder(
110
                                                        null, PluginServices.getText(this, "value_fields")));
111
                        pnlFields.addComponent(
112
                                        PluginServices.getText(this, "color_field"), getCmbColorField());
113
                        pnlFields.addComponent(
114
                                        PluginServices.getText(this, "symbol_field") ,getCmbGraduatedField());
115
                }
116

    
117
                return pnlFields;
118
        }
119

    
120
        private JComboBox getCmbGraduatedField() {
121
                if (cmbGraduatedSymbolField == null) {
122
                        cmbGraduatedSymbolField = new JComboBox();
123
                        cmbGraduatedSymbolField.addActionListener(this);
124
                }
125
                return cmbGraduatedSymbolField;
126
        }
127

    
128
        private JComboBox getCmbColorField() {
129
                if (cmbColorField == null) {
130
                        cmbColorField = new JComboBox();
131
                        cmbColorField.addActionListener(this);
132
                }
133
                return cmbColorField;
134
        }
135

    
136
        public void setData(FLayer lyr, ILegend legend) {
137
                this.layer = lyr;
138
                fillFieldNames();
139
                if (legend instanceof QuantityByCategoryLegend) {
140
                        this.legend = (QuantityByCategoryLegend) legend;
141
                        getCmbColorField().setSelectedItem(
142
                                        this.legend.getColorRampLegend().getClassifyingFieldNames());
143

    
144
                        getCmbGraduatedField().setSelectedItem(
145
                                        this.legend.getGraduatedSymbolLegend().getClassifyingFieldNames());
146
                } else {
147
                        this.legend = new QuantityByCategoryLegend();
148
                        this.legend.setClassifyingFieldNames(
149
                                        new String[] {
150
                                                        (String) getCmbColorField().getSelectedItem(),
151
                                                        (String) getCmbGraduatedField().getSelectedItem()
152
                                        });
153
                        try {
154
                                this.legend.setShapeType(((FLyrVect)lyr).getShapeType());
155
                        } catch (ReadDriverException e) {
156
                                NotificationManager.addError(PluginServices.getText(this, "getting_layer_shape_type"), e);
157
                        }
158
                }
159
        }
160

    
161
        public ILegend getLegend() {
162
                return legend;
163
        }
164

    
165
        public String getDescription() {
166
                return PluginServices.getText(this, "draw_quantities_for_each_category");
167
        }
168

    
169
        public ImageIcon getIcon() {
170
                return new ImageIcon(this.getClass().getClassLoader().
171
                                getResource("images/QuantitiesByCategory.png"));
172
        }
173

    
174
        public Class getParentClass() {
175
                return MultipleAttributes.class;
176
        }
177

    
178
        public String getTitle() {
179
                return PluginServices.getText(this, "quantity_by_category");
180
        }
181

    
182
        public JPanel getPanel() {
183
                return this;
184
        }
185

    
186
        public Class getLegendClass() {
187
                return QuantityByCategoryLegend.class;
188
        }
189

    
190
        public void actionPerformed(ActionEvent e) {
191
                JComponent c = (JComponent) e.getSource();
192
                if (c.equals(getBtnColor())) {
193
                        VectorialIntervalLegend colorRamp = legend.getColorRampLegend();
194
                        String fieldName = (String) getCmbColorField().getSelectedItem();
195
                        if (!fieldName.equals(colorRamp.getClassifyingFieldNames())) {
196
                                // if classification field has changed, clear the legend
197
                                colorRamp.setClassifyingFieldNames(new String[] {fieldName});
198
                                colorRamp.clear();
199
                        }
200

    
201
                        // create a new modal window to edit the color ramp legend
202
                        VectorialInterval legPanel = new VectorialInterval();
203
                        legPanel.setData(layer, legend.getColorRampLegend());
204
                        InnerWindow window = new InnerWindow(legPanel);
205
                        PluginServices.getMDIManager().addWindow(window);
206
                        legend.setColorRampLegend(legPanel.getLegend());
207

    
208
                } else if (c.equals(getBtnSymbol())) {
209
                        GraduatedSymbolLegend graduatedSymbol = legend.getGraduatedSymbolLegend();
210
                        String fieldName = (String) getCmbColorField().getSelectedItem();
211
                        if (!fieldName.equals(graduatedSymbol.getClassifyingFieldNames())) {
212
                                // if classification field has changed, clear the legend
213
                                graduatedSymbol.setClassifyingFieldNames(new String[] {fieldName});
214
                                graduatedSymbol.clear();
215
                        }
216
                        // create a new modal window to edit the graduated symbol legend
217
                        GraduatedSymbols legPanel = new GraduatedSymbols();
218
                        legPanel.setData(layer, legend.getGraduatedSymbolLegend());
219
                        InnerWindow window = new InnerWindow(legPanel);
220
                        PluginServices.getMDIManager().addWindow(window);
221
                        legend.setGraduateSymbolLegend(legPanel.getLegend());
222

    
223
                }
224
        }
225

    
226
        private class InnerWindow extends JPanel implements IWindow {
227
                private ActionListener okAction = new ActionListener() {
228
                        public void actionPerformed(ActionEvent e) {
229
                                legend = panel.getLegend();
230
                                PluginServices.getMDIManager().closeWindow(InnerWindow.this);
231
                        }
232

    
233
                }, cancelAction = new ActionListener() {
234
                        public void actionPerformed(ActionEvent e) {
235
                                PluginServices.getMDIManager().closeWindow(InnerWindow.this);
236
                        }
237
                };
238
                private ILegend legend;
239
                private ILegendPanel panel;
240
                private WindowInfo wi;
241
                public InnerWindow(ILegendPanel panel) {
242
                        this.panel = panel;
243
                        this.setLayout(new BorderLayout());
244
                        add((JComponent) panel, BorderLayout.NORTH);
245
                        add(new AcceptCancelPanel(okAction, cancelAction), BorderLayout.SOUTH);
246
                }
247

    
248
                public ILegend getLegend() {
249
                        return legend;
250
                }
251

    
252
                public WindowInfo getWindowInfo() {
253
                        if (wi == null) {
254
                                wi = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
255
                                JComponent c = (JComponent) panel;
256
                                wi.setWidth(c.getWidth());
257
                                wi.setHeight(c.getHeight());
258
                                wi.setTitle(panel.getDescription());
259
                        }
260
                        return wi;
261
                }
262
        }
263

    
264
    protected void fillFieldNames() {
265
        DataSource rs = null;
266
        ArrayList nomFields = null;
267

    
268
        try {
269
            rs = ((AlphanumericData) layer).getRecordset();
270
            rs.start();
271

    
272
            nomFields = new ArrayList();
273

    
274
            int type;
275
            for (int i = 0; i < rs.getFieldCount(); i++) {
276
                type = rs.getFieldType(i);
277

    
278
                if (type == Types.NULL) {
279
                    continue;
280
                }
281

    
282
                if ((type == Types.INTEGER) ||
283
                        (type == Types.DOUBLE) ||
284
                        (type == Types.FLOAT) ||
285
                        (type == Types.BIGINT)) {
286
                    nomFields.add(rs.getFieldName(i).trim());
287
                }
288
            }
289

    
290
            rs.stop();
291
        } catch (ReadDriverException e) {
292
                NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
293
        }
294

    
295
        DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields.toArray());
296
        cmbColorField.setModel(cM);
297
        cM = new DefaultComboBoxModel(nomFields.toArray());
298
        cmbGraduatedSymbolField.setModel(cM);
299

    
300
    }
301

    
302
    private boolean isNumericField(int fieldType) {
303
                switch (fieldType) {
304
                case Types.BIGINT:
305
                case Types.DECIMAL:
306
                case Types.DOUBLE:
307
                case Types.FLOAT:
308
                case Types.INTEGER:
309
                case Types.NUMERIC:
310
                case Types.REAL:
311
                case Types.SMALLINT:
312
                case Types.TINYINT:
313
                        return true;
314
                default:
315
                        return false;
316
                }
317

    
318
        }
319

    
320
        public boolean isSuitableFor(FLayer layer) {
321
                if (layer instanceof FLyrVect) {
322
                        try {
323
                                FLyrVect lyr = (FLyrVect) layer;
324

    
325
                                if (lyr.getShapeType() != FShape.POLYGON)
326
                                        return false;
327

    
328
                                SelectableDataSource sds;
329
                                sds = ((FLyrVect) layer).getRecordset();
330
                                String[] fNames = sds.getFieldNames();
331
                                for (int i = 0; i < fNames.length; i++) {
332
                                        if (isNumericField(sds.getFieldType(i))) {
333
                                                return true;
334
                                        }
335
                                }
336
                        } catch (ReadDriverException e) {
337
                                return false;
338
                        }
339
                }
340
                return false;
341
        }
342
}