Statistics
| Revision:

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

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.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 cmbIntervalType = 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 (cmbIntervalType == null) {
298
            cmbIntervalType = new JComboBox();
299
            cmbIntervalType.setActionCommand("INTERVAL_TYPE");
300
            cmbIntervalType.addActionListener(listener);
301
            cmbIntervalType.addItem(PluginServices.getText(this, "equal_intervals"));
302
            cmbIntervalType.addItem(PluginServices.getText(this,
303
                    "natural_intervals"));
304
            cmbIntervalType.addItem(PluginServices.getText(this,
305
                    "quantile_intervals"));
306
            cmbIntervalType.setVisible(true);
307
        }
308

    
309
        return cmbIntervalType;
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
                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
          symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
486
          auxLegend.getValues(), auxLegend.getDescriptions());
487

    
488
        } catch (DataException e) {
489
                NotificationManager.addError(PluginServices.getText(this, "could_not_get_shape_type"), e);
490
        } catch (LegendLayerException e) {
491
                NotificationManager.addError(PluginServices.getText(this, "failed_computing_intervals"), e);
492
                }
493

    
494
        bDelAll.setEnabled(true);
495
        bDel.setEnabled(true);
496
    }
497

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

    
513
            try {
514
            auxLegend.setIntervalType(getCmbIntervalTypes().getSelectedIndex());
515

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

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

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

    
541
            getDefaultSymbolPrev(shapeType);
542

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

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

564

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

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

    
617

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

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

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

    
635
            nomFields = new ArrayList<String>();
636

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

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

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

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

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

    
664
        DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields.toArray());
665
        cmbField.setModel(cM);
666

    
667
        symbolTable.removeAllItems();
668
    }
669

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

    
679
                auxLegend.useDefaultSymbol(chkdefaultvalues.isSelected());
680
                auxLegend.setIntervalType(cmbIntervalType.getSelectedIndex());
681
                
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
                ISymbol defsym = null;
703
        if (chkdefaultvalues.isSelected() && defaultSymbolPrev.getSymbol() != null) {
704
            defsym = (ISymbol) defaultSymbolPrev.getSymbol();
705
            String description = PluginServices.getText(this,"default");
706
            defsym.setDescription(description);
707
            auxLegend.setDefaultSymbol(defsym);
708
            auxLegend.useDefaultSymbol(true);
709
        } else {
710
            
711
            auxLegend.useDefaultSymbol(false);
712
        }
713

    
714
                FeatureStore rs;
715
                try {
716

    
717
                        rs = ((FLyrVect) layer).getFeatureStore();
718
                        FeatureType featureType=rs.getDefaultFeatureType();
719
                        auxLegend
720
                                        .setClassifyingFieldTypes(new int[] { new Integer(
721
                                                        featureType.getAttributeDescriptor(fieldName)
722
                                                                        .getType()) });
723

    
724
                } catch (DataException e) {
725
                        NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
726
                }
727

    
728

    
729

    
730
                for (int row = 0; row < symbolTable.getRowCount(); row++) {
731
                        if (symbolTable.getFieldValue(row, 1) instanceof FInterval) {
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
                
740
                if (auxLegend.isUseDefaultSymbol()) {
741
                    theSymbol = auxLegend.getDefaultSymbol();
742
                    auxLegend.addSymbol(theSymbol.getDescription(), theSymbol);
743
                }
744
                
745
        }
746

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

    
757
        return pnlCenter;
758
    }
759

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

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

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

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

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

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

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

    
809
            }
810

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

    
815
            }
816

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

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

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

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

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

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

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

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

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

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