Statistics
| Revision:

svn-gvsig-desktop / tags / PilotoRedes_Build_1 / extensions / extGraph_predes / src / com / iver / cit / gvsig / project / documents / view / legend / gui / PanelLegendDotDensity.java @ 11097

History | View | Annotate | Download (17.3 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: PanelLegendDotDensity.java 11097 2007-04-10 14:39:57Z  $
45
* $Log$
46
* Revision 1.4  2006-11-17 13:53:45  cesar
47
* *** empty log message ***
48
*
49
* Revision 1.3  2006/11/17 12:50:36  jaume
50
* tama?o de punto defecto 2
51
*
52
* Revision 1.2  2006/11/15 12:57:31  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.1  2006/11/14 11:10:27  jaume
56
* *** empty log message ***
57
*
58
*
59
*/
60
package com.iver.cit.gvsig.project.documents.view.legend.gui;
61

    
62
import java.awt.BorderLayout;
63
import java.awt.Color;
64
import java.awt.Component;
65
import java.awt.ComponentOrientation;
66
import java.awt.FlowLayout;
67
import java.awt.GridLayout;
68
import java.awt.LayoutManager;
69
import java.awt.event.ActionEvent;
70
import java.awt.event.ActionListener;
71
import java.awt.event.ItemEvent;
72
import java.awt.event.ItemListener;
73
import java.sql.Types;
74
import java.text.NumberFormat;
75

    
76
import javax.swing.BorderFactory;
77
import javax.swing.BoxLayout;
78
import javax.swing.ButtonGroup;
79
import javax.swing.ImageIcon;
80
import javax.swing.JComboBox;
81
import javax.swing.JLabel;
82
import javax.swing.JPanel;
83
import javax.swing.JRadioButton;
84
import javax.swing.JSlider;
85
import javax.swing.JTextField;
86
import javax.swing.event.ChangeEvent;
87
import javax.swing.event.ChangeListener;
88

    
89
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
90

    
91
import com.hardcode.gdbms.engine.data.DataSource;
92
import com.hardcode.gdbms.engine.data.DataSourceFactory;
93
import com.hardcode.gdbms.engine.values.NumericValue;
94
import com.hardcode.gdbms.engine.values.Value;
95
import com.iver.andami.PluginServices;
96
import com.iver.andami.messages.NotificationManager;
97
import com.iver.cit.gvsig.fmap.core.FShape;
98
import com.iver.cit.gvsig.fmap.core.symbols.DotDensityFillSymbol;
99
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
100
import com.iver.cit.gvsig.fmap.layers.FLayer;
101
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
102
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
103
import com.iver.cit.gvsig.fmap.rendering.DotDensityLegend;
104
import com.iver.cit.gvsig.fmap.rendering.Legend;
105
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
106
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
107
import com.iver.cit.gvsig.project.documents.view.legend.gui.ILegendPanel;
108
import com.iver.utiles.StringUtilities;
109
import com.iver.utiles.XMLEntity;
110

    
111
/**
112
 *
113
 * @author jaume dominguez faus - jaume.dominguez@iver.es
114
 *
115
 */
116
public class PanelLegendDotDensity extends JPanel implements ILegendPanel {
117
        private FLyrVect layer;
118
        private JPanel northPanel = null;
119
        private GridBagLayoutPanel densityButtonsPanel = null;
120
        private JPanel pnlDensities = null;
121
        private JComboBox cmbLegendField = null;
122
        private JRadioButton rdBtnHigh = null;
123
        private JRadioButton rdBtnMedium = null;
124
        private JRadioButton rdBtnLow = null;
125
        private JTextField numDotSize = null;
126
        private double max, min, mean;
127
        private JTextField nmbrDotValue = null;
128
        private JLabel lblLabellingField = null;
129
        private DotDensityLegend legend;
130
        private String fieldName;
131
        private MyListener cmbAction = new MyListener();
132
        NumberFormat nf = NumberFormat.getInstance();
133

    
134
        {
135
                nf.setMaximumFractionDigits(3);
136
        }
137

    
138
        private class MyListener implements ItemListener, ActionListener {
139

    
140
                public void itemStateChanged(ItemEvent e) {
141
                        doIt();
142
                }
143

    
144
                public void actionPerformed(ActionEvent e) {
145
                        doIt();
146
                }
147

    
148
                private void doIt() {
149
                        int index = cmbLegendField.getSelectedIndex();
150
            try {
151
                SelectableDataSource sds = layer.getRecordset();
152

    
153
                if (index != -1) {
154
                    fieldName = (String) cmbLegendField.getSelectedItem();
155
                } else {
156
                    fieldName = (String) cmbLegendField.getItemAt(0);
157
                }
158

    
159
                String sql = "select "+fieldName+" from '"+sds.getName()+"';";
160

    
161
                DataSource recordset = sds.getDataSourceFactory().executeSQL(sql, DataSourceFactory.AUTOMATIC_OPENING );
162
                // TODO implement MAX() and MIN() functions in GDBMS!!!!!
163
                max = Double.MIN_VALUE;
164
                min = Double.MAX_VALUE;
165
                for (int i = 0; i < sds.getRowCount(); i++) {
166
                    double value = ((NumericValue) recordset.getFieldValue(i,0)).doubleValue();
167
                    max = Math.max(max, value);
168
                    min = Math.min(min, value);
169
                }
170
                mean = (max+min) /2;
171
                buttonsListener.actionPerformed(null);
172

    
173
            } catch (Exception ex) {
174
                ex.printStackTrace();
175
            }
176
                }
177
        };
178
        private ActionListener buttonsListener = new ActionListener() {
179
            public void actionPerformed(ActionEvent e) {
180
                    if (getRdBtnHigh().isSelected()) {
181
                            getSldDensity().setValue(66);
182
                    } else if (getRdBtnLow().isSelected()) {
183
                            getSldDensity().setValue(33);
184
                    } else if (getRdBtnMedium().isSelected()) {
185
                            getSldDensity().setValue(50);
186
                    }
187
            }
188
    };
189
        private JPanel centerPanel = null;
190
        private JSlider sldDensity = null;
191
        private JPanel pnlSldDensity;
192
        private ChangeListener sldListener = new ChangeListener() {
193

    
194
                public void stateChanged(ChangeEvent e) {
195

    
196
                        // This is my messy statistic aggregation function
197
                        // TODO if I can, I'll have a look to my statistic books to figure out one better
198
                        int quantileIndex = getSldDensity().getValue();
199
                        final int topLimit = 98;
200
                        if (quantileIndex > topLimit)
201
                                quantileIndex = topLimit;
202
                        double quantileAmount = (max-min)/100;
203
                        nmbrDotValue.setText(nf.format(3*quantileAmount/Math.log(quantileIndex)));
204
                }
205

    
206
        };
207
        private ColorChooserPanel jcc;
208
        private ColorChooserPanel outlineColorChooserPanel;
209
        public PanelLegendDotDensity() {
210
        super();
211
        initialize();
212
    }
213

    
214
    /**
215
     * This method initializes this
216
     *
217
     */
218
    private void initialize() {
219
        this.setLayout(new BorderLayout());
220
        this.setSize(new java.awt.Dimension(492,278));
221
        this.add(getNorthPanel(), java.awt.BorderLayout.NORTH);
222

    
223
        this.add(getCenterPanel(), java.awt.BorderLayout.CENTER);
224
    }
225

    
226
    public void setLayer(FLayer lyr, Legend legend) {
227
        this.layer = (FLyrVect) lyr;
228
        try {
229
                SelectableDataSource sds = layer.getRecordset();
230
                cmbLegendField.removeItemListener(cmbAction);
231
                cmbLegendField.removeAllItems();
232
                String[] fNames = sds.getFieldNames();
233
                for (int i = 0; i < fNames.length; i++) {
234
                        if (isNumericField(sds.getFieldType(i))) {
235
                                cmbLegendField.addItem(fNames[i]);
236
                        }
237
                }
238
                if (!(legend instanceof DotDensityLegend)) {
239
                        legend = new DotDensityLegend();
240
                        ((DotDensityLegend) legend).setFieldName((String) cmbLegendField.getItemAt(0));
241
                }
242

    
243
                DotDensityLegend theLegend = (DotDensityLegend) legend;
244

    
245
                cmbLegendField.addItemListener(cmbAction);
246
                cmbLegendField.addActionListener(cmbAction);
247
                cmbLegendField.setSelectedItem(theLegend.getFieldName());
248
                try {
249
                        getDotColorChooserPanel().setColor(theLegend.getDotColor());
250
                } catch (NullPointerException npEx) {
251
                        getDotColorChooserPanel().setColor(Color.RED);
252
                }
253
                try {
254
                        getOutlineColorChooserPanel().setColor(theLegend.getOutlineColor());
255
                } catch (NullPointerException npEx) {
256
                        getOutlineColorChooserPanel().setColor(Color.BLACK);
257
                }
258
                try {
259
                        double dotValue = theLegend.getDotValue();
260
                        if (dotValue <= 0)
261
                                dotValue = 100;
262
                        getNmbrDotValue().setText(String.valueOf(dotValue));
263
                } catch (NullPointerException npEx) {
264
                        getNmbrDotValue().setText(String.valueOf(max));
265
                }
266
                try {
267
                        double dotSize = theLegend.getDotSize();
268
                        if (dotSize <= 0)
269
                                dotSize = 2;
270
                        getNumDotSize().setText(String.valueOf(dotSize));
271
                } catch (NullPointerException npEx) {
272
                        getNumDotSize().setText(String.valueOf(3));
273
                }
274

    
275
        } catch (Exception e) {
276
                e.printStackTrace();
277
        }
278
    }
279

    
280
    private boolean isNumericField(int fieldType) {
281
        switch (fieldType) {
282
        case Types.BIGINT:
283
        case Types.DECIMAL:
284
        case Types.DOUBLE:
285
        case Types.FLOAT:
286
        case Types.INTEGER:
287
        case Types.NUMERIC:
288
        case Types.REAL:
289
        case Types.SMALLINT:
290
        case Types.TINYINT:
291
            return true;
292
        default:
293
            return false;
294
        }
295

    
296
    }
297

    
298
    public Legend getLegend() {
299
            try {
300
                        SimpleFillSymbol symbol = null;
301

    
302

    
303
                    int shapeType = layer.getShapeType(); // should be always polygon
304
                    if (shapeType != FShape.POLYGON) {
305
                            NotificationManager.addError(PluginServices.getText(this, "cannot_apply_to_a_non_polygon_layer"), new Exception());
306
                    }
307
                    SelectableDataSource sds;
308
                    sds = layer.getRecordset();
309
                    if (-1 == sds.getFieldIndexByName(fieldName))
310
                            return null;
311

    
312
                    double dotValue;
313
                    double dotSize;
314
                    try {
315
                            dotValue = Double.parseDouble(nmbrDotValue.getText());
316
                    } catch (Exception e) {
317
                            dotValue = nf.parse(nmbrDotValue.getText()).doubleValue();
318
                    }
319
                    if (dotValue == 0)
320
                            dotValue = 1;
321
                    try {
322
                            dotSize = Double.parseDouble(numDotSize.getText());
323
                    } catch (Exception e) {
324
                            dotSize = nf.parse(numDotSize.getText()).doubleValue();
325
                    }
326
                    DotDensityFillSymbol densitySym = new DotDensityFillSymbol();
327
                    densitySym.setDotSize(dotSize);
328
                    densitySym.setDotColor(getDotColorChooserPanel().getColor());
329
                    symbol = new SimpleFillSymbol();
330
                    XMLEntity xml = symbol.getXMLEntity();
331
                    xml.putProperty("color", StringUtilities.color2String(Color.WHITE));
332
                    xml.putProperty("outline", StringUtilities.color2String(getOutlineColorChooserPanel().getColor()));
333

    
334
                    xml.addChild(densitySym.getXMLEntity());
335
                    symbol.setXMLEntity(xml);
336

    
337
                    legend = new DotDensityLegend();
338
                    legend.addSymbol("theSymbol", symbol);
339
                    legend.setDefaultSymbol(symbol);
340
                    legend.setDotValue(dotValue);
341
                    legend.setFieldName(fieldName);
342

    
343

    
344
            } catch (Exception e) {
345
                    NotificationManager.addError(PluginServices.getText(this, "could_not_setup_legend"), e);
346
            }
347
            return legend;
348

    
349
    }
350

    
351
    /**
352
     * This method initializes centerPanel
353
     *
354
     * @return javax.swing.JPanel
355
     */
356
    private JPanel getNorthPanel() {
357
        if (northPanel == null) {
358
            lblLabellingField = new JLabel();
359
            lblLabellingField.setText(PluginServices.getText(this, "labelling_field"));
360
            northPanel = new JPanel(new FlowLayout(FlowLayout.LEADING,15,0));
361
            northPanel.add(lblLabellingField, null);
362
            northPanel.add(getCmbLegendField(), null);
363

    
364
        }
365
        return northPanel;
366
    }
367

    
368
    private ColorChooserPanel getDotColorChooserPanel() {
369
            if (jcc == null) {
370
                    jcc = new ColorChooserPanel() ;
371
                    jcc.setAlpha(255);
372
            }
373
            return jcc;
374
        }
375

    
376
        /**
377
     * This method initializes southPanel
378
     *
379
     * @return javax.swing.JPanel
380
     */
381
    private JPanel getDensityButtonsPanel() {
382
        if (densityButtonsPanel == null) {
383
                densityButtonsPanel = new GridBagLayoutPanel();
384
                LayoutManager layout = new FlowLayout(FlowLayout.LEADING, 0,0);
385
                JPanel aux = new JPanel(layout);
386
                aux.add(getNumDotSize());
387
                densityButtonsPanel.addComponent(
388
                                PluginServices.getText(this, "dot_size"), aux);
389
                aux = new JPanel(layout);
390
                aux.add(getNmbrDotValue());
391
            densityButtonsPanel.addComponent(
392
                            PluginServices.getText(this,"dot_value"), aux);
393
            aux = new JPanel(layout);
394
            aux.add(getDotColorChooserPanel());
395
            densityButtonsPanel.addComponent(
396
                            PluginServices.getText(this, "color"), aux);
397
            aux = new JPanel(layout);
398
            aux.add(getOutlineColorChooserPanel());
399
            densityButtonsPanel.addComponent(
400
                            PluginServices.getText(this, "outline_color"), aux);
401
        }
402
        return densityButtonsPanel;
403
    }
404

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

    
413
        /**
414
     * This method initializes pnlDensities
415
     *
416
     * @return javax.swing.JPanel
417
     */
418
    private JPanel getPnlDensities() {
419
        if (pnlDensities == null) {
420
            pnlDensities = new JPanel(new BorderLayout(5,0));
421
            pnlDensities.setBorder(BorderFactory.createTitledBorder(null,
422
                                        PluginServices.getText(this, "densities")));
423
            JPanel aux2 = new JPanel();
424
            JPanel aux;
425
                        aux = new JPanel(new GridLayout(1,3));
426
            aux.add(new JLabel(PluginServices.getText(this, "high")));
427
            aux.add(new JLabel(PluginServices.getText(this, "medium")));
428
            aux.add(new JLabel(PluginServices.getText(this, "low")));
429

    
430
            aux2.add(aux);
431

    
432
                        aux = new JPanel(new GridLayout(1,3));
433
            aux.add(getRdBtnHigh());
434
            aux.add(getRdBtnMedium());
435
            aux.add(getRdBtnLow());
436

    
437
            aux2.add(aux);
438
            aux2.setLayout(new BoxLayout(aux2, BoxLayout.Y_AXIS));
439

    
440
            pnlDensities.add(aux2, BorderLayout.NORTH);
441
            pnlDensities.add(getSldDensity(), BorderLayout.CENTER);
442
            pnlDensities.add(getDensityButtonsPanel(), BorderLayout.SOUTH);
443

    
444
            ButtonGroup group = new ButtonGroup();
445
            group.add(getRdBtnHigh());
446
            group.add(getRdBtnLow());
447
            group.add(getRdBtnMedium());
448
            getRdBtnMedium().setSelected(true);
449
        }
450
        return pnlDensities;
451
    }
452

    
453

    
454
        /**
455
     * This method initializes cmbLegendField
456
     *
457
     * @return javax.swing.JComboBox
458
     */
459
    private JComboBox getCmbLegendField() {
460
        if (cmbLegendField == null) {
461
            cmbLegendField = new JComboBox();
462
        }
463
        return cmbLegendField;
464
    }
465

    
466
    /**
467
     * This method initializes rdBtnHigh
468
     *
469
     * @return javax.swing.JRadioButton
470
     */
471
    private JRadioButton getRdBtnHigh() {
472
        if (rdBtnHigh == null) {
473
            rdBtnHigh = new JRadioButton(new ImageIcon(
474
                            getClass().getClassLoader().
475
                                    getResource("images/high-density-sample.png")));
476
            rdBtnHigh.addActionListener(buttonsListener);
477
        }
478
        return rdBtnHigh;
479
    }
480

    
481
    /**
482
     * This method initializes rdBtnMedium
483
     *
484
     * @return javax.swing.JRadioButton
485
     */
486
    private JRadioButton getRdBtnMedium() {
487
        if (rdBtnMedium == null) {
488
            rdBtnMedium = new JRadioButton(new ImageIcon(
489
                            getClass().getClassLoader().
490
                                getResource("images/medium-density-sample.png")));
491
            rdBtnMedium.addActionListener(buttonsListener);
492
        }
493
        return rdBtnMedium;
494
    }
495

    
496
    /**
497
     * This method initializes rdBtnMax
498
     *
499
     * @return javax.swing.JRadioButton
500
     */
501
    private JRadioButton getRdBtnLow() {
502
        if (rdBtnLow == null) {
503
            rdBtnLow = new JRadioButton(new ImageIcon(
504
                            getClass().getClassLoader().
505
                                getResource("images/low-density-sample.png")));
506
            rdBtnLow.addActionListener(buttonsListener);
507
        }
508
        return rdBtnLow;
509
    }
510

    
511
    /**
512
     * This method initializes numDotSize
513
     *
514
     * @return de.ios.framework.swing.JNumberField
515
     */
516
    private JTextField getNumDotSize() {
517
        if (numDotSize == null) {
518
            numDotSize = new JTextField(4);
519
        }
520
        return numDotSize;
521
    }
522

    
523
        /**
524
         * This method initializes nmbrDotValue
525
         *
526
         * @return de.ios.framework.swing.JNumberField
527
         */
528
        private JTextField getNmbrDotValue() {
529
                if (nmbrDotValue == null) {
530
                        nmbrDotValue = new JTextField(15);
531

    
532
                }
533
                return nmbrDotValue;
534
        }
535

    
536
        /**
537
         * This method initializes centerPanel
538
         *
539
         * @return javax.swing.JPanel
540
         */
541
        private JPanel getCenterPanel() {
542
                if (centerPanel == null) {
543
                        centerPanel = new JPanel();
544
                        centerPanel.setLayout(new BorderLayout(5, 5));
545
                        centerPanel.add(getPnlDensities(), java.awt.BorderLayout.WEST);
546
                }
547
                return centerPanel;
548
        }
549

    
550
        /**
551
         * This method initializes sldDensity
552
         *
553
         * @return javax.swing.JSlider
554
         */
555
        private JSlider getSldDensity() {
556
                if (sldDensity == null) {
557
                        sldDensity = new JSlider();
558
                        sldDensity.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
559
                        sldDensity.addChangeListener(sldListener);
560
                }
561
                return sldDensity;
562
        }
563

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