Statistics
| Revision:

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

History | View | Annotate | Download (34.6 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.gui;
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.NaturalIntervalGenerator;
67
import com.iver.cit.gvsig.project.documents.view.legend.QuantileIntervalGenerator;
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.sql.Types;
77
import java.text.NumberFormat;
78

    
79
import java.util.ArrayList;
80

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

    
90

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

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

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

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

    
183
        return panelN;
184
    }
185

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

    
200
        return cmbField;
201
    }
202

    
203
    /**
204
     * This method initializes txtNumIntervals
205
     *
206
     * @return javax.swing.JTextField
207
     */
208
    private JTextField getTxtNumIntervals() {
209
        if (txtNumIntervals == null) {
210
            txtNumIntervals = new JTextField();
211
            txtNumIntervals.setText("5");
212
            txtNumIntervals.setBounds(new java.awt.Rectangle(449, 49, 37, 20));
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
    private 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.addActionListener(new java.awt.event.ActionListener() {
262
                    public void actionPerformed(java.awt.event.ActionEvent e) {
263
                            if (auxLegend==null)auxLegend=m_Renderer;
264
                            if (chkdefaultvalues.isSelected()) {
265
                            addDefault();
266
                        } else {
267
                            delDefault();
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 getJComboBox1() {
282
        if (cmbFieldType == null) {
283
            cmbFieldType = new JComboBox();
284
            cmbFieldType.setBounds(new java.awt.Rectangle(124, 26, 212, 20));
285
            cmbFieldType.setActionCommand("INTERVAL_TYPE");
286
            cmbFieldType.addActionListener(listener);
287
            cmbFieldType.addItem(PluginServices.getText(this, "equal_intervals"));
288
            cmbFieldType.addItem(PluginServices.getText(this,
289
                    "natural_intervals"));
290
            cmbFieldType.addItem(PluginServices.getText(this,
291
                    "quantile_intervals"));
292
            cmbFieldType.setVisible(true);
293
        }
294

    
295
        return cmbFieldType;
296
    }
297

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

    
313
        return panelS;
314
    }
315

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

    
330
        return bintervals;
331
    }
332

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

    
346
        return bInsert;
347
    }
348

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

    
362
        return bDelAll;
363
    }
364

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

    
378
        return bDel;
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
    private void fillTableValues() {
387
        FInterval[] arrayIntervalos = null;
388
        DataSource elRs;
389
        m_symbolTable.removeAllItems();
390

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

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

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

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

    
410
            String nomField = m_Renderer.getFieldName();
411

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

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

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

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

    
426
                    break;
427
                }
428
            }
429

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

    
433
                return;
434
            }
435

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

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

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

    
449
            Value clave;
450

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

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

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

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

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

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

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

    
492
                count++;
493
            }
494

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

    
500
                    break;
501

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

    
506
                    break;
507

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

    
512
                    break;
513
            }
514

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

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

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

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

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

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

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

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

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

    
577
                // }
578
            } // for
579

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

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

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

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

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

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

    
626
        return theIntervalArray;
627
    }
628

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

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

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

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

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

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

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

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

    
678
        return theIntervalArray;
679
    }
680

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

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

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

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

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

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

    
724
        return theIntervalArray;
725

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

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

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

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

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

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

    
780
            nomFields = new ArrayList();
781

    
782
            int type;
783
            for (int i = 0; i < rs.getFieldCount(); i++) {
784
                type = rs.getFieldType(i);
785

    
786
                if (type == Types.NULL) {
787
                    continue;
788
                }
789

    
790
                if ((type == Types.INTEGER) ||
791
                        (type == Types.DOUBLE) ||
792
                        (type == Types.FLOAT) ||
793
                        (type == Types.BIGINT)) {
794
                    nomFields.add(rs.getFieldName(i).trim());
795
                }
796
            }
797

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

    
805
        /*        ArrayList nomFields = new ArrayList();
806
           for (int i = 0; i < rs.getFieldsCount(); i++) {
807
                   if ((rs.getFieldType(i) == FRecordset.INTEGER) ||
808
                           (rs.getFieldType(i) == FRecordset.DECIMAL) ||
809
                           (rs.getFieldType(i) == FRecordset.DATE))
810
                   {
811
                           nomFields.add(rs.getFieldName(i).trim());
812
                   }
813

814
           }
815
         */
816
        DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields.toArray());
817
        cmbField.setModel(cM);
818

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

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

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

    
834
        return m_Renderer;
835
    }
836

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

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

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

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

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

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

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

    
902
        return panelC;
903
    }
904

    
905
    /**
906
     * A?ade la fila y el s?mbolo para el resto de valores no representados.
907
     */
908
    private void addDefault() {
909

    
910
        auxLegend.getDefaultSymbol().setDescription("Default");
911
        auxLegend.addSymbol(new NullIntervalValue(),
912
            auxLegend.getDefaultSymbol());
913
        m_symbolTable.addTableRecord(auxLegend.getDefaultSymbol(),
914
            new NullIntervalValue(),
915
            auxLegend.getDefaultSymbol().getDescription());
916
        m_symbolTable.repaint();
917
    }
918

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

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

    
939
            // m_Parent = p;
940
        }
941

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

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

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

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

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

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

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

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

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

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