Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / filter / NewFilterDialog.java @ 8080

History | View | Annotate | Download (10.5 KB)

1
/* 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

    
42
/**
43
 * 
44
 */
45
package com.iver.cit.gvsig.gui.filter;
46

    
47
import java.text.NumberFormat;
48
import java.text.ParseException;
49
import java.util.ArrayList;
50
import java.util.regex.Matcher;
51
import java.util.regex.Pattern;
52

    
53
import javax.swing.tree.DefaultMutableTreeNode;
54

    
55
import org.apache.log4j.Logger;
56
import org.gvsig.gui.beans.filterPanel.tableFilterQueryPanel.TableFilterQueryJPanel;
57

    
58
import com.hardcode.gdbms.engine.data.driver.DriverException;
59
import com.iver.andami.PluginServices;
60
import com.iver.andami.messages.NotificationManager;
61
import com.iver.andami.ui.mdiManager.IWindow;
62
import com.iver.andami.ui.mdiManager.IWindowListener;
63
import com.iver.andami.ui.mdiManager.WindowInfo;
64
import com.iver.cit.gvsig.project.documents.table.gui.Table;
65
import com.iver.utiles.DefaultCharSet;
66
import com.iver.utiles.StringUtilities;
67
import com.iver.utiles.exceptionHandling.ExceptionHandlingSupport;
68
import com.iver.utiles.exceptionHandling.ExceptionListener;
69

    
70
/**
71
 * This class substitutes the old "FilterDialog" class made by "Fernando Gonz?lez Cort?s"
72
 * The functionality is the same, but now the class is made from separately (and reusable) components
73
 * 
74
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
75
 */
76
public class NewFilterDialog extends TableFilterQueryJPanel implements IWindow, IWindowListener {
77
        private static Logger logger = Logger.getLogger(Table.class.getName());
78
        private ExpressionDataSource model = null;
79
        private ArrayList expressionListeners = new ArrayList();
80
        private ExceptionHandlingSupport exceptionHandlingSupport = new ExceptionHandlingSupport();
81
        private NumberFormat nf = NumberFormat.getNumberInstance();
82
        
83
        private String title;
84

    
85

    
86
        /**
87
         * This is the default constructor
88
         */
89
        public NewFilterDialog(String _title) {                
90
                super();
91
                initialize();
92
                title = _title;
93
        }
94
        /**
95
         * This is the default constructor
96
         */
97
        public NewFilterDialog() {                
98
                super();
99
                initialize();
100
        }
101
        
102
        /**
103
         * DOCUMENT ME!
104
         *
105
         * @param t DOCUMENT ME!
106
         */
107
        public void setModel(ExpressionDataSource t) {
108
                try {
109
                        model = t;
110
            model.start();
111
        } catch (DriverException e1) {
112
            NotificationManager.addError(e1.getMessage(), e1);
113
        }
114
        
115
        jtreeRoot.removeAllChildren();
116
        
117
        try {
118
                        for (int i = 0; i < model.getFieldCount(); i++) {
119
                                Object field = model.getFieldName(i);
120
                                layerFeaturesFields.addField(field);
121
                                jtreeRoot.add(new DefaultMutableTreeNode(field.toString()));
122
                        }
123
                } catch (FilterException e) {
124
                        throwException(e);
125
                }
126
        }
127
        
128
        /**
129
                 * DOCUMENT ME!
130
                 *
131
                 * @return DOCUMENT ME!
132
                 *
133
                 * @throws ParseException DOCUMENT ME!
134
                 */
135
                private String validateExpression() throws ParseException {
136
                        String expression = txtExpression.getText();
137
        //                HashSet variablesIndexes = new HashSet();
138
        //
139
        //                StringBuffer traducida = new StringBuffer();
140
        
141
                        //Se transforman los nombres de los campos en las variables xix que analizar?n
142
                        //Se quitan los Date(fecha) y se mete la fecha correspondiente
143
                        expression = translateDates(expression);
144
                        expression = translateNumber(expression);
145
                        expression = translateWord(expression, "true", "1");
146
                        expression = translateWord(expression, "false", "0");
147
        
148
                        String replacement;
149
                        Pattern patron = Pattern.compile("[^<>!]=");
150
                        Matcher m = patron.matcher(expression);
151
                        int index = 0;
152
        
153
                        while (m.find(index)) {
154
                                index = m.start();
155
                                replacement = expression.charAt(index) + "==";
156
                                m.replaceFirst(replacement);
157
                                index++;
158
                        }
159
        
160
                        expression = expression.replaceAll("[^<>!]=", "==");
161
        
162
                        logger.debug(expression);
163
        
164
                        return expression;
165
                }
166
        /**
167
         * Redefinition of the 'putSymbol' method of AbstractFilterQueryJPanel
168
         *   (I've made this redefinition for write the same code as the 'putSymbol'
169
         *    code of the original class (FilterDialog) that was in this project
170
         *    (appgvSIG) and didn't has path troubles to find 'StringUtilities').
171
         * 
172
         * Sets a symbol on the filter expression (JTextArea that stores and shows
173
         *   the current filter expression)
174
         *
175
         * @param symbol A symbol: character, characters, number, ...
176
         */
177
        protected void putSymbol(String symbol) {
178
                int position = txtExpression.getCaretPosition();
179
                txtExpression.setText(StringUtilities.insert(txtExpression.getText(),
180
                                position, symbol));
181

    
182
                if (symbol.equals(" () ")) {
183
                        position = position + 2;
184
                } else {
185
                        position = position + symbol.length();
186
                }
187

    
188
                txtExpression.setCaretPosition(position);
189
        }
190
        
191
        /**
192
         * DOCUMENT ME!
193
         *
194
         * @param expresion DOCUMENT ME!
195
         * @param substring DOCUMENT ME!
196
         * @param startingPos DOCUMENT ME!
197
         *
198
         * @return DOCUMENT ME!
199
         */
200
        private int getIndex(String expresion, String substring, int startingPos) {
201
                int index = startingPos;
202

    
203
                do {
204
                        index = expresion.indexOf(substring, index);
205
                } while ((StringUtilities.isBetweenSymbols(expresion, index, "\"")) &&
206
                                (index != -1));
207

    
208
                return index;
209
        }
210
        
211
        /**
212
         * DOCUMENT ME!
213
         *
214
         * @param expresion DOCUMENT ME!
215
         * @param word DOCUMENT ME!
216
         * @param translation DOCUMENT ME!
217
         *
218
         * @return DOCUMENT ME!
219
         *
220
         * @throws ParseException DOCUMENT ME!
221
         */
222
        private String translateWord(String expresion, String word,
223
                String translation) throws ParseException {
224
                int booleanIndex = 0;
225
                int endIndex = 0;
226
                StringBuffer res = new StringBuffer();
227

    
228
                while ((booleanIndex = getIndex(expresion, word, booleanIndex)) != -1) {
229
                        res.append(expresion.substring(endIndex, booleanIndex));
230
                        endIndex = booleanIndex + word.length();
231
                        booleanIndex++;
232
                        res.append(translation);
233
                }
234

    
235
                if (endIndex < expresion.length()) {
236
                        res.append(expresion.substring(endIndex));
237
                }
238

    
239
                return res.toString();
240
        }
241

    
242
        /**
243
         * DOCUMENT ME!
244
         *
245
         * @param expresion DOCUMENT ME!
246
         *
247
         * @return DOCUMENT ME!
248
         *
249
         * @throws ParseException DOCUMENT ME!
250
         */
251
        private String translateDates(String expresion) throws ParseException {
252
                //Se obtiene el valor de la fecha
253
                String date = StringUtilities.substringDelimited(expresion, "Date(",
254
                                ")", 0);
255

    
256
                if (date == null) {
257
                        return expresion;
258
                }
259

    
260
                //Se comprueba que no est? entre comillas 
261
                int startIndex = expresion.indexOf(date);
262

    
263
                while (startIndex != -1) {
264
                        if (!StringUtilities.isBetweenSymbols(expresion, startIndex, "\"")) {
265
                                //Se sustituye por el valor ordinal de la fecha
266
                                expresion = expresion.substring(0, startIndex - 5) +
267
                                        expresion.substring(startIndex).replaceFirst(date + "\\)",
268
                                                new Long((filterButtonsJPanel.getDateFormat().parse(date)).getTime()).toString());
269
                                ;
270
                        } else {
271
                                startIndex += date.length();
272
                        }
273

    
274
                        //Se obtiene el valor de la fecha
275

    
276
                        /*            date = StringUtilities.substringDelimited(expresion, "Date(", ")",
277
                           startIndex);
278
                         */
279
                        if (date == null) {
280
                                return expresion;
281
                        }
282

    
283
                        startIndex = expresion.indexOf(date, startIndex);
284
                }
285

    
286
                return expresion;
287
        }
288

    
289
        /**
290
         * DOCUMENT ME!
291
         *
292
         * @param expresion DOCUMENT ME!
293
         *
294
         * @return DOCUMENT ME!
295
         *
296
         * @throws ParseException DOCUMENT ME!
297
         */
298
        public String translateNumber(String expresion) throws ParseException {
299
                DefaultCharSet ss = new DefaultCharSet();
300
                ss.addInterval('0', '9');
301
                ss.addCharacter(',');
302
                ss.addCharacter('.');
303

    
304
                String number = StringUtilities.substringWithSymbols(expresion, ss, 0);
305

    
306
                if (number == null) {
307
                        return expresion;
308
                }
309

    
310
                int startIndex = expresion.indexOf(number);
311

    
312
                while (startIndex != -1) {
313
                        Number n = nf.parse(number);
314

    
315
                        if (!StringUtilities.isBetweenSymbols(expresion, startIndex, "\"")) {
316
                                //Se sustituye por el valor ordinal de la fecha
317
                                expresion = expresion.substring(0, startIndex) +
318
                                        expresion.substring(startIndex).replaceFirst(number,
319
                                                n.toString());
320
                        } else {
321
                                startIndex += n.toString().length();
322
                        }
323

    
324
                        number = StringUtilities.substringWithSymbols(expresion, ss,
325
                                        startIndex);
326

    
327
                        if (number == null) {
328
                                return expresion;
329
                        }
330

    
331
                        startIndex = expresion.indexOf(number, startIndex);
332
                }
333

    
334
                return expresion;
335
        }
336
        
337
        /**
338
         * DOCUMENT ME!
339
         *
340
         * @param arg0
341
         *
342
         * @return
343
         */
344
        public boolean addExpressionListener(ExpressionListener arg0) {
345
                return expressionListeners.add(arg0);
346
        }
347

    
348
        /**
349
         * DOCUMENT ME!
350
         *
351
         * @param arg0
352
         *
353
         * @return
354
         */
355
        public boolean removeExpressionListener(ExpressionListener arg0) {
356
                return expressionListeners.remove(arg0);
357
        }
358
        
359
        /**
360
         * @see com.iver.mdiApp.ui.MDIManager.IWindow#getWindowInfo()
361
         */
362
        public WindowInfo getWindowInfo() {
363
                WindowInfo vi = new WindowInfo(WindowInfo.ICONIFIABLE);
364
                vi.setWidth(480);
365
                vi.setHeight(362);
366
                vi.setTitle(PluginServices.getText( this, "filtro") + " (" + title + ")");
367
                return vi;
368
        }
369
        
370
        /**
371
         * DOCUMENT ME!
372
         *
373
         * @param o DOCUMENT ME!
374
         */
375
        public void addExceptionListener(ExceptionListener o) {
376
                exceptionHandlingSupport.addExceptionListener(o);
377
        }
378

    
379
        /**
380
         * DOCUMENT ME!
381
         *
382
         * @param o DOCUMENT ME!
383
         *
384
         * @return DOCUMENT ME!
385
         */
386
        public boolean removeExceptionListener(ExceptionListener o) {
387
                return exceptionHandlingSupport.removeExceptionListener(o);
388
        }
389
        
390
        /**
391
         * DOCUMENT ME!
392
         *
393
         * @param t DOCUMENT ME!
394
         */
395
        private void throwException(Throwable t) {
396
                exceptionHandlingSupport.throwException(t);
397
        }
398

    
399
    /* (non-Javadoc)
400
     * @see com.iver.andami.ui.mdiManager.ViewListener#viewActivated()
401
     */
402
    public void windowActivated() {
403
    }
404

    
405
    /* (non-Javadoc)
406
     * @see com.iver.andami.ui.mdiManager.ViewListener#viewClosed()
407
     */
408
    public void windowClosed() {
409
        try {
410
            model.stop();
411
        } catch (DriverException e) {
412
            NotificationManager.addError(e.getMessage(), e);
413
        }        
414
    }
415
}