Statistics
| Revision:

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

History | View | Annotate | Download (10.5 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 10315 2007-02-14 09:59:17Z jaume $
45
* $Log$
46
* Revision 1.1.2.3  2007-02-14 09:59:17  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1.2.2  2007/02/13 16:19:19  jaume
50
* graduated symbol legends (start commiting)
51
*
52
* Revision 1.1.2.1  2007/02/12 15:14:41  jaume
53
* refactored interval legend and added graduated symbol legend
54
*
55
*
56
*/
57
package com.iver.cit.gvsig.project.documents.view.legend.gui;
58

    
59
import java.awt.Color;
60
import java.awt.Dimension;
61
import java.awt.FlowLayout;
62
import java.awt.event.ActionEvent;
63
import java.awt.event.ActionListener;
64
import java.text.NumberFormat;
65

    
66
import javax.swing.BorderFactory;
67
import javax.swing.JLabel;
68
import javax.swing.JOptionPane;
69
import javax.swing.JPanel;
70

    
71
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
72

    
73
import com.iver.andami.PluginServices;
74
import com.iver.andami.messages.NotificationManager;
75
import com.iver.cit.gvsig.fmap.DriverException;
76
import com.iver.cit.gvsig.fmap.core.FShape;
77
import com.iver.cit.gvsig.fmap.core.ISymbol;
78
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
79
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
80
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
81
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
82
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
83
import com.iver.cit.gvsig.fmap.layers.FLayer;
84
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
85
import com.iver.cit.gvsig.fmap.rendering.FInterval;
86
import com.iver.cit.gvsig.fmap.rendering.GraduatedSymbolLegend;
87
import com.iver.cit.gvsig.fmap.rendering.Legend;
88
import com.iver.cit.gvsig.fmap.rendering.NullIntervalValue;
89
import com.iver.cit.gvsig.gui.styling.SymbolSelector;
90

    
91
import de.ios.framework.swing.JNumberField;
92

    
93
public class GraduatedSymbols extends VectorialInterval implements ILegendPanel {
94

    
95
        private JNumberField txtMinSize;
96
        private JNumberField txtMaxSize;
97
        private ISymbol templateSymbol;
98
        private JSymbolPreviewButton btnTemplate;
99
        private int shapeType;
100

    
101
        public JPanel getOptionPanel() {
102
            if (optionPanel == null) {
103
                optionPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
104
                optionPanel.setBorder(BorderFactory.
105
                            createTitledBorder(null,
106
                                            PluginServices.getText(this, "symbol")));
107

    
108
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
109
                aux.addComponent(new JLabel(PluginServices.getText(this, "size")));
110
                aux.addComponent(PluginServices.getText(this, "from")+":",
111
                    getTxtMinSize());
112
                aux.addComponent(PluginServices.getText(this, "to")+":",
113
                    getTxtMaxSize());
114
                aux.addComponent(PluginServices.getText(this, "template"), getBtnTemplate());
115
                optionPanel.add(aux);
116

    
117
        }
118
        return optionPanel;
119
        }
120

    
121
        private JSymbolPreviewButton getBtnTemplate() {
122
                if (btnTemplate == null) {
123
                        btnTemplate = new JSymbolPreviewButton();
124
                        btnTemplate.addActionListener(new ActionListener() {
125
                                public void actionPerformed(ActionEvent e) {
126
                                        SymbolSelector sSelect = new SymbolSelector(templateSymbol, shapeType);
127
                                        PluginServices.getMDIManager().addWindow(sSelect);
128
                                        templateSymbol = sSelect.getSymbol();
129
                                        btnTemplate.setSymbol(templateSymbol);
130
                                }
131
                        });
132
                        btnTemplate.setPreferredSize(new Dimension(100, 35));
133
                }
134
                return btnTemplate;
135
        }
136

    
137
        private JNumberField getTxtMaxSize() {
138
                if (txtMaxSize == null) {
139
                        txtMaxSize = new JNumberField();
140

    
141
                }
142
                return txtMaxSize;
143
        }
144

    
145
        private JNumberField getTxtMinSize() {
146
                if (txtMinSize == null) {
147
                        txtMinSize = new JNumberField();
148
                }
149
                return txtMinSize;
150
        }
151

    
152
        public void setData(FLayer lyr, Legend legend) {
153
                this.layer = (FLyrVect) lyr;
154

    
155
            try {
156
                    setShapeType(this.layer.getShapeType());
157
            } catch (DriverException e) {
158
                    NotificationManager.addError(PluginServices.getText(this, "generating_intervals"), e);
159
            }
160

    
161
            if (symbolTable != null)
162
                      pnlCenter.remove(symbolTable);
163

    
164

    
165
            symbolTable = new FSymbolTable("intervals", shapeType);
166
            pnlCenter.add(symbolTable);
167
        fillFieldNames();
168

    
169
        if (legend instanceof GraduatedSymbolLegend) {
170
                renderer = (GraduatedSymbolLegend) legend;
171
                GraduatedSymbolLegend theRenderer = (GraduatedSymbolLegend) legend;
172
                getChkDefaultvalues().setSelected(theRenderer.isUseDefaultSymbol());
173
            cmbField.getModel().setSelectedItem(theRenderer.getFieldName());
174
            symbolTable.fillTableFromSymbolList(theRenderer.getSymbols(),
175
                theRenderer.getValues(), theRenderer.getDescriptions());
176
            getTxtMaxSize().setText(
177
                            String.valueOf(theRenderer.getMaxSymbolSize()));
178
            getTxtMinSize().setText(
179
                            String.valueOf(theRenderer.getMinSymbolSize()));
180

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

    
187
        cmbFieldType.setSelectedIndex(renderer.getIntervalType());
188
        }
189

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

    
202
                case FShape.LINE:
203
                        this.shapeType = FShape.LINE;
204
                        break;
205
                default:
206
                        throw new Error("Unknown symbol type");
207
                }
208

    
209
        }
210

    
211
        public String getDescription() {
212
                return PluginServices.getText(this, "draw_quantities_using_symbol_size_to_show_relative_values");
213
        }
214

    
215
        public Legend getLegend() {
216
                GraduatedSymbolLegend l = (GraduatedSymbolLegend) super.getLegend();
217
                l.setMinSymbolSize(getTxtMinSize().getValue().doubleValue());
218
                l.setMaxSymbolSize(getTxtMaxSize().getValue().doubleValue());
219
                l.setDefaultSymbol(templateSymbol);
220
                return l;
221
        }
222

    
223
        public ISymbol getPreviewSymbol() {
224
                // TODO Implement it
225
                return null;
226
        }
227

    
228
        public Class getParentClass() {
229
                return Quantities.class;
230
        }
231

    
232
        public String getTitle() {
233
                return PluginServices.getText(this, "graduated_symbols");
234
        }
235

    
236
        public JPanel getPanel() {
237
                return this;
238
        }
239

    
240
        public Class getLegendClass() {
241
                return GraduatedSymbolLegend.class;
242
        }
243

    
244

    
245
        private ISymbol newSymbol(int shapeType, double size) {
246
                if (templateSymbol == null) {
247
                        switch (shapeType) {
248
                        case FShape.POINT:
249
                        case FShape.POLYGON:
250
                                templateSymbol = new SimpleMarkerSymbol();
251
                                ((SimpleMarkerSymbol) templateSymbol).setSize(size);
252
                                ((SimpleMarkerSymbol) templateSymbol).setColor(Color.DARK_GRAY);
253
                                break;
254

    
255
                        case FShape.LINE:
256
                                templateSymbol = new SimpleLineSymbol();
257
                                ((SimpleLineSymbol) templateSymbol).setWidth(size);
258
                                ((SimpleLineSymbol) templateSymbol).setColor(Color.DARK_GRAY);
259
                                break;
260
                        default:
261
                                throw new Error("Unknown symbol type");
262
                        }
263
                        return newSymbol(shapeType, size);
264
                } else {
265
                        // clone symbol
266
                        ISymbol mySymbol = SymbologyFactory.createSymbolFromXML(templateSymbol.getXMLEntity(), null);
267

    
268
                        if (mySymbol instanceof ILineSymbol) {
269
                                ILineSymbol lSym = (ILineSymbol) templateSymbol;
270
                                lSym.setWidth(size);
271

    
272
                        }
273
                        if (mySymbol instanceof IMarkerSymbol) {
274
                                IMarkerSymbol mSym = (IMarkerSymbol) templateSymbol;
275
                                mSym.setSize(size);
276

    
277
                        }
278
                        return mySymbol;
279
                }
280
        }
281
        /**
282
     * Damos una primera pasada para saber los l?mites inferior y superior y
283
     * rellenar un array con los valores. Luego dividimos ese array en
284
     * intervalos.
285
     */
286
        protected void fillTableValues() {
287
                try {
288

    
289
                        symbolTable.removeAllItems();
290

    
291
                        FInterval[] arrayIntervalos = calculateIntervals();
292
                        if (arrayIntervalos == null)
293
                                return;
294

    
295
                        FInterval interval;
296
                        NumberFormat.getInstance().setMaximumFractionDigits(2);
297
                        renderer.clear();
298

    
299
                        float minSize = getTxtMinSize().getValue().floatValue();
300
                        float maxSize = getTxtMaxSize().getValue().floatValue();
301
                        ISymbol theSymbol;
302

    
303
                        if (chkdefaultvalues.isSelected()) {
304
                                auxLegend.getDefaultSymbol().setDescription("Default");
305
                                auxLegend.addSymbol(new NullIntervalValue(),
306
                                                auxLegend.getDefaultSymbol());
307
                        }
308

    
309
                        int symbolType;
310
                        symbolType = layer.getShapeType();
311
                        int numSymbols = 0;
312

    
313
                        double step = (maxSize - minSize) / arrayIntervalos.length;
314
                        double size = minSize;
315
                        for (int k = 0; k < arrayIntervalos.length; k++) {
316
                                interval = arrayIntervalos[k];
317
                                theSymbol = newSymbol(symbolType, size);
318

    
319
                                auxLegend.addSymbol(interval, theSymbol);
320
                                System.out.println("addSymbol = " + interval +
321
                                                " theSymbol = " + theSymbol.getDescription());
322
                                numSymbols++;
323

    
324
                                if (numSymbols > 100) {
325
                                        int resp = JOptionPane.showConfirmDialog(this,
326
                                                        PluginServices.getText(this, "mas_de_100_simbolos"),
327
                                                        PluginServices.getText(this, "quiere_continuar"),
328
                                                        JOptionPane.YES_NO_OPTION,
329
                                                        JOptionPane.WARNING_MESSAGE);
330

    
331
                                        if ((resp == JOptionPane.NO_OPTION) ||
332
                                                        (resp == JOptionPane.DEFAULT_OPTION)) {
333
                                                return;
334
                                        }
335
                                }
336

    
337

    
338
                                theSymbol.setDescription(NumberFormat.getInstance().format(interval.getMin()) +
339
                                                " - " +
340
                                                NumberFormat.getInstance().format(interval.getMax()));
341

    
342
                                size = size + step;
343
                        } // for
344

    
345
                        symbolTable.fillTableFromSymbolList(auxLegend.getSymbols(),
346
                                        auxLegend.getValues(), auxLegend.getDescriptions());
347

    
348
                } catch (DriverException e) {
349
                        NotificationManager.addError(PluginServices.getText(this, "could_not_get_shape_type"), e);
350
                }
351

    
352
                bDelAll.setEnabled(true);
353
                bDel.setEnabled(true);
354

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