Statistics
| Revision:

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

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

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

    
60
import org.apache.log4j.Logger;
61

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

    
85

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

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

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

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

    
178
        return panelN;
179
    }
180

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

    
195
        return cmbField;
196
    }
197

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

    
210
        return txtNumIntervals;
211
    }
212

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

    
226
        return colorChooserPanel;
227
    }
228

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

    
242
        return colorChooserPanel1;
243
    }
244

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

    
268
        return chkdefaultvalues;
269
    }
270

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

    
290
        return cmbFieldType;
291
    }
292

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

    
308
        return panelS;
309
    }
310

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

    
325
        return bintervals;
326
    }
327

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

    
341
        return bInsert;
342
    }
343

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

    
357
        return bDelAll;
358
    }
359

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

    
373
        return bDel;
374
    }
375

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

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

    
391
            int idField = -1;
392
            int numIntervalos = 1;
393

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

    
400
            String fieldName = (String) cmbField.getSelectedItem();
401
            System.out.println("Nombre del campo de clasificaci?n: " +
402
                fieldName);
403
            m_Renderer.setFieldName(fieldName);
404

    
405
            String nomField = m_Renderer.getFieldName();
406

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

    
412
            Color endColor = colorChooserPanel1.getColor();
413
            m_Renderer.setEndColor(endColor);
414

    
415
            for (int i = 0; i < elRs.getFieldCount(); i++) {
416
                String nomAux = elRs.getFieldName(i).trim();
417

    
418
                if (nomField.compareToIgnoreCase(nomAux) == 0) {
419
                    idField = i;
420

    
421
                    break;
422
                }
423
            }
424

    
425
            if (idField == -1) {
426
                System.err.println("Campo no reconocido " + nomField);
427

    
428
                return;
429
            }
430

    
431
            ///m_symbolTable.removeAllItems();
432
            ///m_valuesCache = new Object[m_lyr.getSource().getShapeCount()];
433
            int numSymbols = 0;
434
            FSymbol theSymbol;
435

    
436
            //Date valorDate;
437
            double minValue = Double.MAX_VALUE;
438
            double maxValue = Double.MIN_VALUE;
439

    
440
            //Date minValueDate = null;
441
            //Date maxValueDate = null;
442
            auxLegend = LegendFactory.createVectorialIntervalLegend(m_lyr.getShapeType());
443

    
444
            Value clave;
445

    
446
            //Object resul;
447
            int symbolType = 0;
448
            count = 0;
449

    
450
            for (int j = 0; j < elRs.getRowCount(); j++) {
451
                clave = elRs.getFieldValue(j, idField);
452

    
453
                IInterval interval = auxLegend.getInterval(clave);
454

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

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

    
478
                    if (valor < minValue) {
479
                        minValue = (double) valor;
480
                    }
481

    
482
                    if (valor > maxValue) {
483
                        maxValue = (double) valor;
484
                    }
485
                }
486

    
487
                count++;
488
            }
489

    
490
            switch (m_Renderer.getIntervalType()) {
491
                case VectorialIntervalLegend.EQUAL_INTERVALS:
492
                    arrayIntervalos = calculateEqualIntervals(numIntervalos,
493
                            minValue, maxValue, fieldName);
494

    
495
                    break;
496

    
497
                case VectorialIntervalLegend.NATURAL_INTERVALS:
498
                    arrayIntervalos = calculateNaturalIntervals(numIntervalos,
499
                            minValue, maxValue, fieldName);
500

    
501
                    break;
502

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

    
507
                    break;
508
            }
509

    
510
            //intervals = arrayIntervalos;
511
            FInterval elIntervalo;
512
            NumberFormat.getInstance().setMaximumFractionDigits(2);
513
            m_Renderer.clear();
514

    
515
            int r;
516
            int g;
517
            int b;
518
            int stepR;
519
            int stepG;
520
            int stepB;
521
            r = startColor.getRed();
522
            g = startColor.getGreen();
523
            b = startColor.getBlue();
524
            stepR = (endColor.getRed() - r) / arrayIntervalos.length;
525
            stepG = (endColor.getGreen() - g) / arrayIntervalos.length;
526
            stepB = (endColor.getBlue() - b) / arrayIntervalos.length;
527

    
528
            if (chkdefaultvalues.isSelected()) {
529
                auxLegend.getDefaultSymbol().setDescription("Default");
530
                auxLegend.addSymbol(new NullIntervalValue(),
531
                    auxLegend.getDefaultSymbol());
532
            }
533

    
534
            for (int k = 0; k < arrayIntervalos.length; k++) {
535
                elIntervalo = arrayIntervalos[k];
536

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

    
547
                //////////////////////////////////////
548
                // CALCULAMOS UN COLOR APROPIADO
549
                r = r + stepR;
550
                g = g + stepG;
551
                b = b + stepB;
552

    
553
                /////////////////////////////////
554
                auxLegend.addSymbol(elIntervalo, theSymbol);
555
                System.out.println("addSymbol = " + elIntervalo +
556
                    " theSymbol = " + theSymbol.getDescription());
557
                numSymbols++;
558

    
559
                if (numSymbols > 100) {
560
                    int resp = JOptionPane.showConfirmDialog(this,
561
                            PluginServices.getText(this, "mas_de_100_simbolos"),
562
                            PluginServices.getText(this, "quiere_continuar"),
563
                            JOptionPane.YES_NO_OPTION,
564
                            JOptionPane.WARNING_MESSAGE);
565

    
566
                    if ((resp == JOptionPane.NO_OPTION) ||
567
                            (resp == JOptionPane.DEFAULT_OPTION)) {
568
                        return;
569
                    }
570
                }
571

    
572
                // }
573
            } // for
574

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

    
587
        bDelAll.setEnabled(true);
588
        bDel.setEnabled(true);
589
    }
590

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

    
607
        if (numIntervals > 1) {
608
            theIntervalArray[0] = new FInterval(minValue, minValue + step);
609

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

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

    
621
        return theIntervalArray;
622
    }
623

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

    
640
        try {
641
            intervalGenerator.generarIntervalos();
642
        } catch (DriverException e) {
643
            e.printStackTrace();
644
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
645
            e.printStackTrace();
646
        }
647

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

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

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

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

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

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

    
673
        return theIntervalArray;
674
    }
675

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

    
692
        try {
693
            intervalGenerator.generarIntervalos();
694
        } catch (DriverException e) {
695
            e.printStackTrace();
696
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
697
            e.printStackTrace();
698
        }
699

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

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

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

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

    
719
        return theIntervalArray;
720

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

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

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

    
760
        cmbFieldType.setSelectedIndex(m_Renderer.getIntervalType());
761
    }
762

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

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

    
775
            nomFields = new ArrayList();
776

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

    
781
                if (type == Types.NULL) {
782
                    continue;
783
                }
784

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

    
793
            rs.stop();
794
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
795
            e1.printStackTrace();
796
        } catch (DriverException e) {
797
            e.printStackTrace();
798
        }
799

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

809
           }
810
         */
811
        DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields.toArray());
812
        cmbField.setModel(cM);
813

    
814
        // fieldsListValor.setSelectedIndex(0);
815
        m_symbolTable.removeAllItems();
816
    }
817

    
818
    /**
819
     * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
820
     */
821
    public Legend getLegend() {
822
        fillSymbolListFromTable();
823

    
824
        if (auxLegend != null) {
825
            m_Renderer.setDefaultSymbol(auxLegend.getDefaultSymbol());
826
            m_Renderer.useDefaultSymbol(chkdefaultvalues.isSelected());
827
        }
828

    
829
        return m_Renderer;
830
    }
831

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

    
841
        // Borramos las anteriores listas:
842
        m_Renderer.clear();
843

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

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

    
873
            ///m_Renderer.m_legendIntervals.add(theInterval);
874
        }
875

    
876
        /*        if (bRestoValores) {
877
           //m_Renderer.m_bUseDefaultSymbol = true;
878
           theSymbol = (FSymbol) m_symbolTable.getFieldValue(hasta, 0);
879
           m_Renderer.setDefaultSymbol(theSymbol);
880
           }*/
881
    }
882

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

    
897
        return panelC;
898
    }
899

    
900
    /**
901
     * A?ade la fila y el s?mbolo para el resto de valores no representados.
902
     */
903
    private void addDefault() {
904

    
905
        auxLegend.getDefaultSymbol().setDescription("Default");
906
        auxLegend.addSymbol(new NullIntervalValue(),
907
            auxLegend.getDefaultSymbol());
908
        m_symbolTable.addTableRecord(auxLegend.getDefaultSymbol(),
909
            new NullIntervalValue(),
910
            auxLegend.getDefaultSymbol().getDescription());
911
        m_symbolTable.repaint();
912
    }
913

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

    
924
    /**
925
     * Listener.
926
     *
927
     * @author Vicente Caballero Navarro
928
     */
929
    class MyListener implements ActionListener {
930
        //private FLyrShp m_layer;
931
        // private FPanelLegendValues m_Parent;
932
        public MyListener() { // FPanelLegendValues p) {
933

    
934
            // m_Parent = p;
935
        }
936

    
937
        /**
938
         * DOCUMENT ME!
939
         *
940
         * @param e DOCUMENT ME!
941
         */
942
        public void actionPerformed(ActionEvent e) {
943
            // rellenarValue();
944
            System.out.println("ActionEvent con " + e.getActionCommand());
945

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

    
953
                m_Renderer.setFieldName(fieldName);
954
            } else if (e.getActionCommand() == "INTERVAL_TYPE") {
955
                JComboBox cb = (JComboBox) e.getSource();
956

    
957
                if ((m_Renderer != null) &&
958
                        (cb.getSelectedIndex() != m_Renderer.getIntervalType())) {
959
                    m_Renderer.setIntervalType(cb.getSelectedIndex());
960
                    m_symbolTable.removeAllItems();
961
                }
962
            }
963

    
964
            //A?adir todos los elementos por valor
965
            if (e.getActionCommand() == "ADD_ALL_VALUES") {
966
                fillTableValues();
967
            }
968

    
969
            //A?adir un ?nico elemento
970
            if (e.getActionCommand() == "ADD_VALUE") {
971
                try {
972
                    m_symbolTable.addTableRecord(new FSymbol(
973
                            m_lyr.getShapeType()), new FInterval(0, 0), "0 - 0");
974
                } catch (DriverException e1) {
975
                    e1.printStackTrace();
976
                }
977

    
978
                /*        a?adir("Nuevo_Valor");
979
                   aceptar.setEnabled(true);
980
                   quitartodo.setEnabled(true);
981
                   quitar.setEnabled(true); */
982
            }
983

    
984
            //Vacia la tabla
985
            if (e.getActionCommand() == "REMOVE_ALL") {
986
                m_symbolTable.removeAllItems();
987

    
988
                // aceptar.setEnabled(false);
989
            }
990

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