Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / QuantityByCategory.java @ 11704

History | View | Annotate | Download (10 KB)

1
package com.iver.cit.gvsig.project.documents.view.legend.gui;
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.io.IOException;
8
import java.sql.Types;
9
import java.util.ArrayList;
10

    
11
import javax.swing.BorderFactory;
12
import javax.swing.DefaultComboBoxModel;
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

    
20
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
21
import com.hardcode.gdbms.engine.data.DataSource;
22
import com.iver.andami.PluginServices;
23
import com.iver.andami.messages.NotificationManager;
24
import com.iver.andami.ui.mdiManager.IWindow;
25
import com.iver.andami.ui.mdiManager.WindowInfo;
26
import com.iver.cit.gvsig.fmap.core.FShape;
27
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
28
import com.iver.cit.gvsig.fmap.core.symbols.PictureMarkerSymbol;
29
import com.iver.cit.gvsig.fmap.layers.FLayer;
30
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
31
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
32
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
33
import com.iver.cit.gvsig.fmap.rendering.GraduatedSymbolLegend;
34
import com.iver.cit.gvsig.fmap.rendering.ILegend;
35
import com.iver.cit.gvsig.fmap.rendering.QuantityByCategoryLegend;
36
import com.iver.cit.gvsig.fmap.rendering.VectorialIntervalLegend;
37
import com.iver.utiles.swing.JComboBox;
38

    
39
public class QuantityByCategory extends JPanel implements ILegendPanel, ActionListener {
40

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

    
52
        public QuantityByCategory() {
53
                super();
54
                initialize();
55
        }
56

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

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

    
70
                return pnlNorth;
71
        }
72

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

    
83
                return pnlColorAndSymbol;
84
        }
85

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

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

    
100
                return btnColor;
101
        }
102

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

    
115
                return pnlFields;
116
        }
117

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

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

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

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

    
159
        public ILegend getLegend() {
160
                return legend;
161
        }
162

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

    
167
        public ISymbol getPreviewSymbol() {
168
                if (previewSymbol == null) {
169
                        try {
170
                                previewSymbol = new PictureMarkerSymbol(
171
                                                this.getClass().getClassLoader()
172
                                                                .getResource("images/QuantitiesByCategory.png"));
173
                        } catch (IOException e) {
174
                                return null;
175
                        }
176
                }
177
                return previewSymbol;
178
        }
179

    
180
        public Class getParentClass() {
181
                return MultipleAttributes.class;
182
        }
183

    
184
        public String getTitle() {
185
                return PluginServices.getText(this, "quantity_by_category");
186
        }
187

    
188
        public JPanel getPanel() {
189
                return this;
190
        }
191

    
192
        public Class getLegendClass() {
193
                return QuantityByCategoryLegend.class;
194
        }
195

    
196
        public void actionPerformed(ActionEvent e) {
197
                JComponent c = (JComponent) e.getSource();
198
                if (c.equals(getBtnColor())) {
199
                        VectorialIntervalLegend colorRamp = legend.getColorRampLegend();
200
                        String fieldName = (String) getCmbColorField().getSelectedItem();
201
                        if (!fieldName.equals(colorRamp.getFieldNames())) {
202
                                // if classification field has changed, clear the legend
203
                                colorRamp.setFieldNames(new String[] {fieldName});
204
                                colorRamp.clear();
205
                        }
206

    
207
                        // create a new modal window to edit the color ramp legend
208
                        VectorialInterval legPanel = new VectorialInterval();
209
                        legPanel.setData(layer, legend.getColorRampLegend());
210
                        InnerWindow window = new InnerWindow(legPanel);
211
                        PluginServices.getMDIManager().addWindow(window);
212
                        legend.setColorRampLegend(legPanel.getLegend());
213

    
214
                } else if (c.equals(getBtnSymbol())) {
215
                        GraduatedSymbolLegend graduatedSymbol = legend.getGraduatedSymbolLegend();
216
                        String fieldName = (String) getCmbColorField().getSelectedItem();
217
                        if (!fieldName.equals(graduatedSymbol.getFieldNames())) {
218
                                // if classification field has changed, clear the legend
219
                                graduatedSymbol.setFieldNames(new String[] {fieldName});
220
                                graduatedSymbol.clear();
221
                        }
222
                        // create a new modal window to edit the graduated symbol legend
223
                        GraduatedSymbols legPanel = new GraduatedSymbols();
224
                        legPanel.setData(layer, legend.getGraduatedSymbolLegend());
225
                        InnerWindow window = new InnerWindow(legPanel);
226
                        PluginServices.getMDIManager().addWindow(window);
227
                        legend.setGraduateSymbolLegend(legPanel.getLegend());
228

    
229
                }
230
        }
231

    
232
        private class InnerWindow extends JPanel implements IWindow {
233
                private ActionListener okAction = new ActionListener() {
234
                        public void actionPerformed(ActionEvent e) {
235
                                legend = panel.getLegend();
236
                                PluginServices.getMDIManager().closeWindow(InnerWindow.this);
237
                        }
238

    
239
                }, cancelAction = new ActionListener() {
240
                        public void actionPerformed(ActionEvent e) {
241
                                PluginServices.getMDIManager().closeWindow(InnerWindow.this);
242
                        }
243
                };
244
                private ILegend legend;
245
                private ILegendPanel panel;
246
                private WindowInfo wi;
247
                public InnerWindow(ILegendPanel panel) {
248
                        this.panel = panel;
249
                        this.setLayout(new BorderLayout());
250
                        add((JComponent) panel, BorderLayout.NORTH);
251
                        add(new AcceptCancelPanel(okAction, cancelAction), BorderLayout.SOUTH);
252
                }
253

    
254
                public ILegend getLegend() {
255
                        return legend;
256
                }
257

    
258
                public WindowInfo getWindowInfo() {
259
                        if (wi == null) {
260
                                wi = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
261
                                JComponent c = (JComponent) panel;
262
                                wi.setWidth(c.getWidth());
263
                                wi.setHeight(c.getHeight());
264
                                wi.setTitle(panel.getDescription());
265
                        }
266
                        return wi;
267
                }
268
        }
269

    
270
    protected void fillFieldNames() {
271
        DataSource rs = null;
272
        ArrayList nomFields = null;
273

    
274
        try {
275
            rs = ((AlphanumericData) layer).getRecordset();
276
            rs.start();
277

    
278
            nomFields = new ArrayList();
279

    
280
            int type;
281
            for (int i = 0; i < rs.getFieldCount(); i++) {
282
                type = rs.getFieldType(i);
283

    
284
                if (type == Types.NULL) {
285
                    continue;
286
                }
287

    
288
                if ((type == Types.INTEGER) ||
289
                        (type == Types.DOUBLE) ||
290
                        (type == Types.FLOAT) ||
291
                        (type == Types.BIGINT)) {
292
                    nomFields.add(rs.getFieldName(i).trim());
293
                }
294
            }
295

    
296
            rs.stop();
297
        } catch (ReadDriverException e) {
298
                NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
299
        }
300

    
301
        DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields.toArray());
302
        cmbColorField.setModel(cM);
303
        cM = new DefaultComboBoxModel(nomFields.toArray());
304
        cmbGraduatedSymbolField.setModel(cM);
305

    
306
    }
307

    
308
    private boolean isNumericField(int fieldType) {
309
                switch (fieldType) {
310
                case Types.BIGINT:
311
                case Types.DECIMAL:
312
                case Types.DOUBLE:
313
                case Types.FLOAT:
314
                case Types.INTEGER:
315
                case Types.NUMERIC:
316
                case Types.REAL:
317
                case Types.SMALLINT:
318
                case Types.TINYINT:
319
                        return true;
320
                default:
321
                        return false;
322
                }
323

    
324
        }
325

    
326
        public boolean isSuitableFor(FLayer layer) {
327
                if (layer instanceof FLyrVect) {
328
                        try {
329
                                FLyrVect lyr = (FLyrVect) layer;
330

    
331
                                if (lyr.getShapeType() != FShape.POLYGON)
332
                                        return false;
333

    
334
                                SelectableDataSource sds;
335
                                sds = ((FLyrVect) layer).getRecordset();
336
                                String[] fNames = sds.getFieldNames();
337
                                for (int i = 0; i < fNames.length; i++) {
338
                                        if (isNumericField(sds.getFieldType(i))) {
339
                                                return true;
340
                                        }
341
                                }
342
                        } catch (ReadDriverException e) {
343
                                return false;
344
                        }
345
                }
346
                return false;
347
        }
348
}