Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extSymbology / src / com / iver / cit / gvsig / project / documents / view / legend / gui / VectorFilterExpression.java @ 23555

History | View | Annotate | Download (11.9 KB)

1 20768 jdominguez
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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
package com.iver.cit.gvsig.project.documents.view.legend.gui;
42
43
import java.awt.BorderLayout;
44
import java.awt.event.ActionEvent;
45
import java.awt.event.ActionListener;
46
import java.io.File;
47
import java.io.IOException;
48
import java.util.ArrayList;
49
50
import javax.swing.ImageIcon;
51
import javax.swing.JCheckBox;
52
import javax.swing.JOptionPane;
53
import javax.swing.JPanel;
54
55
import org.gvsig.gui.beans.swing.JButton;
56 22187 jdominguez
import org.gvsig.symbology.fmap.rendering.VectorFilterExpressionLegend;
57 20768 jdominguez
import org.gvsig.symbology.fmap.symbols.PictureFillSymbol;
58
59
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
60
import com.iver.andami.PluginServices;
61
import com.iver.andami.messages.NotificationManager;
62
import com.iver.andami.ui.mdiManager.IWindow;
63
import com.iver.cit.gvsig.fmap.core.FShape;
64
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillPropertiesStyle;
65
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
66
import com.iver.cit.gvsig.fmap.layers.FLayer;
67
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
68
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
69
import com.iver.cit.gvsig.fmap.rendering.ILegend;
70
71
/**
72
 * Implements the JPanel that shows the properties of a VectorialFilterExpressionLegend
73
 * in order to allows the user to modify its characteristics
74
 *
75
 * @author Pepe Vidal Salvador - jose.vidal.salvador@iver.es
76
 *
77
 */
78 22187 jdominguez
public class VectorFilterExpression extends JPanel implements ILegendPanel,ActionListener {
79 20768 jdominguez
        private static final long serialVersionUID = -7187473609965942511L;
80 22187 jdominguez
        private VectorFilterExpressionLegend theLegend;
81 20768 jdominguez
        private ClassifiableVectorial layer;
82
        private PictureFillSymbol previewSymbol;
83
        private JPanel pnlCenter;
84
        private SymbolTable symbolTable;
85
        private JButton btnAddExpression;
86
        private JButton btnModExpression;
87
        private JButton btnRemoveExpression;
88
        private JCheckBox useDefaultSymbol;
89
        private int shapeType;
90
        /**
91
         * This is the default constructor
92
         */
93 22187 jdominguez
        public VectorFilterExpression() {
94 20768 jdominguez
                super();
95
                initialize();
96
        }
97
98
99
        /**
100
         * This method initializes this
101
         */
102
        private void initialize() {
103
104
                pnlCenter = new JPanel();
105
                pnlCenter.setLayout(new BorderLayout());
106
107
                JPanel pnlButtons = new JPanel();
108
109
                useDefaultSymbol = new JCheckBox(PluginServices.getText(this, "use_default_symbol"));
110
                useDefaultSymbol.setActionCommand("USE_DEFAULT_SYMBOL");
111
                useDefaultSymbol.addActionListener(this);
112
                pnlButtons.add(useDefaultSymbol);
113
114
                btnAddExpression = new JButton(PluginServices.getText(this, "new_filter_expression"));
115
                btnAddExpression.setActionCommand("NEW_EXPRESSION");
116
                btnAddExpression.addActionListener(this);
117
                pnlButtons.add(btnAddExpression);
118
119
                btnModExpression = new JButton(PluginServices.getText(this, "modify_filter_expression"));
120
                btnModExpression.setActionCommand("MODIFY_EXPRESSION");
121
                btnModExpression.addActionListener(this);
122
                pnlButtons.add(btnModExpression);
123
124
                btnRemoveExpression = new JButton(PluginServices.getText(this, "delete_filter_expression"));
125
                btnRemoveExpression.setActionCommand("REMOVE");
126
                btnRemoveExpression.addActionListener(this);
127
                pnlButtons.add(btnRemoveExpression);
128
129
                this.setLayout(new BorderLayout());
130
                this.add(pnlCenter, BorderLayout.CENTER);
131
                this.add(pnlButtons, BorderLayout.SOUTH);
132
133
        }
134
135
136
        public String getDescription() {
137
                return PluginServices.getText(this,"shows_the_elements_of_the_layer_depending_on_the_value_of_a_filter_expression") + ".";
138
        }
139
140
        public ISymbol getIconSymbol() {
141
                if (previewSymbol == null) {
142
                        try {
143
                                previewSymbol = new PictureFillSymbol();
144
                                previewSymbol.setImage( new File(
145
                                                this.getClass().getClassLoader().
146
                                                getResource("images/ValoresUnicos.png").
147
                                                getFile()).toURL());
148
                                previewSymbol.getMarkerFillProperties().
149
                                setFillStyle(
150
                                                IMarkerFillPropertiesStyle.SINGLE_CENTERED_SYMBOL);
151
                        } catch (IOException e) {
152
                                return null;
153
                        }
154
                }
155
                return previewSymbol;
156
        }
157
158
        public ILegend getLegend() {
159
                theLegend.clear();
160
                fillSymbolListFromTable();
161
                return theLegend;
162
        }
163
        /**
164
         * Fills the list of symbols of the legend
165
         *
166
         */
167
        private void fillSymbolListFromTable() {
168
                Object clave;
169
                ISymbol theSymbol;
170
                boolean bRestoValores = false;
171
                int hasta;
172
173
                FLyrVect m = (FLyrVect) layer;
174
                try {
175
176
                        if(theLegend.getClassifyingFieldNames() != null) {
177
                                String[] fNames= theLegend.getClassifyingFieldNames();
178
                                int[] fieldTypes  = new int[theLegend.getClassifyingFieldNames().length];
179
180
                                for (int i = 0; i < theLegend.getClassifyingFieldNames().length; i++) {
181
                                        int fieldIndex = m.getSource().getRecordset().getFieldIndexByName(fNames[i]);
182
                                        fieldTypes[i]= m.getSource().getRecordset().getFieldType(fieldIndex);
183
                                }
184
185
                                theLegend.setClassifyingFieldTypes(fieldTypes);
186
                        }
187
                } catch (ReadDriverException e) {
188
                        NotificationManager.addError(PluginServices.getText(this, "could_not_setup_legend"), e);
189
                }
190
191
192
                if (bRestoValores) {
193
                        hasta = symbolTable.getRowCount() - 1;
194
                } else {
195
                        hasta = symbolTable.getRowCount();
196
                }
197
198
                for (int row = 0; row < symbolTable.getRowCount(); row++) {
199
                        clave =  symbolTable.getFieldValue(row, 1);
200
                        theSymbol = (ISymbol) symbolTable.getFieldValue(row, 0);
201
                        theSymbol.setDescription((String) symbolTable.getFieldValue(row, 2));
202
                        theLegend.addSymbol(clave, theSymbol);
203
                }
204
205
                if (bRestoValores) {
206
                        theSymbol = (ISymbol) symbolTable.getFieldValue(hasta, 0);
207
                        theLegend.setDefaultSymbol(theSymbol);
208
                }
209
210
211
        }
212
213
        public Class getLegendClass() {
214 22187 jdominguez
                return VectorFilterExpressionLegend.class;
215 20768 jdominguez
        }
216
217
        public JPanel getPanel() {
218
                return this;
219
        }
220
221
        public Class getParentClass() {
222
                return Categories.class;
223
        }
224
225
        public String getTitle() {
226
                return PluginServices.getText(this,"expressions");
227
        }
228
229
        public boolean isSuitableFor(FLayer layer) {
230
231
                FLyrVect lVect = (FLyrVect) layer;
232
                try {
233
                        return lVect.getShapeType() != FShape.MULTI;
234
                } catch (ReadDriverException e) {
235
                        return false;
236
                }
237
238
        }
239
240
        public void setData(FLayer lyr, ILegend legend) {
241
                this.layer = (ClassifiableVectorial) lyr;
242
                shapeType = 0;
243
244
                try {
245
                        shapeType = this.layer.getShapeType();
246
                } catch (ReadDriverException e) {
247
                        NotificationManager.addError(PluginServices.getText(this, "generating_intervals"), e);
248
                }
249
250
                if (symbolTable != null)
251
                        pnlCenter.remove(symbolTable);
252
253
                symbolTable = new SymbolTable(this, "expressions", shapeType);
254
                pnlCenter.add(symbolTable, BorderLayout.CENTER);
255
256 22187 jdominguez
                if (legend instanceof VectorFilterExpressionLegend) {
257
                        theLegend = (VectorFilterExpressionLegend) legend;
258 20768 jdominguez
                        symbolTable.fillTableFromSymbolList(theLegend.getSymbols(),
259
                                        theLegend.getValues(), theLegend.getDescriptions());
260
                        useDefaultSymbol.setSelected(theLegend.isUseDefaultSymbol());
261
                } else {
262 22187 jdominguez
                        theLegend = new VectorFilterExpressionLegend();
263 20768 jdominguez
                        theLegend.setShapeType(shapeType);
264
                        useDefaultSymbol.setSelected(false);
265
                        theLegend.useDefaultSymbol(useDefaultSymbol.isSelected());
266
                }
267
        }
268
269
        public void actionPerformed(ActionEvent e) {
270
                if (e.getActionCommand() == "NEW_EXPRESSION") {
271
                        ExpressionCreator newExpression = new ExpressionCreator((FLyrVect) this.layer );
272
                        PluginServices.getMDIManager().addWindow((IWindow) newExpression);
273
                        String expression = ((ExpressionCreator) newExpression).getExpression();
274
                        if(newExpression.getFieldNamesExpression() != null)
275
                                addClassFieldNames(newExpression.getFieldNamesExpression());
276
277
                        if(expression != null)
278
                                if(expression.compareTo("") != 0) {
279
                                        symbolTable.addTableRecord(newExpression.getSymbolForExpression(), expression, newExpression.getDescriptionForExpression());
280
                                        theLegend.addSymbol(expression, newExpression.getSymbolForExpression());
281
                                }
282
                        repaint();
283
                }
284
                else if (e.getActionCommand() == "MODIFY_EXPRESSION") {
285
286
                        if(symbolTable.getSelectedRowElements() == null) {
287
                                JOptionPane.showMessageDialog(this, PluginServices.getText(this, "select_one_row")+".\n");
288
                        }
289
290
                        else {
291
                                ISymbol mySymbol = (ISymbol) symbolTable.getSelectedRowElements()[0];
292
                                String expression = (String) symbolTable.getSelectedRowElements()[1];
293
                                String myDesc = (String) symbolTable.getSelectedRowElements()[2];
294
295
                                ExpressionCreator newExpression = new ExpressionCreator((FLyrVect) this.layer );
296
                                newExpression.setExpression(expression);
297
                                newExpression.setDescriptionForExpression(myDesc);
298
                                newExpression.setSymbolForExpression(mySymbol);
299
                                PluginServices.getMDIManager().addWindow((IWindow) newExpression);
300
301
                                expression = ((ExpressionCreator) newExpression).getExpression();
302
                                if(expression != null)
303
                                        if(expression.compareTo("") != 0) {
304
                                                symbolTable.removeSelectedRows();
305
                                                theLegend.delSymbol(expression);
306
                                                symbolTable.addTableRecord(newExpression.getSymbolForExpression(), ((ExpressionCreator) newExpression).getExpression(), newExpression.getDescriptionForExpression());
307
                                                theLegend.addSymbol(((ExpressionCreator) newExpression).getExpression(), newExpression.getSymbolForExpression());
308
                                                repaint();
309
                                        }
310
                        }
311
                }
312
                else if (e.getActionCommand() == "USE_DEFAULT_SYMBOL" ) {
313
                        if(theLegend != null) {
314
                                theLegend.useDefaultSymbol(useDefaultSymbol.isSelected());
315
                                repaint();
316
                        }
317
                }
318
                else if (e.getActionCommand() == "REMOVE") {
319
                        if(symbolTable.getSelectedRowElements() == null) {
320
                                JOptionPane.showMessageDialog(this, PluginServices.getText(this, "select_one_row")+".\n");
321
                        }
322
                        else
323
                                symbolTable.removeSelectedRows();
324
                }
325
        }
326
        /**
327
         * Adds new classifying field names to the legend when a new expression is
328
         * created or an existing one is modified
329
         * @param fieldNamesExpression
330
         */
331
        private void addClassFieldNames(Object[] fieldNamesExpression) {
332
                boolean appears = false;
333
                ArrayList<String> myFieldNames = new ArrayList<String>();
334
335
                if (theLegend.getClassifyingFieldNames() != null) {
336
337
                        for (int i = 0; i < theLegend.getClassifyingFieldNames().length; i++) {
338
                                myFieldNames.add(theLegend.getClassifyingFieldNames()[i]);
339
                        }
340
                        for (int i = 0; i < fieldNamesExpression.length; i++) {
341
                                appears = false;
342
                                for (int j = 0; j < theLegend.getClassifyingFieldNames().length; j++) {
343
                                        if (theLegend.getClassifyingFieldNames()[j].compareTo((String) fieldNamesExpression[i]) == 0)
344
                                                appears = true;
345
                                }
346
                                if(!appears) {
347
                                        myFieldNames.add((String) fieldNamesExpression[i]);
348
                                }
349
                        }
350
351
                        theLegend.setClassifyingFieldNames((String[])myFieldNames.toArray(new
352
                                        String[myFieldNames.size()]));
353
                }
354
355
                else {
356
                        for (int i = 0; i < fieldNamesExpression.length; i++) {
357
                                myFieldNames.add((String) fieldNamesExpression[i]);
358
                        }
359
                        theLegend.setClassifyingFieldNames((String[])myFieldNames.toArray(new
360
                                        String[myFieldNames.size()]));
361
                }
362
        }
363
364
365
        public ImageIcon getIcon() {
366
                // TODO Auto-generated method stub
367
                return null;
368
        }
369
370
371
}