Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / legend / gui / VectorialInterval.java @ 33740

History | View | Annotate | Download (28.7 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 org.gvsig.app.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.text.NumberFormat;
51
import java.util.ArrayList;
52
import java.util.Iterator;
53
import java.util.List;
54

    
55
import javax.swing.BorderFactory;
56
import javax.swing.DefaultComboBoxModel;
57
import javax.swing.ImageIcon;
58
import javax.swing.JCheckBox;
59
import javax.swing.JComboBox;
60
import javax.swing.JOptionPane;
61
import javax.swing.JPanel;
62
import javax.swing.JTextField;
63

    
64
import org.gvsig.andami.PluginServices;
65
import org.gvsig.andami.messages.NotificationManager;
66
import org.gvsig.app.gui.panels.ColorChooserPanel;
67
import org.gvsig.fmap.dal.DataTypes;
68
import org.gvsig.fmap.dal.exception.DataException;
69
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
70
import org.gvsig.fmap.dal.feature.FeatureStore;
71
import org.gvsig.fmap.dal.feature.FeatureType;
72
import org.gvsig.fmap.mapcontext.MapContextLocator;
73
import org.gvsig.fmap.mapcontext.MapContextManager;
74
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException;
75
import org.gvsig.fmap.mapcontext.layers.FLayer;
76
import org.gvsig.fmap.mapcontext.layers.operations.ClassifiableVectorial;
77
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
78
import org.gvsig.fmap.mapcontext.rendering.legend.IInterval;
79
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
80
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialIntervalLegend;
81
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
82
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
83
import org.gvsig.gui.beans.swing.JButton;
84
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.FInterval;
85
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.VectorialIntervalLegend;
86
import org.slf4j.Logger;
87
import org.slf4j.LoggerFactory;
88

    
89

    
90

    
91
/**
92
 * DOCUMENT ME!
93
 *
94
 * @author Vicente Caballero Navarro
95
 */
96
public class VectorialInterval extends JPanel implements ILegendPanel{
97
    private static final Logger logger = LoggerFactory
98
            .getLogger(VectorialInterval.class);
99
    private GridBagLayoutPanel pnlGeneral = null;
100
    protected JComboBox cmbField = null;
101
    protected JTextField txtNumIntervals = null;
102
    private ColorChooserPanel colorChooserPanel = null;
103
    private ColorChooserPanel colorChooserPanel1 = null;
104
    protected JCheckBox chkdefaultvalues = null;
105
    protected JComboBox cmbFieldType = null;
106
    private JPanel panelS = null;
107
    private JButton bintervals = null;
108
    private JButton bInsert = null;
109
    protected JButton bDelAll = null;
110
    protected JButton bDel = null;
111
    private int count = 0;
112
    protected ClassifiableVectorial layer;
113
    protected VectorialIntervalLegend theLegend;
114
    protected IVectorialIntervalLegend auxLegend = null;
115
    protected SymbolTable symbolTable;
116
    private MyListener listener = new MyListener();
117
    protected JPanel pnlCenter = null;
118
    protected JPanel optionPanel;
119
    private JPanel pnlNorth;
120
        protected JSymbolPreviewButton defaultSymbolPrev;
121
        private GridBagLayoutPanel defaultSymbolPanel = new GridBagLayoutPanel();
122
        
123
        private MapContextManager mapContextManager = MapContextLocator
124
                        .getMapContextManager();
125

    
126
    /**
127
     * This is the default constructor
128
     */
129
    public VectorialInterval() {
130
        super();
131
        initialize();
132
    }
133

    
134
    /**
135
     * This method initializes this
136
     */
137
    private void initialize() {
138
        this.setLayout(new BorderLayout());
139
        this.setSize(700, 350);
140
        this.add(getPnlNorth(), BorderLayout.NORTH);
141

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

    
147
    private JPanel getPnlNorth() {
148
        if (pnlNorth == null) {
149
            pnlNorth = new JPanel(new GridLayout(1, 2));
150
            pnlNorth.add(getGeneralPanel());
151
        }
152
        return pnlNorth;
153
    }
154

    
155
    /**
156
     * This method initializes panelN
157
     *
158
     * @return javax.swing.JPanel
159
     */
160
    private JPanel getGeneralPanel() {
161
        if (pnlGeneral == null) {
162
            pnlGeneral = new GridBagLayoutPanel();
163
            pnlGeneral.setBorder(BorderFactory.
164
                    createTitledBorder(null,
165
                            PluginServices.getText(this, "fields")));
166
            pnlGeneral.addComponent(PluginServices.getText(this, "Campo_de_clasificacion"),
167
                    getCmbFields());
168
            pnlGeneral.addComponent(PluginServices.getText(this, "tipo_de_intervalo"),
169
                    getCmbIntervalTypes());
170

    
171
            JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
172
            aux.add(getTxtNumIntervals());
173
            pnlGeneral.addComponent(PluginServices.getText(this, "No_de_intervalos"),
174
                    aux);
175
            defaultSymbolPanel.add(getChkDefaultvalues(), null);
176
                        pnlGeneral.addComponent(defaultSymbolPanel);
177
        }
178
        return pnlGeneral;
179
    }
180

    
181
    public JPanel getOptionPanel() {
182
        if (optionPanel == null) {
183
            optionPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
184
            optionPanel.setBorder(BorderFactory.
185
                    createTitledBorder(null,
186
                            PluginServices.getText(this, "color_ramp")));
187

    
188
            GridBagLayoutPanel aux = new GridBagLayoutPanel();
189
            aux.addComponent(PluginServices.getText(this, "Color_final"),
190
                    getColorChooserPanel());
191
            aux.addComponent(PluginServices.getText(this, "Color_inicio"),
192
                    getColorChooserPanel1());
193
            optionPanel.add(aux);
194
        }
195
        return optionPanel;
196
    }
197

    
198
    private void setOptionPanel(JPanel p) {
199
        getPnlNorth().remove(getOptionPanel());
200
        getPnlNorth().add(p, BorderLayout.NORTH);
201
    }
202
    /**
203
     * This method initializes jComboBox
204
     *
205
     * @return javax.swing.JComboBox
206
     */
207
    private JComboBox getCmbFields() {
208
        if (cmbField == null) {
209
            cmbField = new JComboBox();
210
            cmbField.setActionCommand("FIELD_SELECTED");
211
            cmbField.addActionListener(listener);
212
            cmbField.setVisible(true);
213
        }
214

    
215
        return cmbField;
216
    }
217

    
218
    /**
219
     * This method initializes txtNumIntervals
220
     *
221
     * @return javax.swing.JTextField
222
     */
223
    private JTextField getTxtNumIntervals() {
224
        if (txtNumIntervals == null) {
225
            txtNumIntervals = new JTextField(5);
226
            txtNumIntervals.setText("5");
227
        }
228

    
229
        return txtNumIntervals;
230
    }
231

    
232
    /**
233
     * This method initializes colorChooserPanel
234
     *
235
     * @return com.iver.cit.gvsig.gui.Panels.ColorChooserPanel
236
     */
237
    private ColorChooserPanel getColorChooserPanel() {
238
        if (colorChooserPanel == null) {
239
            colorChooserPanel = new ColorChooserPanel();
240
            colorChooserPanel.setBounds(new java.awt.Rectangle(108, 49, 54, 20));
241
            colorChooserPanel.setAlpha(255);
242
            colorChooserPanel.setColor(Color.red);
243
        }
244

    
245
        return colorChooserPanel;
246
    }
247

    
248
    /**
249
     * This method initializes colorChooserPanel1
250
     *
251
     * @return com.iver.cit.gvsig.gui.Panels.ColorChooserPanel
252
     */
253
    private ColorChooserPanel getColorChooserPanel1() {
254
        if (colorChooserPanel1 == null) {
255
            colorChooserPanel1 = new ColorChooserPanel();
256
            colorChooserPanel1.setBounds(new java.awt.Rectangle(251, 49, 54, 20));
257
            colorChooserPanel1.setAlpha(255);
258
            colorChooserPanel1.setColor(Color.blue);
259
        }
260

    
261
        return colorChooserPanel1;
262
    }
263

    
264
    /**
265
     * This method initializes chkdefaultvalues
266
     *
267
     * @return javax.swing.JCheckBox
268
     */
269
    protected JCheckBox getChkDefaultvalues() {
270
                if (chkdefaultvalues == null) {
271
                        chkdefaultvalues = new JCheckBox();
272
                        chkdefaultvalues.setText(PluginServices.getText(this,
273
                        "resto_valores")+": ");
274
                        chkdefaultvalues.setBounds(new java.awt.Rectangle(342, 26, 141, 20));
275
                        chkdefaultvalues.setSelected(false);
276
                        chkdefaultvalues.addActionListener(new java.awt.event.ActionListener() {
277
                                public void actionPerformed(java.awt.event.ActionEvent e) {
278
                                        if (chkdefaultvalues.isSelected()) {
279
                                                auxLegend.useDefaultSymbol(true);
280
                                        } else {
281
                                                auxLegend.useDefaultSymbol(false);
282
                                        }
283
                                }
284
                        });
285
                }
286

    
287
                return chkdefaultvalues;
288
        }
289

    
290
    /**
291
     * This method initializes jComboBox1
292
     *
293
     * @return javax.swing.JComboBox
294
     */
295
    private JComboBox getCmbIntervalTypes() {
296
        if (cmbFieldType == null) {
297
            cmbFieldType = new JComboBox();
298
            cmbFieldType.setActionCommand("INTERVAL_TYPE");
299
            cmbFieldType.addActionListener(listener);
300
            cmbFieldType.addItem(PluginServices.getText(this, "equal_intervals"));
301
            cmbFieldType.addItem(PluginServices.getText(this,
302
                    "natural_intervals"));
303
            cmbFieldType.addItem(PluginServices.getText(this,
304
                    "quantile_intervals"));
305
            cmbFieldType.setVisible(true);
306
        }
307

    
308
        return cmbFieldType;
309
    }
310

    
311
    /**
312
     * This method initializes panelS
313
     *
314
     * @return javax.swing.JPanel
315
     */
316
    protected JPanel getPnlButtons() {
317
        if (panelS == null) {
318
            panelS = new JPanel();
319
            panelS.setPreferredSize(new java.awt.Dimension(417, 32));
320
            panelS.add(getBintervals(), null);
321
            panelS.add(getBInsert(), null);
322
            panelS.add(getBDelAll(), null);
323
            panelS.add(getBDel(), null);
324
        }
325

    
326
        return panelS;
327
    }
328

    
329
    /**
330
     * This method initializes bintervals
331
     *
332
     * @return javax.swing.JButton
333
     */
334
    private JButton getBintervals() {
335
        if (bintervals == null) {
336
            bintervals = new JButton();
337
            bintervals.setActionCommand("ADD_ALL_VALUES");
338
            bintervals.addActionListener(listener);
339
            bintervals.setText(PluginServices.getText(this,
340
                    "Calcular_intervalos"));
341
        }
342

    
343
        return bintervals;
344
    }
345

    
346
    /**
347
     * This method initializes bInsert
348
     *
349
     * @return javax.swing.JButton
350
     */
351
    private JButton getBInsert() {
352
        if (bInsert == null) {
353
            bInsert = new JButton();
354
            bInsert.setActionCommand("ADD_VALUE");
355
            bInsert.addActionListener(listener);
356
            bInsert.setText(PluginServices.getText(this, "Anadir"));
357
        }
358

    
359
        return bInsert;
360
    }
361

    
362
    /**
363
     * This method initializes bDelAll
364
     *
365
     * @return javax.swing.JButton
366
     */
367
    private JButton getBDelAll() {
368
        if (bDelAll == null) {
369
            bDelAll = new JButton();
370
            bDelAll.setActionCommand("REMOVE_ALL");
371
            bDelAll.addActionListener(listener);
372
            bDelAll.setText(PluginServices.getText(this, "Quitar_todos"));
373
        }
374

    
375
        return bDelAll;
376
    }
377

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

    
391
        return bDel;
392
    }
393

    
394

    
395
    /**
396
     * Damos una primera pasada para saber los l�mites inferior y superior y
397
     * rellenar un array con los valores. Luego dividimos ese array en
398
     * intervalos.
399
     */
400
    protected void fillTableValues() {
401

    
402
        symbolTable.removeAllItems();
403

    
404
        try {
405
            IInterval[] arrayIntervalos = calculateIntervals();
406
            if (arrayIntervalos == null) {
407
                                return;
408
                        }
409

    
410
            IInterval interval;
411
            NumberFormat.getInstance().setMaximumFractionDigits(2);
412
            //theLegend.clear();
413
            auxLegend.clear();
414

    
415
            int r;
416
            int g;
417
            int b;
418
            int stepR;
419
            int stepG;
420
            int stepB;
421

    
422
            // Cogemos el tipo de gradaci�n de colores que quiere el usuario y
423
            // Creamos el primer y �ltimo color.
424
            Color startColor = colorChooserPanel.getColor();
425

    
426
            Color endColor = colorChooserPanel1.getColor();
427

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

    
435
//            auxLegend = LegendFactory.createVectorialIntervalLegend(layer.getShapeType());
436
                        auxLegend = (IVectorialIntervalLegend) MapContextLocator
437
                                        .getMapContextManager().createLegend(
438
                                                        IVectorialIntervalLegend.LEGEND_NAME);
439
            auxLegend.setShapeType(layer.getShapeType());
440
            auxLegend.useDefaultSymbol(false);
441
            auxLegend.setStartColor(startColor);
442
            auxLegend.setEndColor(endColor);
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 =
451
                                                mapContextManager.getSymbolManager().createSymbol(
452
                                symbolType,
453
                                new Color(r, g, b));
454
                theSymbol.setDescription(NumberFormat.getInstance().format(interval.getMin()) +
455
                    " - " +
456
                    NumberFormat.getInstance().format(interval.getMax()));
457

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

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

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

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

    
483
                // }
484
            } // for
485

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

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

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

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

    
516
            try {
517

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

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

    
539
            if (symbolTable != null) {
540
                        pnlCenter.remove(symbolTable);
541
                }
542

    
543
            getDefaultSymbolPrev(shapeType);
544

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

    
549
/*        if (legend instanceof VectorialIntervalLegend) {
550
            theLegend = (VectorialIntervalLegend) legend;
551
            getChkDefaultvalues().setSelected(theLegend.isUseDefaultSymbol());
552
            cmbField.getModel().setSelectedItem(theLegend.getClassifyingFieldNames()[0]);
553
            symbolTable.fillTableFromSymbolList(theLegend.getSymbols(),
554
                theLegend.getValues(), theLegend.getDescriptions());
555
            colorChooserPanel.setColor(theLegend.getStartColor());
556
            colorChooserPanel1.setColor(theLegend.getEndColor());
557
            colorChooserPanel.repaint();
558
            colorChooserPanel1.repaint();
559
        } else {
560
                        // Si la capa viene con otro tipo de leyenda, creamos
561
                        // una nueva del tipo que maneja este panel
562
                        theLegend = new VectorialIntervalLegend();
563
                        theLegend.setShapeType(shapeType);
564
        }
565

566

567
        cmbFieldType.setSelectedIndex(theLegend.getIntervalType());
568
*/
569
                if (VectorialIntervalLegend.class.equals(legend.getClass())) {
570
                        auxLegend = (VectorialIntervalLegend) legend.cloneLegend();
571
                        chkdefaultvalues.setSelected(auxLegend.isUseDefaultSymbol());
572
                        cmbField.getModel().setSelectedItem(auxLegend.getClassifyingFieldNames()[0]);
573
                        
574
                        //Patch to fix an array overflow 
575
            Object[] auxValues = auxLegend.getValues();
576
            List<ISymbol> symbols = new ArrayList();
577
            List<Object> values = new ArrayList();
578
            List<String> descriptions = new ArrayList();
579
            
580
            for( int i=0; i<auxValues.length; i++){
581
                Object key = auxValues[i];
582
                if (key!=null){
583
                    IInterval auxInterval = null;
584
                    if (key instanceof IInterval){
585
                        auxInterval = (IInterval) key;
586
                        ISymbol symbol = auxLegend.getSymbolByInterval(auxInterval);
587
                        symbols.add(symbol);
588
                        values.add(auxValues[i]);
589
                        descriptions.add(symbol.getDescription());
590
                    }
591
                }
592
            }
593
            symbolTable.fillTableFromSymbolList(
594
                symbols.toArray(new ISymbol[0]),
595
                values.toArray(),
596
                descriptions.toArray(new String[0]));
597
            //End Patch
598

    
599
                        colorChooserPanel.setColor(auxLegend.getStartColor());
600
                        colorChooserPanel1.setColor(auxLegend.getEndColor());
601
                        colorChooserPanel.repaint();
602
                        colorChooserPanel1.repaint();
603
                        if(auxLegend.isUseDefaultSymbol()) {
604
                                txtNumIntervals.setText(String.valueOf(auxLegend.getSymbols().length - 1));
605
                        } else {
606
                                txtNumIntervals.setText(String.valueOf(auxLegend.getSymbols().length));
607
                        }
608
                } else {
609
                        // Si la capa viene con otro tipo de leyenda, creamos
610
                        // una nueva del tipo que maneja este panel
611
                        auxLegend = new VectorialIntervalLegend();
612
                        auxLegend.setShapeType(shapeType);
613
                        auxLegend.useDefaultSymbol(false);
614
                }
615
                defaultSymbolPrev.setSymbol(auxLegend.getDefaultSymbol());
616
                cmbFieldType.setSelectedIndex(auxLegend.getIntervalType());
617
    }
618

    
619

    
620
        public void getDefaultSymbolPrev(int shapeType) {
621
                if(defaultSymbolPrev == null){
622
                defaultSymbolPrev = new JSymbolPreviewButton(shapeType);
623
                defaultSymbolPrev.setPreferredSize(new Dimension(110,20));
624
                defaultSymbolPanel.add(defaultSymbolPrev,null);
625
                }
626
        }
627

    
628
    protected void fillFieldNames() {
629
        FeatureStore rs = null;
630
        ArrayList<String> nomFields = null;
631

    
632
        try {
633
            rs = ((FLyrVect) layer).getFeatureStore();
634
//            logger.debug("rs.start()");
635
//            rs.start();
636

    
637
            nomFields = new ArrayList<String>();
638

    
639
            int type;
640
            Iterator<FeatureAttributeDescriptor> iterator=rs.getDefaultFeatureType().iterator();
641
            while (iterator.hasNext()) {
642
                                FeatureAttributeDescriptor descriptor = iterator.next();
643

    
644
//                        }
645
//            for (int i = 0; i < rs.getFieldCount(); i++) {
646
                type = descriptor.getType();//rs.getFieldType(i);
647

    
648
//                if (type.equals(FeatureAttributeDescriptor.) == Types.NULL) {
649
//                    continue;
650
//                }
651

    
652
                if (type == DataTypes.INT ||
653
                                type == DataTypes.DOUBLE ||
654
                                type == DataTypes.FLOAT ||
655
                                type  == DataTypes.LONG)
656
                {
657
                        nomFields.add(descriptor.getName());
658
                }
659
            }
660

    
661
//            rs.stop();
662
        } catch (DataException e) {
663
                NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
664
        }
665

    
666
        DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields.toArray());
667
        cmbField.setModel(cM);
668

    
669
        symbolTable.removeAllItems();
670
    }
671

    
672
    /**
673
     * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
674
     */
675
     public ILegend getLegend() {
676
                fillSymbolListFromTable();
677
                if(defaultSymbolPrev.getSymbol() != null) {
678
                        auxLegend.setDefaultSymbol(defaultSymbolPrev.getSymbol());
679
                }
680

    
681
                auxLegend.useDefaultSymbol(chkdefaultvalues.isSelected());
682

    
683
                theLegend = (VectorialIntervalLegend) auxLegend.cloneLegend();
684
                return theLegend;
685
        }
686

    
687
    /**
688
     * A partir de los registros de la tabla, regenera el FRenderer. (No solo
689
     * el symbolList, si no tambi�n el arrayKeys y el defaultRenderer
690
     */
691

    
692
    private void fillSymbolListFromTable() {
693
                ISymbol theSymbol;
694
                IInterval theInterval = null;
695

    
696
                // Borramos las anteriores listas:
697
                auxLegend.clear();
698

    
699
                String fieldName = (String) cmbField.getSelectedItem();
700
                auxLegend.setClassifyingFieldNames(new String[] {fieldName});
701

    
702
                auxLegend.useDefaultSymbol(chkdefaultvalues.isSelected());
703

    
704
                FeatureStore rs;
705
                try {
706

    
707
                        rs = ((FLyrVect) layer).getFeatureStore();
708
//                        rs.start();
709
                        FeatureType featureType=rs.getDefaultFeatureType();
710
                        auxLegend
711
                                        .setClassifyingFieldTypes(new int[] { new Integer(
712
                                                        featureType.getAttributeDescriptor(fieldName)
713
                                                                        .getType()) });
714
                        logger.debug("rs.start()");
715
//                        rs.stop();
716

    
717
                } catch (DataException e) {
718
                        NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
719
                }
720

    
721

    
722

    
723
                for (int row = 0; row < symbolTable.getRowCount(); row++) {
724
                        if (!(symbolTable.getFieldValue(row, 1) instanceof FInterval)) {
725
                                theSymbol = (ISymbol) symbolTable.getFieldValue(row, 0);
726
                                theSymbol.setDescription((String) symbolTable.getFieldValue(
727
                                                row, 2));
728
                                auxLegend.addSymbol(null, theSymbol);
729
                        } else {
730
                                theInterval = (IInterval) symbolTable.getFieldValue(row, 1);
731
                                theSymbol = (ISymbol) symbolTable.getFieldValue(row, 0);
732
                                theSymbol.setDescription((String) symbolTable.getFieldValue(
733
                                                row, 2));
734
                                auxLegend.addSymbol(theInterval, theSymbol);
735
                        }
736
                }
737
                if(chkdefaultvalues.isSelected()){
738
                        if(defaultSymbolPrev.getSymbol() != null){
739
                                String description = PluginServices.getText(this,"default");
740
                                defaultSymbolPrev.getSymbol().setDescription(description);
741
                                auxLegend.addSymbol(null, defaultSymbolPrev.getSymbol());
742
                        }
743
                }
744
        }
745

    
746
    /**
747
     * This method initializes panelC
748
     *
749
     * @return javax.swing.JPanel
750
     */
751
    private JPanel getPnlCenter() {
752
        if (pnlCenter == null) {
753
            pnlCenter = new JPanel();
754
        }
755

    
756
        return pnlCenter;
757
    }
758

    
759
    /**
760
     * Listener.
761
     *
762
     * @author Vicente Caballero Navarro
763
     */
764
    class MyListener implements ActionListener {
765

    
766
       public void actionPerformed(ActionEvent e) {
767
            System.out.println("ActionEvent con " + e.getActionCommand());
768

    
769
            //modificar el combobox de valor
770
            if (e.getActionCommand() == "FIELD_SELECTED") {
771
                JComboBox cb = (JComboBox) e.getSource();
772
                String fieldName = (String) cb.getSelectedItem();
773
                System.out.println("Nombre del campo: " + fieldName);
774
                symbolTable.removeAllItems();
775

    
776
                //theLegend.setClassifyingFieldNames(new String[] {fieldName});
777
                auxLegend.setClassifyingFieldNames(new String[] {fieldName});
778
            } else if (e.getActionCommand() == "INTERVAL_TYPE") {
779
                JComboBox cb = (JComboBox) e.getSource();
780

    
781
                //if ((theLegend != null) &&
782
                if ((auxLegend != null) &&
783
                    //    (cb.getSelectedIndex() != theLegend.getIntervalType())) {
784
                        (cb.getSelectedIndex() != auxLegend.getIntervalType())) {
785
                    //theLegend.setIntervalType(cb.getSelectedIndex());
786
                    auxLegend.setIntervalType(cb.getSelectedIndex());
787
                    symbolTable.removeAllItems();
788
                }
789
            }
790

    
791
            //A�adir todos los elementos por valor
792
            if (e.getActionCommand() == "ADD_ALL_VALUES") {
793
                fillTableValues();
794
            }
795

    
796
            //A�adir un �nico elemento
797
            if (e.getActionCommand() == "ADD_VALUE") {
798
                try {
799
                    symbolTable.addTableRecord(
800
                                                        mapContextManager.getSymbolManager().createSymbol(
801
                                                                        layer.getShapeType()),
802
                                    new FInterval(0, 0),
803
                                    "0 - 0");
804
                } catch (DataException e1) {
805
                        NotificationManager.addError(PluginServices.getText(this, "could_not_get_shape_type"), e1);
806
                }
807

    
808
            }
809

    
810
            //Vacia la tabla
811
            if (e.getActionCommand() == "REMOVE_ALL") {
812
                symbolTable.removeAllItems();
813

    
814
            }
815

    
816
            //Quitar solo el elemento seleccionado
817
            if (e.getActionCommand() == "REMOVE") {
818
                symbolTable.removeSelectedRows();
819
            }
820
        }
821
    }
822

    
823
    public String getDescription() {
824
        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") + ".";
825
    }
826

    
827
    public ImageIcon getIcon() {
828
        return new ImageIcon(this.getClass().getClassLoader().
829
                                getResource("images/Intervalos.png"));
830
    }
831

    
832
    public Class getParentClass() {
833
        return Quantities.class;
834
    }
835

    
836
    public String getTitle() {
837
        return PluginServices.getText(this,"Intervalos");
838
    }
839

    
840
    public JPanel getPanel() {
841
        return this;
842
    }
843

    
844
    public Class getLegendClass() {
845
        return VectorialIntervalLegend.class;
846
    }
847

    
848
    private boolean isNumericField(int fieldType) {
849
                if (fieldType == DataTypes.DOUBLE || fieldType == DataTypes.FLOAT
850
                                || fieldType == DataTypes.LONG || fieldType == DataTypes.INT) {
851
                        return true;
852
                }else{
853
                        return false;
854
                }
855
//            switch (fieldType) {
856
//                case Types.BIGINT:
857
//                case Types.DECIMAL:
858
//                case Types.DOUBLE:
859
//                case Types.FLOAT:
860
//                case Types.INTEGER:
861
//                case Types.NUMERIC:
862
//                case Types.REAL:
863
//                case Types.SMALLINT:
864
//                case Types.TINYINT:
865
//                        return true;
866
//                default:
867
//                        return false;
868
//                }
869

    
870
        }
871
        public boolean isSuitableFor(FLayer layer) {
872
                if (layer instanceof FLyrVect) {
873
                        FeatureStore sds;
874
                        try {
875
                                sds = ((FLyrVect) layer).getFeatureStore();
876
                                FeatureType featureType=sds.getDefaultFeatureType();
877
                                Iterator iterator=featureType.iterator();
878
                                while (iterator.hasNext()) {
879
                                        FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator.next();
880

    
881
//                                }
882
//                                String[] fNames = sds.getFieldNames();
883
//                                for (int i = 0; i < fNames.length; i++) {
884
                                        if (descriptor.getDataType().isNumeric() ) {
885
                                                return true;
886
                                        }
887
                                }
888
                        } catch (DataException e) {
889
                                return false;
890
                        }
891
                }
892
                return false;
893
        }
894
}