Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / PanelLegendBreaks.java @ 7304

History | View | Annotate | Download (34.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 com.iver.cit.gvsig.project.documents.view.legend;
42

    
43
import com.hardcode.gdbms.engine.data.DataSource;
44
import com.hardcode.gdbms.engine.values.DateValue;
45
import com.hardcode.gdbms.engine.values.DoubleValue;
46
import com.hardcode.gdbms.engine.values.FloatValue;
47
import com.hardcode.gdbms.engine.values.IntValue;
48
import com.hardcode.gdbms.engine.values.LongValue;
49
import com.hardcode.gdbms.engine.values.NullValue;
50
import com.hardcode.gdbms.engine.values.Value;
51

    
52
import com.iver.andami.PluginServices;
53

    
54
import com.iver.cit.gvsig.fmap.DriverException;
55
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
56
import com.iver.cit.gvsig.fmap.layers.FLayer;
57
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
58
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
59
import com.iver.cit.gvsig.fmap.rendering.FInterval;
60
import com.iver.cit.gvsig.fmap.rendering.IInterval;
61
import com.iver.cit.gvsig.fmap.rendering.Legend;
62
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
63
import com.iver.cit.gvsig.fmap.rendering.NullIntervalValue;
64
import com.iver.cit.gvsig.fmap.rendering.VectorialIntervalLegend;
65
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
66
import com.iver.cit.gvsig.project.documents.view.legend.gui.FSymbolTable;
67
import com.iver.cit.gvsig.project.documents.view.legend.gui.ILegendPanel;
68

    
69
import org.apache.log4j.Logger;
70

    
71
import java.awt.BorderLayout;
72
import java.awt.Color;
73
import java.awt.event.ActionEvent;
74
import java.awt.event.ActionListener;
75

    
76
import java.text.NumberFormat;
77

    
78
import java.util.ArrayList;
79

    
80
import javax.swing.DefaultComboBoxModel;
81
import javax.swing.JButton;
82
import javax.swing.JCheckBox;
83
import javax.swing.JComboBox;
84
import javax.swing.JLabel;
85
import javax.swing.JOptionPane;
86
import javax.swing.JPanel;
87
import javax.swing.JTextField;
88

    
89

    
90
/**
91
 * DOCUMENT ME!
92
 *
93
 * @author Vicente Caballero Navarro
94
 */
95
public class PanelLegendBreaks extends JPanel {
96
    private static Logger logger = Logger.getLogger(PanelLegendBreaks.class.getName());
97
    private JPanel panelN = null;
98
    private JLabel lblField = null;
99
    private JComboBox cmbField = null;
100
    private JLabel lblNumIntervals = null;
101
    private JTextField txtNumIntervals = null;
102
    private JLabel lblInitColor = null;
103
    private JLabel lblEndColor = null;
104
    private ColorChooserPanel colorChooserPanel = null;
105
    private ColorChooserPanel colorChooserPanel1 = null;
106
    private JCheckBox chkdefaultvalues = null;
107
    private JLabel lblType = null;
108
    private JComboBox cmbFieldType = null;
109
    private JPanel panelS = null;
110
    private JButton bintervals = null;
111
    private JButton bInsert = null;
112
    private JButton bDelAll = null;
113
    private JButton bDel = null;
114
    private int count = 0;
115
    private ClassifiableVectorial m_lyr;
116
    private VectorialIntervalLegend m_Renderer;
117
    private VectorialIntervalLegend auxLegend = null;
118
    private FSymbolTable m_symbolTable = new FSymbolTable("intervals");
119
    private MyListener listener = new MyListener();
120
    private JPanel panelC = null;
121

    
122
    /**
123
     * This is the default constructor
124
     */
125
    public PanelLegendBreaks() {
126
        super();
127
        initialize();
128
    }
129

    
130
    /**
131
     * This method initializes this
132
     */
133
    private void initialize() {
134
        //this.setLayout(new BorderLayout());
135
        this.setLayout(new BorderLayout());
136
        this.setSize(490, 300);
137
        this.add(getPanelN(), java.awt.BorderLayout.NORTH);
138
        this.add(getPanelS(), java.awt.BorderLayout.SOUTH);
139
        this.add(getPanelC(), java.awt.BorderLayout.CENTER);
140
    }
141

    
142
    /**
143
     * This method initializes panelN
144
     *
145
     * @return javax.swing.JPanel
146
     */
147
    private JPanel getPanelN() {
148
        if (panelN == null) {
149
            lblType = new JLabel();
150
            lblType.setBounds(new java.awt.Rectangle(1, 26, 122, 20));
151
            lblType.setText(PluginServices.getText(this, "tipo_de_intervalo"));
152
            lblEndColor = new JLabel();
153
            lblEndColor.setBounds(new java.awt.Rectangle(169, 49, 84, 20));
154
            lblEndColor.setText(PluginServices.getText(this, "Color_final"));
155
            lblInitColor = new JLabel();
156
            lblInitColor.setBounds(new java.awt.Rectangle(1, 49, 105, 20));
157
            lblInitColor.setText(PluginServices.getText(this, "Color_inicio"));
158
            lblNumIntervals = new JLabel();
159
            lblNumIntervals.setBounds(new java.awt.Rectangle(315, 49, 133, 20));
160
            lblNumIntervals.setText(PluginServices.getText(this,
161
                    "No_de_intervalos"));
162
            lblField = new JLabel();
163
            lblField.setBounds(new java.awt.Rectangle(1, 3, 170, 20));
164
            lblField.setText(PluginServices.getText(this,
165
                    "Campo_de_clasificacion"));
166
            panelN = new JPanel();
167
            panelN.setLayout(null);
168
            panelN.setPreferredSize(new java.awt.Dimension(450, 75));
169
            panelN.add(lblField, null);
170
            panelN.add(getJComboBox(), null);
171
            panelN.add(lblNumIntervals, null);
172
            panelN.add(getTxtNumIntervals(), null);
173
            panelN.add(lblInitColor, null);
174
            panelN.add(lblEndColor, null);
175
            panelN.add(getColorChooserPanel(), null);
176
            panelN.add(getColorChooserPanel1(), null);
177
            panelN.add(getChkdefaultvalues(), null);
178
            panelN.add(lblType, null);
179
            panelN.add(getJComboBox1(), null);
180
        }
181

    
182
        return panelN;
183
    }
184

    
185
    /**
186
     * This method initializes jComboBox
187
     *
188
     * @return javax.swing.JComboBox
189
     */
190
    private JComboBox getJComboBox() {
191
        if (cmbField == null) {
192
            cmbField = new JComboBox();
193
            cmbField.setBounds(new java.awt.Rectangle(172, 3, 312, 20));
194
            cmbField.setActionCommand("FIELD_SELECTED");
195
            cmbField.addActionListener(listener);
196
            cmbField.setVisible(true);
197
        }
198

    
199
        return cmbField;
200
    }
201

    
202
    /**
203
     * This method initializes txtNumIntervals
204
     *
205
     * @return javax.swing.JTextField
206
     */
207
    private JTextField getTxtNumIntervals() {
208
        if (txtNumIntervals == null) {
209
            txtNumIntervals = new JTextField();
210
            txtNumIntervals.setText("5");
211
            txtNumIntervals.setBounds(new java.awt.Rectangle(449, 49, 37, 20));
212
        }
213

    
214
        return txtNumIntervals;
215
    }
216

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

    
230
        return colorChooserPanel;
231
    }
232

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

    
246
        return colorChooserPanel1;
247
    }
248

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

    
272
        return chkdefaultvalues;
273
    }
274

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

    
294
        return cmbFieldType;
295
    }
296

    
297
    /**
298
     * This method initializes panelS
299
     *
300
     * @return javax.swing.JPanel
301
     */
302
    private JPanel getPanelS() {
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
     * Damos una primera pasada para saber los l?mites inferior y superior y
382
     * rellenar un array con los valores. Luego dividimos ese array en
383
     * intervalos.
384
     */
385
    private void fillTableValues() {
386
        FInterval[] arrayIntervalos = null;
387
        DataSource elRs;
388
        m_symbolTable.removeAllItems();
389

    
390
        try {
391
            elRs = ((AlphanumericData) m_lyr).getRecordset();
392
            logger.debug("elRs.start()");
393
            elRs.start();
394

    
395
            int idField = -1;
396
            int numIntervalos = 1;
397

    
398
            try {
399
                numIntervalos = Integer.parseInt(txtNumIntervals.getText());
400
            } catch (NumberFormatException e) {
401
                System.out.println("Foramto de n?mero erroneo");
402
            }
403

    
404
            String fieldName = (String) cmbField.getSelectedItem();
405
            System.out.println("Nombre del campo de clasificaci?n: " +
406
                fieldName);
407
            m_Renderer.setFieldName(fieldName);
408

    
409
            String nomField = m_Renderer.getFieldName();
410

    
411
            // Cogemos el tipo de gradaci?n de colores que quiere el usuario y 
412
            // Creamos el primer y ?ltimo color.
413
            Color startColor = colorChooserPanel.getColor();
414
            m_Renderer.setStartColor(startColor);
415

    
416
            Color endColor = colorChooserPanel1.getColor();
417
            m_Renderer.setEndColor(endColor);
418

    
419
            for (int i = 0; i < elRs.getFieldCount(); i++) {
420
                String nomAux = elRs.getFieldName(i).trim();
421

    
422
                if (nomField.compareToIgnoreCase(nomAux) == 0) {
423
                    idField = i;
424

    
425
                    break;
426
                }
427
            }
428

    
429
            if (idField == -1) {
430
                System.err.println("Campo no reconocido " + nomField);
431

    
432
                return;
433
            }
434

    
435
            ///m_symbolTable.removeAllItems();
436
            ///m_valuesCache = new Object[m_lyr.getSource().getShapeCount()];
437
            int numSymbols = 0;
438
            FSymbol theSymbol;
439

    
440
            //Date valorDate;
441
            double minValue = Double.MAX_VALUE;
442
            double maxValue = Double.MIN_VALUE;
443

    
444
            //Date minValueDate = null;
445
            //Date maxValueDate = null;
446
            auxLegend = LegendFactory.createVectorialIntervalLegend(m_lyr.getShapeType());
447

    
448
            Value clave;
449

    
450
            //Object resul;
451
            int symbolType = 0;
452
            count = 0;
453

    
454
            for (int j = 0; j < elRs.getRowCount(); j++) {
455
                clave = elRs.getFieldValue(j, idField);
456

    
457
                IInterval interval = auxLegend.getInterval(clave);
458

    
459
                ////Comprobar que no esta repetido y no hace falta introducir en el hashtable el campo junto con el simbolo.
460
                if (auxLegend.getSymbolByInterval(interval) == null) {
461
                    //si no esta creado el simbolo se crea
462
                    double valor = 0;
463
                    symbolType = m_lyr.getShapeType();
464

    
465
                    if (clave instanceof IntValue) {
466
                        valor = ((IntValue) clave).getValue();
467
                    } else if (clave instanceof DoubleValue) {
468
                        valor = ((DoubleValue) clave).getValue();
469
                    } else if (clave instanceof FloatValue) {
470
                        valor = ((FloatValue) clave).getValue();
471
                    } else if (clave instanceof LongValue) {
472
                        valor = ((LongValue) clave).getValue();
473
                    } else if (clave instanceof DateValue) {
474
                        //TODO POR IMPLEMENTAR
475
                        ///valorDate = elRs.getFieldValueAsDate(idField);
476
                        ///if (valorDate.before(minValueDate)) minValueDate = valorDate;
477
                        ///if (valorDate.after(maxValueDate)) maxValueDate = valorDate;
478
                    } else if (clave instanceof NullValue) {
479
                        continue;
480
                    }
481

    
482
                    if (valor < minValue) {
483
                        minValue = (double) valor;
484
                    }
485

    
486
                    if (valor > maxValue) {
487
                        maxValue = (double) valor;
488
                    }
489
                }
490

    
491
                count++;
492
            }
493

    
494
            switch (m_Renderer.getIntervalType()) {
495
                case VectorialIntervalLegend.EQUAL_INTERVALS:
496
                    arrayIntervalos = calculateEqualIntervals(numIntervalos,
497
                            minValue, maxValue, fieldName);
498

    
499
                    break;
500

    
501
                case VectorialIntervalLegend.NATURAL_INTERVALS:
502
                    arrayIntervalos = calculateNaturalIntervals(numIntervalos,
503
                            minValue, maxValue, fieldName);
504

    
505
                    break;
506

    
507
                case VectorialIntervalLegend.QUANTILE_INTERVALS:
508
                    arrayIntervalos = calculateQuantileIntervals(numIntervalos,
509
                            minValue, maxValue, fieldName);
510

    
511
                    break;
512
            }
513

    
514
            //intervals = arrayIntervalos;
515
            FInterval elIntervalo;
516
            NumberFormat.getInstance().setMaximumFractionDigits(2);
517
            m_Renderer.clear();
518

    
519
            int r;
520
            int g;
521
            int b;
522
            int stepR;
523
            int stepG;
524
            int stepB;
525
            r = startColor.getRed();
526
            g = startColor.getGreen();
527
            b = startColor.getBlue();
528
            stepR = (endColor.getRed() - r) / arrayIntervalos.length;
529
            stepG = (endColor.getGreen() - g) / arrayIntervalos.length;
530
            stepB = (endColor.getBlue() - b) / arrayIntervalos.length;
531

    
532
            if (chkdefaultvalues.isSelected()) {
533
                auxLegend.getDefaultSymbol().setDescription("Default");
534
                auxLegend.addSymbol(new NullIntervalValue(),
535
                    auxLegend.getDefaultSymbol());
536
            }
537

    
538
            for (int k = 0; k < arrayIntervalos.length; k++) {
539
                elIntervalo = arrayIntervalos[k];
540

    
541
                // clave = elIntervalo; // elIntervalo.getMin() + " - " + elIntervalo.getMax();
542
                // System.out.println("k = " + k + " clave = " + clave);
543
                ////Comprobar que no esta repetido y no hace falta introducir en el hashtable el campo junto con el simbolo.
544
                // if (!m_Renderer.m_symbolList.containsKey(elIntervalo)) {
545
                //si no esta creado el simbolo se crea
546
                theSymbol = new FSymbol(symbolType, new Color(r, g, b));
547
                theSymbol.setDescription(NumberFormat.getInstance().format(elIntervalo.getMin()) +
548
                    " - " +
549
                    NumberFormat.getInstance().format(elIntervalo.getMax()));
550

    
551
                //////////////////////////////////////
552
                // CALCULAMOS UN COLOR APROPIADO
553
                r = r + stepR;
554
                g = g + stepG;
555
                b = b + stepB;
556

    
557
                /////////////////////////////////
558
                auxLegend.addSymbol(elIntervalo, theSymbol);
559
                System.out.println("addSymbol = " + elIntervalo +
560
                    " theSymbol = " + theSymbol.getDescription());
561
                numSymbols++;
562

    
563
                if (numSymbols > 100) {
564
                    int resp = JOptionPane.showConfirmDialog(this,
565
                            PluginServices.getText(this, "mas_de_100_simbolos"),
566
                            PluginServices.getText(this, "quiere_continuar"),
567
                            JOptionPane.YES_NO_OPTION,
568
                            JOptionPane.WARNING_MESSAGE);
569

    
570
                    if ((resp == JOptionPane.NO_OPTION) ||
571
                            (resp == JOptionPane.DEFAULT_OPTION)) {
572
                        return;
573
                    }
574
                }
575

    
576
                // }
577
            } // for
578

    
579
            System.out.println("Num. Simbolos = " +
580
                auxLegend.getValues().length);
581
            m_symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
582
                auxLegend.getValues(), auxLegend.getDescriptions());
583
            elRs.stop();
584
        } catch (DriverException e) {
585
            // TODO Auto-generated catch block
586
            e.printStackTrace();
587
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
588
            e.printStackTrace();
589
        }
590

    
591
        bDelAll.setEnabled(true);
592
        bDel.setEnabled(true);
593
    }
594

    
595
    /**
596
     * EQUAL INTERVAL Devuelve un Array con el n?mero de intervalos que se
597
     * quieren crear. Los intervalos se crean con un tama?o igual entre ellos.
598
     *
599
     * @param numIntervals n?mero de intervalos
600
     * @param minValue Valor m?nimo.
601
     * @param maxValue Valor m?ximo.
602
     * @param fieldName Nombre del campo
603
     *
604
     * @return Array con los intervalos.
605
     */
606
    FInterval[] calculateEqualIntervals(int numIntervals, double minValue,
607
        double maxValue, String fieldName) {
608
        FInterval[] theIntervalArray = new FInterval[numIntervals];
609
        double step = (maxValue - minValue) / numIntervals;
610

    
611
        if (numIntervals > 1) {
612
            theIntervalArray[0] = new FInterval(minValue, minValue + step);
613

    
614
            for (int i = 1; i < (numIntervals - 1); i++) {
615
                theIntervalArray[i] = new FInterval(minValue + (i * step) +
616
                        0.01, minValue + ((i + 1) * step));
617
            }
618

    
619
            theIntervalArray[numIntervals - 1] = new FInterval(minValue +
620
                    ((numIntervals - 1) * step) + 0.01, maxValue);
621
        } else {
622
            theIntervalArray[0] = new FInterval(minValue, maxValue);
623
        }
624

    
625
        return theIntervalArray;
626
    }
627

    
628
    /**
629
     * NATURAL INTERVAL Devuelve un Array con el n?mero de intervalos que se
630
     * quieren crear. Los intervalos se distribuyen de forma natural.
631
     *
632
     * @param numIntervals n?mero de intervalos
633
     * @param minValue Valor m?nimo.
634
     * @param maxValue Valor m?ximo.
635
     * @param fieldName Nombre del campo
636
     *
637
     * @return Array con los intervalos.
638
     */
639
    FInterval[] calculateNaturalIntervals(int numIntervals, double minValue,
640
        double maxValue, String fieldName) {
641
        NaturalIntervalGenerator intervalGenerator = new NaturalIntervalGenerator(((AlphanumericData) m_lyr),
642
                fieldName, numIntervals);
643

    
644
        try {
645
            intervalGenerator.generarIntervalos();
646
        } catch (DriverException e) {
647
            e.printStackTrace();
648
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
649
            e.printStackTrace();
650
        }
651

    
652
        int numIntervalsGen = intervalGenerator.getNumIntervals() - 1;
653

    
654
        if (numIntervalsGen == -1) {
655
            //TODO cuando no puede calcular los intervalos.
656
            numIntervalsGen = 1;
657
        }
658

    
659
        FInterval[] theIntervalArray = new FInterval[numIntervalsGen];
660

    
661
        if (numIntervalsGen > 1) {
662
            theIntervalArray[0] = new FInterval(minValue,
663
                    intervalGenerator.getValorRuptura(0));
664

    
665
            for (int i = 1; i < (numIntervalsGen - 1); i++) {
666
                theIntervalArray[i] = new FInterval(intervalGenerator.getValInit(i -
667
                            1), intervalGenerator.getValorRuptura(i));
668
            }
669

    
670
            theIntervalArray[numIntervalsGen - 1] = new FInterval(intervalGenerator.getValInit(numIntervalsGen -
671
                        2), maxValue);
672
        } else {
673
            theIntervalArray[numIntervalsGen - 1] = new FInterval(minValue,
674
                    maxValue);
675
        }
676

    
677
        return theIntervalArray;
678
    }
679

    
680
    /**
681
     * QUANTILE INTERVAL Devuelve un Array con el n?mero de intervalos que se
682
     * quieren crear. Los intervalos se distribuyen de forma quantile.
683
     *
684
     * @param numIntervals n?mero de intervalos
685
     * @param minValue Valor m?nimo.
686
     * @param maxValue Valor m?ximo.
687
     * @param fieldName Nombre del campo
688
     *
689
     * @return Array con los intervalos.
690
     */
691
    FInterval[] calculateQuantileIntervals(int numIntervals, double minValue,
692
        double maxValue, String fieldName) {
693
        QuantileIntervalGenerator intervalGenerator = new QuantileIntervalGenerator(((AlphanumericData) m_lyr),
694
                fieldName, numIntervals);
695

    
696
        try {
697
            intervalGenerator.generarIntervalos();
698
        } catch (DriverException e) {
699
            e.printStackTrace();
700
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
701
            e.printStackTrace();
702
        }
703

    
704
        int numIntervalsGen = intervalGenerator.getNumIntervalGen();
705
        FInterval[] theIntervalArray = new FInterval[numIntervalsGen];
706

    
707
        if (intervalGenerator.getNumIntervalGen() > 1) {
708
            theIntervalArray[0] = new FInterval(minValue,
709
                    intervalGenerator.getValRuptura(0));
710

    
711
            for (int i = 1; i < (numIntervalsGen - 1); i++) {
712
                theIntervalArray[i] = new FInterval(intervalGenerator.getValInit(i -
713
                            1), intervalGenerator.getValRuptura(i));
714
            }
715

    
716
            theIntervalArray[numIntervalsGen - 1] = new FInterval(intervalGenerator.getValInit(numIntervalsGen -
717
                        2), maxValue);
718
        } else {
719
            theIntervalArray[numIntervalsGen - 1] = new FInterval(minValue,
720
                    maxValue);
721
        }
722

    
723
        return theIntervalArray;
724

    
725
        /*        double numQ = count / numIntervals;
726
           for (int i = 0; i < count; i++) {
727
           }
728
           return theIntervalArray;
729
         */
730
    }
731

    
732
    /**
733
     * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#setLayer(com.iver.cit.gvsig.fmap.layers.FLayer,
734
     *      com.iver.cit.gvsig.fmap.rendering.Legend)
735
     */
736
    public void setLayer(FLayer lyr, Legend r) {
737
        //                 m_TOC = t;
738
        // OJO, COMPROBAR ANTES SI ES DE TIPO VECTORIAL
739
        // m_lyr = (FLyrVect) t.getFirstLyrVectSelected();
740
        m_lyr = (ClassifiableVectorial) lyr;
741
        fillFieldNames();
742

    
743
        if (r instanceof VectorialIntervalLegend) {
744
            m_Renderer = (VectorialIntervalLegend) r;
745
            getChkdefaultvalues().setSelected(m_Renderer.isUseDefaultSymbol());
746
            cmbField.getModel().setSelectedItem(m_Renderer.getFieldName());
747
            m_symbolTable.fillTableFromSymbolList(m_Renderer.getSymbols(),
748
                m_Renderer.getValues(), m_Renderer.getDescriptions());
749
            colorChooserPanel.setColor(m_Renderer.getStartColor());
750
            colorChooserPanel1.setColor(m_Renderer.getEndColor());
751
            colorChooserPanel.repaint();
752
            colorChooserPanel1.repaint();
753
        } else {
754
            try {
755
                // Si la capa viene con otro tipo de leyenda, creamos
756
                // una nueva del tipo que maneja este panel
757
                m_Renderer = new VectorialIntervalLegend(m_lyr.getShapeType());
758
            } catch (DriverException e) {
759
                // TODO Auto-generated catch block
760
                e.printStackTrace();
761
            }
762
        }
763

    
764
        cmbFieldType.setSelectedIndex(m_Renderer.getIntervalType());
765
    }
766

    
767
    /**
768
     * DOCUMENT ME!
769
     */
770
    private void fillFieldNames() {
771
        DataSource rs = null;
772
        ArrayList nomFields = null;
773

    
774
        try {
775
            rs = ((AlphanumericData) m_lyr).getRecordset();
776
            logger.debug("rs.start()");
777
            rs.start();
778

    
779
            nomFields = new ArrayList();
780

    
781
            Value val;
782

    
783
            for (int i = 0; i < rs.getFieldCount(); i++) {
784
                //TODO deber?a de existir un getFieldType
785
                val = rs.getFieldValue(0, i);
786

    
787
                if (val.getClass() == NullValue.class) {
788
                    continue;
789
                }
790

    
791
                if ((val.getClass() == IntValue.class) ||
792
                        (val.getClass() == DoubleValue.class) ||
793
                        (val.getClass() == FloatValue.class) ||
794
                        (val.getClass() == LongValue.class)) {
795
                    nomFields.add(rs.getFieldName(i).trim());
796
                }
797
            }
798

    
799
            rs.stop();
800
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
801
            e1.printStackTrace();
802
        } catch (DriverException e) {
803
            e.printStackTrace();
804
        }
805

    
806
        /*        ArrayList nomFields = new ArrayList();
807
           for (int i = 0; i < rs.getFieldsCount(); i++) {
808
                   if ((rs.getFieldType(i) == FRecordset.INTEGER) ||
809
                           (rs.getFieldType(i) == FRecordset.DECIMAL) ||
810
                           (rs.getFieldType(i) == FRecordset.DATE))
811
                   {
812
                           nomFields.add(rs.getFieldName(i).trim());
813
                   }
814
        
815
           }
816
         */
817
        DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields.toArray());
818
        cmbField.setModel(cM);
819

    
820
        // fieldsListValor.setSelectedIndex(0);
821
        m_symbolTable.removeAllItems();
822
    }
823

    
824
    /**
825
     * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
826
     */
827
    public Legend getLegend() {
828
        fillSymbolListFromTable();
829

    
830
        if (auxLegend != null) {
831
            m_Renderer.setDefaultSymbol(auxLegend.getDefaultSymbol());
832
            m_Renderer.useDefaultSymbol(chkdefaultvalues.isSelected());
833
        }
834

    
835
        return m_Renderer;
836
    }
837

    
838
    /**
839
     * A partir de los registros de la tabla, regenera el FRenderer. (No solo
840
     * el symbolList, si no tambi?n el arrayKeys y el defaultRenderer
841
     */
842
    private void fillSymbolListFromTable() {
843
        //String clave;
844
        FSymbol theSymbol;
845
        IInterval theInterval = null;
846

    
847
        // Borramos las anteriores listas:
848
        m_Renderer.clear();
849

    
850
        //boolean bRestoValores = false; // PONERLO EN UN CHECKBOX
851
        //double from = 0;
852
        //double to = 0;
853
        //String[] arraySplit = new String[2];
854
        //int hasta;
855
        String fieldName = (String) cmbField.getSelectedItem();
856
        m_Renderer.setFieldName(fieldName);
857

    
858
        /*if (bRestoValores) {
859
           hasta = m_symbolTable.getRowCount() - 1;
860
           } else {
861
                   hasta = m_symbolTable.getRowCount();
862
           }
863
         */
864
        for (int row = 0; row < m_symbolTable.getRowCount(); row++) {
865
            // clave = m_symbolTable.getFieldValue(row,1);
866
            if (!(m_symbolTable.getFieldValue(row, 1) instanceof FInterval)) {
867
                theSymbol = (FSymbol) m_symbolTable.getFieldValue(row, 0);
868
                theSymbol.setDescription((String) m_symbolTable.getFieldValue(
869
                        row, 2));
870
                m_Renderer.addSymbol(new NullIntervalValue(), theSymbol);
871
            } else {
872
                theInterval = (IInterval) m_symbolTable.getFieldValue(row, 1);
873
                theSymbol = (FSymbol) m_symbolTable.getFieldValue(row, 0);
874
                theSymbol.setDescription((String) m_symbolTable.getFieldValue(
875
                        row, 2));
876
                m_Renderer.addSymbol(theInterval, theSymbol);
877
            }
878

    
879
            ///m_Renderer.m_legendIntervals.add(theInterval);
880
        }
881

    
882
        /*        if (bRestoValores) {
883
           //m_Renderer.m_bUseDefaultSymbol = true;
884
           theSymbol = (FSymbol) m_symbolTable.getFieldValue(hasta, 0);
885
           m_Renderer.setDefaultSymbol(theSymbol);
886
           }*/
887
    }
888

    
889
    /**
890
     * This method initializes panelC
891
     *
892
     * @return javax.swing.JPanel
893
     */
894
    private JPanel getPanelC() {
895
        if (panelC == null) {
896
            panelC = new JPanel();
897
            panelC.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0,
898
                    0, 0));
899
            panelC.setPreferredSize(new java.awt.Dimension(420, 200));
900
            panelC.add(m_symbolTable);
901
        }
902

    
903
        return panelC;
904
    }
905

    
906
    /**
907
     * A?ade la fila y el s?mbolo para el resto de valores no representados.
908
     */
909
    private void addDefault() {
910
            
911
        auxLegend.getDefaultSymbol().setDescription("Default");
912
        auxLegend.addSymbol(new NullIntervalValue(),
913
            auxLegend.getDefaultSymbol());
914
        m_symbolTable.addTableRecord(auxLegend.getDefaultSymbol(),
915
            new NullIntervalValue(),
916
            auxLegend.getDefaultSymbol().getDescription());
917
        m_symbolTable.repaint();
918
    }
919

    
920
    /**
921
     * Elimina la fila de la tabla y el s?mbolo del resto de valores no
922
     * representados por ning?n valor.
923
     */
924
    private void delDefault() {
925
        auxLegend.delSymbol(new NullIntervalValue());
926
        m_symbolTable.removeRow(new NullIntervalValue());
927
        m_symbolTable.repaint();
928
    }
929

    
930
    /**
931
     * Listener.
932
     *
933
     * @author Vicente Caballero Navarro
934
     */
935
    class MyListener implements ActionListener {
936
        //private FLyrShp m_layer;
937
        // private FPanelLegendValues m_Parent;
938
        public MyListener() { // FPanelLegendValues p) {
939

    
940
            // m_Parent = p;
941
        }
942

    
943
        /**
944
         * DOCUMENT ME!
945
         *
946
         * @param e DOCUMENT ME!
947
         */
948
        public void actionPerformed(ActionEvent e) {
949
            // rellenarValue();
950
            System.out.println("ActionEvent con " + e.getActionCommand());
951

    
952
            //modificar el combobox de valor
953
            if (e.getActionCommand() == "FIELD_SELECTED") {
954
                JComboBox cb = (JComboBox) e.getSource();
955
                String fieldName = (String) cb.getSelectedItem();
956
                System.out.println("Nombre del campo: " + fieldName);
957
                m_symbolTable.removeAllItems();
958

    
959
                m_Renderer.setFieldName(fieldName);
960
            } else if (e.getActionCommand() == "INTERVAL_TYPE") {
961
                JComboBox cb = (JComboBox) e.getSource();
962

    
963
                if ((m_Renderer != null) &&
964
                        (cb.getSelectedIndex() != m_Renderer.getIntervalType())) {
965
                    m_Renderer.setIntervalType(cb.getSelectedIndex());
966
                    m_symbolTable.removeAllItems();
967
                }
968
            }
969

    
970
            //A?adir todos los elementos por valor
971
            if (e.getActionCommand() == "ADD_ALL_VALUES") {
972
                fillTableValues();
973
            }
974

    
975
            //A?adir un ?nico elemento
976
            if (e.getActionCommand() == "ADD_VALUE") {
977
                try {
978
                    m_symbolTable.addTableRecord(new FSymbol(
979
                            m_lyr.getShapeType()), new FInterval(0, 0), "0 - 0");
980
                } catch (DriverException e1) {
981
                    e1.printStackTrace();
982
                }
983

    
984
                /*        a?adir("Nuevo_Valor");
985
                   aceptar.setEnabled(true);
986
                   quitartodo.setEnabled(true);
987
                   quitar.setEnabled(true); */
988
            }
989

    
990
            //Vacia la tabla
991
            if (e.getActionCommand() == "REMOVE_ALL") {
992
                m_symbolTable.removeAllItems();
993

    
994
                // aceptar.setEnabled(false);
995
            }
996

    
997
            //Quitar solo el elemento seleccionado
998
            if (e.getActionCommand() == "REMOVE") {
999
                m_symbolTable.removeSelectedRows();
1000
            }
1001
        }
1002
    }
1003
} //  @jve:decl-index=0:visual-constraint="10,10"