Statistics
| Revision:

root / trunk / extensions / extWFS2 / src / com / iver / cit / gvsig / gui / panels / WFSFilterPanel.java @ 10073

History | View | Annotate | Download (47.9 KB)

1
package com.iver.cit.gvsig.gui.panels;
2

    
3
import java.awt.Color;
4
import java.awt.event.MouseAdapter;
5
import java.awt.event.MouseEvent;
6
import java.io.UnsupportedEncodingException;
7
import java.net.URLEncoder;
8
import java.text.DateFormat;
9
import java.text.NumberFormat;
10
import java.text.ParseException;
11
import java.util.ArrayList;
12
import java.util.Comparator;
13
import java.util.HashMap;
14
import java.util.Iterator;
15
import java.util.Map;
16
import java.util.Set;
17
import java.util.StringTokenizer;
18
import java.util.TreeSet;
19
import java.util.Vector;
20
import java.util.regex.Matcher;
21
import java.util.regex.Pattern;
22

    
23
import javax.swing.DefaultListModel;
24
import javax.swing.JLabel;
25
import javax.swing.JOptionPane;
26
import javax.swing.event.DocumentEvent;
27
import javax.swing.event.DocumentListener;
28
import javax.swing.event.TreeSelectionEvent;
29
import javax.swing.event.TreeSelectionListener;
30
import javax.swing.tree.DefaultMutableTreeNode;
31
import javax.swing.tree.DefaultTreeModel;
32
import javax.swing.tree.TreePath;
33

    
34
import org.apache.log4j.Logger;
35
import org.gvsig.gui.beans.Messages;
36
import org.gvsig.gui.beans.filterPanel.filterQueryPanel.FilterQueryJPanel;
37
import org.gvsig.gui.beans.filterPanel.filterQueryPanel.jLabelAsCell.DefaultListModelForJLabelAsCell;
38
import org.gvsig.gui.beans.filterPanel.filterQueryPanel.jLabelAsCell.JLabelAsCellValueLoaded;
39
import org.gvsig.gui.beans.filterPanel.filterQueryPanel.jLabelAsCell.JLabelAsCellValueNotLoaded;
40
import org.gvsig.remoteClient.gml.schemas.XMLElement;
41
import org.gvsig.remoteClient.gml.types.IXMLType;
42

    
43
import com.hardcode.gdbms.engine.data.driver.DriverException;
44
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException;
45
import com.hardcode.gdbms.engine.values.BooleanValue;
46
import com.hardcode.gdbms.engine.values.ComplexValue;
47
import com.hardcode.gdbms.engine.values.NullValue;
48
import com.hardcode.gdbms.engine.values.Value;
49
import com.iver.andami.PluginServices;
50
import com.iver.andami.messages.NotificationManager;
51
import com.iver.andami.ui.mdiManager.IWindow;
52
import com.iver.cit.gvsig.ProjectExtension;
53
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
54
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
55
import com.iver.cit.gvsig.fmap.layers.FLayer;
56
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
57
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
58
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
59
import com.iver.cit.gvsig.fmap.layers.WFSLayerNode;
60
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
61
import com.iver.cit.gvsig.gui.filter.ExpressionDataSource;
62
import com.iver.cit.gvsig.gui.filter.ExpressionListener;
63
import com.iver.cit.gvsig.gui.filter.FilterException;
64
import com.iver.cit.gvsig.gui.panels.fieldsTree.FieldsTreeTableModel;
65
import com.iver.cit.gvsig.project.ProjectFactory;
66
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
67
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
68
import com.iver.cit.gvsig.project.documents.table.gui.Table;
69
import com.iver.cit.gvsig.project.documents.view.gui.View;
70
import com.iver.cit.gvsig.sqlQueryValidation.SQLQueryValidation;
71
import com.iver.utiles.DefaultCharSet;
72
import com.iver.utiles.StringUtilities;
73
import com.iver.utiles.exceptionHandling.ExceptionHandlingSupport;
74
import com.iver.utiles.exceptionHandling.ExceptionListener;
75
import com.iver.utiles.stringNumberUtilities.StringNumberUtilities;
76

    
77

    
78
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
79
 *
80
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
81
 *
82
 * This program is free software; you can redistribute it and/or
83
 * modify it under the terms of the GNU General Public License
84
 * as published by the Free Software Foundation; either version 2
85
 * of the License, or (at your option) any later version.
86
 *
87
 * This program is distributed in the hope that it will be useful,
88
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
89
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
90
 * GNU General Public License for more details.
91
 *
92
 * You should have received a copy of the GNU General Public License
93
 * along with this program; if not, write to the Free Software
94
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
95
 *
96
 * For more information, contact:
97
 *
98
 *  Generalitat Valenciana
99
 *   Conselleria d'Infraestructures i Transport
100
 *   Av. Blasco Ib??ez, 50
101
 *   46010 VALENCIA
102
 *   SPAIN
103
 *
104
 *      +34 963862235
105
 *   gvsig@gva.es
106
 *      www.gvsig.gva.es
107
 *
108
 *    or
109
 *
110
 *   IVER T.I. S.A
111
 *   Salamanca 50
112
 *   46005 Valencia
113
 *   Spain
114
 *
115
 *   +34 963163400
116
 *   dac@iver.es
117
 */
118

    
119
/**
120
 * This will be the tab for add a filter to a WFS query.
121
 * This class gets the graphical interface from FilterQueryJPanel and add logic.
122
 * 
123
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
124
 */
125
public class WFSFilterPanel extends FilterQueryJPanel {
126
        private static Logger logger = Logger.getLogger(Table.class.getName());
127
        private WFSParamsPanel parent = null;
128
        private ArrayList expressionListeners = new ArrayList();
129
        private ExpressionDataSource model = null;
130
        private NumberFormat nf = NumberFormat.getNumberInstance();
131
        private ExceptionHandlingSupport exceptionHandlingSupport = new ExceptionHandlingSupport();
132
        private FieldsTreeTableModel fieldsTreeTableModel;
133
        private boolean panelAsATabForWFSLayersLoad;
134
        private TreePath currentPath;
135
        private String featureName;
136
        private Map allFieldsAndValuesKnownOfCurrentLayer; // This will have all values (not repeated) known of all fields (not repeated)
137
        
138
        ///// GUI METHODS ////
139
        
140
        /**
141
         * This method initializes
142
         * 
143
         * @param parent A reference to the parent container component of this component
144
         */
145
        public WFSFilterPanel(WFSParamsPanel parent) {
146
                super();
147
                this.parent = parent;
148
                currentPath = null;
149
                featureName = null;
150
                allFieldsAndValuesKnownOfCurrentLayer = new HashMap(); // Initial capacity = 0
151

    
152
                // At beginning, the JList is disabled (and its set a particular color for user could knew it)
153
                super.getValuesJList().setEnabled(false);
154
                getValuesJList().setBackground(new Color(220, 220, 220));
155
        }
156
        
157
        /*
158
         *  (non-Javadoc)
159
         * @see org.gvsig.gui.beans.filterPanel.AbstractFilterQueryJPanel#initialize()
160
         */
161
        protected void initialize() {
162
                super.initialize();
163
                this.resizeHeight(380);
164

    
165
                defaultTreeModel = (DefaultTreeModel)fieldsJTree.getModel();
166
                
167
                this.addNewListeners();
168
                panelAsATabForWFSLayersLoad = true;
169
                getValidateFilterExpressionJCheckBox().setSelected(true);
170
                
171
                getValuesJLabel().setToolTipText(Messages.getText("values_of_the_selected_field_explanation"));
172
                getFieldsJLabel().setToolTipText(Messages.getText("fields_of_the_selected_feature_explanation"));
173
        }
174
        
175
        /**
176
         * Adds some more listener to the components of the panel
177
         */
178
        private void addNewListeners() {
179
                
180
                // Enable "Apply" button when user changes the filter query
181
                txtExpression.getDocument().addDocumentListener(new DocumentListener() {
182
                        /*
183
                         *  (non-Javadoc)
184
                         * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
185
                         */
186
                        public void changedUpdate(DocumentEvent e) {
187
                        }
188

    
189
                        /*
190
                         *  (non-Javadoc)
191
                         * @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent)
192
                         */
193
                        public void insertUpdate(DocumentEvent e) {
194
                                if (!panelAsATabForWFSLayersLoad)
195
                                        parent.isApplicable(true);
196
                        }
197

    
198
                        /*
199
                         *  (non-Javadoc)
200
                         * @see javax.swing.event.DocumentListener#removeUpdate(javax.swing.event.DocumentEvent)
201
                         */
202
                        public void removeUpdate(DocumentEvent e) {
203
                                if (!panelAsATabForWFSLayersLoad)
204
                                        parent.isApplicable(true);
205
                        }
206
                });
207
                
208
                // Listener for "fieldsJTree" 
209
                getFieldsJTree().addMouseListener(new MouseAdapter() {
210
                        /*
211
                         *  (non-Javadoc)
212
                         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
213
                         */
214
                        public void mouseClicked(MouseEvent e) {
215
                                int row = fieldsJTree.getRowForLocation(e.getX(), e.getY());
216
                                TreePath treePath = fieldsJTree.getPathForLocation(e.getX(), e.getY());
217

    
218
                                if (row > -1) {
219
                                        switch (e.getClickCount()) {
220
                                                case 2:                                                        
221
                                                        putSymbolOfSelectedByMouseBranch(treePath);
222
                                                        break;
223
                                        }
224
                                }
225
                        }
226
                });
227
                
228
                // Listener for "valuesJList"
229
                getValuesJList().addMouseListener(new MouseAdapter() {
230
                        /*
231
                         *  (non-Javadoc)
232
                         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
233
                         */
234
                        public void mouseClicked(MouseEvent e) {
235
                                int index = getValuesJList().getSelectedIndex();
236
                                
237
                                // Avoids exception when no value is in the list
238
                                if (index == -1)
239
                                        return;
240
                                        
241
                                if (e.getClickCount() == 2){
242
                                        String valor = ((JLabel) valuesListModel.getElementAt(index)).getText();
243
                                        
244
                                        // If value is an string -> set it between apostrophes
245
                                        if (getNodeOfCurrentPath().getEntityType().getName().compareTo("xs:string") == 0) {
246
                                                putSymbol("'" + valor + "'");
247
                                        }
248
                                        else {
249
                                                putSymbol(valor);
250
                                        }
251
                                }
252
                        }
253
                });
254
                
255
                // Listener for a branch of the tree selection
256
                getFieldsJTree().addTreeSelectionListener(new TreeSelectionListener() {
257
                        /*
258
                         *  (non-Javadoc)
259
                         * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
260
                         */
261
                        public void valueChanged(TreeSelectionEvent e) {
262
                                if (!panelAsATabForWFSLayersLoad) {                                        
263
                                        DataReturnedOfDataLoadingFromActiveView data = DataLoadingFromActiveView.getDefaultExpressionDataSource();
264
                
265
                                        if ((data != null) && (data.getData() != null)) {
266
//                                                setModel(data.getData());
267
                                                currentPath = e.getPath();
268
                                                fillValuesByPath(currentPath);
269
                                        }
270
                                }
271
                        }                        
272
                });
273
                
274
                // Listener: when a user writes something on the textarea -> set it's foreground color to black
275
                getTxtExpression().getDocument().addDocumentListener(new DocumentListener() {
276
                        /*
277
                         *  (non-Javadoc)
278
                         * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
279
                         */
280
                        public void changedUpdate(DocumentEvent e) {
281
                        }
282

    
283
                        /*
284
                         *  (non-Javadoc)
285
                         * @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent)
286
                         */
287
                        public void insertUpdate(DocumentEvent e) {
288
                                getTxtExpression().setForeground(Color.BLACK);
289
                        }
290

    
291
                        /*
292
                         *  (non-Javadoc)
293
                         * @see javax.swing.event.DocumentListener#removeUpdate(javax.swing.event.DocumentEvent)
294
                         */
295
                        public void removeUpdate(DocumentEvent e) {                                
296
                                getTxtExpression().setForeground(Color.BLACK);
297
                        }                        
298
                });
299
                
300
//                // Listener: if user wants or not that the filter expression would be validated
301
//                getValidateFilterExpressionJCheckBox().addItemListener(new ItemListener() {
302
//                        /*
303
//                         * (non-Javadoc)
304
//                         * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
305
//                         */
306
//                        public void itemStateChanged(ItemEvent e) {
307
//                                if (e.getStateChange() == ItemEvent.SELECTED)
308
//                                        System.out.println("Seleccionada");
309
//                                else {
310
//                                        if (e.getStateChange() == ItemEvent.DESELECTED)
311
//                                                System.out.println("Deseleccionada");
312
//                                }
313
//                        }
314
//                });
315
        }
316
        
317
        /**
318
         * Gets the element that the 'currentPath' field aims
319
         * 
320
         * @return An XMLElement
321
         */
322
        private XMLElement getNodeOfCurrentPath() {
323
                
324
                if (currentPath != null) {
325
                        Object node = currentPath.getLastPathComponent();
326
                        
327
                        if ((node != null) && (node instanceof XMLElement)) {
328
                                return (XMLElement) node;
329
                        }
330
                }
331
                
332
                return null;
333
        }
334
        
335
        /**
336
         * Puts the symbol of selected brach
337
         * 
338
         * @param mouseEvent A MouseEvent with information  of the selected branch
339
         */
340
        public void putSymbolOfSelectedByMouseBranch(TreePath treePath) {
341
                // Sets the node selected
342
                if (treePath != null) {
343
                        putSymbol("\"" + this.getPathOfLeafWithoutRoot(treePath.getLastPathComponent()) + "\"");
344
                }
345
        }
346
        
347
        /**
348
         * This method returns the path of a node of a tree
349
         * Each node is separated from its parent with the symbol "/"
350
         * 
351
         * @param node A node of a 
352
         * @return An string with the path
353
         */
354
        private String getPathOfLeaf(Object node) {
355
                String path = "";
356
                
357
                if ((node != null) && (node instanceof XMLElement)) {
358
                        XMLElement element = (XMLElement) node;
359
                        XMLElement parent = element.getParentElement();
360
                        path = element.getName();
361
                
362
                        while (parent != null){
363
                                path = parent.getName() + "/" + path;
364
                                parent = parent.getParentElement();
365
                        }
366
                }
367
                        
368
                return path;
369
        }
370
        
371
        /**
372
         * This method returns the path without the root, of a node of a tree
373
         * Each node is separated from its parent with the symbol "/"
374
         * 
375
         * @param node A node of a 
376
         * @return An string with the path
377
         */
378
        private String getPathOfLeafWithoutRoot(Object node) {
379
                String path = "";
380
                
381
                if ((node != null) && (node instanceof XMLElement)) {
382
                        XMLElement element = (XMLElement) node;
383
                        XMLElement parent = element.getParentElement();
384
                        path = element.getName();
385
                
386
                        while (parent.getParentElement() != null){
387
                                path = parent.getName() + "/" + path;
388
                                parent = parent.getParentElement();
389
                        }
390
                }
391
                        
392
                return path;
393
        }
394
        
395
        /**
396
         * Gets the value of the inner attribute: 'panelAsATabForWFSLayersLoad'
397
         * 
398
         * @return A boolean value
399
         */
400
        public boolean getWFSFilterPanelIsAsTabForWFSLayersLoad() {
401
                return this.panelAsATabForWFSLayersLoad;
402
        }
403
        
404
        /**
405
         * Sets the value of the inner attribute: 'panelAsATabForWFSLayersLoad'
406
         * 
407
         * @param b A boolean value
408
         */
409
        public void setWFSFilterPanelIsAsTabForWFSLayersLoad (boolean b) {
410
                this.panelAsATabForWFSLayersLoad = b;
411
                
412
                if (this.panelAsATabForWFSLayersLoad == true) {
413
                        // At beginning, the JList is disabled (and its set a particular color for user could knew it)
414
                        super.getValuesJList().setEnabled(false);
415
                        super.getValuesJList().setBackground(new Color(220, 220, 220)); // a grey color
416
                        
417
                        this.allFieldsAndValuesKnownOfCurrentLayer.clear();
418
                        getValidateFilterExpressionJCheckBox().setSelected(true);
419
                }
420
                else {
421
                        // Unselect the selected path in the tree (if there was any selected)
422
                        if (this.currentPath != null) {
423
                                this.currentPath = null;
424
                                this.getFieldsJTree().removeSelectionPath(this.getFieldsJTree().getSelectionPath());
425
                        }
426
                        
427
                        // Resets data loaded
428
                        super.getValuesJList().setEnabled(true);
429
                        super.getValuesJList().setBackground(Color.WHITE);
430
                        
431
                        this.getFieldsJTree().removeAll();
432
                        this.getValuesJList().removeAll();
433
                        
434
                        // Updates data associated to view with the new layer data
435
                        this.setNewDataToTable();
436
                        
437
                        // If theres is any table associated to the current view -> update that table/s
438
//                        this.updateTablesThatHasNewData();
439
                        
440
                        // Reads that new data
441
                        DataReturnedOfDataLoadingFromActiveView data = DataLoadingFromActiveView.getDefaultExpressionDataSource();
442
                        
443
                        if ((data != null) && (data.getData() != null)) {
444
                                setModel(data.getData());
445
//                                currentPath = e.getPath();
446
//                                fillValuesByPath(currentPath);
447
                        }
448
                        
449
                        // Loads values known of fields
450
                        this.setValuesKnownOfFields();
451
                }
452
        }
453
        
454
        /**
455
         * Refresh all information about fields
456
         * 
457
         * @param feature
458
         */
459
        public void refresh(WFSLayerNode feature) {
460
                featureName = feature.getTitle();
461
                setFields(feature);
462
        }
463
        
464
        ///// END GUI METHODS /////
465

    
466
        ///// METHODS FOR THE FILTER QUERY /////
467
        
468
        /**
469
         * Gets the query that will be send to the server
470
         * @return SQL query (just the where part)
471
         */
472
        public String getQuery(){
473
                String writtenQuery = txtExpression.getText().trim();
474
                
475
                // Validate expression
476
                if (!this.validateExpression(writtenQuery)) {
477
                        getTxtExpression().setForeground(Color.red);
478
                        return null;
479
                }
480
                else
481
                        // Codify expression (only if the validation has been successful)
482
                        return this.codifyExpression(writtenQuery); // Ignores the spaces at beginning and end of the chain of characters
483
        }
484
        
485
        /**
486
         * Gets the filter expression from the user interface
487
         */
488
        public String getFilterExpressionFromInterface() {
489
                return getTxtExpression().getText();
490
        }
491
        
492
        /**
493
         * Writes the filter expression into the user interface
494
         * @param filterExpression An string
495
         */
496
        public void setFilterExpressionIntoInterface(String filterExpression){
497
                getTxtExpression().setText(filterExpression);
498
        }
499
        
500
        /**
501
         * Removes text in the JTextArea that has the filter subconsultation
502
         */
503
        public void removeFilterExpression() {
504
                getTxtExpression().setText("");
505
        }
506
        
507
        /**
508
         * Codifies the expression to ISO ISO-8859_1 and a format that the SQL parser could validate
509
         * 
510
         * @param expression The expression to be codified 
511
         * 
512
         * @return The expression codified
513
         */
514
        private String codifyExpression(String expression) {
515
                String result = new String("");
516
                        
517
                // Encode each string of the query
518
                int index = 0;
519
                int lastIndex = 0;
520
                boolean endInnerLoop;
521
                // Encodes all inner strings to the equivalent codification in ISO-8859_1 with each ' symbol converted to ''
522
                while (index != -1) {
523
                        index = expression.indexOf("'", index);
524
                        
525
                        // Add the parts of the chain of characters that not are string
526
                        if (index == -1) {
527
                                result += expression.substring(lastIndex, expression.length());
528
                        }
529
                        else {
530
                                result += expression.substring(lastIndex, index).replaceAll(" [ ]+", " ");
531
                        }
532
                        
533
                        lastIndex = index;
534
                        endInnerLoop = false;
535
                        
536
                        // Tries to find each first apostrophe of each string of the query
537
                        if ((index > 0) && (expression.charAt(index - 1) == ' ')) {
538
                                index++;
539
                                
540
                                // Ignore all inner apostrophes and try to find the last of the string
541
                                while (!endInnerLoop)  {
542
                                        index = expression.indexOf("'", index);
543
                                        index++;
544
                                        
545
                                        // If we haven't arrived to the finish of the string
546
                                        if (index != expression.length()) {
547
                                                if ((index == -1) || (expression.charAt(index) == ' ')) {
548
                                                        result += translateString(expression.substring(lastIndex, index));
549
                                                        endInnerLoop = true;
550
                                                }
551
                                        }
552
                                        else {
553
                                                result += translateString(expression.substring(lastIndex, index));
554
                                                endInnerLoop = true;
555
                                                index = -1; // Force to finish the external loop
556
                                        }
557
                                }
558
                                lastIndex = index;
559
                        }
560
                }
561
                        
562
                // Field names are transformated in xix variables that will be analyzed
563
                // Date(date) is substituted by the correct date format
564
                try {
565
                        result = translateDates(result);
566
                }
567
                catch(ParseException e) {
568
                        JOptionPane.showMessageDialog(this, PluginServices.getText(null, "error_codifying_dates"), PluginServices.getText(null, "error_coding_filter_query"), JOptionPane.ERROR_MESSAGE);
569
                        return null;
570
                }
571

    
572
                try {
573
                        result = translateNumber(result);
574
                }
575
                catch(ParseException e) {
576
                        JOptionPane.showMessageDialog(this, PluginServices.getText(null, "error_codifying_numbers"), PluginServices.getText(null, "error_coding_filter_query"), JOptionPane.ERROR_MESSAGE);
577
                        return null;
578
                }
579

    
580
                try {
581
                        result = translateWord(result, "true", "1");
582
                }
583
                catch(ParseException e) {
584
                        JOptionPane.showMessageDialog(this, PluginServices.getText(null, "error_codifying_words"), PluginServices.getText(null, "error_coding_filter_query"), JOptionPane.ERROR_MESSAGE);
585
                        return null;
586
                }
587

    
588
                try {
589
                        result = translateWord(result, "false", "0");
590
                }
591
                catch(ParseException e) {
592
                        JOptionPane.showMessageDialog(this, PluginServices.getText(null, "error_codifying_words"), PluginServices.getText(null, "error_coding_filter_query"), JOptionPane.ERROR_MESSAGE);
593
                        return null;
594
                }
595
        
596
                logger.debug(result);                        
597

    
598
                return result;
599
        }
600
        
601
        /**
602
         * Checks the filter expression if it's correct
603
         * 
604
         * @param query The query expression to analyze
605
         * @return True if it's valid or false if not
606
         */
607
        private boolean validateExpression(String query) {
608
                // If it's needed to validate the query
609
                if (getValidateFilterExpressionJCheckBox().isSelected()) {                
610
                        // If it's an empty query -> ok 
611
                        if (query.trim().length() == 0)
612
                                return true;
613
                        
614
                        // Replace all Date(dd-mmm-yyyy) format to ddd-mmm-yyyy (characters will replaced to spaces)
615
                        int index = 0;
616
                        String query_copy = new String(query);
617
                        while ((index = query_copy.indexOf("Date(", index)) != -1) {
618
                                if (index > 0) {
619
                                        if ((query_copy.charAt(index-1) != ' ') && (query_copy.charAt(index-1) != '('))
620
                                                break;
621
                                }
622
                                
623
                                if (((index + 16) < query_copy.length()) && (query_copy.charAt(index + 16) == ')')) { // +17 is the length of Date(dd-mmm-yyyy)
624
                                        if ((index + 17) < query_copy.length()) {
625
                                                query_copy = query_copy.substring(0, index) + "     " + query_copy.substring(index+6, index+16) + " " + query_copy.substring(index+17);
626
                                        }
627
                                        else {
628
                                                query_copy = query_copy.substring(0, index) + "     " + query_copy.substring(index+6, index+16);
629
                                        }
630
                                }
631
                        }
632
                        
633
                        SQLQueryValidation sQLQueryValidation = new SQLQueryValidation(query_copy, true);
634
        
635
                        // Tries to validate the query, and if fails shows a message
636
                        if (!sQLQueryValidation.validateQuery()) {
637
                                JOptionPane.showMessageDialog(null, PluginServices.getText(null, "filter_with_an_incorrect_format") + ": " + PluginServices.getText(null, "finded") + " " + sQLQueryValidation.getTokenThatProducedTheSyntacticError() + " " + PluginServices.getText(null, "in")  + " " + sQLQueryValidation.getErrorPositionAsMessage() + ".", PluginServices.getText(null, "error_validating_filter_query"), JOptionPane.ERROR_MESSAGE);
638
                                return false;
639
                        }
640
                        else {
641
                                // Analyzes tokens in query
642
                                StringTokenizer tokens = new StringTokenizer(query, " ");
643
                                String token, token_aux;
644
                                boolean finish = false;
645
        
646
                                // If there is a field or a value with spaces, (and then it's on differents tokens) -> unify them
647
                                while (tokens.hasMoreTokens()) {
648
                                        token = tokens.nextToken().trim();
649
                                        
650
                                        if (token.charAt(0) == '\'') {
651
                                                if (token.charAt(token.length() -1) != '\'') {
652
                                                        while (!finish) {
653
                                                                if (!tokens.hasMoreTokens()) {
654
                                                                        JOptionPane.showMessageDialog(null, PluginServices.getText(null, "filter_with_an_incorrect_format") + ": " + PluginServices.getText(null, "the_token") + " " + token + " " + PluginServices.getText(null, "has_bad_format"), PluginServices.getText(null, "error_validating_filter_query"), JOptionPane.ERROR_MESSAGE);
655
                                                                        return false;
656
                                                                }
657
                                                                else {
658
                                                                        token_aux = tokens.nextToken().trim();
659
                                                                        token += " " + token_aux;
660
                                                                        
661
                                                                        if (token_aux.charAt(token_aux.length() -1) == '\'')
662
                                                                                finish = true;
663
                                                                }
664
                                                        }
665
                                                        
666
                                                        finish = false;
667
                                                }
668
                                        }
669
                                        
670
                                        if (token.charAt(0) == '\"') {
671
                                                if (token.charAt(token.length() -1) != '\"') {
672
                                                        while (!finish) {
673
                                                                if (!tokens.hasMoreTokens()) {
674
                                                                        JOptionPane.showMessageDialog(null, PluginServices.getText(null, "filter_with_an_incorrect_format") + ": " + PluginServices.getText(null, "the_token") + " " + token + " " + PluginServices.getText(null, "has_bad_format"), PluginServices.getText(null, "error_validating_filter_query"), JOptionPane.ERROR_MESSAGE);
675
                                                                        return false;
676
                                                                }
677
                                                                else {
678
                                                                        token_aux = tokens.nextToken().trim();
679
                                                                        token += " " + token_aux;
680
                                                                        
681
                                                                        if (token_aux.charAt(token_aux.length() -1) == '\"')
682
                                                                                finish = true;
683
                                                                }
684
                                                        }
685
                                                        
686
                                                        finish = false;
687
                                                }
688
                                        }
689
        
690
                                        // Tries to find an invalid token
691
                                        if (token.length() > 0) {
692
                                                // Validates if a supposed field exists
693
                                                if ( (token.length() > 2) && (token.charAt(0) == '\"') && (token.charAt(token.length()-1) == '\"') ) {
694
                                                        if (! this.isAField(token.substring(1, token.length()-1))) {
695
                                                                JOptionPane.showMessageDialog(null, PluginServices.getText(null, "filter_with_an_incorrect_format") + ": " + PluginServices.getText(null, "the_token") + " " + token + " " + PluginServices.getText(null, "isnt_a_field_of_layer"), PluginServices.getText(null, "error_validating_filter_query"), JOptionPane.ERROR_MESSAGE);
696
                                                                return false;
697
                                                        }
698
                                                }
699
                                                else {
700
                                                        // If it's an string -> ignore
701
                                                        if (! ((token.charAt(0) == token.charAt(token.length() - 1)) && (token.charAt(0) == '\''))) {
702
                                                                
703
                                                                // If it's a date -> ignore
704
                                                                int returnValue = validateDate(token);
705
                                                                
706
                                                                if (returnValue == 1) {
707
                                                                        JOptionPane.showMessageDialog(null, PluginServices.getText(null, "filter_with_an_incorrect_format") + ": " + PluginServices.getText(null, "incorrect_format_on_date") + " " + token.substring(5, 16) + " .", PluginServices.getText(null, "error_validating_filter_query"), JOptionPane.ERROR_MESSAGE);
708
                                                                        return false;
709
                                                                }
710
                                                                
711
                                                                if (returnValue == 2) {                                                                
712
                                                                        // Else -> Checks if the current token is a valid number or symbol
713
                                                                        if ((! StringNumberUtilities.isExponentialRealNumber(token)) && (! this.isAnOperatorNameOrSymbol(token, getAllOperatorSymbols()))) {
714
                                                                                JOptionPane.showMessageDialog(null, PluginServices.getText(null, "filter_with_an_incorrect_format") + ": " + PluginServices.getText(null, "not_valid_token") + ": " + token, PluginServices.getText(null, "error_validating_filter_query"), JOptionPane.ERROR_MESSAGE);
715
                                                                                return false;
716
                                                                        }
717
                                                                }
718
                                                        }
719
                                                }
720
                                        }
721
                                }
722
        
723
                                // If has validate all tokens -> query validated
724
                                return true;
725
                        }
726
                }
727
                else
728
                        return true; // No validation done because user selected that option
729
        }
730
        
731
        /**
732
         * Returns true if there is a field with the same name as 'text'
733
         * 
734
         * @param text An string
735
         * @return A boolean value
736
         */
737
        private boolean isAField(String text) {
738
                return this.allFieldsAndValuesKnownOfCurrentLayer.containsKey(text);                
739
        }
740
        
741
        /**
742
         * Validates if a text has a correct date format as Date(dd-mmm-yyyy)  (Ex. Date(03-feb-2004) )
743
         * 
744
         * @param text
745
         * @return 0 -> if has a date format; 1 -> if it's a date that has a but format; 2 -> if it isn't a date
746
         */
747
        private int validateDate(String text) {
748
                // If it's a date -> check if format is correct (Ex.  Date(01-feb-2004) )
749
                if ( ((text.length() == 17) && (text.startsWith("Date(")) && (text.endsWith(")"))) && (text.charAt(7) == '-') && (text.charAt(11) == '-') ) {
750
                        if ( (StringNumberUtilities.isNaturalNumber(text.substring(5, 7))) && (StringNumberUtilities.isNaturalNumber(text.substring(12, 16))) ) {
751
                                try {
752
                                        // If can parse the date -> date with a correct format 
753
                                        DateFormat.getDateInstance().parse(text.substring(5, 16));
754
                                        return 0;
755
                                } catch (ParseException e) {
756
                                        // If can't parse the date -> date with an incorrect format 
757
                                        return 1;
758
                                }
759
                        }
760
                        else {
761
                                return 1;
762
                        }
763
                }
764
                
765
                return 2;
766

    
767
        }
768

    
769
        /**
770
         * Returns true if there is the 'text' is a symbol or a operator name
771
         * 
772
         * @param text An string
773
         * @return A boolean value
774
         */
775
        private boolean isAnOperatorNameOrSymbol(String text, Set operatorNamesAndSymbols) {                
776
                return operatorNamesAndSymbols.contains(text);
777
        }
778
        
779
        /**
780
         * DOCUMENT ME!
781
         *
782
         * @return DOCUMENT ME!
783
         *
784
         * @throws ParseException DOCUMENT ME!
785
         * @deprecated In this moments its a reference of the old Validate Expression method
786
         */
787
        private String oldValidateExpressionMethod() throws ParseException {
788
                String expression = txtExpression.getText();
789
//                HashSet variablesIndexes = new HashSet();
790
//
791
//                StringBuffer traducida = new StringBuffer();
792

    
793
                //Se transforman los nombres de los campos en las variables xix que analizar?n
794
                //Se quitan los Date(fecha) y se mete la fecha correspondiente
795
                expression = translateDates(expression);
796
                expression = translateNumber(expression);
797
                expression = translateWord(expression, "true", "1");
798
                expression = translateWord(expression, "false", "0");
799

    
800
                String replacement;
801
                Pattern patron = Pattern.compile("[^<>!]=");
802
                Matcher m = patron.matcher(expression);
803
                int index = 0;
804

    
805
                while (m.find(index)) {
806
                        index = m.start();
807
                        replacement = expression.charAt(index) + "==";
808
                        m.replaceFirst(replacement);
809
                        index++;
810
                }
811

    
812
                expression = expression.replaceAll("[^<>!]=", "==");
813

    
814
                logger.debug(expression);
815

    
816
                return expression;
817
        }
818
        
819
        ///// END METHODS FOR THE FILTER QUERY /////
820

    
821
        ///// METHODS FOR TRANSLATE DATA IN FILTER SENTENCES /////
822

    
823
        /** 
824
         * DOCUMENT ME!
825
         *
826
         * @param expresion DOCUMENT ME!
827
         * @param word DOCUMENT ME!
828
         * @param translation DOCUMENT ME!
829
         *
830
         * @return DOCUMENT ME!
831
         *
832
         * @throws ParseException DOCUMENT ME!
833
         */
834
        private String translateWord(String expresion, String word,        String translation) throws ParseException {
835
                int booleanIndex = 0;
836
                int endIndex = 0;
837
                StringBuffer res = new StringBuffer();
838

    
839
                while ((booleanIndex = getIndex(expresion, word, booleanIndex)) != -1) {
840
                        res.append(expresion.substring(endIndex, booleanIndex));
841
                        endIndex = booleanIndex + word.length();
842
                        booleanIndex++;
843
                        res.append(translation);
844
                }
845

    
846
                if (endIndex < expresion.length()) {
847
                        res.append(expresion.substring(endIndex));
848
                }
849

    
850
                return res.toString();
851
        }
852

    
853
        /**
854
         * DOCUMENT ME!
855
         *
856
         * @param expresion DOCUMENT ME!
857
         *
858
         * @return DOCUMENT ME!
859
         *
860
         * @throws ParseException DOCUMENT ME!
861
         */
862
        private String translateDates(String expresion) throws ParseException {
863
                //Se obtiene el valor de la fecha
864
                String date = StringUtilities.substringDelimited(expresion, "Date(", ")", 0);
865

    
866
                if (date == null) {
867
                        return expresion;
868
                }
869

    
870
                //Se comprueba que no est? entre comillas 
871
                int startIndex = expresion.indexOf(date);
872

    
873
                while (startIndex != -1) {
874
                        if (!StringUtilities.isBetweenSymbols(expresion, startIndex, "\"")) {
875
                                
876
                                //Se sustituye por el valor ordinal de la fecha
877
                                expresion = expresion.substring(0, startIndex - 5) +
878
                                        expresion.substring(startIndex).replaceFirst(date + "\\)",                                                        
879
                                                new Long((filterButtonsJPanel.getDateFormat().parse(date)).getTime()).toString());
880

    
881
                        } else {
882
                                startIndex += date.length();
883
                        }
884
                        
885
                        if (date == null) {
886
                                return expresion;
887
                        }
888

    
889
                        startIndex = expresion.indexOf(date, startIndex);
890
                }
891

    
892
                return expresion;
893
        }
894

    
895
        /**
896
         * DOCUMENT ME!
897
         *
898
         * @param expresion DOCUMENT ME!
899
         *
900
         * @return DOCUMENT ME!
901
         *
902
         * @throws ParseException DOCUMENT ME!
903
         */
904
        private String translateNumber(String expresion) throws ParseException {
905
                DefaultCharSet ss = new DefaultCharSet();
906
                ss.addInterval('0', '9');
907
                ss.addCharacter(',');
908
                ss.addCharacter('.');
909

    
910
                String number = StringUtilities.substringWithSymbols(expresion, ss, 0);
911

    
912
                if (number == null) {
913
                        return expresion;
914
                }
915

    
916
                int startIndex = expresion.indexOf(number);
917

    
918
                while (startIndex != -1) {
919
                        Number n = nf.parse(number);
920

    
921
                        if (!StringUtilities.isBetweenSymbols(expresion, startIndex, "\"")) {
922
                                
923
                                //Se sustituye por el valor ordinal de la fecha
924
                                expresion = expresion.substring(0, startIndex) +
925
                                        expresion.substring(startIndex).replaceFirst(number,
926
                                                n.toString());
927
                        } else {
928
                                startIndex += n.toString().length();
929
                        }
930

    
931
                        number = StringUtilities.substringWithSymbols(expresion, ss,
932
                                        startIndex);
933

    
934
                        if (number == null) {
935
                                return expresion;
936
                        }
937

    
938
                        startIndex = expresion.indexOf(number, startIndex);
939
                }
940

    
941
                return expresion;
942
        }
943
        
944
        /**
945
         * Encodes an string to ISO 8859_1 with each ' symbol converted to '' 
946
         * 
947
         * @param text An string started and finished with simple apostrophes
948
         * 
949
         * @return An string started and finished with simple apostrophes
950
         */
951
        private String translateString(String text) {
952
                // Encode to the string to ISO 8859_1 (the URL codification)
953
                try {
954
                        
955
                        // Ignore the first and last apostrophes
956
                        if (text.length() > 2) {
957
                                text = text.substring(1, text.length() -1);
958
                                
959
                                // Convert the string to ISO 8859_1 codification
960
                                text = URLEncoder.encode(text, "8859_1");
961
                                
962
                                // Change '  (%27 code) to '' for the SQL parser (bebore sent the query)
963
                                text = text.replaceAll("\\%27", "\\'\\'");
964
                        }
965

    
966
                } catch (UnsupportedEncodingException e1) {
967
                        e1.printStackTrace();
968
                }
969
                
970
                return "'" + text + "'";
971
        }
972
        
973
        ///// END METHODS FOR TRANSLATE DATA IN FILTER SENTENCES /////
974
        
975
        ///// METHODS FOR MANIPULATE 'fields' and 'values' /////
976
        
977
        /**
978
         * DOCUMENT ME!
979
         *
980
         * @param t DOCUMENT ME!
981
         */
982
        public void setModel(ExpressionDataSource t) {
983
                try {
984
                        model = t;
985
            model.start();
986
        } catch (DriverException e1) {
987
            NotificationManager.addError(e1.getMessage(), e1);
988
        }
989

    
990
        try {
991
                int numberOfFields = model.getFieldCount();
992

    
993
                if (numberOfFields > 0) {
994
                        Vector fields = new Vector(0, 1);
995
                        int j = 0;
996
                
997
                                for (int i = 0; i < numberOfFields; i++) {
998
                                         Object field = model.getFieldName(i);
999
                                        
1000
                                        if (field != null) {
1001
                                                fields.add(field);
1002

    
1003
                                                String completeFieldPath = this.getPathOfLeafWithoutRoot(field);
1004
                                                
1005
                                                if (! allFieldsAndValuesKnownOfCurrentLayer.containsKey(completeFieldPath) ) {
1006
                                                        allFieldsAndValuesKnownOfCurrentLayer.put(completeFieldPath, new HashMap());
1007
                                                }
1008
                                                
1009
                                                j++;
1010
                                        }
1011
                                }
1012
                        
1013
                                fieldsTreeTableModel = new FieldsTreeTableModel(fields.toArray());                        
1014
                }
1015
                } catch (FilterException e) {
1016
                        throwException(e);
1017
                }
1018
        }
1019
        
1020
        /**
1021
         * If there is a field selected, show its new values
1022
         */
1023
        public void updateFieldValues() {
1024
                if (currentPath != null) {
1025
                        
1026
                        DataReturnedOfDataLoadingFromActiveView data = DataLoadingFromActiveView.getDefaultExpressionDataSource();
1027
                                                
1028
                        if ((data != null) && (data.getData() != null)) {
1029
                                setModel(data.getData());                                
1030
                                fillValuesByPath(currentPath);
1031
                                
1032
//                                valuesListModel.clear();
1033
                                
1034
                                // Updates all tables that their data is about the changed view
1035
                                this.updateTablesThatHasNewData();                                
1036
                                
1037
                                // Adjust JScrollPanes to the the beginning
1038
                                getFieldsJScrollPane().getHorizontalScrollBar().setValue(-1);
1039
                                getFieldsJScrollPane().getVerticalScrollBar().setValue(-1);
1040

    
1041
                                // Adjust JScrollPanes to the the beginning
1042
                                getValuesJScrollPane().getHorizontalScrollBar().setValue(-1);
1043
                                getValuesJScrollPane().getVerticalScrollBar().setValue(-1);
1044

    
1045
                                // Adjusts valuesJList to the default position
1046
                                getValuesJList().setSelectedIndex(-1); // No item selected
1047
                        }
1048
                }
1049
        }
1050
        
1051
        /**
1052
         * Sets Fields
1053
         *
1054
         * @param feature A Layer node with fields information
1055
         */
1056
        private void setFields(WFSLayerNode feature) {
1057
                Vector fields = feature.getFields();
1058
                                
1059
                this.resetFieldsAndValuesData();
1060
                
1061
                int numberOfFields = fields.size();
1062
                
1063
                if (numberOfFields > 0) {
1064
                        Vector fieldBranches = new Vector(0, 1);
1065
                        
1066
                        for (int i=0; i<fields.size(); i++) {
1067
                                XMLElement field = (XMLElement)fields.get(i);
1068

    
1069
                                IXMLType type = field.getEntityType();
1070
                                
1071
                                if (type != null) {                                        
1072
                                        
1073
                                        switch (type.getType()) {
1074
                                                case IXMLType.GML_GEOMETRY: // Don't add branch / field
1075
                                                        break;
1076
                                                case IXMLType.COMPLEX: case IXMLType.SIMPLE: // Add branch / field
1077
                                                        fieldBranches.add(field);
1078
                                                        
1079
                                                        break;
1080
                                        }
1081
                                }
1082
                        }
1083
                        
1084
                        fieldsTreeTableModel = new FieldsTreeTableModel(fieldBranches.get(0));
1085
                        fieldsJTree.setModel(new FieldsTreeTableModel(fieldBranches.get(0), false));
1086
                        
1087
                        // Stores the name of all leafs (fields) of treeTableModel                        
1088
                        Object root = fieldsTreeTableModel.getRoot();
1089
                        
1090
                        if (root != null) {
1091
                                Vector fieldsNames = fieldsTreeTableModel.getLeafsFromNodeBranch(root);
1092
                                
1093
                                for (int j = 0; j < fieldsNames.size(); j++) {
1094
                                        Object field = fieldsNames.get(j);
1095
                                        
1096
                                        // Avoid errors
1097
                                        if ( (! (field instanceof XMLElement)) || (field == null) )
1098
                                                continue;
1099
                                        
1100
                                        // Don't load a geometry field                                        
1101
                                        if (((XMLElement)field).getEntityType() != null){
1102
                                                if ( ((XMLElement)field).getEntityType().getType() == IXMLType.GML_GEOMETRY ){
1103
                                                        continue;
1104
                                                }
1105
                                        }
1106
                                        
1107
                                        String completeFieldPath = this.getPathOfLeafWithoutRoot(field);
1108
                                        
1109
                                        if (! allFieldsAndValuesKnownOfCurrentLayer.containsKey(completeFieldPath) ) {
1110
                                                allFieldsAndValuesKnownOfCurrentLayer.put(completeFieldPath, new HashMap());
1111
                                        }
1112
                                }
1113
                        }
1114
                }
1115
        }
1116

    
1117
        /**
1118
         * This method load all values known of all fields known
1119
         * (It's used when a new layer is load)
1120
         */
1121
        private void setValuesKnownOfFields() {
1122
                // Desde el modelo se deber?a acceder a los campos y sus valores cargados                
1123
                try {
1124
                        for (int i = 0; i < model.getFieldCount(); i++) {
1125
                                String fieldName = model.getFieldName(i);
1126
                                HashMap fieldValues = (HashMap) allFieldsAndValuesKnownOfCurrentLayer.get(fieldName);
1127
                                
1128
                                if (fieldValues != null) {
1129
                                        for (int j = 0; j < model.getRowCount(); j++) {
1130
                                                Value value = model.getFieldValue(j, i);                                        
1131
                                                
1132
                                                if (value instanceof NullValue)
1133
                                                    continue;
1134
                                                
1135
                                                Object obj = (Object)value;
1136
                                                
1137
                                                if (obj == null)
1138
                                                        continue;
1139
                                                
1140
                                                fieldValues.put(obj.toString(), obj.toString());
1141
                                                
1142
                                        }
1143
                                }
1144
                        }
1145
                }
1146
                catch (Exception e) {
1147
                        e.printStackTrace();
1148
                }
1149
        }
1150
        
1151
        /**
1152
         * Resets the data of fields and their values of the current layer feature, and removes the branches of JTree
1153
         */
1154
        private void resetFieldsAndValuesData() {
1155
                fieldsJTree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode()));
1156
                txtExpression.setText("");
1157
                ((DefaultListModel)valuesJList.getModel()).removeAllElements();
1158
        }
1159
        
1160
        /** 
1161
         * FIlls list with the values of selected field
1162
         * 
1163
         * @param treePath A path in the tree
1164
         */
1165
        private void fillValuesByPath(TreePath treePath) {
1166
                // Duplicates are removed
1167
                TreeSet conjunto = new TreeSet(new Comparator() {
1168
                        public int compare(Object o1, Object o2) {
1169
                                if ((o1 != null) && (o2 != null)) {
1170
                                        Value v2 = (Value) o2;
1171
                                        Value v1 = (Value) o1;
1172
                                        BooleanValue boolVal;
1173
                                        
1174
                                        try {
1175
                                                boolVal = (BooleanValue) (v1.greater(v2));
1176
                                                
1177
                                                if (boolVal.getValue()) {
1178
                                                        return 1;
1179
                                                }
1180
                                                
1181
                                                boolVal = (BooleanValue) (v1.less(v2));
1182
                                                
1183
                                                if (boolVal.getValue()) {
1184
                                                        return -1;
1185
                                                }
1186
                                        } catch (IncompatibleTypesException e) {
1187
                                                throw new RuntimeException(e);
1188
                                        }
1189
                                }
1190
                                
1191
                                return 0;
1192
                        }
1193
                }); // For ordernation
1194
                
1195
                // Remove the previous items
1196
                valuesListModel.clear();
1197
                
1198
                try {
1199
                        //Object root = treePath.getPath()[0];
1200
                        XMLElement element = ((XMLElement)treePath.getLastPathComponent());
1201
                        
1202
                        // Gets the values associated to the selected branch 
1203
                        switch (element.getEntityType().getType()) {
1204
                                case IXMLType.SIMPLE:
1205
                                        
1206
                                        if(element.getParentElement().getParentElement() == null) {                                        
1207
                                                // Find the selected field and try to obtein values related
1208
                                                for (int i = 0; i < model.getFieldCount(); i++) {                                        
1209
                                                        String name = model.getFieldName(i);
1210
                                                        
1211
                                                        // If we find the field (this means that are loaded its values and we can obtein them)
1212
                                                        if (name.equals(element.getName())) {                                                
1213
                                                                for (int j = 0; j < model.getRowCount(); j++) {                                        
1214
                                                                        Value value = model.getFieldValue(j, i);
1215
                                                                        
1216
                                                                        if (value instanceof NullValue)
1217
                                                                            continue;
1218
                                                                        
1219
                                                                        if (!conjunto.contains(value)) {
1220
                                                                                conjunto.add(value);
1221
                                                                        }
1222
                                                                }
1223
                                                                
1224
                                                                break;
1225
                                                        }
1226
                                                }
1227
                                        }else{
1228
                                                //create a vector with the parent names from the leaf until the root
1229
                                                XMLElement parent = element.getParentElement();
1230
                                                Vector parentNames = new Vector();
1231
                                                parentNames.add(element.getName());
1232
                                                while (parent != null){
1233
                                                        parentNames.add(parent.getName());
1234
                                                        parent = parent.getParentElement();                                                        
1235
                                                }
1236
                                                
1237
                                                //The field name (in the gvSIG table) is the second field name
1238
                                                String fieldName = (String)parentNames.get(parentNames.size()-2);
1239
                                                
1240
                                                for (int i = 0; i < model.getFieldCount(); i++) {                                        
1241
                                                        String name = model.getFieldName(i);
1242
                                                        
1243
                                                        // If we find the field (this means that are loaded its values and we can obtein them)
1244
                                                        if (name.equals(fieldName)) {                                                
1245
                                                                for (int j = 0; j < model.getRowCount(); j++) {                                        
1246
                                                                        Value value = model.getFieldValue(j, i);
1247
                                                                                                                        
1248
                                                                        if (value instanceof NullValue)
1249
                                                                            continue;
1250
                                                                        
1251
                                                                        if (value instanceof ComplexValue){
1252
                                                                                for (int k=parentNames.size()-3 ; k>=0 ; k--){
1253
                                                                                        ComplexValue complex = (ComplexValue)value;
1254
                                                                                        Value childValue = (Value)complex.get(parentNames.get(k));
1255
                                                                                        if (k==0){
1256
                                                                                                if (!conjunto.contains(childValue)) {
1257
                                                                                                        conjunto.add(childValue);
1258
                                                                                                }
1259
                                                                                        }else{
1260
                                                                                                value = childValue;
1261
                                                                                        }
1262
                                                                                }
1263
                                                                        }
1264
                                                                }
1265
                                                                
1266
                                                                break;
1267
                                                        }
1268
                                                }
1269
                                        }
1270
                                        break;
1271
                                case IXMLType.COMPLEX:
1272
                                        break;
1273
                                default:
1274
                                        // Do Nothing
1275
                        }
1276
                        
1277
                        // Add the values to the model of the graphic list
1278
                        Iterator it = conjunto.iterator();
1279
                        Object[] objects = currentPath.getPath();
1280

    
1281
                        if (objects.length == 0)
1282
                                return;
1283
                        
1284
                        String selectedField = ((XMLElement)objects[objects.length-1]).getName(); // Gets the selected field
1285
                        
1286
                        if (selectedField != null) { // If there is a selected field
1287
                                Map fieldValues = (HashMap)allFieldsAndValuesKnownOfCurrentLayer.get(selectedField); // Gets valus stored associated to this field
1288
                                JLabel currentValueLabel = null;
1289
                                
1290
                                // If the field doesn't exits -> create a new Map with its values
1291
                                if (fieldValues == null) {
1292
                                        fieldValues = new HashMap();
1293
                                        allFieldsAndValuesKnownOfCurrentLayer.put(selectedField, fieldValues);        
1294
                                
1295
                                        while (it.hasNext()) {
1296
                                                // A label with text with yelow background color for values that are loaded in the layer
1297
                                                currentValueLabel = new JLabelAsCellValueLoaded();
1298
                                                
1299
                                                currentValueLabel.setText(it.next().toString());
1300
                                                
1301
                                                if (currentValueLabel.getText().compareTo("") != 0) {                                                                                
1302
                                                        fieldValues.put(currentValueLabel.getText(), currentValueLabel.getText());
1303
                                                
1304
                                                        // All values loaded in this loop must be at beginning of the list (and in a differenciated color)
1305
                                                        if ( ! valuesListModel.contains(currentValueLabel) )
1306
                                                                valuesListModel.addElement(currentValueLabel);                                                        
1307
                                                }
1308
                                        }
1309
                                }
1310
                                else { // Else -> Adds the new ones, and changes element labels that have changed  (before were loaded and now not, or before weren't loaded but now yes)
1311
                                        
1312
                                        // Changes element labels that have changed  (before were loaded and now not, or before weren't loaded but now yes)
1313
                                        ((DefaultListModelForJLabelAsCell)valuesListModel).setAllElementsToNotLoaded();
1314
                                        
1315
                                        // For each current value associated to de current selected field -> if its loaded -> put it at beginning of the list and change to 'JLabelLoadedValue' 
1316
                                        while (it.hasNext()) {
1317
                                                String text = it.next().toString();
1318
                                                int elementPosition = ((DefaultListModelForJLabelAsCell)valuesListModel).getIndexOfJLabelText(text);
1319
                                                
1320
                                                if (elementPosition == -1) // If it must be added                                                
1321
                                                        valuesListModel.addElement(new JLabelAsCellValueLoaded(text));
1322
                                                else
1323
                                                        ((DefaultListModelForJLabelAsCell)valuesListModel).changeElementThatHasTextToJLabelLoadedValue(text); // Change to 'JLabelLoadedValue'
1324
                                        }
1325
                                }
1326
                        
1327
                                // Load the rest of the values associated to the current selected field
1328
                                if (fieldValues != null) {
1329
                                        // A label with text with yelow background color for values that are loaded in the layer
1330
                                        currentValueLabel = new JLabelAsCellValueNotLoaded();
1331

    
1332
                                        Set values = fieldValues.keySet();
1333

    
1334
                                        it = values.iterator();
1335

    
1336
                                        while (it.hasNext()) {
1337
                                                String name = it.next().toString();
1338
                                                
1339
                                                if ( ! ((DefaultListModelForJLabelAsCell)valuesListModel).containsJLabelText(name) )
1340
                                                        valuesListModel.addElement(new JLabelAsCellValueNotLoaded(name));
1341
                                        }
1342
                                }
1343
                        }
1344
                        
1345
                } catch (Exception e) {
1346
                        throwException(e);
1347
                }
1348
        }
1349
        
1350
        ///// END METHODS FOR MANIPULATE 'fields' and 'values' /////
1351
        
1352
        ///// METHODS FOR PARENT NOTIFICATIONS /////
1353
        
1354
        /**
1355
         * @see WFSParamsPanel#isApplicable(boolean)
1356
         * 
1357
         * This also loads values of fields 
1358
         * 
1359
         * @param b A boolean value
1360
         */
1361
        private void setApplicate(boolean b) {
1362
                parent.isApplicable(b);
1363
        }
1364
        
1365
        ///// END METHODS FOR PARENT NOTIFICATIONS /////
1366
        
1367
        ///// METHOS FOR 'allFieldsAndValuesKnownOfCurrentLayer' /////
1368
        
1369
        /**
1370
         * Sets all fields and values known about the current layer
1371
         * 
1372
         * @param _allFieldsAndValuesKnownOfCurrentLayer A Map object
1373
         */
1374
        public void setAllFieldsAndValuesKnownOfCurrentLayer(Map _allFieldsAndValuesKnownOfCurrentLayer) {
1375
                if (_allFieldsAndValuesKnownOfCurrentLayer == null)
1376
                        this.allFieldsAndValuesKnownOfCurrentLayer = new HashMap();
1377
                else
1378
                        this.allFieldsAndValuesKnownOfCurrentLayer = _allFieldsAndValuesKnownOfCurrentLayer;
1379
        }
1380
        
1381
        /**
1382
         * Gets all fields and values known about the current layer
1383
         * 
1384
         * @return _allFieldsAndValuesKnownOfCurrentLayer A Map object
1385
         */
1386
        public Map getAllFieldsAndValuesKnownOfCurrentLayer() {
1387
                return allFieldsAndValuesKnownOfCurrentLayer;
1388
        }
1389
        
1390
        ///// METHOS FOR 'allFieldsAndValuesKnownOfCurrentLayer' /////
1391
        
1392
        ///// UPDATE TABLES DATA /////
1393
        
1394
        /**
1395
         * Updates all tables that their data is about the changed view
1396
         */
1397
        private void updateTablesThatHasNewData() {
1398
                boolean oneTimeNewDataToTableAdded = false;
1399
                
1400
                IWindow[] activeNoModalWindows = PluginServices.getMDIManager().getAllWindows();
1401
                
1402
                for (int i = 0; i < activeNoModalWindows.length; i++) {
1403
                        IWindow window = activeNoModalWindows[i];
1404
                        if (window instanceof Table) {
1405
                                Table table = (Table) window;
1406
                                
1407
                                int pos1 = featureName.indexOf(':');
1408
                                
1409
                                if ((pos1 >= 0) && (pos1 < featureName.length()))                                                
1410
                                        featureName = featureName.substring(pos1 +1, featureName.length());
1411
                                
1412
//                                        String featureOfTable = ((XMLElement)currentPath.getParentPath().getLastPathComponent()).getName();
1413
                                        String featureOfTable = table.getModel().getName();
1414
                                        int pos2 = featureOfTable.indexOf(':');
1415
                                                                                                
1416
                                        if ((pos2 >= 0) && (pos2 < featureName.length()))
1417
                                                featureOfTable = featureOfTable.substring(pos2 +1, featureOfTable.length());                                                
1418
                                
1419
                                if (featureName.trim().compareTo(featureOfTable.trim()) == 0) {
1420
                                        // Only add the new data associated to the table one time
1421
                                        if (oneTimeNewDataToTableAdded == false) {
1422
                                                setNewDataToTable();
1423
                                                oneTimeNewDataToTableAdded = true;
1424
                                        }
1425
                                
1426
                                        // Refresh the table with the new data
1427
                                        table.refresh();
1428
                                }
1429
                        }
1430
                }                
1431
        }        
1432
        
1433
        /**
1434
         * This method is a modification of the "execute" method from the "ShowTable" class 
1435
         *
1436
         * @see com.iver.cit.gvsig.ShowTable#execute(String)
1437
         */
1438
        private void setNewDataToTable() {
1439
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
1440
                FLayer[] actives = vista.getModel().getMapContext().getLayers().getActives();
1441

    
1442
                try {
1443
                        for (int i = 0; i < actives.length; i++) {
1444
                                if (actives[i] instanceof AlphanumericData) {
1445
                                        AlphanumericData co = (AlphanumericData) actives[i];
1446

    
1447
                                        //SelectableDataSource dataSource;
1448
                                        //dataSource = co.getRecordset();
1449

    
1450
                                        ProjectExtension ext = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
1451

    
1452
                                        ProjectTable projectTable = ext.getProject().getTable(co);
1453
                                        EditableAdapter ea=null;
1454
                                        ReadableVectorial rv=((FLyrVect)actives[i]).getSource();
1455
                                        if (rv instanceof VectorialEditableAdapter){
1456
                                                ea=(EditableAdapter)((FLyrVect)actives[i]).getSource();
1457
                                        }else{
1458
                                                ea=new EditableAdapter();
1459
                                                SelectableDataSource sds=((FLyrVect)actives[i]).getRecordset();
1460
                                                ea.setOriginalDataSource(sds);
1461
                                        }
1462

    
1463
                                        if (projectTable == null) {
1464
                                                projectTable = ProjectFactory.createTable(PluginServices.getText(this, "Tabla_de_Atributos") + ": " + actives[i].getName(),
1465
                                                                ea);
1466
                                                projectTable.setProjectDocumentFactory(new ProjectTableFactory());
1467
                                                projectTable.setAssociatedTable(co);
1468
                                                ext.getProject().addDocument(projectTable);
1469
                                        }
1470
                                        projectTable.setModel(ea);
1471
                                        
1472
                                        // Removed part of code that created another (visual) table
1473
//                                        Table t = new Table();
1474
//                                        t.setModel(projectTable);
1475
//                                        PluginServices.getMDIManager().addWindow(t);
1476
                                }
1477
                        }
1478
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
1479
            NotificationManager.addError(PluginServices.getText(this,"No_se_pudo_obtener_la_tabla_de_la_capa"), e);
1480
        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
1481
                        e.printStackTrace();
1482
                        NotificationManager.addError(PluginServices.getText(this,"No_se_pudo_obtener_la_tabla_de_la_capa"), e);
1483
        }
1484
        }
1485
        
1486
        ///// END UPDATE TABLES DATA /////
1487
        
1488
        ///// OTHER METHODS /////
1489
        
1490
        /**
1491
         * DOCUMENT ME!
1492
         *
1493
         * @param expresion DOCUMENT ME!
1494
         * @param substring DOCUMENT ME!
1495
         * @param startingPos DOCUMENT ME!
1496
         *
1497
         * @return DOCUMENT ME!
1498
         */
1499
        private int getIndex(String expresion, String substring, int startingPos) {
1500
                int index = startingPos;
1501

    
1502
                do {
1503
                        index = expresion.indexOf(substring, index);
1504
                } while ((StringUtilities.isBetweenSymbols(expresion, index, "\"")) &&
1505
                                (index != -1));
1506

    
1507
                return index;
1508
        }
1509
        
1510
        /**
1511
         * DOCUMENT ME!
1512
         *
1513
         * @param arg0
1514
         *
1515
         * @return
1516
         */
1517
        public boolean addExpressionListener(ExpressionListener arg0) {
1518
                return expressionListeners.add(arg0);
1519
        }
1520

    
1521
        /**
1522
         * DOCUMENT ME!
1523
         *
1524
         * @param arg0
1525
         *
1526
         * @return
1527
         */
1528
        public boolean removeExpressionListener(ExpressionListener arg0) {
1529
                return expressionListeners.remove(arg0);
1530
        }
1531
        /**
1532
         * DOCUMENT ME!
1533
         *
1534
         * @param o DOCUMENT ME!
1535
         *
1536
         * @return DOCUMENT ME!
1537
         */
1538
        public boolean removeExceptionListener(ExceptionListener o) {
1539
                return exceptionHandlingSupport.removeExceptionListener(o);
1540
        }
1541

    
1542
        /**
1543
         * DOCUMENT ME!
1544
         *
1545
         * @param t DOCUMENT ME!
1546
         */
1547
        private void throwException(Throwable t) {
1548
                exceptionHandlingSupport.throwException(t);
1549
        }
1550
        
1551
        ///// END OTHER METHODS ///// 
1552
}