Statistics
| Revision:

svn-gvsig-desktop / branches / simbologia / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / DotDensity.java @ 10437

History | View | Annotate | Download (18.9 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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

    
42
/* CVS MESSAGES:
43
*
44
* $Id: DotDensity.java 10437 2007-02-21 07:35:14Z jaume $
45
* $Log$
46
* Revision 1.1.2.4  2007-02-21 07:35:14  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1.2.3  2007/02/12 15:14:41  jaume
50
* refactored interval legend and added graduated symbol legend
51
*
52
* Revision 1.1.2.2  2007/02/09 11:00:03  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.1.2.1  2007/01/26 13:49:03  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.4  2006/11/17 13:53:45  cesar
59
* *** empty log message ***
60
*
61
* Revision 1.3  2006/11/17 12:50:36  jaume
62
* tama?o de punto defecto 2
63
*
64
* Revision 1.2  2006/11/15 12:57:31  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1  2006/11/14 11:10:27  jaume
68
* *** empty log message ***
69
*
70
*
71
*/
72
package com.iver.cit.gvsig.project.documents.view.legend.gui;
73

    
74
import java.awt.BorderLayout;
75
import java.awt.Color;
76
import java.awt.ComponentOrientation;
77
import java.awt.Dimension;
78
import java.awt.FlowLayout;
79
import java.awt.GridLayout;
80
import java.awt.LayoutManager;
81
import java.awt.event.ActionEvent;
82
import java.awt.event.ActionListener;
83
import java.awt.event.ItemEvent;
84
import java.awt.event.ItemListener;
85
import java.sql.Types;
86
import java.text.NumberFormat;
87

    
88
import javax.swing.BorderFactory;
89
import javax.swing.BoxLayout;
90
import javax.swing.ButtonGroup;
91
import javax.swing.ImageIcon;
92
import javax.swing.JComboBox;
93
import javax.swing.JLabel;
94
import javax.swing.JOptionPane;
95
import javax.swing.JPanel;
96
import javax.swing.JRadioButton;
97
import javax.swing.JSlider;
98
import javax.swing.JTextField;
99
import javax.swing.event.ChangeEvent;
100
import javax.swing.event.ChangeListener;
101

    
102
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
103

    
104
import com.hardcode.gdbms.engine.data.DataSource;
105
import com.hardcode.gdbms.engine.data.DataSourceFactory;
106
import com.hardcode.gdbms.engine.values.NumericValue;
107
import com.iver.andami.PluginServices;
108
import com.iver.andami.messages.NotificationManager;
109
import com.iver.cit.gvsig.fmap.core.FShape;
110
import com.iver.cit.gvsig.fmap.core.symbols.DotDensityFillSymbol;
111
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
112
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
113
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerFillSymbol;
114
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
115
import com.iver.cit.gvsig.fmap.layers.FLayer;
116
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
117
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
118
import com.iver.cit.gvsig.fmap.rendering.DotDensityLegend;
119
import com.iver.cit.gvsig.fmap.rendering.Legend;
120
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
121

    
122
/**
123
 *
124
 * @author jaume dominguez faus - jaume.dominguez@iver.es
125
 *
126
 */
127
public class DotDensity extends JPanel implements ILegendPanel {
128
        private FLyrVect layer;
129
        private JPanel northPanel = null;
130
        private GridBagLayoutPanel densityButtonsPanel = null;
131
        private JPanel pnlDensities = null;
132
        private JComboBox cmbLegendField = null;
133
        private JRadioButton rdBtnHigh = null;
134
        private JRadioButton rdBtnMedium = null;
135
        private JRadioButton rdBtnLow = null;
136
        private JTextField numDotSize = null;
137
        private double max, min, mean;
138
        private JTextField nmbrDotValue = null;
139
        private JLabel lblLabellingField = null;
140
        private DotDensityLegend legend;
141
        private String fieldName;
142
        private boolean initializing;
143

    
144
        private MyListener cmbAction = new MyListener();
145
        NumberFormat nf = NumberFormat.getInstance();
146

    
147
        {
148
                nf.setMaximumFractionDigits(3);
149
        }
150

    
151
        private class MyListener implements ItemListener, ActionListener {
152

    
153

    
154
                public void itemStateChanged(ItemEvent e) {
155
                        if (!initializing)
156
                                doIt();
157
                }
158

    
159
                public void actionPerformed(ActionEvent e) {
160
                        if (!initializing)
161
                                doIt();
162
                }
163

    
164
                private void doIt() {
165
                        int index = cmbLegendField.getSelectedIndex();
166
            try {
167
                SelectableDataSource sds = layer.getRecordset();
168

    
169
                if (index != -1) {
170
                    fieldName = (String) cmbLegendField.getSelectedItem();
171
                } else {
172
                    fieldName = (String) cmbLegendField.getItemAt(0);
173
                }
174

    
175
                String sql = "select "+fieldName+" from '"+sds.getName()+"';";
176

    
177
                DataSource recordset = sds.getDataSourceFactory().executeSQL(sql, DataSourceFactory.AUTOMATIC_OPENING );
178
                // TODO implement MAX() and MIN() functions in GDBMS!!!!!
179
                max = Double.MIN_VALUE;
180
                min = Double.MAX_VALUE;
181
                for (int i = 0; i < sds.getRowCount(); i++) {
182
                    double value = ((NumericValue) recordset.getFieldValue(i,0)).doubleValue();
183
                    max = Math.max(max, value);
184
                    min = Math.min(min, value);
185
                }
186
                mean = (max+min) /2;
187
                buttonsListener.actionPerformed(null);
188

    
189
            } catch (Exception ex) {
190
                ex.printStackTrace();
191
            }
192
                }
193
        };
194
        private ActionListener buttonsListener = new ActionListener() {
195
            public void actionPerformed(ActionEvent e) {
196
                    if (getRdBtnHigh().isSelected()) {
197
                            getSldDensity().setValue(66);
198
                    } else if (getRdBtnLow().isSelected()) {
199
                            getSldDensity().setValue(33);
200
                    } else if (getRdBtnMedium().isSelected()) {
201
                            getSldDensity().setValue(50);
202
                    }
203
            }
204
    };
205
        private JPanel centerPanel = null;
206
        private JSlider sldDensity = null;
207

    
208
        private ChangeListener sldListener = new ChangeListener() {
209

    
210
                public void stateChanged(ChangeEvent e) {
211

    
212
                        // This is my messy statistic aggregation function
213
                        // TODO if I can, I'll have a look to my statistic books to figure out one better
214
                        int quantileIndex = getSldDensity().getValue();
215
                        final int topLimit = 98;
216
                        if (quantileIndex > topLimit)
217
                                quantileIndex = topLimit;
218
                        double quantileAmount = (max-min)/100;
219
                        nmbrDotValue.setText(nf.format(3*quantileAmount/Math.log(quantileIndex)));
220
                }
221

    
222
        };
223
        private ColorChooserPanel jcc;
224
        private ColorChooserPanel outlineColorChooserPanel;
225
        private Legend oldLegend;
226
        private JSymbolPreviewButton btnOutline;
227

    
228
        public DotDensity() {
229
        super();
230
        initialize();
231
    }
232

    
233
    /**
234
     * This method initializes this
235
     *
236
     */
237
    private void initialize() {
238
        this.setLayout(new BorderLayout());
239
        this.setSize(new java.awt.Dimension(492,278));
240
        this.add(getNorthPanel(), java.awt.BorderLayout.NORTH);
241

    
242
        this.add(getCenterPanel(), java.awt.BorderLayout.CENTER);
243
    }
244

    
245
    public void setData(FLayer lyr, Legend legend) {
246
        this.layer = (FLyrVect) lyr;
247
        this.oldLegend = legend;
248
        try {
249
                SelectableDataSource sds = layer.getRecordset();
250
                initializing = true; // silents events to the combo box
251
                cmbLegendField.removeAllItems();
252
                String[] fNames = sds.getFieldNames();
253
                for (int i = 0; i < fNames.length; i++) {
254
                        if (isNumericField(sds.getFieldType(i))) {
255
                                cmbLegendField.addItem(fNames[i]);
256
                        }
257
                }
258
                if (!(legend instanceof DotDensityLegend)) {
259
                        legend = new DotDensityLegend();
260
                        ((DotDensityLegend) legend).setFieldName((String) cmbLegendField.getItemAt(0));
261
                }
262

    
263
                DotDensityLegend theLegend = (DotDensityLegend) legend;
264

    
265
                initializing = false; // enables events to the combo box
266

    
267
                cmbLegendField.setSelectedItem(theLegend.getFieldName());
268
                try {
269
                        getDotColorChooserPanel().setColor(theLegend.getDotColor());
270
                } catch (NullPointerException npEx) {
271
                        getDotColorChooserPanel().setColor(Color.RED);
272
                }
273
                getBtnOutline().setSymbol(theLegend.getOutline());
274
                try {
275
                        double dotValue = theLegend.getDotValue();
276
                        if (dotValue <= 0)
277
                                dotValue = 100;
278
                        getNmbrDotValue().setText(String.valueOf(dotValue));
279
                } catch (NullPointerException npEx) {
280
                        getNmbrDotValue().setText(String.valueOf(max));
281
                }
282
                try {
283
                        double dotSize = theLegend.getDotSize();
284
                        if (dotSize <= 0)
285
                                dotSize = 2;
286
                        getNumDotSize().setText(String.valueOf(dotSize));
287
                } catch (NullPointerException npEx) {
288
                        getNumDotSize().setText(String.valueOf(3));
289
                }
290

    
291
        } catch (Exception e) {
292
                e.printStackTrace();
293
        }
294
    }
295

    
296
    private boolean isNumericField(int fieldType) {
297
        switch (fieldType) {
298
        case Types.BIGINT:
299
        case Types.DECIMAL:
300
        case Types.DOUBLE:
301
        case Types.FLOAT:
302
        case Types.INTEGER:
303
        case Types.NUMERIC:
304
        case Types.REAL:
305
        case Types.SMALLINT:
306
        case Types.TINYINT:
307
            return true;
308
        default:
309
            return false;
310
        }
311

    
312
    }
313

    
314
    public Legend getLegend() {
315
            try {
316
                        int shapeType = layer.getShapeType();
317

    
318
                    // shapeType should be always polygon
319
                    if (shapeType != FShape.POLYGON) {
320
                            NotificationManager.addError(PluginServices.getText(this, "cannot_apply_to_a_non_polygon_layer"), new Exception());
321
                    }
322

    
323
                    // check if the field exists (this is probably dead code)
324
                    SelectableDataSource sds;
325
                    sds = layer.getRecordset();
326
                    if (-1 == sds.getFieldIndexByName(fieldName))
327
                            return null;
328

    
329
                    // gather values
330
                    double dotValue;
331
                    double dotSize;
332
                    try {
333
                            dotValue = Double.parseDouble(nmbrDotValue.getText());
334
                    } catch (Exception e) {
335
                            dotValue = nf.parse(nmbrDotValue.getText()).doubleValue();
336
                    }
337
                    if (dotValue == 0)
338
                            dotValue = 1;
339
                    try {
340
                            dotSize = Double.parseDouble(numDotSize.getText());
341
                    } catch (Exception e) {
342
                            dotSize = nf.parse(numDotSize.getText()).doubleValue();
343
                    }
344

    
345
                    if (max/dotValue > 50000) {
346
                            int option = JOptionPane.showConfirmDialog(this,
347
                                            PluginServices.getText(this, "looks_like_too_low_value_for_this_field_may_cause_system_to_run_slow"),
348
                                            PluginServices.getText(this, "warning"),
349
                                            JOptionPane.OK_CANCEL_OPTION);
350
                            if (option        == JOptionPane.CANCEL_OPTION)
351
                                    return oldLegend;
352
                    }
353

    
354
                    // create the density symbol with the values set above
355
                    DotDensityFillSymbol densitySymbol = new DotDensityFillSymbol();
356
                    densitySymbol.setDotSize(dotSize);
357
                    densitySymbol.setDotColor(getDotColorChooserPanel().getColor());
358

    
359
                    // create a simple-fill symbol over which the dot density will be drawn
360
                    SimpleFillSymbol fillSymbol = new SimpleFillSymbol();
361
                    fillSymbol.setFillColor(Color.WHITE);
362
                    fillSymbol.setOutline((ILineSymbol) getBtnOutline().getSymbol());
363

    
364
                    // combine both the DotDensitySymbol and the SimpleFillSymbol in
365
                    // MultiLayerSymbol so they will be paint as a unique ISymbol
366
                    MultiLayerFillSymbol symbol = new MultiLayerFillSymbol();
367
//                    symbol.setMultiLayerSymbolType(FShape.POLYGON);
368
                    symbol.setDescription(
369
                                    "DotDensitySymbol" + PluginServices.getText(this, "in_layer") +
370
                                    ": '"+layer.getName()+"'");
371
                    symbol.addLayer(fillSymbol);
372
                    symbol.addLayer(densitySymbol);
373

    
374
                    legend = new DotDensityLegend();
375
                    legend.addSymbol("theSymbol", symbol);
376
                    legend.setDefaultSymbol(symbol);
377
                    legend.setDotValue(dotValue);
378
                    legend.setFieldName(fieldName);
379

    
380

    
381
            } catch (Exception e) {
382
                    NotificationManager.addError(PluginServices.getText(this, "could_not_setup_legend")+".", e);
383
            }
384
            return legend;
385

    
386
    }
387

    
388
    /**
389
     * This method initializes centerPanel
390
     *
391
     * @return javax.swing.JPanel
392
     */
393
    private JPanel getNorthPanel() {
394
        if (northPanel == null) {
395
            lblLabellingField = new JLabel();
396
            lblLabellingField.setText(PluginServices.getText(this, "labelling_field")+".");
397
            northPanel = new JPanel(new FlowLayout(FlowLayout.LEADING,15,0));
398
            northPanel.add(lblLabellingField, null);
399
            northPanel.add(getCmbLegendField(), null);
400

    
401
        }
402
        return northPanel;
403
    }
404

    
405
    private ColorChooserPanel getDotColorChooserPanel() {
406
            if (jcc == null) {
407
                    jcc = new ColorChooserPanel() ;
408
                    jcc.setAlpha(255);
409
            }
410
            return jcc;
411
        }
412

    
413
        /**
414
     * This method initializes southPanel
415
     *
416
     * @return javax.swing.JPanel
417
     */
418
    private JPanel getDensityButtonsPanel() {
419
        if (densityButtonsPanel == null) {
420
                densityButtonsPanel = new GridBagLayoutPanel();
421
                LayoutManager layout = new FlowLayout(FlowLayout.LEADING, 0,0);
422
                JPanel aux = new JPanel(layout);
423
                aux.add(getNumDotSize());
424
                densityButtonsPanel.addComponent(
425
                                PluginServices.getText(this, "dot_size"), aux);
426
                aux = new JPanel(layout);
427
                aux.add(getNmbrDotValue());
428
            densityButtonsPanel.addComponent(
429
                            PluginServices.getText(this,"dot_value"), aux);
430
            aux = new JPanel(layout);
431
            aux.add(getDotColorChooserPanel());
432
            densityButtonsPanel.addComponent(
433
                            PluginServices.getText(this, "color"), aux);
434
            aux = new JPanel(layout);
435
            aux.add(getBtnOutline());
436
            densityButtonsPanel.addComponent(
437
                            PluginServices.getText(this, "outline_color"), aux);
438
        }
439
        return densityButtonsPanel;
440
    }
441

    
442
        private JSymbolPreviewButton getBtnOutline() {
443
                if (btnOutline == null) {
444
                        btnOutline = new JSymbolPreviewButton(FShape.LINE);
445
                        btnOutline.setPreferredSize(new Dimension(100, 35));
446
                }
447
                return btnOutline;
448
        }
449

    
450
        /**
451
     * This method initializes pnlDensities
452
     *
453
     * @return javax.swing.JPanel
454
     */
455
    private JPanel getPnlDensities() {
456
        if (pnlDensities == null) {
457
            pnlDensities = new JPanel(new BorderLayout(5,0));
458
            pnlDensities.setBorder(BorderFactory.createTitledBorder(null,
459
                                        PluginServices.getText(this, "densities")));
460
            JPanel aux2 = new JPanel();
461
            JPanel aux;
462
                        aux = new JPanel(new GridLayout(1,3));
463
            aux.add(new JLabel(PluginServices.getText(this, "high")));
464
            aux.add(new JLabel(PluginServices.getText(this, "medium")));
465
            aux.add(new JLabel(PluginServices.getText(this, "low")));
466

    
467
            aux2.add(aux);
468

    
469
                        aux = new JPanel(new GridLayout(1,3));
470
            aux.add(getRdBtnHigh());
471
            aux.add(getRdBtnMedium());
472
            aux.add(getRdBtnLow());
473

    
474
            aux2.add(aux);
475
            aux2.setLayout(new BoxLayout(aux2, BoxLayout.Y_AXIS));
476

    
477
            pnlDensities.add(aux2, BorderLayout.NORTH);
478
            pnlDensities.add(getSldDensity(), BorderLayout.CENTER);
479
            pnlDensities.add(getDensityButtonsPanel(), BorderLayout.SOUTH);
480

    
481
            ButtonGroup group = new ButtonGroup();
482
            group.add(getRdBtnHigh());
483
            group.add(getRdBtnLow());
484
            group.add(getRdBtnMedium());
485
            getRdBtnMedium().setSelected(true);
486
        }
487
        return pnlDensities;
488
    }
489

    
490

    
491
        /**
492
     * This method initializes cmbLegendField
493
     *
494
     * @return javax.swing.JComboBox
495
     */
496
    private JComboBox getCmbLegendField() {
497
        if (cmbLegendField == null) {
498
            cmbLegendField = new JComboBox();
499
            cmbLegendField.addActionListener(cmbAction);
500
        }
501
        return cmbLegendField;
502
    }
503

    
504
    /**
505
     * This method initializes rdBtnHigh
506
     *
507
     * @return javax.swing.JRadioButton
508
     */
509
    private JRadioButton getRdBtnHigh() {
510
        if (rdBtnHigh == null) {
511
            rdBtnHigh = new JRadioButton(new ImageIcon(
512
                            getClass().getClassLoader().
513
                                    getResource("images/high-density-sample.png")));
514
            rdBtnHigh.addActionListener(buttonsListener);
515
        }
516
        return rdBtnHigh;
517
    }
518

    
519
    /**
520
     * This method initializes rdBtnMedium
521
     *
522
     * @return javax.swing.JRadioButton
523
     */
524
    private JRadioButton getRdBtnMedium() {
525
        if (rdBtnMedium == null) {
526
            rdBtnMedium = new JRadioButton(new ImageIcon(
527
                            getClass().getClassLoader().
528
                                getResource("images/medium-density-sample.png")));
529
            rdBtnMedium.addActionListener(buttonsListener);
530
        }
531
        return rdBtnMedium;
532
    }
533

    
534
    /**
535
     * This method initializes rdBtnMax
536
     *
537
     * @return javax.swing.JRadioButton
538
     */
539
    private JRadioButton getRdBtnLow() {
540
        if (rdBtnLow == null) {
541
            rdBtnLow = new JRadioButton(new ImageIcon(
542
                            getClass().getClassLoader().
543
                                getResource("images/low-density-sample.png")));
544
            rdBtnLow.addActionListener(buttonsListener);
545
        }
546
        return rdBtnLow;
547
    }
548

    
549
    /**
550
     * This method initializes numDotSize
551
     *
552
     * @return de.ios.framework.swing.JNumberField
553
     */
554
    private JTextField getNumDotSize() {
555
        if (numDotSize == null) {
556
            numDotSize = new JTextField(4);
557
        }
558
        return numDotSize;
559
    }
560

    
561
        /**
562
         * This method initializes nmbrDotValue
563
         *
564
         * @return de.ios.framework.swing.JNumberField
565
         */
566
        private JTextField getNmbrDotValue() {
567
                if (nmbrDotValue == null) {
568
                        nmbrDotValue = new JTextField(15);
569

    
570
                }
571
                return nmbrDotValue;
572
        }
573

    
574
        /**
575
         * This method initializes centerPanel
576
         *
577
         * @return javax.swing.JPanel
578
         */
579
        private JPanel getCenterPanel() {
580
                if (centerPanel == null) {
581
                        centerPanel = new JPanel();
582
                        centerPanel.setLayout(new BorderLayout(5, 5));
583
                        centerPanel.add(getPnlDensities(), java.awt.BorderLayout.WEST);
584
                }
585
                return centerPanel;
586
        }
587

    
588
        /**
589
         * This method initializes sldDensity
590
         *
591
         * @return javax.swing.JSlider
592
         */
593
        private JSlider getSldDensity() {
594
                if (sldDensity == null) {
595
                        sldDensity = new JSlider();
596
                        sldDensity.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
597
                        sldDensity.addChangeListener(sldListener);
598
                }
599
                return sldDensity;
600
        }
601

    
602
        public String getDescription() {
603
                return PluginServices.getText(this,"Defines_a_dot_density_symbol_based_on_a_field_value") + ".";
604
        }
605

    
606
        public ISymbol getPreviewSymbol() {
607
                // TODO Implement it
608
                return null;
609
        }
610

    
611
        public Class getParentClass() {
612
                return Quantities.class;
613
        }
614

    
615
        public String getTitle() {
616
                return PluginServices.getText(this, "dot_density");
617
        }
618

    
619
        public JPanel getPanel() {
620
                return this;
621
        }
622

    
623
        public Class getLegendClass() {
624
                return DotDensityLegend.class;
625
        }
626

    
627
}  //  @jve:decl-index=0:visual-constraint="10,10"