Statistics
| Revision:

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

History | View | Annotate | Download (23.3 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
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
 *   Av. Blasco Ib��ez, 50
24
 *   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
import java.awt.Dimension;
46
import java.awt.FlowLayout;
47
import java.awt.GridLayout;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.io.File;
51
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
import javax.swing.ImageIcon;
59
import javax.swing.JCheckBox;
60
import javax.swing.JComboBox;
61
import javax.swing.JLabel;
62
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
import org.gvsig.gui.beans.swing.JBlank;
69
import org.gvsig.gui.beans.swing.JButton;
70

    
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
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException;
76
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
77
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillPropertiesStyle;
78
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
79
import com.iver.cit.gvsig.fmap.layers.FLayer;
80
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
81
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
82
import com.iver.cit.gvsig.fmap.layers.XMLException;
83
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
import com.iver.cit.gvsig.fmap.rendering.ILegend;
88
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
89
import com.iver.cit.gvsig.fmap.rendering.NullIntervalValue;
90
import com.iver.cit.gvsig.fmap.rendering.NullUniqueValue;
91
import com.iver.cit.gvsig.fmap.rendering.VectorialIntervalLegend;
92
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
93
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
94
import com.iver.cit.gvsig.gui.styling.JComboBoxColorScheme;
95

    
96

    
97
/**
98
 * DOCUMENT ME!
99
 *
100
 * @author Vicente Caballero Navarro
101
 */
102
public class VectorialInterval extends JPanel implements ILegendPanel{
103
        protected static Logger logger = Logger.getLogger(VectorialInterval.class.getName());
104
        private GridBagLayoutPanel pnlGeneral = null;
105
        protected JComboBox cmbField = null;
106
        protected JTextField txtNumIntervals = null;
107
        protected ColorChooserPanel colorChooserPanel = null;
108
        protected ColorChooserPanel colorChooserPanel1 = null;
109
        protected JCheckBox chkdefaultvalues = null;
110
        protected JComboBox cmbFieldType = null;
111
        protected JPanel panelS = null;
112
        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
        protected MyListener listener = new MyListener();
122
        protected JPanel pnlCenter = null;
123
        protected JPanel optionPanel;
124
        private JPanel pnlNorth;
125
        protected JSymbolPreviewButton defaultSymbolPrev;
126
        private GridBagLayoutPanel defaultSymbolPanel = new GridBagLayoutPanel();
127
        /**
128
         * This is the default constructor
129
         */
130
        public VectorialInterval() {
131
                super();
132
                initialize();
133
        }
134

    
135
        /**
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

    
143
                this.add(getPnlButtons(), BorderLayout.SOUTH);
144
                this.add(getPnlCenter(), BorderLayout.CENTER);
145
                setOptionPanel(getOptionPanel());
146
        }
147

    
148
        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

    
156
        /**
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

    
172
                        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

    
177

    
178
                        defaultSymbolPanel.add(getChkDefaultvalues(), null);
179
                        pnlGeneral.addComponent(defaultSymbolPanel);
180

    
181
                }
182
                return pnlGeneral;
183
        }
184

    
185
        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

    
192
                        GridBagLayoutPanel aux = new GridBagLayoutPanel();
193
                        aux.addComponent(PluginServices.getText(this, "Color_inicio"),
194
                                        getColorChooserPanel());
195
                        aux.addComponent(PluginServices.getText(this, "Color_final"),
196
                                        getColorChooserPanel1());
197
                        optionPanel.add(aux);
198
                }
199
                return optionPanel;
200
        }
201

    
202
        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

    
219
                return cmbField;
220
        }
221

    
222
        /**
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

    
233
                return txtNumIntervals;
234
        }
235

    
236
        /**
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

    
249
                return colorChooserPanel;
250
        }
251

    
252
        /**
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

    
265
                return colorChooserPanel1;
266
        }
267

    
268
        /**
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
                        "resto_valores")+": ");
278
                        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
                                                auxLegend.useDefaultSymbol(true);
284
                                        } else {
285
                                                auxLegend.useDefaultSymbol(false);
286
                                        }
287
                                }
288
                        });
289
                }
290

    
291
                return chkdefaultvalues;
292
        }
293

    
294
        /**
295
         * This method initializes jComboBox1
296
         *
297
         * @return javax.swing.JComboBox
298
         */
299
        protected JComboBox getCmbIntervalTypes() {
300
                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

    
312
                return cmbFieldType;
313
        }
314

    
315
        /**
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
                        panelS.add(getBInsert(), null);
326
                        panelS.add(getBDelAll(), null);
327
                        panelS.add(getBDel(), null);
328
                }
329

    
330
                return panelS;
331
        }
332

    
333
        /**
334
         * This method initializes bintervals
335
         *
336
         * @return javax.swing.JButton
337
         */
338
        protected JButton getBintervals() {
339
                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

    
347
                return bintervals;
348
        }
349

    
350
        /**
351
         * This method initializes bInsert
352
         *
353
         * @return javax.swing.JButton
354
         */
355
        protected JButton getBInsert() {
356
                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

    
363
                return bInsert;
364
        }
365

    
366
        /**
367
         * This method initializes bDelAll
368
         *
369
         * @return javax.swing.JButton
370
         */
371
        protected JButton getBDelAll() {
372
                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

    
379
                return bDelAll;
380
        }
381

    
382
        /**
383
         * This method initializes bDel
384
         *
385
         * @return javax.swing.JButton
386
         */
387
        protected JButton getBDel() {
388
                if (bDel == null) {
389
                        bDel = new JButton();
390
                        bDel.setText(PluginServices.getText(this, "Quitar"));
391
                        bDel.setActionCommand("REMOVE");
392
                        bDel.addActionListener(listener);
393
                }
394

    
395
                return bDel;
396
        }
397

    
398

    
399
        /**
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

    
406
                symbolTable.removeAllItems();
407

    
408
                try {
409
                        FInterval[] arrayIntervalos = calculateIntervals();
410
                        if (arrayIntervalos == null)
411
                                return;
412

    
413
                        FInterval interval;
414
                        NumberFormat.getInstance().setMaximumFractionDigits(2);
415
                        //theLegend.clear();
416
                        auxLegend.clear();
417

    
418
                        int r;
419
                        int g;
420
                        int b;
421
                        int stepR;
422
                        int stepG;
423
                        int stepB;
424

    
425
                        // 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

    
429
                        Color endColor = colorChooserPanel1.getColor();
430

    
431
                        r = startColor.getRed();
432
                        g = startColor.getGreen();
433
                        b = startColor.getBlue();
434
                        stepR = (endColor.getRed() - r) / arrayIntervalos.length;
435
                        stepG = (endColor.getGreen() - g) / arrayIntervalos.length;
436
                        stepB = (endColor.getBlue() - b) / arrayIntervalos.length;
437

    
438
                        auxLegend = LegendFactory.createVectorialIntervalLegend(layer.getShapeType());
439
                        auxLegend.setStartColor(startColor);
440
                        auxLegend.setEndColor(endColor);
441
                        auxLegend.setIntervalType(getCmbIntervalTypes().getSelectedIndex());
442
                        auxLegend.useDefaultSymbol(false);
443

    
444
                        int symbolType = layer.getShapeType();
445
                        int numSymbols = 0;
446

    
447
                        for (int k = 0; k < arrayIntervalos.length; k++) {
448
                                interval = arrayIntervalos[k];
449

    
450
                                ISymbol theSymbol = SymbologyFactory.createDefaultSymbolByShapeType(
451
                                                symbolType,
452
                                                new Color(r, g, b));
453
                                theSymbol.setDescription(NumberFormat.getInstance().format(interval.getMin()) +
454
                                                " - " +
455
                                                NumberFormat.getInstance().format(interval.getMax()));
456

    
457
                                //////////////////////////////////////
458
                                // CALCULAMOS UN COLOR APROPIADO
459
                                r = r + stepR;
460
                                g = g + stepG;
461
                                b = b + stepB;
462

    
463
                                /////////////////////////////////
464
                                auxLegend.addSymbol(interval, theSymbol);
465
                                System.out.println("addSymbol = " + interval +
466
                                                " theSymbol = " + theSymbol.getDescription());
467
                                numSymbols++;
468

    
469
                                if (numSymbols > 100) {
470
                                        int resp = JOptionPane.showConfirmDialog(this,
471
                                                        PluginServices.getText(this, "mas_de_100_simbolos"),
472
                                                        PluginServices.getText(this, "quiere_continuar"),
473
                                                        JOptionPane.YES_NO_OPTION,
474
                                                        JOptionPane.WARNING_MESSAGE);
475

    
476
                                        if ((resp == JOptionPane.NO_OPTION) ||
477
                                                        (resp == JOptionPane.DEFAULT_OPTION)) {
478
                                                return;
479
                                        }
480
                                }
481

    
482
                                // }
483
                        } // for
484

    
485
                        System.out.println("Num. Simbolos = " +
486
                                        auxLegend.getValues().length);
487
                        symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
488
                                        auxLegend.getValues(),auxLegend.getDescriptions());
489

    
490
                } catch (ReadDriverException e) {
491
                        NotificationManager.addError(PluginServices.getText(this, "could_not_get_shape_type"), e);
492
                } catch (LegendLayerException e) {
493
                        NotificationManager.addError(PluginServices.getText(this, "failed_computing_intervals"), e);
494
                }
495

    
496
                bDelAll.setEnabled(true);
497
                bDel.setEnabled(true);
498
        }
499

    
500
        protected FInterval[] calculateIntervals() throws LegendLayerException {
501
                int intervalCount = 1;
502
                // ensure the interval value is an integer greather than 0
503
                try {
504
                        intervalCount = (int) Double.
505
                        parseDouble(txtNumIntervals.getText());
506
                        if (intervalCount<1) {
507
                                throw new Exception();
508
                        }
509
                } catch (Exception e) {
510
                        JOptionPane.showMessageDialog(this,
511
                                        PluginServices.getText(this, "invalid_interval_count_value"));
512
                        return null;
513
                }
514

    
515
                try {
516

    
517
                        return auxLegend.calculateIntervals(
518
                                        //return theLegend.calculateIntervals(
519
                                        ((AlphanumericData) layer).getRecordset(),
520
                                        (String) cmbField.getSelectedItem(),
521
                                        intervalCount,
522
                                        layer.getShapeType()
523
                        );
524
                } catch (ReadDriverException e) {
525
                        return null;
526
                }
527
        }
528

    
529
        public void setData(FLayer layer, ILegend legend) {
530
                this.layer = (ClassifiableVectorial) layer;
531
                int shapeType = 0;
532
                try {
533
                        shapeType = this.layer.getShapeType();
534
                } catch (ReadDriverException e) {
535
                        NotificationManager.addError(PluginServices.getText(this, "generating_intervals"), e);
536
                }
537

    
538
                if (symbolTable != null)
539
                        pnlCenter.remove(symbolTable);
540

    
541

    
542
                getDefaultSymbolPrev(shapeType);
543

    
544
                symbolTable = new SymbolTable(this, SymbolTable.INTERVALS_TYPE, shapeType);
545
                pnlCenter.add(symbolTable);
546
                fillFieldNames();
547

    
548
                if (VectorialIntervalLegend.class.equals(legend.getClass())) {
549
                        try {
550
                                auxLegend = (VectorialIntervalLegend) legend.cloneLegend();
551
                        } catch (XMLException e) {
552
                                // TODO Auto-generated catch block
553
                                e.printStackTrace();
554
                        }
555
                        chkdefaultvalues.setSelected(auxLegend.isUseDefaultSymbol());
556
                        cmbField.getModel().setSelectedItem(auxLegend.getClassifyingFieldNames()[0]);
557
                        symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
558
                                        auxLegend.getValues(),auxLegend.getDescriptions());
559
                        colorChooserPanel.setColor(auxLegend.getStartColor());
560
                        colorChooserPanel1.setColor(auxLegend.getEndColor());
561
                        colorChooserPanel.repaint();
562
                        colorChooserPanel1.repaint();
563
                        if(auxLegend.isUseDefaultSymbol())
564
                                txtNumIntervals.setText(String.valueOf(auxLegend.getSymbols().length - 1));
565
                        else
566
                                txtNumIntervals.setText(String.valueOf(auxLegend.getSymbols().length));
567
                } else {
568
                        // Si la capa viene con otro tipo de leyenda, creamos
569
                        // una nueva del tipo que maneja este panel
570
                        auxLegend = new VectorialIntervalLegend();
571
                        auxLegend.setShapeType(shapeType);
572
                        auxLegend.useDefaultSymbol(false);
573
                }
574
                defaultSymbolPrev.setSymbol(auxLegend.getDefaultSymbol());
575
                cmbFieldType.setSelectedIndex(auxLegend.getIntervalType());
576
        }
577

    
578
        public void getDefaultSymbolPrev(int shapeType) {
579
                if(defaultSymbolPrev == null){
580
                        defaultSymbolPrev = new JSymbolPreviewButton(shapeType);
581
                        defaultSymbolPrev.setPreferredSize(new Dimension(110,20));
582
                        defaultSymbolPanel.add(defaultSymbolPrev,null);
583
                }
584
        }
585

    
586
        protected void fillFieldNames() {
587
                SelectableDataSource rs = null;
588
                ArrayList nomFields = null;
589

    
590
                try {
591
//                        rs = ((FLyrVect) layer).getSource().getRecordset();
592
                        rs = ((FLyrVect) layer).getRecordset();
593
                        logger.debug("rs.start()");
594
                        rs.start();
595

    
596
                        nomFields = new ArrayList();
597
                        int fieldCount=rs.getFieldCount();
598
                        int type;
599
                        for (int i = 0; i < fieldCount; i++) {
600
                                type = rs.getFieldType(i);
601

    
602
                                if (type == Types.NULL) {
603
                                        continue;
604
                                }
605

    
606
                                if ((type == Types.INTEGER) ||
607
                                                (type == Types.DOUBLE) ||
608
                                                (type == Types.FLOAT) ||
609
                                                (type == Types.BIGINT)) {
610
                                        nomFields.add(rs.getFieldAlias(i));
611
                                }
612
                        }
613

    
614
                        rs.stop();
615
                } catch (ReadDriverException e) {
616
                        NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
617
                }
618

    
619
                DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields.toArray());
620
                cmbField.setModel(cM);
621

    
622
                symbolTable.removeAllItems();
623
        }
624

    
625
        /**
626
         * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
627
         */
628
        public ILegend getLegend() {
629
                fillSymbolListFromTable();
630

    
631
                if(defaultSymbolPrev.getSymbol() != null)
632
                        auxLegend.setDefaultSymbol(defaultSymbolPrev.getSymbol());
633

    
634
                auxLegend.useDefaultSymbol(chkdefaultvalues.isSelected());
635

    
636
                try {
637
                        theLegend = (VectorialIntervalLegend) auxLegend.cloneLegend();
638
                } catch (XMLException e) {
639
                        // TODO Auto-generated catch block
640
                        e.printStackTrace();
641
                }
642
                return theLegend;
643
        }
644

    
645
        /**
646
         * A partir de los registros de la tabla, regenera el FRenderer. (No solo
647
         * el symbolList, si no tambi�n el arrayKeys y el defaultRenderer
648
         */
649

    
650
        protected void fillSymbolListFromTable() {
651
                ISymbol theSymbol;
652
                IInterval theInterval = null;
653

    
654
                // Borramos las anteriores listas:
655
                auxLegend.clear();
656

    
657
                String fieldName = (String) cmbField.getSelectedItem();
658
                auxLegend.setClassifyingFieldNames(new String[] {fieldName});
659

    
660
                auxLegend.useDefaultSymbol(chkdefaultvalues.isSelected());
661

    
662
                DataSource rs;
663
                try {
664

    
665
                        rs = ((AlphanumericData) layer).getRecordset();
666
                        rs.start();
667
                        auxLegend.setClassifyingFieldTypes(new int[] {rs.getFieldType(rs.getFieldIndexByName(fieldName))});
668
                        logger.debug("rs.start()");
669
                        rs.stop();
670

    
671
                } catch (ReadDriverException e) {
672
                        NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
673
                }
674

    
675

    
676

    
677
                for (int row = 0; row < symbolTable.getRowCount(); row++) {
678
                        if (!(symbolTable.getFieldValue(row, 1) instanceof FInterval)) {
679
                                theSymbol = (ISymbol) symbolTable.getFieldValue(row, 0);
680
                                theSymbol.setDescription((String) symbolTable.getFieldValue(
681
                                                row, 2));
682
                                auxLegend.addSymbol(new NullIntervalValue(), theSymbol);
683
                        } else {
684
                                theInterval = (IInterval) symbolTable.getFieldValue(row, 1);
685
                                theSymbol = (ISymbol) symbolTable.getFieldValue(row, 0);
686
                                theSymbol.setDescription((String) symbolTable.getFieldValue(
687
                                                row, 2));
688
                                auxLegend.addSymbol(theInterval, theSymbol);
689
                        }
690
                }
691

    
692
                if(chkdefaultvalues.isSelected()){
693
                        if(defaultSymbolPrev.getSymbol() != null){
694
                                String description = PluginServices.getText(this,"default");
695
                                defaultSymbolPrev.getSymbol().setDescription(description);
696
                                auxLegend.addSymbol(new NullIntervalValue(), defaultSymbolPrev.getSymbol());
697
                        }
698
                }
699
        }
700

    
701
        /**
702
         * This method initializes panelC
703
         *
704
         * @return javax.swing.JPanel
705
         */
706
        private JPanel getPnlCenter() {
707
                if (pnlCenter == null) {
708
                        pnlCenter = new JPanel();
709
                }
710

    
711
                return pnlCenter;
712
        }
713

    
714
        /**
715
         * Listener.
716
         *
717
         * @author Vicente Caballero Navarro
718
         */
719
        class MyListener implements ActionListener {
720

    
721
                public void actionPerformed(ActionEvent e) {
722
                        System.out.println("ActionEvent con " + e.getActionCommand());
723

    
724
                        //modificar el combobox de valor
725
                        if (e.getActionCommand() == "FIELD_SELECTED") {
726
                                fieldSelectedActionPerformed((JComboBox) e.getSource());
727
                        } else if (e.getActionCommand() == "INTERVAL_TYPE") {
728
                                JComboBox cb = (JComboBox) e.getSource();
729

    
730
                                //if ((theLegend != null) &&
731
                                if ((auxLegend != null) &&
732
                                                //    (cb.getSelectedIndex() != theLegend.getIntervalType())) {
733
                                                (cb.getSelectedIndex() != auxLegend.getIntervalType())) {
734
                                        //theLegend.setIntervalType(cb.getSelectedIndex());
735
                                        auxLegend.setIntervalType(cb.getSelectedIndex());
736
                                        symbolTable.removeAllItems();
737
                                }
738
                        }
739

    
740
                        //A�adir todos los elementos por valor
741
                        if (e.getActionCommand() == "ADD_ALL_VALUES") {
742
                                fillTableValues();
743
                        }
744

    
745
                        //A�adir un �nico elemento
746
                        if (e.getActionCommand() == "ADD_VALUE") {
747
                                try {
748
                                        symbolTable.addTableRecord(
749
                                                        SymbologyFactory.createDefaultSymbolByShapeType(layer.getShapeType()),
750
                                                        new FInterval(0, 0),"0 - 0");
751
                                } catch (ReadDriverException e1) {
752
                                        NotificationManager.addError(PluginServices.getText(this, "could_not_get_shape_type"), e1);
753
                                }
754

    
755
                        }
756

    
757
                        //Vacia la tabla
758
                        if (e.getActionCommand() == "REMOVE_ALL") {
759
                                symbolTable.removeAllItems();
760
                        }
761

    
762
                        //Quitar solo el elemento seleccionado
763
                        if (e.getActionCommand() == "REMOVE") {
764
                                symbolTable.removeSelectedRows();
765
                        }
766
                }
767
        }
768

    
769
        protected void fieldSelectedActionPerformed(JComboBox cb){
770
                String fieldName = (String) cb.getSelectedItem();
771
                System.out.println("Nombre del campo: " + fieldName);
772
                symbolTable.removeAllItems();
773

    
774
                //theLegend.setClassifyingFieldNames(new String[] {fieldName});
775
                auxLegend.setClassifyingFieldNames(new String[] {fieldName});
776
        }
777

    
778
        public String getDescription() {
779
                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") + ".";
780
        }
781

    
782
        public ImageIcon getIcon() {
783
                return new ImageIcon(this.getClass().getClassLoader().
784
                                getResource("images/Intervalos.png"));
785
        }
786

    
787
        public Class getParentClass() {
788
                return Quantities.class;
789
        }
790

    
791
        public String getTitle() {
792
                return PluginServices.getText(this,"Intervalos");
793
        }
794

    
795
        public JPanel getPanel() {
796
                return this;
797
        }
798

    
799
        public Class getLegendClass() {
800
                return VectorialIntervalLegend.class;
801
        }
802

    
803
        private boolean isNumericField(int fieldType) {
804
                switch (fieldType) {
805
                case Types.BIGINT:
806
                case Types.DECIMAL:
807
                case Types.DOUBLE:
808
                case Types.FLOAT:
809
                case Types.INTEGER:
810
                case Types.NUMERIC:
811
                case Types.REAL:
812
                case Types.SMALLINT:
813
                case Types.TINYINT:
814
                        return true;
815
                default:
816
                        return false;
817
                }
818

    
819
        }
820
        public boolean isSuitableFor(FLayer layer) {
821

    
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
}