Statistics
| Revision:

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

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.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_inicio"),
190
                    getColorChooserPanel());
191
            aux.addComponent(PluginServices.getText(this, "Color_final"),
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
            auxLegend.setIntervalType(getCmbIntervalTypes().getSelectedIndex());
518

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

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

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

    
544
            getDefaultSymbolPrev(shapeType);
545

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

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

567

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

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

    
620

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

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

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

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

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

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

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

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

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

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

    
670
        symbolTable.removeAllItems();
671
    }
672

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

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

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

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

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

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

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

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

    
705
                FeatureStore rs;
706
                try {
707

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

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

    
722

    
723

    
724
                for (int row = 0; row < symbolTable.getRowCount(); row++) {
725
                        if (!(symbolTable.getFieldValue(row, 1) instanceof FInterval)) {
726
                                theSymbol = (ISymbol) symbolTable.getFieldValue(row, 0);
727
                                theSymbol.setDescription((String) symbolTable.getFieldValue(
728
                                                row, 2));
729
                                auxLegend.addSymbol(null, theSymbol);
730
                        } else {
731
                                theInterval = (IInterval) symbolTable.getFieldValue(row, 1);
732
                                theSymbol = (ISymbol) symbolTable.getFieldValue(row, 0);
733
                                theSymbol.setDescription((String) symbolTable.getFieldValue(
734
                                                row, 2));
735
                                auxLegend.addSymbol(theInterval, theSymbol);
736
                        }
737
                }
738
                if(chkdefaultvalues.isSelected()){
739
                        if(defaultSymbolPrev.getSymbol() != null){
740
                                String description = PluginServices.getText(this,"default");
741
                                defaultSymbolPrev.getSymbol().setDescription(description);
742
                                auxLegend.addSymbol(null, defaultSymbolPrev.getSymbol());
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 new ImageIcon(this.getClass().getClassLoader().
830
                                getResource("images/Intervalos.png"));
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
}