Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / legend / gui / VectorialInterval.java @ 42250

History | View | Annotate | Download (29.4 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.project.documents.view.legend.gui;
25

    
26
import java.awt.*;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.text.NumberFormat;
30
import java.util.ArrayList;
31
import java.util.Iterator;
32
import java.util.List;
33

    
34
import javax.swing.BorderFactory;
35
import javax.swing.DefaultComboBoxModel;
36
import javax.swing.ImageIcon;
37
import javax.swing.JCheckBox;
38
import javax.swing.JComboBox;
39
import javax.swing.JOptionPane;
40
import javax.swing.JPanel;
41
import javax.swing.JTextField;
42
import javax.swing.border.EmptyBorder;
43

    
44
import org.gvsig.andami.IconThemeHelper;
45
import org.gvsig.andami.PluginServices;
46
import org.gvsig.andami.messages.NotificationManager;
47
import org.gvsig.app.gui.panels.ColorChooserPanel;
48
import org.gvsig.fmap.dal.DataTypes;
49
import org.gvsig.fmap.dal.exception.DataException;
50
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
51
import org.gvsig.fmap.dal.feature.FeatureStore;
52
import org.gvsig.fmap.dal.feature.FeatureType;
53
import org.gvsig.fmap.mapcontext.MapContextLocator;
54
import org.gvsig.fmap.mapcontext.MapContextManager;
55
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException;
56
import org.gvsig.fmap.mapcontext.layers.FLayer;
57
import org.gvsig.fmap.mapcontext.layers.operations.ClassifiableVectorial;
58
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
59
import org.gvsig.fmap.mapcontext.rendering.legend.IInterval;
60
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
61
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialIntervalLegend;
62
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
63
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
64
import org.gvsig.gui.beans.swing.JButton;
65
import org.gvsig.symbology.SymbologyLocator;
66
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.FInterval;
67
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.VectorialIntervalLegend;
68
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.IMultiShapeSymbol;
69
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
70

    
71
import org.slf4j.Logger;
72
import org.slf4j.LoggerFactory;
73

    
74

    
75

    
76
/**
77
 * DOCUMENT ME!
78
 *
79
 * @author Vicente Caballero Navarro
80
 */
81
public class VectorialInterval extends JPanel implements ILegendPanel{
82
    private static final Logger logger = LoggerFactory
83
            .getLogger(VectorialInterval.class);
84
    private GridBagLayoutPanel pnlGeneral = null;
85
    protected JComboBox cmbField = null;
86
    protected JTextField txtNumIntervals = null;
87
    private ColorChooserPanel colorChooserPanel = null;
88
    private ColorChooserPanel colorChooserPanel1 = null;
89
    protected JCheckBox chkdefaultvalues = null;
90
    protected JComboBox cmbIntervalType = null;
91
    private JPanel panelS = null;
92
    private JButton bintervals = null;
93
    private JButton bInsert = null;
94
    protected JButton bDelAll = null;
95
    protected JButton bDel = null;
96
    private int count = 0;
97
    protected ClassifiableVectorial layer;
98
    protected VectorialIntervalLegend theLegend;
99
    protected IVectorialIntervalLegend auxLegend = null;
100
    protected SymbolTable symbolTable;
101
    private MyListener listener = new MyListener();
102
    protected JPanel pnlCenter = null;
103
    protected JPanel optionPanel;
104
    private JPanel pnlNorth;
105
        protected JSymbolPreviewButton defaultSymbolPrev;
106
        private GridBagLayoutPanel defaultSymbolPanel = new GridBagLayoutPanel();
107
        
108
        private MapContextManager mapContextManager = MapContextLocator
109
                        .getMapContextManager();
110

    
111
    /**
112
     * This is the default constructor
113
     */
114
    public VectorialInterval() {
115
        super();
116
        initialize();
117
    }
118

    
119
    /**
120
     * This method initializes this
121
     */
122
    private void initialize() {
123
        this.setLayout(new BorderLayout());
124
        this.setSize(700, 300);
125
        this.setBorder(new EmptyBorder(new Insets(5,5,5,5)));
126
        
127
        this.add(getPnlNorth(), BorderLayout.NORTH);
128
        this.add(getPnlButtons(), BorderLayout.SOUTH);
129
        this.add(getPnlCenter(), BorderLayout.CENTER);
130
        setOptionPanel(getOptionPanel());
131
    }
132

    
133
    private JPanel getPnlNorth() {
134
        if (pnlNorth == null) {
135
            pnlNorth = new JPanel(new GridLayout(1, 2, 5, 5));
136
            pnlNorth.add(getGeneralPanel());
137
        }
138
        return pnlNorth;
139
    }
140

    
141
    /**
142
     * This method initializes panelN
143
     *
144
     * @return javax.swing.JPanel
145
     */
146
    private JPanel getGeneralPanel() {
147
        if (pnlGeneral == null) {
148
            pnlGeneral = new GridBagLayoutPanel();
149
            pnlGeneral.setBorder(BorderFactory.
150
                    createTitledBorder(null,
151
                            PluginServices.getText(this, "fields")));
152
            pnlGeneral.addComponent(PluginServices.getText(this, "Campo_de_clasificacion_XcolonX"),
153
                    getCmbFields(),new Insets(2, 5, 2, 5));
154
            pnlGeneral.addComponent(PluginServices.getText(this, "tipo_de_intervalo_XcolonX"),
155
                    getCmbIntervalTypes(),new Insets(2, 5, 2, 5));
156

    
157
            JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
158
            aux.add(getTxtNumIntervals());
159
            pnlGeneral.addComponent(PluginServices.getText(this, "No_de_intervalos_XcolonX"),
160
                    aux,new Insets(2, 5, 2, 5));
161
            defaultSymbolPanel.add(getChkDefaultvalues(), null);
162
                        pnlGeneral.addComponent(defaultSymbolPanel,new Insets(10, 25, 2, 5));
163
        }
164
        return pnlGeneral;
165
    }
166

    
167
    public JPanel getOptionPanel() {
168
        if (optionPanel == null) {
169
            optionPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
170
            optionPanel.setBorder(BorderFactory.
171
                    createTitledBorder(null,
172
                            PluginServices.getText(this, "color_ramp")));
173

    
174
            GridBagLayoutPanel aux = new GridBagLayoutPanel();
175
            aux.addComponent(PluginServices.getText(this, "Color_inicio_XcolonX"),
176
                    getColorChooserPanel());
177
            aux.addComponent(PluginServices.getText(this, "Color_final_XcolonX"),
178
                    getColorChooserPanel1());
179
            optionPanel.add(aux);
180
        }
181
        return optionPanel;
182
    }
183

    
184
    private void setOptionPanel(JPanel p) {
185
        getPnlNorth().remove(getOptionPanel());
186
        getPnlNorth().add(p, BorderLayout.NORTH);
187
    }
188
    /**
189
     * This method initializes jComboBox
190
     *
191
     * @return javax.swing.JComboBox
192
     */
193
    private JComboBox getCmbFields() {
194
        if (cmbField == null) {
195
            cmbField = new JComboBox();
196
            cmbField.setActionCommand("FIELD_SELECTED");
197
            cmbField.addActionListener(listener);
198
            cmbField.setVisible(true);
199
        }
200

    
201
        return cmbField;
202
    }
203

    
204
    /**
205
     * This method initializes txtNumIntervals
206
     *
207
     * @return javax.swing.JTextField
208
     */
209
    private JTextField getTxtNumIntervals() {
210
        if (txtNumIntervals == null) {
211
            txtNumIntervals = new JTextField(5);
212
            txtNumIntervals.setText("5");
213
        }
214

    
215
        return txtNumIntervals;
216
    }
217

    
218
    /**
219
     * This method initializes colorChooserPanel
220
     *
221
     * @return com.iver.cit.gvsig.gui.Panels.ColorChooserPanel
222
     */
223
    private ColorChooserPanel getColorChooserPanel() {
224
        if (colorChooserPanel == null) {
225
            colorChooserPanel = new ColorChooserPanel();
226
            colorChooserPanel.setBounds(new java.awt.Rectangle(108, 49, 54, 20));
227
            colorChooserPanel.setAlpha(255);
228
            colorChooserPanel.setColor(Color.red);
229
        }
230

    
231
        return colorChooserPanel;
232
    }
233

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

    
247
        return colorChooserPanel1;
248
    }
249

    
250
    /**
251
     * This method initializes chkdefaultvalues
252
     *
253
     * @return javax.swing.JCheckBox
254
     */
255
    protected JCheckBox getChkDefaultvalues() {
256
                if (chkdefaultvalues == null) {
257
                        chkdefaultvalues = new JCheckBox();
258
                        chkdefaultvalues.setText(PluginServices.getText(this,
259
                        "resto_valores")+": ");
260
                        chkdefaultvalues.setBounds(new java.awt.Rectangle(342, 26, 141, 20));
261
                        chkdefaultvalues.setSelected(false);
262
                        chkdefaultvalues.addActionListener(new java.awt.event.ActionListener() {
263
                                public void actionPerformed(java.awt.event.ActionEvent e) {
264
                                        if (chkdefaultvalues.isSelected()) {
265
                                                auxLegend.useDefaultSymbol(true);
266
                                        } else {
267
                                                auxLegend.useDefaultSymbol(false);
268
                                        }
269
                                }
270
                        });
271
                }
272

    
273
                return chkdefaultvalues;
274
        }
275

    
276
    /**
277
     * This method initializes jComboBox1
278
     *
279
     * @return javax.swing.JComboBox
280
     */
281
    private JComboBox getCmbIntervalTypes() {
282
        if (cmbIntervalType == null) {
283
            cmbIntervalType = new JComboBox();
284
            cmbIntervalType.setActionCommand("INTERVAL_TYPE");
285
            cmbIntervalType.addActionListener(listener);
286
            cmbIntervalType.addItem(PluginServices.getText(this, "equal_intervals"));
287
            cmbIntervalType.addItem(PluginServices.getText(this,
288
                    "natural_intervals"));
289
            cmbIntervalType.addItem(PluginServices.getText(this,
290
                    "quantile_intervals"));
291
            cmbIntervalType.setVisible(true);
292
        }
293

    
294
        return cmbIntervalType;
295
    }
296

    
297
    /**
298
     * This method initializes panelS
299
     *
300
     * @return javax.swing.JPanel
301
     */
302
    protected JPanel getPnlButtons() {
303
        if (panelS == null) {
304
            panelS = new JPanel();
305
            panelS.setPreferredSize(new java.awt.Dimension(417, 32));
306
            panelS.add(getBintervals(), null);
307
            panelS.add(getBInsert(), null);
308
            panelS.add(getBDelAll(), null);
309
            panelS.add(getBDel(), null);
310
        }
311

    
312
        return panelS;
313
    }
314

    
315
    /**
316
     * This method initializes bintervals
317
     *
318
     * @return javax.swing.JButton
319
     */
320
    private JButton getBintervals() {
321
        if (bintervals == null) {
322
            bintervals = new JButton();
323
            bintervals.setActionCommand("ADD_ALL_VALUES");
324
            bintervals.addActionListener(listener);
325
            bintervals.setText(PluginServices.getText(this,
326
                    "Calcular_intervalos"));
327
        }
328

    
329
        return bintervals;
330
    }
331

    
332
    /**
333
     * This method initializes bInsert
334
     *
335
     * @return javax.swing.JButton
336
     */
337
    private JButton getBInsert() {
338
        if (bInsert == null) {
339
            bInsert = new JButton();
340
            bInsert.setActionCommand("ADD_VALUE");
341
            bInsert.addActionListener(listener);
342
            bInsert.setText(PluginServices.getText(this, "Anadir"));
343
        }
344

    
345
        return bInsert;
346
    }
347

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

    
361
        return bDelAll;
362
    }
363

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

    
377
        return bDel;
378
    }
379

    
380

    
381
    /**
382
     * Damos una primera pasada para saber los l�mites inferior y superior y
383
     * rellenar un array con los valores. Luego dividimos ese array en
384
     * intervalos.
385
     */
386
    protected void fillTableValues() {
387

    
388
        symbolTable.removeAllItems();
389

    
390
        try {
391
            IInterval[] arrayIntervalos = calculateIntervals();
392
            if (arrayIntervalos == null) {
393
                                return;
394
                        }
395

    
396
            IInterval interval;
397
            NumberFormat.getInstance().setMaximumFractionDigits(2);
398
            //theLegend.clear();
399
            auxLegend.clear();
400

    
401
            int r;
402
            int g;
403
            int b;
404
            int stepR;
405
            int stepG;
406
            int stepB;
407

    
408
            // Cogemos el tipo de gradaci�n de colores que quiere el usuario y
409
            // Creamos el primer y �ltimo color.
410
            Color startColor = colorChooserPanel.getColor();
411

    
412
            Color endColor = colorChooserPanel1.getColor();
413

    
414
            r = startColor.getRed();
415
            g = startColor.getGreen();
416
            b = startColor.getBlue();
417
            stepR = (endColor.getRed() - r) / arrayIntervalos.length;
418
            stepG = (endColor.getGreen() - g) / arrayIntervalos.length;
419
            stepB = (endColor.getBlue() - b) / arrayIntervalos.length;
420

    
421
//            auxLegend = LegendFactory.createVectorialIntervalLegend(layer.getShapeType());
422
                        auxLegend = (IVectorialIntervalLegend) MapContextLocator
423
                                        .getMapContextManager().createLegend(
424
                                                        IVectorialIntervalLegend.LEGEND_NAME);
425
            auxLegend.setShapeType(layer.getShapeType());
426
            auxLegend.useDefaultSymbol(false);
427
            auxLegend.setStartColor(startColor);
428
            auxLegend.setEndColor(endColor);
429

    
430
            int geomType = layer.getGeometryType().getType();
431
            int numSymbols = 0;
432

    
433
            for (int k = 0; k < arrayIntervalos.length; k++) {
434
                interval = arrayIntervalos[k];
435

    
436
                                ISymbol theSymbol = null;
437
                                Color col = new Color(r, g, b);
438
                                
439
                theSymbol = mapContextManager.getSymbolManager().createSymbol(
440
                    geomType, col);
441
                
442
                /*
443
                 * If multishape, we need to set line and fill color here.
444
                 * This is because the symbol manager is in mapcontext.api, which cannot
445
                 * depend on symbology.lib.api
446
                 */
447
                if (theSymbol instanceof IMultiShapeSymbol) {
448
                    IMultiShapeSymbol mss = (IMultiShapeSymbol) theSymbol; 
449
                    mss.getLineSymbol().setLineColor(col);
450
                    mss.getFillSymbol().setFillColor(col);
451
                }
452

    
453
                                
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
                numSymbols++;
467

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

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

    
481
                // }
482
            } // for
483

    
484
          symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
485
          auxLegend.getValues(), auxLegend.getDescriptions());
486

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

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

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

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

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

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

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

    
540
            getDefaultSymbolPrev(shapeType);
541

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

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

563

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

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

    
616

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

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

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

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

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

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

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

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

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

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

    
666
        symbolTable.removeAllItems();
667
    }
668

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

    
678
                auxLegend.useDefaultSymbol(chkdefaultvalues.isSelected());
679
                auxLegend.setIntervalType(cmbIntervalType.getSelectedIndex());
680
                
681

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

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

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

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

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

    
701
                ISymbol defsym = null;
702
        if (chkdefaultvalues.isSelected() && defaultSymbolPrev.getSymbol() != null) {
703
            defsym = (ISymbol) defaultSymbolPrev.getSymbol();
704
            String description = PluginServices.getText(this,"default");
705
            defsym.setDescription(description);
706
            auxLegend.setDefaultSymbol(defsym);
707
            auxLegend.useDefaultSymbol(true);
708
        } else {
709
            
710
            auxLegend.useDefaultSymbol(false);
711
        }
712

    
713
                FeatureStore rs;
714
                try {
715

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

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

    
727

    
728

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

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

    
756
        return pnlCenter;
757
    }
758

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

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

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

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

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

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

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

    
808
            }
809

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

    
814
            }
815

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

    
823
    public String getDescription() {
824
        return PluginServices.getText(this,"Muestra_los_elementos_de_la_capa_usando_una_gama_de_colores_en_funcion_del_valor_de_un_determinado_campo_de_atributos") + ".";
825
    }
826

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

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

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

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

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

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

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

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