Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / legend / gui / VectorialInterval.java @ 38638

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

    
90

    
91

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

    
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
            defaultSymbolPanel.add(getChkDefaultvalues(), null);
177
                        pnlGeneral.addComponent(defaultSymbolPanel);
178
        }
179
        return pnlGeneral;
180
    }
181

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

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

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

    
216
        return cmbField;
217
    }
218

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

    
230
        return txtNumIntervals;
231
    }
232

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

    
246
        return colorChooserPanel;
247
    }
248

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

    
262
        return colorChooserPanel1;
263
    }
264

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

    
288
                return chkdefaultvalues;
289
        }
290

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

    
309
        return cmbFieldType;
310
    }
311

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

    
327
        return panelS;
328
    }
329

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

    
344
        return bintervals;
345
    }
346

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

    
360
        return bInsert;
361
    }
362

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

    
376
        return bDelAll;
377
    }
378

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

    
392
        return bDel;
393
    }
394

    
395

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

    
403
        symbolTable.removeAllItems();
404

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

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

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

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

    
427
            Color endColor = colorChooserPanel1.getColor();
428

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

    
436
//            auxLegend = LegendFactory.createVectorialIntervalLegend(layer.getShapeType());
437
                        auxLegend = (IVectorialIntervalLegend) MapContextLocator
438
                                        .getMapContextManager().createLegend(
439
                                                        IVectorialIntervalLegend.LEGEND_NAME);
440
            auxLegend.setShapeType(layer.getShapeType());
441
            auxLegend.useDefaultSymbol(false);
442
            auxLegend.setStartColor(startColor);
443
            auxLegend.setEndColor(endColor);
444

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

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

    
451
                                ISymbol theSymbol =
452
                                                mapContextManager.getSymbolManager().createSymbol(
453
                                symbolType,
454
                                new Color(r, g, b));
455
                theSymbol.setDescription(NumberFormat.getInstance().format(interval.getMin()) +
456
                    " - " +
457
                    NumberFormat.getInstance().format(interval.getMax()));
458

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

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

    
471
                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

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

    
484
                // }
485
            } // for
486

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

    
492
        } catch (DataException 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

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

    
502
    protected IInterval[] 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

    
517
            try {
518
            auxLegend.setIntervalType(getCmbIntervalTypes().getSelectedIndex());
519

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

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

    
541
            if (symbolTable != null) {
542
                        pnlCenter.remove(symbolTable);
543
                }
544

    
545
            getDefaultSymbolPrev(shapeType);
546

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

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

568

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

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

    
621

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

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

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

    
639
            nomFields = new ArrayList<String>();
640

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

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

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

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

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

    
668
        DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields.toArray());
669
        cmbField.setModel(cM);
670

    
671
        symbolTable.removeAllItems();
672
    }
673

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

    
683
                auxLegend.useDefaultSymbol(chkdefaultvalues.isSelected());
684

    
685
                theLegend = (VectorialIntervalLegend) auxLegend.cloneLegend();
686
                return theLegend;
687
        }
688

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

    
694
    private void fillSymbolListFromTable() {
695
                ISymbol theSymbol;
696
                IInterval theInterval = null;
697

    
698
                // Borramos las anteriores listas:
699
                auxLegend.clear();
700

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

    
704
                auxLegend.useDefaultSymbol(chkdefaultvalues.isSelected());
705

    
706
                FeatureStore rs;
707
                try {
708

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

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

    
723

    
724

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

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

    
758
        return pnlCenter;
759
    }
760

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

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

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

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

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

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

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

    
810
            }
811

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

    
816
            }
817

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

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

    
829
    public ImageIcon getIcon() {
830
        return IconThemeHelper.getImageIcon(
831
            "legend-overview-vectorial-interval");
832
    }
833

    
834
    public Class getParentClass() {
835
        return Quantities.class;
836
    }
837

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

    
842
    public JPanel getPanel() {
843
        return this;
844
    }
845

    
846
    public Class getLegendClass() {
847
        return VectorialIntervalLegend.class;
848
    }
849

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

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

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