Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / gui / layerproperties / GraduatedSymbols.java @ 29131

History | View | Annotate | Download (16.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: GraduatedSymbols.java 16232 2007-11-09 13:36:35Z jdominguez $
45
 * $Log$
46
 * Revision 1.8  2007-09-19 15:44:44  jaume
47
 * new signal catched (legend error)
48
 *
49
 * Revision 1.7  2007/05/21 10:38:27  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.6  2007/05/17 09:32:37  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.5  2007/05/10 09:46:45  jaume
56
 * Refactored legend interface names
57
 *
58
 * Revision 1.4  2007/05/08 15:45:31  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.3  2007/03/21 08:03:03  jaume
62
 * refactored to use ISymbol instead of FSymbol
63
 *
64
 * Revision 1.2  2007/03/09 11:25:00  jaume
65
 * Advanced symbology (start committing)
66
 *
67
 * Revision 1.1.2.4  2007/02/21 07:35:14  jaume
68
 * *** empty log message ***
69
 *
70
 * Revision 1.1.2.3  2007/02/14 09:59:17  jaume
71
 * *** empty log message ***
72
 *
73
 * Revision 1.1.2.2  2007/02/13 16:19:19  jaume
74
 * graduated symbol legends (start commiting)
75
 *
76
 * Revision 1.1.2.1  2007/02/12 15:14:41  jaume
77
 * refactored interval legend and added graduated symbol legend
78
 *
79
 *
80
 */
81
package org.gvsig.symbology.gui.layerproperties;
82

    
83
import java.awt.Color;
84
import java.awt.Dimension;
85
import java.awt.FlowLayout;
86
import java.text.NumberFormat;
87

    
88
import javax.swing.BorderFactory;
89
import javax.swing.ImageIcon;
90
import javax.swing.JLabel;
91
import javax.swing.JOptionPane;
92
import javax.swing.JPanel;
93

    
94
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
95
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
96
import org.gvsig.symbology.fmap.rendering.GraduatedSymbolLegend;
97
import org.gvsig.symbology.fmap.symbols.MarkerFillSymbol;
98

    
99
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
100
import com.iver.andami.PluginServices;
101
import com.iver.andami.messages.NotificationManager;
102
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException;
103
import com.iver.cit.gvsig.fmap.core.FShape;
104
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
105
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillPropertiesStyle;
106
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
107
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
108
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
109
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
110
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
111
import com.iver.cit.gvsig.fmap.layers.FLayer;
112
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
113
import com.iver.cit.gvsig.fmap.layers.XMLException;
114
import com.iver.cit.gvsig.fmap.rendering.FInterval;
115
import com.iver.cit.gvsig.fmap.rendering.ILegend;
116
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
117
import com.iver.cit.gvsig.fmap.rendering.NullIntervalValue;
118
import com.iver.cit.gvsig.fmap.rendering.VectorialIntervalLegend;
119
import com.iver.cit.gvsig.project.documents.view.legend.gui.ILegendPanel;
120
import com.iver.cit.gvsig.project.documents.view.legend.gui.JSymbolPreviewButton;
121
import com.iver.cit.gvsig.project.documents.view.legend.gui.Quantities;
122
import com.iver.cit.gvsig.project.documents.view.legend.gui.SymbolTable;
123
import com.iver.cit.gvsig.project.documents.view.legend.gui.VectorialInterval;
124

    
125
/**
126
 * Implements the interface that shows the information of a legend which draws quantities
127
 * using symbol size to show relative values.
128
 *
129
 * @author Pepe Vidal Salvador - jose.vidal.salvador@iver.es
130
 *
131
 */
132

    
133
public class GraduatedSymbols extends VectorialInterval implements ILegendPanel {
134
        private static final long serialVersionUID = -263195536343912694L;
135
        static final double DEFAULT_SYMBOL_MAX_SIZE = 7;
136
        private static final double DEFAULT_SYMBOL_MIN_SIZE = 1;
137
        private JIncrementalNumberField txtMinSize;
138
        private JIncrementalNumberField txtMaxSize;
139
        private JSymbolPreviewButton btnTemplate;
140
        private JSymbolPreviewButton btnBackground;
141
        private int shapeType;
142
        private GridBagLayoutPanel aux;
143
        private int templateShapeType = 0 ;
144
        private boolean showBackground = true;
145

    
146
        public GraduatedSymbols() {
147
                super();
148
                this.showBackground = true;
149
        }
150

    
151
        public GraduatedSymbols(boolean showBackground) {
152
                super();
153
                this.showBackground = showBackground;
154
        }
155

    
156
        @Override
157
        public JPanel getOptionPanel() {
158

    
159
                if (optionPanel == null) {
160
                        optionPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
161
                        optionPanel.setBorder(BorderFactory.
162
                                        createTitledBorder(null,
163
                                                        PluginServices.getText(this, "symbol")));
164

    
165
                        aux = new GridBagLayoutPanel();
166
                        aux.addComponent(new JLabel(PluginServices.getText(this, "size")));
167
                        aux.addComponent(PluginServices.getText(this, "from")+":",
168
                                        getTxtMinSize());
169
                        aux.addComponent(PluginServices.getText(this, "to")+":",
170
                                        getTxtMaxSize());
171
                        aux.addComponent(PluginServices.getText(this, "template"), getBtnTemplate());
172
                        optionPanel.add(aux);
173

    
174
                }
175
                return optionPanel;
176
        }
177

    
178
        /**
179
         * Adds the button that allows the user to select a background for the graduated symbol.
180
         */
181
        private void getBackgroundPanel() {
182
                aux.addComponent(PluginServices.getText(this, "background"), getBtnBackground());
183
        }
184
        /**
185
         * Creates the JSymbolPreviewButton for the background of the graduated symbol.
186
         */
187
        private JSymbolPreviewButton getBtnBackground() {
188
                if (btnBackground == null) {
189
                        btnBackground = new JSymbolPreviewButton(FShape.POLYGON);
190
                        btnBackground.setPreferredSize(new Dimension(100, 35));
191
                        btnBackground.setSymbol(SymbologyFactory.createDefaultFillSymbol());
192
                }
193
                return btnBackground;
194
        }
195
        /**
196
         * Creates the JSymbolPreviewButton for the graduated symbol.
197
         */
198
        private JSymbolPreviewButton getBtnTemplate() {
199
                if (btnTemplate == null) {
200
                        templateShapeType = ((shapeType%FShape.Z) == FShape.POLYGON) ? FShape.POINT : shapeType;
201
                        btnTemplate = new JSymbolPreviewButton(templateShapeType);
202
                        btnTemplate.setPreferredSize(new Dimension(100, 35));
203

    
204
                }
205
                return btnTemplate;
206
        }
207
        /**
208
         * Creates the JIncrementalNumberField which is used to specify the maximum size of the
209
         * graduated symbol
210
         */
211
        private JIncrementalNumberField getTxtMaxSize() {
212
                if (txtMaxSize == null) {
213
                        txtMaxSize = new JIncrementalNumberField(String.valueOf(25), 7,0,100,1);
214

    
215
                }
216
                return txtMaxSize;
217
        }
218
        /**
219
         * Creates the JIncrementalNumberField which is used to specify the minimum size of the
220
         * graduated symbol
221
         */
222
        private JIncrementalNumberField getTxtMinSize() {
223
                if (txtMinSize == null) {
224
                        txtMinSize = new JIncrementalNumberField(String.valueOf(3), 7,0,100,1);
225
                }
226
                return txtMinSize;
227
        }
228

    
229
        @Override
230
        public void setData(FLayer lyr, ILegend legend) {
231
                this.layer = (FLyrVect) lyr;
232

    
233
                try {
234
                        shapeType = (this.layer.getShapeType());
235
                        templateShapeType = ((shapeType%FShape.Z) == FShape.POLYGON) ? FShape.POINT : shapeType;
236
                        getBtnTemplate().setShapeType(templateShapeType);
237
                        if(showBackground){
238
                                if((shapeType%FShape.Z) == FShape.POLYGON && btnBackground == null) {
239
                                        getBackgroundPanel();
240
                                        getBtnBackground().setShapeType(FShape.POLYGON);
241
                                }
242
                        }
243
                } catch (ReadDriverException e) {
244
                        NotificationManager.addError(PluginServices.getText(this, "generating_intervals"), e);
245
                }
246

    
247
                if (symbolTable != null)
248
                        pnlCenter.remove(symbolTable);
249

    
250
                getDefaultSymbolPrev(templateShapeType);
251

    
252
                symbolTable = new SymbolTable(this, SymbolTable.INTERVALS_TYPE,templateShapeType);
253
                pnlCenter.add(symbolTable);
254

    
255
                fillFieldNames();
256

    
257
                if (legend instanceof GraduatedSymbolLegend) {
258
                        try {
259
                                auxLegend = (GraduatedSymbolLegend) legend.cloneLegend();
260
                        } catch (XMLException e) {
261
                                // TODO Auto-generated catch block
262
                                e.printStackTrace();
263
                        }
264

    
265
                        getChkDefaultvalues().setSelected(auxLegend.isUseDefaultSymbol());
266
                        cmbField.setSelectedItem(auxLegend.getClassifyingFieldNames()[0]);
267
                        symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
268
                                        auxLegend.getValues(), auxLegend.getDescriptions());
269
                        getTxtMaxSize().setDouble(((GraduatedSymbolLegend) auxLegend).getMaxSymbolSize());
270
                        getTxtMinSize().setDouble(((GraduatedSymbolLegend) auxLegend).getMinSymbolSize());
271
                        if (auxLegend.getSymbols().length > 0){
272
                                getBtnTemplate().setSymbol(((GraduatedSymbolLegend) auxLegend).getSymbols()[auxLegend.getSymbols().length-1]);
273
                        } else {
274
                                getBtnTemplate().setSymbol(newSymbol(shapeType, shapeType == FShape.LINE ? 3 : 10));
275
                        }
276
                        if(showBackground){
277
                                if((shapeType%FShape.Z) == FShape.POLYGON)
278
                                        getBtnBackground().setSymbol(((GraduatedSymbolLegend) auxLegend).getBackgroundSymbol());
279
                        }
280
                        if (auxLegend.isUseDefaultSymbol())
281
                                txtNumIntervals.setText(String.valueOf(auxLegend.getSymbols().length - 1));
282
                        else
283
                                txtNumIntervals.setText(String.valueOf(auxLegend.getSymbols().length));
284

    
285
                } else {
286
                        // Si la capa viene con otro tipo de leyenda, creamos
287
                        // una nueva del tipo que maneja este panel
288
                        auxLegend = new GraduatedSymbolLegend();
289
                        getTxtMaxSize().setDouble(DEFAULT_SYMBOL_MAX_SIZE);
290
                        getTxtMinSize().setDouble(DEFAULT_SYMBOL_MIN_SIZE);
291
                        auxLegend.setDefaultSymbol(newSymbol(shapeType, shapeType == FShape.LINE ? 3 : 10));
292
                        getBtnTemplate().setSymbol(auxLegend.getDefaultSymbol());
293
                        ((GraduatedSymbolLegend) auxLegend).setTemplateShapeType(templateShapeType);
294

    
295
                }
296
                cmbFieldType.setSelectedIndex(auxLegend.getIntervalType());
297
                defaultSymbolPrev.setSymbol(auxLegend.getDefaultSymbol());
298
        }
299

    
300

    
301
        @Override
302
        public String getDescription() {
303
                return PluginServices.getText(this, "draw_quantities_using_symbol_size_to_show_relative_values");
304
        }
305

    
306
        @Override
307
        public ILegend getLegend() {
308

    
309
                GraduatedSymbolLegend gsl = new GraduatedSymbolLegend((VectorialIntervalLegend) super.getLegend());
310

    
311
                double minSize = getTxtMinSize().getDouble();
312
                gsl.setMinSymbolSize(minSize);
313
                gsl.setMaxSymbolSize(getTxtMaxSize().getDouble());
314

    
315
                try {
316
                        gsl.setShapeType(layer.getShapeType());
317
                } catch (ReadDriverException e) {
318
                        NotificationManager.addWarning("Reached what should be unreachable code", e);
319
                }
320

    
321
                ISymbol sym = getBtnTemplate().getSymbol();
322

    
323
                if (sym != null){
324
                        gsl.setTemplateShapeType(sym.getSymbolType());
325
                        gsl.setDefaultSymbol(sym);
326
                } else {
327
                        gsl.setTemplateShapeType(templateShapeType);
328
                }
329

    
330
                gsl.useDefaultSymbol(getChkDefaultvalues().isSelected());
331
                if(defaultSymbolPrev.getSymbol() != null)
332
                        gsl.setDefaultSymbol(defaultSymbolPrev.getSymbol());
333

    
334
                if (this.showBackground){
335
                        if((shapeType%FShape.Z) == FShape.POLYGON)
336
                                gsl.setBackgroundSymbol(btnBackground.getSymbol());
337
                }
338

    
339
                return gsl;
340
        }
341

    
342
        @Override
343
        public ImageIcon getIcon() {
344
                return new ImageIcon(this.getClass().getClassLoader().
345
                                getResource("images/GraduatedSymbols.PNG"));
346
        }
347

    
348
        @Override
349
        public Class getParentClass() {
350
                return Quantities.class;
351
        }
352

    
353
        @Override
354
        public String getTitle() {
355
                return PluginServices.getText(this, "graduated_symbols");
356
        }
357

    
358
        @Override
359
        public JPanel getPanel() {
360
                return this;
361
        }
362

    
363
        @Override
364
        public Class getLegendClass() {
365
                return GraduatedSymbolLegend.class;
366
        }
367

    
368
        @Override
369
        public boolean isSuitableFor(FLayer layer) {
370

    
371
                if ( super.isSuitableFor(layer)) {
372
                        FLyrVect lVect = (FLyrVect) layer;
373
                        try {
374
                                return lVect.getShapeType() != FShape.MULTI;
375
                        } catch (ReadDriverException e) {
376
                                return false;
377
                        }
378
                }
379
                return false;
380
        }
381

    
382
        private ISymbol newSymbol(int shapeType, double size) {
383
                if (getBtnTemplate().getSymbol() == null) {
384
                        ISymbol templateSymbol;
385
                        switch (shapeType) {
386
                        case FShape.POINT:
387
                        case FShape.POLYGON:
388
                        case FShape.POINT | FShape.Z:
389
                        case FShape.POINT | FShape.M:
390
                        case FShape.POLYGON | FShape.Z:
391
                        case FShape.POLYGON | FShape.M:
392
                                templateSymbol = new SimpleMarkerSymbol();
393
                                ((SimpleMarkerSymbol) templateSymbol).setSize(size);
394
                                ((SimpleMarkerSymbol) templateSymbol).setColor(Color.DARK_GRAY);
395
                                break;
396

    
397
                        case FShape.LINE:
398
                        case FShape.LINE | FShape.Z:
399
                        case FShape.LINE | FShape.M:
400
                                templateSymbol = new SimpleLineSymbol();
401
                                ((SimpleLineSymbol) templateSymbol).setLineWidth(size);
402
                                ((SimpleLineSymbol) templateSymbol).setLineColor(Color.DARK_GRAY);
403
                                break;
404
                        default:
405
                                throw new Error("Unknown symbol type");
406
                        }
407
                        getBtnTemplate().setSymbol(templateSymbol);
408
                        return newSymbol(shapeType, size);
409
                } else {
410
                        // clone symbol
411
                        ISymbol mySymbol = SymbologyFactory.createSymbolFromXML(
412
                                        getBtnTemplate().getSymbol().getXMLEntity(), null);
413

    
414
                        if (mySymbol instanceof ILineSymbol) {
415
                                ILineSymbol lSym = (ILineSymbol) mySymbol;
416
                                lSym.setLineWidth(size);
417

    
418
                        }
419
                        if (mySymbol instanceof IMarkerSymbol) {
420
                                IMarkerSymbol mSym = (IMarkerSymbol) mySymbol;
421
                                mSym.setSize(size);
422
                                if ((shapeType%FShape.Z) == FShape.POLYGON) {
423
                                        // this is to allow using in Polygon layers
424
                                        MarkerFillSymbol fillSymbol = new MarkerFillSymbol();
425
                                        fillSymbol.setOutline(null);
426
                                        fillSymbol.setFillColor(null);
427
                                        fillSymbol.getMarkerFillProperties().
428
                                        setFillStyle(IMarkerFillPropertiesStyle.SINGLE_CENTERED_SYMBOL);
429
                                        fillSymbol.setMarker(mSym);
430
                                }
431
                        }
432
                        return mySymbol;
433
                }
434
        }
435

    
436
        @Override
437
        protected void fillTableValues() {
438

    
439
                symbolTable.removeAllItems();
440

    
441
                try {
442
                        FInterval[] arrayIntervalos = calculateIntervals();
443
                        if (arrayIntervalos == null)
444
                                return;
445

    
446
                        FInterval interval;
447
                        NumberFormat.getInstance().setMaximumFractionDigits(2);
448
                        auxLegend.clear();
449

    
450
                        float minSize = (float) getTxtMinSize().getDouble();
451
                        float maxSize = (float) getTxtMaxSize().getDouble();
452
                        ISymbol theSymbol;
453

    
454
                        this.shapeType = layer.getShapeType();
455

    
456
                        if((layer.getShapeType()%FShape.Z) == FShape.POLYGON)
457
                                this.templateShapeType = FShape.POINT;
458

    
459

    
460

    
461
                        auxLegend = LegendFactory.createVectorialIntervalLegend(this.shapeType);
462
                        auxLegend.setIntervalType(getCmbIntervalTypes().getSelectedIndex());
463
                        if (chkdefaultvalues.isSelected()) {
464
                                auxLegend.getDefaultSymbol().setDescription("Default");
465
                                auxLegend.addSymbol(new NullIntervalValue(),
466
                                                SymbologyFactory.createDefaultSymbolByShapeType(this.templateShapeType));
467
                        }
468

    
469
                        int symbolType;
470
                        symbolType = layer.getShapeType();
471
                        int numSymbols = 0;
472

    
473
                        double step = (maxSize - minSize) / arrayIntervalos.length;
474
                        double size = minSize;
475
                        for (int k = 0; k < arrayIntervalos.length; k++) {
476
                                interval = arrayIntervalos[k];
477
                                theSymbol = newSymbol(symbolType, size);
478

    
479
                                auxLegend.addSymbol(interval, theSymbol);
480
                                System.out.println("addSymbol = " + interval +
481
                                                " theSymbol = " + theSymbol.getDescription());
482
                                numSymbols++;
483

    
484
                                if (numSymbols > 100) {
485
                                        int resp = JOptionPane.showConfirmDialog(this,
486
                                                        PluginServices.getText(this, "mas_de_100_simbolos"),
487
                                                        PluginServices.getText(this, "quiere_continuar"),
488
                                                        JOptionPane.YES_NO_OPTION,
489
                                                        JOptionPane.WARNING_MESSAGE);
490

    
491
                                        if ((resp == JOptionPane.NO_OPTION) ||
492
                                                        (resp == JOptionPane.DEFAULT_OPTION)) {
493
                                                return;
494
                                        }
495
                                }
496

    
497

    
498
                                theSymbol.setDescription(NumberFormat.getInstance().format(interval.getMin()) +
499
                                                " - " +
500
                                                NumberFormat.getInstance().format(interval.getMax()));
501

    
502
                                size = size + step;
503
                        } // for
504

    
505

    
506

    
507
                        symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
508
                                        auxLegend.getValues(),auxLegend.getDescriptions());
509

    
510

    
511
                } catch (ReadDriverException e) {
512
                        NotificationManager.addError(PluginServices.getText(this, "could_not_get_shape_type"), e);
513
                } catch (LegendLayerException e) {
514
                        NotificationManager.addError(PluginServices.getText(this, "failed_computing_intervals"), e);
515
                }
516

    
517
                bDelAll.setEnabled(true);
518
                bDel.setEnabled(true);
519

    
520
        }
521

    
522
        public void setShowBackground(boolean showBackground){
523
                this.showBackground = showBackground;
524
        }
525

    
526
        public boolean getShowBackground(){
527
                return this.showBackground;
528
        }
529

    
530

    
531

    
532

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