Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / GraduatedSymbols.java @ 11704

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

    
77
import java.awt.Color;
78
import java.awt.Dimension;
79
import java.awt.FlowLayout;
80
import java.text.NumberFormat;
81

    
82
import javax.swing.BorderFactory;
83
import javax.swing.JLabel;
84
import javax.swing.JOptionPane;
85
import javax.swing.JPanel;
86

    
87
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
88

    
89
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
90
import com.iver.andami.PluginServices;
91
import com.iver.andami.messages.NotificationManager;
92
import com.iver.cit.gvsig.fmap.core.FShape;
93
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
94
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
95
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
96
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
97
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
98
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
99
import com.iver.cit.gvsig.fmap.layers.FLayer;
100
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
101
import com.iver.cit.gvsig.fmap.rendering.FInterval;
102
import com.iver.cit.gvsig.fmap.rendering.GraduatedSymbolLegend;
103
import com.iver.cit.gvsig.fmap.rendering.ILegend;
104
import com.iver.cit.gvsig.fmap.rendering.NullIntervalValue;
105
import com.iver.cit.gvsig.gui.IncrementalNumberField;
106

    
107
public class GraduatedSymbols extends VectorialInterval implements ILegendPanel {
108

    
109
        private IncrementalNumberField txtMinSize;
110
        private IncrementalNumberField txtMaxSize;
111
        private JSymbolPreviewButton btnTemplate;
112
        private int shapeType;
113

    
114
        public JPanel getOptionPanel() {
115
            if (optionPanel == null) {
116
                optionPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
117
                optionPanel.setBorder(BorderFactory.
118
                            createTitledBorder(null,
119
                                            PluginServices.getText(this, "symbol")));
120

    
121
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
122
                aux.addComponent(new JLabel(PluginServices.getText(this, "size")));
123
                aux.addComponent(PluginServices.getText(this, "from")+":",
124
                    getTxtMinSize());
125
                aux.addComponent(PluginServices.getText(this, "to")+":",
126
                    getTxtMaxSize());
127
                aux.addComponent(PluginServices.getText(this, "template"), getBtnTemplate());
128
                optionPanel.add(aux);
129

    
130
        }
131
        return optionPanel;
132
        }
133

    
134
        private JSymbolPreviewButton getBtnTemplate() {
135
                if (btnTemplate == null) {
136
                        btnTemplate = new JSymbolPreviewButton(shapeType);
137
                        btnTemplate.setPreferredSize(new Dimension(100, 35));
138
                }
139
                return btnTemplate;
140
        }
141

    
142
        private IncrementalNumberField getTxtMaxSize() {
143
                if (txtMaxSize == null) {
144
                        txtMaxSize = new IncrementalNumberField(String.valueOf(25), 7);
145

    
146
                }
147
                return txtMaxSize;
148
        }
149

    
150
        private IncrementalNumberField getTxtMinSize() {
151
                if (txtMinSize == null) {
152
                        txtMinSize = new IncrementalNumberField(String.valueOf(3), 7);
153
                }
154
                return txtMinSize;
155
        }
156

    
157
        public void setData(FLayer lyr, ILegend legend) {
158
                this.layer = (FLyrVect) lyr;
159

    
160
            try {
161
                    setShapeType(this.layer.getShapeType());
162
            } catch (ReadDriverException e) {
163
                    NotificationManager.addError(PluginServices.getText(this, "generating_intervals"), e);
164
            }
165

    
166
            if (symbolTable != null)
167
                      pnlCenter.remove(symbolTable);
168

    
169

    
170
            symbolTable = new FSymbolTable("intervals", shapeType);
171
            pnlCenter.add(symbolTable);
172
        fillFieldNames();
173

    
174
        if (legend instanceof GraduatedSymbolLegend) {
175
                renderer = (GraduatedSymbolLegend) legend;
176
                GraduatedSymbolLegend theRenderer = (GraduatedSymbolLegend) legend;
177
                getChkDefaultvalues().setSelected(theRenderer.isUseDefaultSymbol());
178
            cmbField.setSelectedItem(theRenderer.getFieldNames()[0]);
179
            symbolTable.fillTableFromSymbolList(theRenderer.getSymbols(),
180
                theRenderer.getValues(), theRenderer.getDescriptions());
181
            getTxtMaxSize().setDouble(theRenderer.getMaxSymbolSize());
182
            getTxtMinSize().setDouble(theRenderer.getMinSymbolSize());
183

    
184
        } else {
185
                // Si la capa viene con otro tipo de leyenda, creamos
186
                // una nueva del tipo que maneja este panel
187
                renderer = new GraduatedSymbolLegend();
188
        }
189

    
190
        cmbFieldType.setSelectedIndex(renderer.getIntervalType());
191
        }
192

    
193
        /**
194
         * Polygon type does not have any sense in this context, it is considered
195
         * as Marker type. Lines are lines, and markers ar markers.
196
         * @param shapeType
197
         */
198
        private void setShapeType(int shapeType) {
199
                switch (shapeType) {
200
                case FShape.POINT:
201
                case FShape.POLYGON:
202
                        this.shapeType = FShape.POINT;
203
                        break;
204

    
205
                case FShape.LINE:
206
                        this.shapeType = FShape.LINE;
207
                        break;
208
                default:
209
                        throw new Error("Unknown symbol type");
210
                }
211
                getBtnTemplate().setShapeType(this.shapeType);
212
        }
213

    
214
        public String getDescription() {
215
                return PluginServices.getText(this, "draw_quantities_using_symbol_size_to_show_relative_values");
216
        }
217

    
218
        public ILegend getLegend() {
219
                GraduatedSymbolLegend l = (GraduatedSymbolLegend) super.getLegend();
220
                l.setMinSymbolSize(getTxtMinSize().getDouble());
221
                l.setMaxSymbolSize(getTxtMaxSize().getDouble());
222
                l.setDefaultSymbol(getBtnTemplate().getSymbol());
223
                try {
224
                        l.setShapeType(layer.getShapeType());
225
                } catch (ReadDriverException e) {
226
                        // TODO Auto-generated catch block
227
                        NotificationManager.addWarning("Reached what should be unreachable code", e);
228
                }
229
                return l;
230
        }
231

    
232
        public ISymbol getPreviewSymbol() {
233
                // TODO Implement it
234
                return null;
235
        }
236

    
237
        public Class getParentClass() {
238
                return Quantities.class;
239
        }
240

    
241
        public String getTitle() {
242
                return PluginServices.getText(this, "graduated_symbols");
243
        }
244

    
245
        public JPanel getPanel() {
246
                return this;
247
        }
248

    
249
        public Class getLegendClass() {
250
                return GraduatedSymbolLegend.class;
251
        }
252

    
253

    
254
        private ISymbol newSymbol(int shapeType, double size) {
255
                if (getBtnTemplate().getSymbol() == null) {
256
                        ISymbol templateSymbol;
257
                        switch (shapeType) {
258
                        case FShape.POINT:
259
                        case FShape.POLYGON:
260
                                templateSymbol = new SimpleMarkerSymbol();
261
                                ((SimpleMarkerSymbol) templateSymbol).setSize(size);
262
                                ((SimpleMarkerSymbol) templateSymbol).setColor(Color.DARK_GRAY);
263
                                break;
264

    
265
                        case FShape.LINE:
266
                                templateSymbol = new SimpleLineSymbol();
267
                                ((SimpleLineSymbol) templateSymbol).setLineWidth(size);
268
                                ((SimpleLineSymbol) templateSymbol).setLineColor(Color.DARK_GRAY);
269
                                break;
270
                        default:
271
                                throw new Error("Unknown symbol type");
272
                        }
273
                        getBtnTemplate().setSymbol(templateSymbol);
274
                        return newSymbol(shapeType, size);
275
                } else {
276
                        // clone symbol
277
                        ISymbol mySymbol = SymbologyFactory.createSymbolFromXML(
278
                                        getBtnTemplate().getSymbol().getXMLEntity(), null);
279

    
280
                        if (mySymbol instanceof ILineSymbol) {
281
                                ILineSymbol lSym = (ILineSymbol) mySymbol;
282
                                lSym.setLineWidth(size);
283

    
284
                        }
285
                        if (mySymbol instanceof IMarkerSymbol) {
286
                                IMarkerSymbol mSym = (IMarkerSymbol) mySymbol;
287
                                mSym.setSize(size);
288

    
289
                        }
290
                        return mySymbol;
291
                }
292
        }
293
        /**
294
     * Damos una primera pasada para saber los l?mites inferior y superior y
295
     * rellenar un array con los valores. Luego dividimos ese array en
296
     * intervalos.
297
     */
298
        protected void fillTableValues() {
299
                try {
300

    
301
                        symbolTable.removeAllItems();
302

    
303
                        FInterval[] arrayIntervalos = calculateIntervals();
304
                        if (arrayIntervalos == null)
305
                                return;
306

    
307
                        FInterval interval;
308
                        NumberFormat.getInstance().setMaximumFractionDigits(2);
309
                        renderer.clear();
310

    
311
                        float minSize = (float) getTxtMinSize().getDouble();
312
                        float maxSize = (float) getTxtMaxSize().getDouble();
313
                        ISymbol theSymbol;
314

    
315
                        if (chkdefaultvalues.isSelected()) {
316
                                auxLegend.getDefaultSymbol().setDescription("Default");
317
                                auxLegend.addSymbol(new NullIntervalValue(),
318
                                                auxLegend.getDefaultSymbol());
319
                        }
320

    
321
                        int symbolType;
322
                        symbolType = layer.getShapeType();
323
                        int numSymbols = 0;
324

    
325
                        double step = (maxSize - minSize) / arrayIntervalos.length;
326
                        double size = minSize;
327
                        for (int k = 0; k < arrayIntervalos.length; k++) {
328
                                interval = arrayIntervalos[k];
329
                                theSymbol = newSymbol(symbolType, size);
330

    
331
                                auxLegend.addSymbol(interval, theSymbol);
332
                                System.out.println("addSymbol = " + interval +
333
                                                " theSymbol = " + theSymbol.getDescription());
334
                                numSymbols++;
335

    
336
                                if (numSymbols > 100) {
337
                                        int resp = JOptionPane.showConfirmDialog(this,
338
                                                        PluginServices.getText(this, "mas_de_100_simbolos"),
339
                                                        PluginServices.getText(this, "quiere_continuar"),
340
                                                        JOptionPane.YES_NO_OPTION,
341
                                                        JOptionPane.WARNING_MESSAGE);
342

    
343
                                        if ((resp == JOptionPane.NO_OPTION) ||
344
                                                        (resp == JOptionPane.DEFAULT_OPTION)) {
345
                                                return;
346
                                        }
347
                                }
348

    
349

    
350
                                theSymbol.setDescription(NumberFormat.getInstance().format(interval.getMin()) +
351
                                                " - " +
352
                                                NumberFormat.getInstance().format(interval.getMax()));
353

    
354
                                size = size + step;
355
                        } // for
356

    
357
                        symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
358
                                        auxLegend.getValues(), auxLegend.getDescriptions());
359

    
360
                } catch (ReadDriverException e) {
361
                        NotificationManager.addError(PluginServices.getText(this, "could_not_get_shape_type"), e);
362
                }
363

    
364
                bDelAll.setEnabled(true);
365
                bDel.setEnabled(true);
366

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