Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / PanelLegendBreaks.java @ 10626

History | View | Annotate | Download (34.4 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 java.awt.BorderLayout;
44
import java.awt.Color;
45
import java.awt.Component;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.sql.Types;
49
import java.text.NumberFormat;
50
import java.util.ArrayList;
51

    
52
import javax.swing.DefaultComboBoxModel;
53
import javax.swing.JButton;
54
import javax.swing.JCheckBox;
55
import javax.swing.JComboBox;
56
import javax.swing.JLabel;
57
import javax.swing.JOptionPane;
58
import javax.swing.JPanel;
59
import javax.swing.JTextField;
60

    
61
import org.apache.log4j.Logger;
62

    
63
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
64
import com.hardcode.gdbms.engine.data.DataSource;
65
import com.hardcode.gdbms.engine.values.DateValue;
66
import com.hardcode.gdbms.engine.values.DoubleValue;
67
import com.hardcode.gdbms.engine.values.FloatValue;
68
import com.hardcode.gdbms.engine.values.IntValue;
69
import com.hardcode.gdbms.engine.values.LongValue;
70
import com.hardcode.gdbms.engine.values.NullValue;
71
import com.hardcode.gdbms.engine.values.Value;
72
import com.iver.andami.PluginServices;
73
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
74
import com.iver.cit.gvsig.fmap.layers.FLayer;
75
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
76
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
77
import com.iver.cit.gvsig.fmap.rendering.FInterval;
78
import com.iver.cit.gvsig.fmap.rendering.IInterval;
79
import com.iver.cit.gvsig.fmap.rendering.Legend;
80
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
81
import com.iver.cit.gvsig.fmap.rendering.NullIntervalValue;
82
import com.iver.cit.gvsig.fmap.rendering.VectorialIntervalLegend;
83
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
84
import com.iver.cit.gvsig.project.documents.view.legend.NaturalIntervalGenerator;
85
import com.iver.cit.gvsig.project.documents.view.legend.QuantileIntervalGenerator;
86

    
87

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

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

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

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

    
180
        return panelN;
181
    }
182

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

    
197
        return cmbField;
198
    }
199

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

    
212
        return txtNumIntervals;
213
    }
214

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

    
228
        return colorChooserPanel;
229
    }
230

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

    
244
        return colorChooserPanel1;
245
    }
246

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

    
270
        return chkdefaultvalues;
271
    }
272

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

    
292
        return cmbFieldType;
293
    }
294

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

    
310
        return panelS;
311
    }
312

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

    
327
        return bintervals;
328
    }
329

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

    
343
        return bInsert;
344
    }
345

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

    
359
        return bDelAll;
360
    }
361

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

    
375
        return bDel;
376
    }
377

    
378
    /**
379
     * Damos una primera pasada para saber los l?mites inferior y superior y
380
     * rellenar un array con los valores. Luego dividimos ese array en
381
     * intervalos.
382
     */
383
    private void fillTableValues() {
384
        FInterval[] arrayIntervalos = null;
385
        DataSource elRs;
386
        m_symbolTable.removeAllItems();
387

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

    
393
            int idField = -1;
394
            int numIntervalos = 1;
395

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

    
402
            String fieldName = (String) cmbField.getSelectedItem();
403
            if (fieldName==null) {
404
                    JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_hay_campo_seleccionado"));
405
                    return;
406
            }
407
            System.out.println("Nombre del campo de clasificaci?n: " +
408
                fieldName);
409
            m_Renderer.setFieldName(fieldName);
410
            m_Renderer.useDefaultSymbol(getChkdefaultvalues().isSelected());
411
            String nomField = m_Renderer.getFieldName();
412

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

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

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

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

    
427
                    break;
428
                }
429
            }
430

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

    
434
                return;
435
            }
436

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

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

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

    
450
            Value clave;
451

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

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

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

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

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

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

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

    
493
                count++;
494
            }
495

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

    
501
                    break;
502

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

    
507
                    break;
508

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

    
513
                    break;
514
            }
515

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

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

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

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

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

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

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

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

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

    
578
                // }
579
            } // for
580

    
581
            System.out.println("Num. Simbolos = " +
582
                auxLegend.getValues().length);
583
            m_symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
584
                auxLegend.getValues(), auxLegend.getDescriptions());
585
            elRs.stop();
586
        } catch (ReadDriverException e) {
587
                        e.printStackTrace();
588
                }
589

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

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

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

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

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

    
624
        return theIntervalArray;
625
    }
626

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

    
643
        try {
644
            intervalGenerator.generarIntervalos();
645
        } catch (ReadDriverException e) {
646
            e.printStackTrace();
647
        }
648

    
649
        int numIntervalsGen = intervalGenerator.getNumIntervals() - 1;
650

    
651
        if (numIntervalsGen == -1) {
652
            //TODO cuando no puede calcular los intervalos.
653
            numIntervalsGen = 1;
654
        }
655

    
656
        FInterval[] theIntervalArray = new FInterval[numIntervalsGen];
657

    
658
        if (numIntervalsGen > 1) {
659
            theIntervalArray[0] = new FInterval(minValue,
660
                    intervalGenerator.getValorRuptura(0));
661

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

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

    
674
        return theIntervalArray;
675
    }
676

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

    
693
        try {
694
            intervalGenerator.generarIntervalos();
695
        } catch (ReadDriverException e) {
696
            e.printStackTrace();
697
        }
698

    
699
        int numIntervalsGen = intervalGenerator.getNumIntervalGen();
700
        FInterval[] theIntervalArray = new FInterval[numIntervalsGen];
701

    
702
        if (intervalGenerator.getNumIntervalGen() > 1) {
703
            theIntervalArray[0] = new FInterval(minValue,
704
                    intervalGenerator.getValRuptura(0));
705

    
706
            for (int i = 1; i < (numIntervalsGen - 1); i++) {
707
                theIntervalArray[i] = new FInterval(intervalGenerator.getValInit(i -
708
                            1), intervalGenerator.getValRuptura(i));
709
            }
710

    
711
            theIntervalArray[numIntervalsGen - 1] = new FInterval(intervalGenerator.getValInit(numIntervalsGen -
712
                        2), maxValue);
713
        } else {
714
            theIntervalArray[numIntervalsGen - 1] = new FInterval(minValue,
715
                    maxValue);
716
        }
717

    
718
        return theIntervalArray;
719

    
720
        /*        double numQ = count / numIntervals;
721
           for (int i = 0; i < count; i++) {
722
           }
723
           return theIntervalArray;
724
         */
725
    }
726

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

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

    
758
        cmbFieldType.setSelectedIndex(m_Renderer.getIntervalType());
759
    }
760

    
761
    /**
762
     * DOCUMENT ME!
763
     */
764
    private void fillFieldNames() {
765
        DataSource rs = null;
766
        ArrayList nomFields = null;
767

    
768
        try {
769
            rs = ((AlphanumericData) m_lyr).getRecordset();
770
            logger.debug("rs.start()");
771
            rs.start();
772

    
773
            nomFields = new ArrayList();
774

    
775
            int type;
776
            for (int i = 0; i < rs.getFieldCount(); i++) {
777
                type = rs.getFieldType(i);
778

    
779
                if (type == Types.NULL) {
780
                    continue;
781
                }
782

    
783
                if ((type == Types.INTEGER) ||
784
                        (type == Types.DOUBLE) ||
785
                        (type == Types.FLOAT) ||
786
                        (type == Types.BIGINT)) {
787
                    nomFields.add(rs.getFieldName(i).trim());
788
                }
789
            }
790

    
791
            rs.stop();
792
        } catch (ReadDriverException e1) {
793
            e1.printStackTrace();
794
        }
795

    
796
        /*        ArrayList nomFields = new ArrayList();
797
           for (int i = 0; i < rs.getFieldsCount(); i++) {
798
                   if ((rs.getFieldType(i) == FRecordset.INTEGER) ||
799
                           (rs.getFieldType(i) == FRecordset.DECIMAL) ||
800
                           (rs.getFieldType(i) == FRecordset.DATE))
801
                   {
802
                           nomFields.add(rs.getFieldName(i).trim());
803
                   }
804

805
           }
806
         */
807
        DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields.toArray());
808
        cmbField.setModel(cM);
809

    
810
        // fieldsListValor.setSelectedIndex(0);
811
        m_symbolTable.removeAllItems();
812
    }
813

    
814
    /**
815
     * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
816
     */
817
    public Legend getLegend() {
818
        fillSymbolListFromTable();
819

    
820
        if (auxLegend != null) {
821
            m_Renderer.setDefaultSymbol(auxLegend.getDefaultSymbol());
822
            m_Renderer.useDefaultSymbol(chkdefaultvalues.isSelected());
823
        }
824

    
825
        return m_Renderer;
826
    }
827

    
828
    /**
829
     * A partir de los registros de la tabla, regenera el FRenderer. (No solo
830
     * el symbolList, si no tambi?n el arrayKeys y el defaultRenderer
831
     */
832
    private void fillSymbolListFromTable() {
833
        //String clave;
834
        FSymbol theSymbol;
835
        IInterval theInterval = null;
836

    
837
        // Borramos las anteriores listas:
838
        m_Renderer.clear();
839

    
840
        //boolean bRestoValores = false; // PONERLO EN UN CHECKBOX
841
        //double from = 0;
842
        //double to = 0;
843
        //String[] arraySplit = new String[2];
844
        //int hasta;
845
        String fieldName = (String) cmbField.getSelectedItem();
846
        m_Renderer.setFieldName(fieldName);
847

    
848
        /*if (bRestoValores) {
849
           hasta = m_symbolTable.getRowCount() - 1;
850
           } else {
851
                   hasta = m_symbolTable.getRowCount();
852
           }
853
         */
854
        for (int row = 0; row < m_symbolTable.getRowCount(); row++) {
855
            // clave = m_symbolTable.getFieldValue(row,1);
856
            if (!(m_symbolTable.getFieldValue(row, 1) instanceof FInterval)) {
857
                theSymbol = (FSymbol) m_symbolTable.getFieldValue(row, 0);
858
                theSymbol.setDescription((String) m_symbolTable.getFieldValue(
859
                        row, 2));
860
                m_Renderer.addSymbol(new NullIntervalValue(), theSymbol);
861
            } else {
862
                theInterval = (IInterval) m_symbolTable.getFieldValue(row, 1);
863
                theSymbol = (FSymbol) m_symbolTable.getFieldValue(row, 0);
864
                theSymbol.setDescription((String) m_symbolTable.getFieldValue(
865
                        row, 2));
866
                m_Renderer.addSymbol(theInterval, theSymbol);
867
            }
868

    
869
            ///m_Renderer.m_legendIntervals.add(theInterval);
870
        }
871

    
872
        /*        if (bRestoValores) {
873
           //m_Renderer.m_bUseDefaultSymbol = true;
874
           theSymbol = (FSymbol) m_symbolTable.getFieldValue(hasta, 0);
875
           m_Renderer.setDefaultSymbol(theSymbol);
876
           }*/
877
    }
878

    
879
    /**
880
     * This method initializes panelC
881
     *
882
     * @return javax.swing.JPanel
883
     */
884
    private JPanel getPanelC() {
885
        if (panelC == null) {
886
            panelC = new JPanel();
887
            panelC.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0,
888
                    0, 0));
889
            panelC.setPreferredSize(new java.awt.Dimension(420, 200));
890
            panelC.add(m_symbolTable);
891
        }
892

    
893
        return panelC;
894
    }
895

    
896
    /**
897
     * A?ade la fila y el s?mbolo para el resto de valores no representados.
898
     */
899
    private void addDefault() {
900

    
901
        auxLegend.getDefaultSymbol().setDescription("Default");
902
        auxLegend.addSymbol(new NullIntervalValue(),
903
            auxLegend.getDefaultSymbol());
904
        m_symbolTable.addTableRecord(auxLegend.getDefaultSymbol(),
905
            new NullIntervalValue(),
906
            auxLegend.getDefaultSymbol().getDescription());
907
        m_symbolTable.repaint();
908
    }
909

    
910
    /**
911
     * Elimina la fila de la tabla y el s?mbolo del resto de valores no
912
     * representados por ning?n valor.
913
     */
914
    private void delDefault() {
915
        auxLegend.delSymbol(new NullIntervalValue());
916
        m_symbolTable.removeRow(new NullIntervalValue());
917
        m_symbolTable.repaint();
918
    }
919

    
920
    /**
921
     * Listener.
922
     *
923
     * @author Vicente Caballero Navarro
924
     */
925
    class MyListener implements ActionListener {
926
        //private FLyrShp m_layer;
927
        // private FPanelLegendValues m_Parent;
928
        public MyListener() { // FPanelLegendValues p) {
929

    
930
            // m_Parent = p;
931
        }
932

    
933
        /**
934
         * DOCUMENT ME!
935
         *
936
         * @param e DOCUMENT ME!
937
         */
938
        public void actionPerformed(ActionEvent e) {
939
            // rellenarValue();
940
            System.out.println("ActionEvent con " + e.getActionCommand());
941

    
942
            //modificar el combobox de valor
943
            if (e.getActionCommand() == "FIELD_SELECTED") {
944
                JComboBox cb = (JComboBox) e.getSource();
945
                String fieldName = (String) cb.getSelectedItem();
946
                System.out.println("Nombre del campo: " + fieldName);
947
                m_symbolTable.removeAllItems();
948

    
949
                m_Renderer.setFieldName(fieldName);
950
            } else if (e.getActionCommand() == "INTERVAL_TYPE") {
951
                JComboBox cb = (JComboBox) e.getSource();
952

    
953
                if ((m_Renderer != null) &&
954
                        (cb.getSelectedIndex() != m_Renderer.getIntervalType())) {
955
                    m_Renderer.setIntervalType(cb.getSelectedIndex());
956
                    m_symbolTable.removeAllItems();
957
                }
958
            }
959

    
960
            //A?adir todos los elementos por valor
961
            if (e.getActionCommand() == "ADD_ALL_VALUES") {
962
                fillTableValues();
963
            }
964

    
965
            //A?adir un ?nico elemento
966
            if (e.getActionCommand() == "ADD_VALUE") {
967
                try {
968
                    m_symbolTable.addTableRecord(new FSymbol(
969
                            m_lyr.getShapeType()), new FInterval(0, 0), "0 - 0");
970
                } catch (ReadDriverException e1) {
971
                    e1.printStackTrace();
972
                }
973

    
974
                /*        a?adir("Nuevo_Valor");
975
                   aceptar.setEnabled(true);
976
                   quitartodo.setEnabled(true);
977
                   quitar.setEnabled(true); */
978
            }
979

    
980
            //Vacia la tabla
981
            if (e.getActionCommand() == "REMOVE_ALL") {
982
                m_symbolTable.removeAllItems();
983

    
984
                // aceptar.setEnabled(false);
985
            }
986

    
987
            //Quitar solo el elemento seleccionado
988
            if (e.getActionCommand() == "REMOVE") {
989
                m_symbolTable.removeSelectedRows();
990
            }
991
        }
992
    }
993
} //  @jve:decl-index=0:visual-constraint="10,10"