Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / VectorialInterval.java @ 28460

History | View | Annotate | Download (23.4 KB)

1 14821 jmvivo
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2 10679 jaume
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23 14821 jmvivo
 *   Av. Blasco Ib��ez, 50
24 10679 jaume
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.project.documents.view.legend.gui;
42
43
import java.awt.BorderLayout;
44
import java.awt.Color;
45 22726 vcaballero
import java.awt.Dimension;
46 10679 jaume
import java.awt.FlowLayout;
47
import java.awt.GridLayout;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50 14313 jdominguez
import java.io.File;
51 10679 jaume
import java.io.IOException;
52
import java.sql.Types;
53
import java.text.NumberFormat;
54
import java.util.ArrayList;
55
56
import javax.swing.BorderFactory;
57
import javax.swing.DefaultComboBoxModel;
58 18623 jdominguez
import javax.swing.ImageIcon;
59 10679 jaume
import javax.swing.JCheckBox;
60
import javax.swing.JComboBox;
61 22726 vcaballero
import javax.swing.JLabel;
62 10679 jaume
import javax.swing.JOptionPane;
63
import javax.swing.JPanel;
64
import javax.swing.JTextField;
65
66
import org.apache.log4j.Logger;
67
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
68 22726 vcaballero
import org.gvsig.gui.beans.swing.JBlank;
69 13862 jaume
import org.gvsig.gui.beans.swing.JButton;
70 10679 jaume
71
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
72
import com.hardcode.gdbms.engine.data.DataSource;
73
import com.iver.andami.PluginServices;
74
import com.iver.andami.messages.NotificationManager;
75 13862 jaume
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException;
76 10825 jaume
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
77 14313 jdominguez
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillPropertiesStyle;
78 10679 jaume
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
79
import com.iver.cit.gvsig.fmap.layers.FLayer;
80 11704 jaume
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
81
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
82 24284 vcaballero
import com.iver.cit.gvsig.fmap.layers.XMLException;
83 10679 jaume
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
84
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
85
import com.iver.cit.gvsig.fmap.rendering.FInterval;
86
import com.iver.cit.gvsig.fmap.rendering.IInterval;
87 11558 jaume
import com.iver.cit.gvsig.fmap.rendering.ILegend;
88 14553 jdominguez
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
89 10679 jaume
import com.iver.cit.gvsig.fmap.rendering.NullIntervalValue;
90 22726 vcaballero
import com.iver.cit.gvsig.fmap.rendering.NullUniqueValue;
91 10679 jaume
import com.iver.cit.gvsig.fmap.rendering.VectorialIntervalLegend;
92 20779 vcaballero
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
93 10679 jaume
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
94 22726 vcaballero
import com.iver.cit.gvsig.gui.styling.JComboBoxColorScheme;
95 10679 jaume
96
97
/**
98
 * DOCUMENT ME!
99
 *
100
 * @author Vicente Caballero Navarro
101
 */
102
public class VectorialInterval extends JPanel implements ILegendPanel{
103 25765 jpiera
        protected static Logger logger = Logger.getLogger(VectorialInterval.class.getName());
104 22710 vcaballero
        private GridBagLayoutPanel pnlGeneral = null;
105
        protected JComboBox cmbField = null;
106
        protected JTextField txtNumIntervals = null;
107 25765 jpiera
        protected ColorChooserPanel colorChooserPanel = null;
108
        protected ColorChooserPanel colorChooserPanel1 = null;
109 22710 vcaballero
        protected JCheckBox chkdefaultvalues = null;
110
        protected JComboBox cmbFieldType = null;
111 25765 jpiera
        protected JPanel panelS = null;
112 22710 vcaballero
        private JButton bintervals = null;
113
        private JButton bInsert = null;
114
        protected JButton bDelAll = null;
115
        protected JButton bDel = null;
116
        private int count = 0;
117
        protected ClassifiableVectorial layer;
118
        protected VectorialIntervalLegend theLegend;
119
        protected VectorialIntervalLegend auxLegend = null;
120
        protected SymbolTable symbolTable;
121 25765 jpiera
        protected MyListener listener = new MyListener();
122 22710 vcaballero
        protected JPanel pnlCenter = null;
123
        protected JPanel optionPanel;
124
        private JPanel pnlNorth;
125 22726 vcaballero
        protected JSymbolPreviewButton defaultSymbolPrev;
126
        private GridBagLayoutPanel defaultSymbolPanel = new GridBagLayoutPanel();
127 22710 vcaballero
        /**
128
         * This is the default constructor
129
         */
130
        public VectorialInterval() {
131
                super();
132
                initialize();
133
        }
134 10679 jaume
135 22710 vcaballero
        /**
136
         * This method initializes this
137
         */
138
        private void initialize() {
139
                this.setLayout(new BorderLayout());
140
                this.setSize(700, 350);
141
                this.add(getPnlNorth(), BorderLayout.NORTH);
142 10679 jaume
143 22710 vcaballero
                this.add(getPnlButtons(), BorderLayout.SOUTH);
144
                this.add(getPnlCenter(), BorderLayout.CENTER);
145
                setOptionPanel(getOptionPanel());
146
        }
147 10679 jaume
148 22710 vcaballero
        private JPanel getPnlNorth() {
149
                if (pnlNorth == null) {
150
                        pnlNorth = new JPanel(new GridLayout(1, 2));
151
                        pnlNorth.add(getGeneralPanel());
152
                }
153
                return pnlNorth;
154
        }
155 10679 jaume
156 22710 vcaballero
        /**
157
         * This method initializes panelN
158
         *
159
         * @return javax.swing.JPanel
160
         */
161
        private JPanel getGeneralPanel() {
162
                if (pnlGeneral == null) {
163
                        pnlGeneral = new GridBagLayoutPanel();
164
                        pnlGeneral.setBorder(BorderFactory.
165
                                        createTitledBorder(null,
166
                                                        PluginServices.getText(this, "fields")));
167
                        pnlGeneral.addComponent(PluginServices.getText(this, "Campo_de_clasificacion"),
168
                                        getCmbFields());
169
                        pnlGeneral.addComponent(PluginServices.getText(this, "tipo_de_intervalo"),
170
                                        getCmbIntervalTypes());
171 10679 jaume
172 22710 vcaballero
                        JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
173
                        aux.add(getTxtNumIntervals());
174
                        pnlGeneral.addComponent(PluginServices.getText(this, "No_de_intervalos"),
175
                                        aux);
176 10679 jaume
177 22726 vcaballero
178
                        defaultSymbolPanel.add(getChkDefaultvalues(), null);
179
                        pnlGeneral.addComponent(defaultSymbolPanel);
180
181 22710 vcaballero
                }
182
                return pnlGeneral;
183
        }
184 10679 jaume
185 22710 vcaballero
        public JPanel getOptionPanel() {
186
                if (optionPanel == null) {
187
                        optionPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
188
                        optionPanel.setBorder(BorderFactory.
189
                                        createTitledBorder(null,
190
                                                        PluginServices.getText(this, "color_ramp")));
191 10679 jaume
192 22710 vcaballero
                        GridBagLayoutPanel aux = new GridBagLayoutPanel();
193
                        aux.addComponent(PluginServices.getText(this, "Color_final"),
194
                                        getColorChooserPanel());
195
                        aux.addComponent(PluginServices.getText(this, "Color_inicio"),
196
                                        getColorChooserPanel1());
197
                        optionPanel.add(aux);
198
                }
199
                return optionPanel;
200
        }
201 10679 jaume
202 22710 vcaballero
        private void setOptionPanel(JPanel p) {
203
                getPnlNorth().remove(getOptionPanel());
204
                getPnlNorth().add(p, BorderLayout.NORTH);
205
        }
206
        /**
207
         * This method initializes jComboBox
208
         *
209
         * @return javax.swing.JComboBox
210
         */
211
        private JComboBox getCmbFields() {
212
                if (cmbField == null) {
213
                        cmbField = new JComboBox();
214
                        cmbField.setActionCommand("FIELD_SELECTED");
215
                        cmbField.addActionListener(listener);
216
                        cmbField.setVisible(true);
217
                }
218 10679 jaume
219 22710 vcaballero
                return cmbField;
220
        }
221 10679 jaume
222 22710 vcaballero
        /**
223
         * This method initializes txtNumIntervals
224
         *
225
         * @return javax.swing.JTextField
226
         */
227
        private JTextField getTxtNumIntervals() {
228
                if (txtNumIntervals == null) {
229
                        txtNumIntervals = new JTextField(5);
230
                        txtNumIntervals.setText("5");
231
                }
232 10679 jaume
233 22710 vcaballero
                return txtNumIntervals;
234
        }
235 10679 jaume
236 22710 vcaballero
        /**
237
         * This method initializes colorChooserPanel
238
         *
239
         * @return com.iver.cit.gvsig.gui.Panels.ColorChooserPanel
240
         */
241
        private ColorChooserPanel getColorChooserPanel() {
242
                if (colorChooserPanel == null) {
243
                        colorChooserPanel = new ColorChooserPanel();
244
                        colorChooserPanel.setBounds(new java.awt.Rectangle(108, 49, 54, 20));
245
                        colorChooserPanel.setAlpha(255);
246
                        colorChooserPanel.setColor(Color.red);
247
                }
248 10679 jaume
249 22710 vcaballero
                return colorChooserPanel;
250
        }
251 10679 jaume
252 22710 vcaballero
        /**
253
         * This method initializes colorChooserPanel1
254
         *
255
         * @return com.iver.cit.gvsig.gui.Panels.ColorChooserPanel
256
         */
257
        private ColorChooserPanel getColorChooserPanel1() {
258
                if (colorChooserPanel1 == null) {
259
                        colorChooserPanel1 = new ColorChooserPanel();
260
                        colorChooserPanel1.setBounds(new java.awt.Rectangle(251, 49, 54, 20));
261
                        colorChooserPanel1.setAlpha(255);
262
                        colorChooserPanel1.setColor(Color.blue);
263
                }
264 10679 jaume
265 22710 vcaballero
                return colorChooserPanel1;
266
        }
267 10679 jaume
268 22710 vcaballero
        /**
269
         * This method initializes chkdefaultvalues
270
         *
271
         * @return javax.swing.JCheckBox
272
         */
273
        protected JCheckBox getChkDefaultvalues() {
274
                if (chkdefaultvalues == null) {
275
                        chkdefaultvalues = new JCheckBox();
276
                        chkdefaultvalues.setText(PluginServices.getText(this,
277 22726 vcaballero
                        "resto_valores")+": ");
278 22710 vcaballero
                        chkdefaultvalues.setBounds(new java.awt.Rectangle(342, 26, 141, 20));
279
                        chkdefaultvalues.setSelected(false);
280
                        chkdefaultvalues.addActionListener(new java.awt.event.ActionListener() {
281
                                public void actionPerformed(java.awt.event.ActionEvent e) {
282
                                        if (chkdefaultvalues.isSelected()) {
283 22726 vcaballero
                                                auxLegend.useDefaultSymbol(true);
284 22710 vcaballero
                                        } else {
285 22726 vcaballero
                                                auxLegend.useDefaultSymbol(false);
286 22710 vcaballero
                                        }
287
                                }
288
                        });
289
                }
290 10679 jaume
291 22710 vcaballero
                return chkdefaultvalues;
292
        }
293 10679 jaume
294 22710 vcaballero
        /**
295
         * This method initializes jComboBox1
296
         *
297
         * @return javax.swing.JComboBox
298
         */
299 25765 jpiera
        protected JComboBox getCmbIntervalTypes() {
300 22710 vcaballero
                if (cmbFieldType == null) {
301
                        cmbFieldType = new JComboBox();
302
                        cmbFieldType.setActionCommand("INTERVAL_TYPE");
303
                        cmbFieldType.addActionListener(listener);
304
                        cmbFieldType.addItem(PluginServices.getText(this, "equal_intervals"));
305
                        cmbFieldType.addItem(PluginServices.getText(this,
306
                        "natural_intervals"));
307
                        cmbFieldType.addItem(PluginServices.getText(this,
308
                        "quantile_intervals"));
309
                        cmbFieldType.setVisible(true);
310
                }
311 10679 jaume
312 22710 vcaballero
                return cmbFieldType;
313
        }
314 10679 jaume
315 22710 vcaballero
        /**
316
         * This method initializes panelS
317
         *
318
         * @return javax.swing.JPanel
319
         */
320
        protected JPanel getPnlButtons() {
321
                if (panelS == null) {
322
                        panelS = new JPanel();
323
                        panelS.setPreferredSize(new java.awt.Dimension(417, 32));
324
                        panelS.add(getBintervals(), null);
325 25765 jpiera
                        panelS.add(getBInsert(), null);
326 22710 vcaballero
                        panelS.add(getBDelAll(), null);
327
                        panelS.add(getBDel(), null);
328
                }
329 10679 jaume
330 22710 vcaballero
                return panelS;
331
        }
332 10679 jaume
333 22710 vcaballero
        /**
334
         * This method initializes bintervals
335
         *
336
         * @return javax.swing.JButton
337
         */
338 25765 jpiera
        protected JButton getBintervals() {
339 22710 vcaballero
                if (bintervals == null) {
340
                        bintervals = new JButton();
341
                        bintervals.setActionCommand("ADD_ALL_VALUES");
342
                        bintervals.addActionListener(listener);
343
                        bintervals.setText(PluginServices.getText(this,
344
                        "Calcular_intervalos"));
345
                }
346 10679 jaume
347 22710 vcaballero
                return bintervals;
348
        }
349 10679 jaume
350 22710 vcaballero
        /**
351
         * This method initializes bInsert
352
         *
353
         * @return javax.swing.JButton
354
         */
355 25765 jpiera
        protected JButton getBInsert() {
356 22710 vcaballero
                if (bInsert == null) {
357
                        bInsert = new JButton();
358
                        bInsert.setActionCommand("ADD_VALUE");
359
                        bInsert.addActionListener(listener);
360
                        bInsert.setText(PluginServices.getText(this, "Anadir"));
361
                }
362 10679 jaume
363 22710 vcaballero
                return bInsert;
364
        }
365 10679 jaume
366 22710 vcaballero
        /**
367
         * This method initializes bDelAll
368
         *
369
         * @return javax.swing.JButton
370
         */
371 25765 jpiera
        protected JButton getBDelAll() {
372 22710 vcaballero
                if (bDelAll == null) {
373
                        bDelAll = new JButton();
374
                        bDelAll.setActionCommand("REMOVE_ALL");
375
                        bDelAll.addActionListener(listener);
376
                        bDelAll.setText(PluginServices.getText(this, "Quitar_todos"));
377
                }
378 10679 jaume
379 22710 vcaballero
                return bDelAll;
380
        }
381 10679 jaume
382 22710 vcaballero
        /**
383
         * This method initializes bDel
384
         *
385
         * @return javax.swing.JButton
386
         */
387 25765 jpiera
        protected JButton getBDel() {
388 22710 vcaballero
                if (bDel == null) {
389
                        bDel = new JButton();
390
                        bDel.setText(PluginServices.getText(this, "Quitar"));
391
                        bDel.setActionCommand("REMOVE");
392
                        bDel.addActionListener(listener);
393
                }
394 10679 jaume
395 22710 vcaballero
                return bDel;
396
        }
397 10825 jaume
398 10679 jaume
399 22710 vcaballero
        /**
400
         * Damos una primera pasada para saber los l�mites inferior y superior y
401
         * rellenar un array con los valores. Luego dividimos ese array en
402
         * intervalos.
403
         */
404
        protected void fillTableValues() {
405 10679 jaume
406 22710 vcaballero
                symbolTable.removeAllItems();
407 14821 jmvivo
408 22710 vcaballero
                try {
409
                        FInterval[] arrayIntervalos = calculateIntervals();
410
                        if (arrayIntervalos == null)
411
                                return;
412 10679 jaume
413 22710 vcaballero
                        FInterval interval;
414
                        NumberFormat.getInstance().setMaximumFractionDigits(2);
415
                        //theLegend.clear();
416
                        auxLegend.clear();
417 14821 jmvivo
418 22710 vcaballero
                        int r;
419
                        int g;
420
                        int b;
421
                        int stepR;
422
                        int stepG;
423
                        int stepB;
424 14821 jmvivo
425 22710 vcaballero
                        // Cogemos el tipo de gradaci�n de colores que quiere el usuario y
426
                        // Creamos el primer y �ltimo color.
427
                        Color startColor = colorChooserPanel.getColor();
428
                        //theLegend.setStartColor(startColor);
429
                        auxLegend.setStartColor(startColor);
430 10679 jaume
431 22710 vcaballero
                        Color endColor = colorChooserPanel1.getColor();
432
                        //theLegend.setEndColor(endColor);
433
                        auxLegend.setEndColor(endColor);
434 14821 jmvivo
435 22710 vcaballero
                        r = startColor.getRed();
436
                        g = startColor.getGreen();
437
                        b = startColor.getBlue();
438
                        stepR = (endColor.getRed() - r) / arrayIntervalos.length;
439
                        stepG = (endColor.getGreen() - g) / arrayIntervalos.length;
440
                        stepB = (endColor.getBlue() - b) / arrayIntervalos.length;
441 14821 jmvivo
442 22710 vcaballero
                        auxLegend = LegendFactory.createVectorialIntervalLegend(layer.getShapeType());
443 25187 vcaballero
                        auxLegend.setIntervalType(getCmbIntervalTypes().getSelectedIndex());
444 22710 vcaballero
                        auxLegend.useDefaultSymbol(false);
445 10679 jaume
446 22710 vcaballero
                        int symbolType = layer.getShapeType();
447
                        int numSymbols = 0;
448 10679 jaume
449 22710 vcaballero
                        for (int k = 0; k < arrayIntervalos.length; k++) {
450
                                interval = arrayIntervalos[k];
451 10679 jaume
452 22710 vcaballero
                                ISymbol theSymbol = SymbologyFactory.createDefaultSymbolByShapeType(
453
                                                symbolType,
454
                                                new Color(r, g, b));
455
                                theSymbol.setDescription(NumberFormat.getInstance().format(interval.getMin()) +
456
                                                " - " +
457
                                                NumberFormat.getInstance().format(interval.getMax()));
458 10679 jaume
459 22710 vcaballero
                                //////////////////////////////////////
460
                                // CALCULAMOS UN COLOR APROPIADO
461
                                r = r + stepR;
462
                                g = g + stepG;
463
                                b = b + stepB;
464 10679 jaume
465 22710 vcaballero
                                /////////////////////////////////
466
                                auxLegend.addSymbol(interval, theSymbol);
467
                                System.out.println("addSymbol = " + interval +
468
                                                " theSymbol = " + theSymbol.getDescription());
469
                                numSymbols++;
470 10679 jaume
471 22710 vcaballero
                                if (numSymbols > 100) {
472
                                        int resp = JOptionPane.showConfirmDialog(this,
473
                                                        PluginServices.getText(this, "mas_de_100_simbolos"),
474
                                                        PluginServices.getText(this, "quiere_continuar"),
475
                                                        JOptionPane.YES_NO_OPTION,
476
                                                        JOptionPane.WARNING_MESSAGE);
477 10679 jaume
478 22710 vcaballero
                                        if ((resp == JOptionPane.NO_OPTION) ||
479
                                                        (resp == JOptionPane.DEFAULT_OPTION)) {
480
                                                return;
481
                                        }
482
                                }
483 10679 jaume
484 22710 vcaballero
                                // }
485
                        } // for
486 14821 jmvivo
487 22710 vcaballero
                        System.out.println("Num. Simbolos = " +
488
                                        auxLegend.getValues().length);
489
                        symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
490
                                        auxLegend.getValues(),auxLegend.getDescriptions());
491 14821 jmvivo
492 22710 vcaballero
                } catch (ReadDriverException e) {
493
                        NotificationManager.addError(PluginServices.getText(this, "could_not_get_shape_type"), e);
494
                } catch (LegendLayerException e) {
495
                        NotificationManager.addError(PluginServices.getText(this, "failed_computing_intervals"), e);
496
                }
497 10679 jaume
498 22710 vcaballero
                bDelAll.setEnabled(true);
499
                bDel.setEnabled(true);
500
        }
501 10679 jaume
502 22710 vcaballero
        protected FInterval[] calculateIntervals() throws LegendLayerException {
503
                int intervalCount = 1;
504
                // ensure the interval value is an integer greather than 0
505
                try {
506
                        intervalCount = (int) Double.
507
                        parseDouble(txtNumIntervals.getText());
508
                        if (intervalCount<1) {
509
                                throw new Exception();
510
                        }
511
                } catch (Exception e) {
512
                        JOptionPane.showMessageDialog(this,
513
                                        PluginServices.getText(this, "invalid_interval_count_value"));
514
                        return null;
515
                }
516 10679 jaume
517 22710 vcaballero
                try {
518 10679 jaume
519 22710 vcaballero
                        return auxLegend.calculateIntervals(
520
                                        //return theLegend.calculateIntervals(
521
                                        ((AlphanumericData) layer).getRecordset(),
522
                                        (String) cmbField.getSelectedItem(),
523
                                        intervalCount,
524
                                        layer.getShapeType()
525
                        );
526
                } catch (ReadDriverException e) {
527
                        return null;
528
                }
529
        }
530 10679 jaume
531 22710 vcaballero
        public void setData(FLayer layer, ILegend legend) {
532
                this.layer = (ClassifiableVectorial) layer;
533
                int shapeType = 0;
534
                try {
535
                        shapeType = this.layer.getShapeType();
536
                } catch (ReadDriverException e) {
537
                        NotificationManager.addError(PluginServices.getText(this, "generating_intervals"), e);
538
                }
539 10679 jaume
540 22710 vcaballero
                if (symbolTable != null)
541
                        pnlCenter.remove(symbolTable);
542 20779 vcaballero
543 22710 vcaballero
544 22726 vcaballero
                getDefaultSymbolPrev(shapeType);
545
546 22710 vcaballero
                symbolTable = new SymbolTable(this, SymbolTable.INTERVALS_TYPE, shapeType);
547
                pnlCenter.add(symbolTable);
548
                fillFieldNames();
549
550 20779 vcaballero
                if (VectorialIntervalLegend.class.equals(legend.getClass())) {
551 24284 vcaballero
                        try {
552
                                auxLegend = (VectorialIntervalLegend) legend.cloneLegend();
553
                        } catch (XMLException e) {
554
                                // TODO Auto-generated catch block
555
                                e.printStackTrace();
556
                        }
557 22710 vcaballero
                        chkdefaultvalues.setSelected(auxLegend.isUseDefaultSymbol());
558 20779 vcaballero
                        cmbField.getModel().setSelectedItem(auxLegend.getClassifyingFieldNames()[0]);
559
                        symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
560 22710 vcaballero
                                        auxLegend.getValues(),auxLegend.getDescriptions());
561 20779 vcaballero
                        colorChooserPanel.setColor(auxLegend.getStartColor());
562
                        colorChooserPanel1.setColor(auxLegend.getEndColor());
563
                        colorChooserPanel.repaint();
564
                        colorChooserPanel1.repaint();
565 23881 vcaballero
                        if(auxLegend.isUseDefaultSymbol())
566
                                txtNumIntervals.setText(String.valueOf(auxLegend.getSymbols().length - 1));
567
                        else
568
                                txtNumIntervals.setText(String.valueOf(auxLegend.getSymbols().length));
569 20779 vcaballero
                } else {
570
                        // Si la capa viene con otro tipo de leyenda, creamos
571
                        // una nueva del tipo que maneja este panel
572
                        auxLegend = new VectorialIntervalLegend();
573
                        auxLegend.setShapeType(shapeType);
574 22710 vcaballero
                        auxLegend.useDefaultSymbol(false);
575 20779 vcaballero
                }
576 22726 vcaballero
                defaultSymbolPrev.setSymbol(auxLegend.getDefaultSymbol());
577 20779 vcaballero
                cmbFieldType.setSelectedIndex(auxLegend.getIntervalType());
578 22710 vcaballero
        }
579 10679 jaume
580 22726 vcaballero
        public void getDefaultSymbolPrev(int shapeType) {
581
                if(defaultSymbolPrev == null){
582 23881 vcaballero
                        defaultSymbolPrev = new JSymbolPreviewButton(shapeType);
583
                        defaultSymbolPrev.setPreferredSize(new Dimension(110,20));
584
                        defaultSymbolPanel.add(defaultSymbolPrev,null);
585 22726 vcaballero
                }
586
        }
587 14350 jdominguez
588 22710 vcaballero
        protected void fillFieldNames() {
589
                DataSource rs = null;
590
                ArrayList nomFields = null;
591 10679 jaume
592 22710 vcaballero
                try {
593
                        rs = ((AlphanumericData) layer).getRecordset();
594
                        logger.debug("rs.start()");
595
                        rs.start();
596 10679 jaume
597 22710 vcaballero
                        nomFields = new ArrayList();
598 10679 jaume
599 22710 vcaballero
                        int type;
600
                        for (int i = 0; i < rs.getFieldCount(); i++) {
601
                                type = rs.getFieldType(i);
602 10679 jaume
603 22710 vcaballero
                                if (type == Types.NULL) {
604
                                        continue;
605
                                }
606 10679 jaume
607 22710 vcaballero
                                if ((type == Types.INTEGER) ||
608
                                                (type == Types.DOUBLE) ||
609
                                                (type == Types.FLOAT) ||
610
                                                (type == Types.BIGINT)) {
611
                                        nomFields.add(rs.getFieldName(i).trim());
612
                                }
613
                        }
614 10679 jaume
615 22710 vcaballero
                        rs.stop();
616
                } catch (ReadDriverException e) {
617
                        NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
618
                }
619 10679 jaume
620 22710 vcaballero
                DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields.toArray());
621
                cmbField.setModel(cM);
622 10679 jaume
623 22710 vcaballero
                symbolTable.removeAllItems();
624
        }
625 10679 jaume
626 22710 vcaballero
        /**
627
         * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
628
         */
629
        public ILegend getLegend() {
630 20779 vcaballero
                fillSymbolListFromTable();
631 22726 vcaballero
632
                if(defaultSymbolPrev.getSymbol() != null)
633
                        auxLegend.setDefaultSymbol(defaultSymbolPrev.getSymbol());
634
635 23255 vcaballero
                auxLegend.useDefaultSymbol(chkdefaultvalues.isSelected());
636
637 24416 vcaballero
                try {
638
                        theLegend = (VectorialIntervalLegend) auxLegend.cloneLegend();
639
                } catch (XMLException e) {
640
                        // TODO Auto-generated catch block
641
                        e.printStackTrace();
642
                }
643 20779 vcaballero
                return theLegend;
644
        }
645 10679 jaume
646 22710 vcaballero
        /**
647
         * A partir de los registros de la tabla, regenera el FRenderer. (No solo
648
         * el symbolList, si no tambi�n el arrayKeys y el defaultRenderer
649
         */
650 14821 jmvivo
651 25765 jpiera
        protected void fillSymbolListFromTable() {
652 20779 vcaballero
                ISymbol theSymbol;
653
                IInterval theInterval = null;
654 10679 jaume
655 20779 vcaballero
                // Borramos las anteriores listas:
656
                auxLegend.clear();
657
658
                String fieldName = (String) cmbField.getSelectedItem();
659
                auxLegend.setClassifyingFieldNames(new String[] {fieldName});
660 22710 vcaballero
661 22726 vcaballero
                auxLegend.useDefaultSymbol(chkdefaultvalues.isSelected());
662
663 20779 vcaballero
                DataSource rs;
664
                try {
665 22710 vcaballero
666 20779 vcaballero
                        rs = ((AlphanumericData) layer).getRecordset();
667
                        rs.start();
668
                        auxLegend.setClassifyingFieldTypes(new int[] {rs.getFieldType(rs.getFieldIndexByName(fieldName))});
669
                        logger.debug("rs.start()");
670
                        rs.stop();
671 22710 vcaballero
672 20779 vcaballero
                } catch (ReadDriverException e) {
673
                        NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
674
                }
675
676 22710 vcaballero
677
678 20779 vcaballero
                for (int row = 0; row < symbolTable.getRowCount(); row++) {
679
                        if (!(symbolTable.getFieldValue(row, 1) instanceof FInterval)) {
680
                                theSymbol = (ISymbol) symbolTable.getFieldValue(row, 0);
681
                                theSymbol.setDescription((String) symbolTable.getFieldValue(
682
                                                row, 2));
683
                                auxLegend.addSymbol(new NullIntervalValue(), theSymbol);
684
                        } else {
685
                                theInterval = (IInterval) symbolTable.getFieldValue(row, 1);
686
                                theSymbol = (ISymbol) symbolTable.getFieldValue(row, 0);
687
                                theSymbol.setDescription((String) symbolTable.getFieldValue(
688
                                                row, 2));
689
                                auxLegend.addSymbol(theInterval, theSymbol);
690
                        }
691 22726 vcaballero
                }
692 20779 vcaballero
693 22726 vcaballero
                if(chkdefaultvalues.isSelected()){
694
                        if(defaultSymbolPrev.getSymbol() != null){
695
                                String description = PluginServices.getText(this,"default");
696
                                defaultSymbolPrev.getSymbol().setDescription(description);
697
                                auxLegend.addSymbol(new NullIntervalValue(), defaultSymbolPrev.getSymbol());
698
                        }
699 20779 vcaballero
                }
700
        }
701
702 22710 vcaballero
        /**
703
         * This method initializes panelC
704
         *
705
         * @return javax.swing.JPanel
706
         */
707
        private JPanel getPnlCenter() {
708
                if (pnlCenter == null) {
709
                        pnlCenter = new JPanel();
710
                }
711 10679 jaume
712 22710 vcaballero
                return pnlCenter;
713
        }
714 10679 jaume
715 22710 vcaballero
        /**
716
         * Listener.
717
         *
718
         * @author Vicente Caballero Navarro
719
         */
720
        class MyListener implements ActionListener {
721 10679 jaume
722 22710 vcaballero
                public void actionPerformed(ActionEvent e) {
723
                        System.out.println("ActionEvent con " + e.getActionCommand());
724 10679 jaume
725 22710 vcaballero
                        //modificar el combobox de valor
726 25765 jpiera
                        if (e.getActionCommand() == "FIELD_SELECTED") {
727
                                fieldSelectedActionPerformed((JComboBox) e.getSource());
728 22710 vcaballero
                        } else if (e.getActionCommand() == "INTERVAL_TYPE") {
729
                                JComboBox cb = (JComboBox) e.getSource();
730 10679 jaume
731 22710 vcaballero
                                //if ((theLegend != null) &&
732
                                if ((auxLegend != null) &&
733
                                                //    (cb.getSelectedIndex() != theLegend.getIntervalType())) {
734
                                                (cb.getSelectedIndex() != auxLegend.getIntervalType())) {
735
                                        //theLegend.setIntervalType(cb.getSelectedIndex());
736
                                        auxLegend.setIntervalType(cb.getSelectedIndex());
737
                                        symbolTable.removeAllItems();
738
                                }
739
                        }
740 10679 jaume
741 22710 vcaballero
                        //A�adir todos los elementos por valor
742
                        if (e.getActionCommand() == "ADD_ALL_VALUES") {
743
                                fillTableValues();
744
                        }
745 10679 jaume
746 22710 vcaballero
                        //A�adir un �nico elemento
747
                        if (e.getActionCommand() == "ADD_VALUE") {
748
                                try {
749
                                        symbolTable.addTableRecord(
750
                                                        SymbologyFactory.createDefaultSymbolByShapeType(layer.getShapeType()),
751
                                                        new FInterval(0, 0),"0 - 0");
752
                                } catch (ReadDriverException e1) {
753
                                        NotificationManager.addError(PluginServices.getText(this, "could_not_get_shape_type"), e1);
754
                                }
755 10679 jaume
756 22710 vcaballero
                        }
757 10679 jaume
758 22710 vcaballero
                        //Vacia la tabla
759
                        if (e.getActionCommand() == "REMOVE_ALL") {
760
                                symbolTable.removeAllItems();
761
                        }
762 10679 jaume
763 22710 vcaballero
                        //Quitar solo el elemento seleccionado
764
                        if (e.getActionCommand() == "REMOVE") {
765
                                symbolTable.removeSelectedRows();
766
                        }
767
                }
768
        }
769 10679 jaume
770 25765 jpiera
        protected void fieldSelectedActionPerformed(JComboBox cb){
771
                String fieldName = (String) cb.getSelectedItem();
772
                System.out.println("Nombre del campo: " + fieldName);
773
                symbolTable.removeAllItems();
774
775
                //theLegend.setClassifyingFieldNames(new String[] {fieldName});
776
                auxLegend.setClassifyingFieldNames(new String[] {fieldName});
777
        }
778
779 22710 vcaballero
        public String getDescription() {
780
                return PluginServices.getText(this,"Muestra_los_elementos_de_la_capa_usando_una_gama_de_colores_en_funcion_del_valor_de_un_determinado_campo_de_atributos") + ".";
781
        }
782
783
        public ImageIcon getIcon() {
784
                return new ImageIcon(this.getClass().getClassLoader().
785 18623 jdominguez
                                getResource("images/Intervalos.png"));
786 22710 vcaballero
        }
787 10679 jaume
788 22710 vcaballero
        public Class getParentClass() {
789
                return Quantities.class;
790
        }
791 10679 jaume
792 22710 vcaballero
        public String getTitle() {
793
                return PluginServices.getText(this,"Intervalos");
794
        }
795 10679 jaume
796 22710 vcaballero
        public JPanel getPanel() {
797
                return this;
798
        }
799 10679 jaume
800 22710 vcaballero
        public Class getLegendClass() {
801
                return VectorialIntervalLegend.class;
802
        }
803 11704 jaume
804 22710 vcaballero
        private boolean isNumericField(int fieldType) {
805 11704 jaume
                switch (fieldType) {
806
                case Types.BIGINT:
807
                case Types.DECIMAL:
808
                case Types.DOUBLE:
809
                case Types.FLOAT:
810
                case Types.INTEGER:
811
                case Types.NUMERIC:
812
                case Types.REAL:
813
                case Types.SMALLINT:
814
                case Types.TINYINT:
815
                        return true;
816
                default:
817
                        return false;
818
                }
819
820
        }
821
        public boolean isSuitableFor(FLayer layer) {
822
                if (layer instanceof FLyrVect) {
823
                        SelectableDataSource sds;
824
                        try {
825
                                sds = ((FLyrVect) layer).getRecordset();
826
                                String[] fNames = sds.getFieldNames();
827
                                for (int i = 0; i < fNames.length; i++) {
828
                                        if (isNumericField(sds.getFieldType(i))) {
829
                                                return true;
830
                                        }
831
                                }
832
                        } catch (ReadDriverException e) {
833
                                return false;
834
                        }
835
                }
836
                return false;
837
        }
838 10679 jaume
}