Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / filter / FilterDialog.java @ 24986

History | View | Annotate | Download (16.4 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
package com.iver.cit.gvsig.gui.filter;
42

    
43
import java.awt.event.MouseAdapter;
44
import java.awt.event.MouseEvent;
45
import java.text.NumberFormat;
46
import java.text.ParseException;
47
import java.util.ArrayList;
48
import java.util.Comparator;
49
import java.util.Iterator;
50
import java.util.TreeSet;
51
import java.util.regex.Matcher;
52
import java.util.regex.Pattern;
53

    
54
import javax.swing.tree.DefaultMutableTreeNode;
55
import javax.swing.tree.DefaultTreeModel;
56

    
57
import org.apache.log4j.Logger;
58
import org.gvsig.gui.beans.filterPanel.tableFilterQueryPanel.TableFilterQueryJPanel;
59

    
60
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
61
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException;
62
import com.hardcode.gdbms.engine.values.BooleanValue;
63
import com.hardcode.gdbms.engine.values.DateValue;
64
import com.hardcode.gdbms.engine.values.NullValue;
65
import com.hardcode.gdbms.engine.values.StringValue;
66
import com.hardcode.gdbms.engine.values.Value;
67
import com.iver.andami.PluginServices;
68
import com.iver.andami.messages.NotificationManager;
69
import com.iver.andami.ui.mdiManager.IWindow;
70
import com.iver.andami.ui.mdiManager.IWindowListener;
71
import com.iver.andami.ui.mdiManager.WindowInfo;
72
import com.iver.cit.gvsig.project.documents.table.gui.Table;
73
import com.iver.utiles.DefaultCharSet;
74
import com.iver.utiles.StringUtilities;
75
import com.iver.utiles.exceptionHandling.ExceptionHandlingSupport;
76
import com.iver.utiles.exceptionHandling.ExceptionListener;
77

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

    
91
        private String title;
92

    
93
        private final int filterDialog_Width = 500;
94
        private final int filterDialog_Height = 362;
95
        private final int widthIncrementForAndami = 20; // This is necessary because when the panel is sent to Andami, that needs a bit more width-space to show that panel.
96

    
97

    
98
        /**
99
         * This is the default constructor
100
         */
101
        public FilterDialog(String _title) {
102
                super();
103
                title = _title;
104
                defaultTreeModel = (DefaultTreeModel)fieldsJTree.getModel();
105
        }
106
        /**
107
         * This is the default constructor
108
         */
109
        public FilterDialog() {
110
                super();
111
                defaultTreeModel = (DefaultTreeModel)fieldsJTree.getModel();
112
        }
113

    
114
        /*
115
         *  (non-Javadoc)
116
         * @see org.gvsig.gui.beans.filterPanel.AbstractFilterQueryJPanel#initialize()
117
         */
118
        protected void initialize() {
119
                super.initialize();
120

    
121
                super.resizeHeight(filterDialog_Height);
122
                super.resizeWidth(filterDialog_Width - widthIncrementForAndami);
123

    
124
                this.addNewListeners();
125
        }
126

    
127
        /**
128
         * Adds some listeners
129
         */
130
        private void addNewListeners() {
131
                // Listener for "btnAdd"
132
                // Adds more elements to the current set
133
                getBtnAddToCurrentSet().addActionListener(new java.awt.event.ActionListener() {
134
                        /*
135
                         *  (non-Javadoc)
136
                         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
137
                         */
138
                        public void actionPerformed(java.awt.event.ActionEvent e) {
139
                                final String expr = "select * from '" +
140
                                        model.getDataSourceName() + "' where " +
141
                                        getTxtExpression().getText() + ";";
142

    
143
                                logger.debug(expr);
144

    
145
                                PluginServices.backgroundExecution(new Runnable() {
146
                                                public void run() {
147
                                                        for (int i = 0;
148
                                                                        i < expressionListeners.size();
149
                                                                        i++) {
150
                                                                ExpressionListener l = (ExpressionListener) expressionListeners.get(i);
151
                                                                l.addToSet(expr);
152
                                                        }
153
                                                }
154
                                        });
155
                        }
156
                });
157

    
158
                // Listener for "btnNuevo"
159
                // Adds a new set
160
                getBtnNewSet().addActionListener(new java.awt.event.ActionListener() {
161
                        public void actionPerformed(java.awt.event.ActionEvent e) {
162
                                final String expr = "select * from '" +
163
                                        model.getDataSourceName() + "' where " +
164
                                        getTxtExpression().getText() + ";";
165

    
166
                                logger.debug(expr);
167

    
168
                                PluginServices.backgroundExecution(new Runnable() {
169
                                        public void run() {
170
                                                for (int i = 0; i < expressionListeners.size(); i++) {
171
                                                        ExpressionListener l = (ExpressionListener) expressionListeners.get(i);
172
                                                        l.newSet(expr);
173
                                                }
174
                                        }
175
                                });
176
                        }
177
                });
178

    
179
                // Listener for "btnFromSet"
180
                // Selects elements in the table that are according the current filter condition
181
                getBtnFromSet().addActionListener(new java.awt.event.ActionListener() {
182
                        public void actionPerformed(java.awt.event.ActionEvent e) {
183
                                final String expr = "select * from '" +
184
                                        model.getDataSourceName() + "' where " +
185
                                        getTxtExpression().getText() + ";";
186

    
187
                                logger.debug(expr);
188

    
189
                                PluginServices.backgroundExecution(new Runnable() {
190
                                        public void run() {
191
                                                for (int i = 0; i < expressionListeners.size(); i++) {
192
                                                        ExpressionListener l = (ExpressionListener) expressionListeners.get(i);
193
                                                        l.fromSet(expr);
194
                                                }
195
                                        }
196
                                });
197
                        }
198
                });
199

    
200
                // Listener for "fieldsJTree"
201
                getFieldsJTree().addMouseListener(new MouseAdapter() {
202
                        /*
203
                         *  (non-Javadoc)
204
                         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
205
                         */
206
                        public void mouseClicked(MouseEvent e) {
207
                                int row = fieldsJTree.getRowForLocation(e.getX(), e.getY());
208

    
209
                                if (row > -1) {
210
                                        switch (e.getClickCount()) {
211
                                                case 1:
212
                                                        fillValues(row);
213
                                                        break;
214
                                                case 2:
215
                                                        putSymbol(jtreeRoot.getChildAt(row).toString());
216
                                                        break;
217
                                        }
218
                                }
219
                        }
220
                });
221

    
222
                // Listener for "valuesJList"
223
                getValuesJList().addMouseListener(new MouseAdapter() {
224
                        /*
225
                         *  (non-Javadoc)
226
                         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
227
                         */
228
                        public void mouseClicked(MouseEvent e) {
229
                                if (e.getClickCount() == 2) {
230
                                        Value valor = (Value) valuesListModel.getElementAt(getValuesJList().getSelectedIndex());
231

    
232
                                        if (valor instanceof DateValue) {
233
                                                putSymbol("date('" + valor + "')");
234
                                        } else if (valor instanceof BooleanValue) {
235
                                                putSymbol("boolean('" + valor.toString() + "')");
236
                                        } else if (valor instanceof StringValue) {
237
                                                putSymbol("'" + valor + "'");
238
                                        } else {
239
                                                putSymbol(valor.toString());
240
                                        }
241
                                }
242
                        }
243
                });
244
        }
245

    
246
        /**
247
         * Rellena la lista con los valores del campo seleccionado
248
         */
249
        private void fillValues(int row) {
250
                //int index = lstCampos.getSelectedIndex();
251

    
252
                //Index es ahora el ?ndice del campo seleccionado
253
                //Se eliminan los duplicados
254
                TreeSet conjunto = new TreeSet(new Comparator() {
255
                        public int compare(Object o1, Object o2) {
256
                                if ((o1 != null) && (o2 != null)) {
257
                                        Value v2 = (Value) o2;
258
                                        Value v1 = (Value) o1;
259
                                        BooleanValue boolVal;
260

    
261
                                        try {
262
                                                boolVal = (BooleanValue) (v1.greater(v2));
263

    
264
                                                if (boolVal.getValue()) {
265
                                                        return 1;
266
                                                }
267

    
268
                                                boolVal = (BooleanValue) (v1.less(v2));
269

    
270
                                                if (boolVal.getValue()) {
271
                                                        return -1;
272
                                                }
273
                                        } catch (IncompatibleTypesException e) {
274
                                                throw new RuntimeException(e);
275
                                        }
276
                                }
277

    
278
                                return 0;
279
                        }
280
                }); // Para poder ordenar
281

    
282

    
283
                valuesListModel.clear();
284
                try {
285
                        for (int i = 0; i < model.getRowCount(); i++) {
286
                                Value value = model.getFieldValue(i, row);
287

    
288
                                if (value instanceof NullValue)
289
                                    continue;
290

    
291
                                if (!conjunto.contains(value)) {
292
                                    conjunto.add(value);
293
                                }
294
                        }
295

    
296
                        Iterator it = conjunto.iterator();
297

    
298
                        while (it.hasNext())
299
                                valuesListModel.addElement(it.next());
300
                } catch (FilterException e) {
301
                        throwException(e);
302
                }
303
        }
304

    
305
        /**
306
         * DOCUMENT ME!
307
         *
308
         * @param t DOCUMENT ME!
309
         */
310
        public void setModel(ExpressionDataSource t) {
311
                try {
312
                        model = t;
313
            model.start();
314
        } catch (ReadDriverException e1) {
315
            NotificationManager.addError(e1.getMessage(), e1);
316
        }
317

    
318
        jtreeRoot.removeAllChildren();
319

    
320
        try {
321
                        for (int i = 0; i < model.getFieldCount(); i++) {
322
                                Object field = model.getFieldName(i);
323

    
324
                                if (field != null) {
325
                                        jtreeRoot.add(new DefaultMutableTreeNode(field.toString()));
326
                                }
327
                        }
328

    
329
                        defaultTreeModel.setRoot(jtreeRoot);
330
                } catch (FilterException e) {
331
                        throwException(e);
332
                }
333
        }
334

    
335
                /**
336
                 * DOCUMENT ME!
337
                 *
338
                 * @return DOCUMENT ME!
339
                 *
340
                 * @throws ParseException DOCUMENT ME!
341
                 */
342
                private String validateExpression() throws ParseException {
343
                        String expression = txtExpression.getText();
344
        //                HashSet variablesIndexes = new HashSet();
345
        //
346
        //                StringBuffer traducida = new StringBuffer();
347

    
348
                        //Se transforman los nombres de los campos en las variables xix que analizar?n
349
                        //Se quitan los Date(fecha) y se mete la fecha correspondiente
350
                        expression = translateDates(expression);
351
                        expression = translateNumber(expression);
352
                        expression = translateWord(expression, "true", "1");
353
                        expression = translateWord(expression, "false", "0");
354

    
355
                        String replacement;
356
                        Pattern patron = Pattern.compile("[^<>!]=");
357
                        Matcher m = patron.matcher(expression);
358
                        int index = 0;
359

    
360
                        while (m.find(index)) {
361
                                index = m.start();
362
                                replacement = expression.charAt(index) + "==";
363
                                m.replaceFirst(replacement);
364
                                index++;
365
                        }
366

    
367
                        expression = expression.replaceAll("[^<>!]=", "==");
368

    
369
                        logger.debug(expression);
370

    
371
                        return expression;
372
                }
373
        /**
374
         * Redefinition of the 'putSymbol' method of AbstractFilterQueryJPanel
375
         *   (I've made this redefinition for write the same code as the 'putSymbol'
376
         *    code of the original class (FilterDialog) that was in this project
377
         *    (appgvSIG) and didn't has path troubles to find 'StringUtilities').
378
         *
379
         * Sets a symbol on the filter expression (JTextArea that stores and shows
380
         *   the current filter expression)
381
         *
382
         * @param symbol A symbol: character, characters, number, ...
383
         */
384
        protected void putSymbol(String symbol) {
385
                int position = txtExpression.getCaretPosition();
386
                txtExpression.setText(StringUtilities.insert(txtExpression.getText(),
387
                                position, symbol));
388

    
389
                if (symbol.equals(" () ")) {
390
                        position = position + 2;
391
                } else {
392
                        position = position + symbol.length();
393
                }
394

    
395
                txtExpression.setCaretPosition(position);
396
        }
397

    
398
        /**
399
         * DOCUMENT ME!
400
         *
401
         * @param expresion DOCUMENT ME!
402
         * @param substring DOCUMENT ME!
403
         * @param startingPos DOCUMENT ME!
404
         *
405
         * @return DOCUMENT ME!
406
         */
407
        private int getIndex(String expresion, String substring, int startingPos) {
408
                int index = startingPos;
409

    
410
                do {
411
                        index = expresion.indexOf(substring, index);
412
                } while ((StringUtilities.isBetweenSymbols(expresion, index, "\"")) &&
413
                                (index != -1));
414

    
415
                return index;
416
        }
417

    
418
        /**
419
         * DOCUMENT ME!
420
         *
421
         * @param expresion DOCUMENT ME!
422
         * @param word DOCUMENT ME!
423
         * @param translation DOCUMENT ME!
424
         *
425
         * @return DOCUMENT ME!
426
         *
427
         * @throws ParseException DOCUMENT ME!
428
         */
429
        private String translateWord(String expresion, String word,
430
                String translation) throws ParseException {
431
                int booleanIndex = 0;
432
                int endIndex = 0;
433
                StringBuffer res = new StringBuffer();
434

    
435
                while ((booleanIndex = getIndex(expresion, word, booleanIndex)) != -1) {
436
                        res.append(expresion.substring(endIndex, booleanIndex));
437
                        endIndex = booleanIndex + word.length();
438
                        booleanIndex++;
439
                        res.append(translation);
440
                }
441

    
442
                if (endIndex < expresion.length()) {
443
                        res.append(expresion.substring(endIndex));
444
                }
445

    
446
                return res.toString();
447
        }
448

    
449
        /**
450
         * DOCUMENT ME!
451
         *
452
         * @param expresion DOCUMENT ME!
453
         *
454
         * @return DOCUMENT ME!
455
         *
456
         * @throws ParseException DOCUMENT ME!
457
         */
458
        private String translateDates(String expresion) throws ParseException {
459
                //Se obtiene el valor de la fecha
460
                String date = StringUtilities.substringDelimited(expresion, "Date(",
461
                                ")", 0);
462

    
463
                if (date == null) {
464
                        return expresion;
465
                }
466

    
467
                //Se comprueba que no est? entre comillas
468
                int startIndex = expresion.indexOf(date);
469

    
470
                while (startIndex != -1) {
471
                        if (!StringUtilities.isBetweenSymbols(expresion, startIndex, "\"")) {
472
                                //Se sustituye por el valor ordinal de la fecha
473
                                expresion = expresion.substring(0, startIndex - 5) +
474
                                        expresion.substring(startIndex).replaceFirst(date + "\\)",
475
                                                new Long((filterButtonsJPanel.getDateFormat().parse(date)).getTime()).toString());
476
                                ;
477
                        } else {
478
                                startIndex += date.length();
479
                        }
480

    
481
                        //Se obtiene el valor de la fecha
482

    
483
                        /*            date = StringUtilities.substringDelimited(expresion, "Date(", ")",
484
                           startIndex);
485
                         */
486
                        if (date == null) {
487
                                return expresion;
488
                        }
489

    
490
                        startIndex = expresion.indexOf(date, startIndex);
491
                }
492

    
493
                return expresion;
494
        }
495

    
496
        /**
497
         * DOCUMENT ME!
498
         *
499
         * @param expresion DOCUMENT ME!
500
         *
501
         * @return DOCUMENT ME!
502
         *
503
         * @throws ParseException DOCUMENT ME!
504
         */
505
        public String translateNumber(String expresion) throws ParseException {
506
                DefaultCharSet ss = new DefaultCharSet();
507
                ss.addInterval('0', '9');
508
                ss.addCharacter(',');
509
                ss.addCharacter('.');
510

    
511
                String number = StringUtilities.substringWithSymbols(expresion, ss, 0);
512

    
513
                if (number == null) {
514
                        return expresion;
515
                }
516

    
517
                int startIndex = expresion.indexOf(number);
518

    
519
                while (startIndex != -1) {
520
                        Number n = nf.parse(number);
521

    
522
                        if (!StringUtilities.isBetweenSymbols(expresion, startIndex, "\"")) {
523
                                //Se sustituye por el valor ordinal de la fecha
524
                                expresion = expresion.substring(0, startIndex) +
525
                                        expresion.substring(startIndex).replaceFirst(number,
526
                                                n.toString());
527
                        } else {
528
                                startIndex += n.toString().length();
529
                        }
530

    
531
                        number = StringUtilities.substringWithSymbols(expresion, ss,
532
                                        startIndex);
533

    
534
                        if (number == null) {
535
                                return expresion;
536
                        }
537

    
538
                        startIndex = expresion.indexOf(number, startIndex);
539
                }
540

    
541
                return expresion;
542
        }
543

    
544
        /**
545
         * DOCUMENT ME!
546
         *
547
         * @param arg0
548
         *
549
         * @return
550
         */
551
        public boolean addExpressionListener(ExpressionListener arg0) {
552
                return expressionListeners.add(arg0);
553
        }
554

    
555
        /**
556
         * DOCUMENT ME!
557
         *
558
         * @param arg0
559
         *
560
         * @return
561
         */
562
        public boolean removeExpressionListener(ExpressionListener arg0) {
563
                return expressionListeners.remove(arg0);
564
        }
565

    
566
        /**
567
         * @see com.iver.mdiApp.ui.MDIManager.IWindow#getWindowInfo()
568
         */
569
        public WindowInfo getWindowInfo() {
570
                WindowInfo vi = new WindowInfo(WindowInfo.ICONIFIABLE);
571

    
572
                //if (System.getProperty("os.name")co.compareTo(arg0))
573
                vi.setHeight(this.filterDialog_Height);
574
                vi.setWidth(this.filterDialog_Width);
575

    
576
                // Old instructions
577
//                vi.setWidth(480);
578
//                vi.setHeight(362);
579
                vi.setTitle(PluginServices.getText( this, "filtro") + " (" + title + ")");
580
                return vi;
581
        }
582

    
583
        /**
584
         * DOCUMENT ME!
585
         *
586
         * @param o DOCUMENT ME!
587
         */
588
        public void addExceptionListener(ExceptionListener o) {
589
                exceptionHandlingSupport.addExceptionListener(o);
590
        }
591

    
592
        /**
593
         * DOCUMENT ME!
594
         *
595
         * @param o DOCUMENT ME!
596
         *
597
         * @return DOCUMENT ME!
598
         */
599
        public boolean removeExceptionListener(ExceptionListener o) {
600
                return exceptionHandlingSupport.removeExceptionListener(o);
601
        }
602

    
603
        /**
604
         * DOCUMENT ME!
605
         *
606
         * @param t DOCUMENT ME!
607
         */
608
        private void throwException(Throwable t) {
609
                exceptionHandlingSupport.throwException(t);
610
        }
611

    
612
    /* (non-Javadoc)
613
     * @see com.iver.andami.ui.mdiManager.ViewListener#viewActivated()
614
     */
615
    public void windowActivated() {
616
    }
617

    
618
    /* (non-Javadoc)
619
     * @see com.iver.andami.ui.mdiManager.ViewListener#viewClosed()
620
     */
621
    public void windowClosed() {
622
        try {
623
            model.stop();
624
        } catch (ReadDriverException e) {
625
            NotificationManager.addError(e.getMessage(), e);
626
        }
627
    }
628
    public Object getWindowProfile() {
629
                return WindowInfo.TOOL_PROFILE;
630
        }
631
}