Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUI / src / org / gvsig / gui / beans / filterPanel / AbstractFilterQueryJPanel.java @ 8237

History | View | Annotate | Download (15.2 KB)

1
package org.gvsig.gui.beans.filterPanel;
2

    
3
import java.awt.Dimension;
4
import java.awt.GridBagConstraints;
5
import java.awt.GridBagLayout;
6
import java.awt.event.MouseAdapter;
7
import java.awt.event.MouseEvent;
8
import java.beans.PropertyChangeEvent;
9
import java.beans.PropertyChangeListener;
10
import java.io.Serializable;
11
import java.util.Vector;
12

    
13
import javax.swing.BorderFactory;
14
import javax.swing.DefaultListModel;
15
import javax.swing.JPanel;
16
import javax.swing.tree.DefaultMutableTreeNode;
17
import javax.swing.tree.DefaultTreeCellRenderer;
18
import javax.swing.tree.DefaultTreeModel;
19

    
20
import org.gvsig.gui.beans.filterPanel.filterButtons.FilterButtonsJPanel;
21

    
22
import com.iver.andami.PluginServices;
23

    
24
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
25
 *
26
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
41
 *
42
 * For more information, contact:
43
 *
44
 *  Generalitat Valenciana
45
 *   Conselleria d'Infraestructures i Transport
46
 *   Av. Blasco Ib??ez, 50
47
 *   46010 VALENCIA
48
 *   SPAIN
49
 *
50
 *      +34 963862235
51
 *   gvsig@gva.es
52
 *      www.gvsig.gva.es
53
 *
54
 *    or
55
 *
56
 *   IVER T.I. S.A
57
 *   Salamanca 50
58
 *   46005 Valencia
59
 *   Spain
60
 *
61
 *   +34 963163400
62
 *   dac@iver.es
63
 */
64

    
65
/**
66
 * This abstract class represents the common components of the FilterQuery panels
67
 * 
68
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
69
 */
70
public abstract class AbstractFilterQueryJPanel extends JPanel implements Serializable {
71
        protected final int defaultHeight = 280;
72
        protected final int defaultWidth = 490; 
73
        
74
        protected final int fieldsJPanelHeight = 135;
75
        protected final int fieldsJPanelWidth = 145;
76
        protected final int valuesJPanelHeight = fieldsJPanelHeight;
77
        protected final int valuesJPanelWidth = fieldsJPanelWidth;
78
        protected final int defaultBottomJPanelWidth = 480;
79
        protected final int defaultBottomJPanelHeight = 120;
80
        protected int filterJScrollPaneHeight;
81
        protected int filterJScrollPanelWidth;
82
        protected final int filterButtonsPanelHeight = FilterButtonsJPanel.default_FilterButtonsJPanellHeight;
83
        protected final int filterButtonsPanelWidth = FilterButtonsJPanel.default_FilterButtonsJPanelWidth;
84
        protected final int defaultTopJPanelWidth = defaultBottomJPanelWidth;
85
        protected final int defaultTopJPanelHeight = 135;
86
        protected int fieldsAndValuesJScrollPaneHeight = 100;
87
        protected int fieldsAndValuesJScrollPaneWidth = fieldsJPanelWidth;
88
        
89
        protected javax.swing.JLabel fieldsJLabel = null;
90
        protected javax.swing.JLabel valuesJLabel = null;
91
        protected javax.swing.JPanel fieldsJPanel = null;
92
        protected javax.swing.JPanel valuesJPanel = null;
93
        protected FilterButtonsJPanel filterButtonsJPanel = null;
94
        protected javax.swing.JScrollPane filterJScrollPane = null;
95
        protected javax.swing.JPanel topJPanel = null;
96
        protected javax.swing.JPanel bottomJPanel = null;
97
        protected javax.swing.JTextArea txtExpression = null;
98
        protected javax.swing.JTree fieldsJTree = null;
99
        protected javax.swing.JList valuesJList = null;
100
        protected javax.swing.JScrollPane fieldsJScrollPane = null;
101
        protected javax.swing.JScrollPane valuesJScrollPane = null;                
102

    
103
        protected String title;
104
        
105
        protected DefaultMutableTreeNode jtreeRoot;
106
        protected DefaultTreeModel defaultTreeModel;
107
        protected LayerFeaturesFields layerFeaturesFields;
108
        
109
        
110
        /**
111
         * This is the default constructor
112
         */
113
        public AbstractFilterQueryJPanel(String _title) {                
114
                super();
115
                title = _title;
116
        }
117
        /**
118
         * This is the default constructor
119
         */
120
        public AbstractFilterQueryJPanel() {                
121
                super();
122
        }
123
        
124
        /**
125
         * This method initializes this
126
         */
127
        protected void initialize() {
128
                this.setPreferredSize(new Dimension(defaultWidth, defaultHeight));
129
                this.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this, "filterOnAWFSLayer")));
130
                this.setLayout(new GridBagLayout());
131
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
132
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
133
                gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
134

    
135
                gridBagConstraints.anchor = GridBagConstraints.NORTH;
136
                this.add(getTopJPanel(), gridBagConstraints);
137

    
138
                gridBagConstraints.anchor = GridBagConstraints.SOUTH;
139
                this.add(getBottomJPanel(), gridBagConstraints);
140
                
141
                layerFeaturesFields = new LayerFeaturesFields();
142
        }
143
        
144
        /**
145
         * This method initializes topJPanel
146
         * 
147
         * @return javax.swing.JPanel
148
         */
149
        protected javax.swing.JPanel getTopJPanel() {
150
                if (topJPanel == null) {
151
                        topJPanel = new JPanel();
152

    
153
                    topJPanel.setPreferredSize(new Dimension(defaultTopJPanelWidth, defaultTopJPanelHeight));
154
                    topJPanel.setLayout(new GridBagLayout());
155
                    
156
                    GridBagConstraints gridBagConstraints = new GridBagConstraints();
157
                    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;            
158
                    
159
                    gridBagConstraints.anchor = GridBagConstraints.WEST;
160
                    topJPanel.add(getFieldsJPanel(), gridBagConstraints);
161
                    
162
                        gridBagConstraints.anchor = GridBagConstraints.CENTER;
163
                        topJPanel.add(getFilterButtonsJPanel(), gridBagConstraints);
164
                        
165
                        gridBagConstraints.anchor = GridBagConstraints.EAST;
166
                        topJPanel.add(getValuesJPanel(), gridBagConstraints);
167
                }
168
                
169
                return topJPanel;
170
        }
171
        
172
        /**
173
         * This method initializes bottomJPanel
174
         * 
175
         * @return javax.swing.JPanel
176
         */
177
        protected abstract JPanel getBottomJPanel();        
178
        
179
        /**
180
         * This method initializes fieldsJLabel
181
         *
182
         * @return javax.swing.JLabel
183
         */
184
        protected javax.swing.JLabel getFieldsJLabel() {
185
                if (fieldsJLabel == null) {
186
                        fieldsJLabel = new javax.swing.JLabel();
187
                        fieldsJLabel.setText(PluginServices.getText(this, "Campos") + ":");
188
                }
189

    
190
                return fieldsJLabel;
191
        }        
192
        
193
        /**
194
         * This method initializes fieldsJPanel
195
         *
196
         * @return javax.swing.JPanel
197
         */
198
        protected abstract JPanel getFieldsJPanel();
199

    
200
        /**
201
         * This method initializes valuesJLabel
202
         *
203
         * @return javax.swing.JLabel
204
         */
205
        protected javax.swing.JLabel getValuesJLabel() {
206
                if (valuesJLabel == null) {
207
                        valuesJLabel = new javax.swing.JLabel();
208
                        valuesJLabel.setText(PluginServices.getText(this, "Valores") + ":");
209
                }
210

    
211
                return valuesJLabel;
212
        }
213
        
214
        /**
215
         * This method initializes valuesJPanel
216
         *
217
         * @return javax.swing.JPanel
218
         */
219
        protected abstract JPanel getValuesJPanel();
220
        
221
        /**
222
         * This method initializes filterJScrollPane
223
         *
224
         * @return javax.swing.JScrollPane
225
         */
226
        protected abstract javax.swing.JScrollPane getFilterJScrollPane();
227
        
228
        /**
229
         * DOCUMENT ME!
230
         *
231
         * @param symbol DOCUMENT ME!
232
         */
233
        protected void putSymbol(String symbol) {
234
                int position = txtExpression.getCaretPosition();
235
                
236
                txtExpression.setText(insert(txtExpression.getText(), position, symbol));
237

    
238
                if (symbol.equals(" () ")) {
239
                        position = position + 2;
240
                } else {
241
                        position = position + symbol.length();
242
                }
243

    
244
                txtExpression.setCaretPosition(position);
245
        }
246
        
247
        /**
248
         * This method initializes fieldsJTree
249
         *
250
         * @return javax.swing.JTree
251
         */
252
        /*
253
         *  (non-Javadoc)
254
         * @see org.gvsig.gui.beans.filterPanel.AbstractFilterQueryJPanel#getLstCampos()
255
         */
256
        protected javax.swing.JTree getFieldsJTree() {
257
                if (fieldsJTree == null) {
258
                        fieldsJTree = new javax.swing.JTree(new Vector(0,1));
259
                        
260
                        // Remove icons:
261
                        DefaultTreeCellRenderer defaultTreeCellRenderer = new DefaultTreeCellRenderer();
262
                        defaultTreeCellRenderer.setOpenIcon(null);
263
                        defaultTreeCellRenderer.setClosedIcon(null);
264
                        defaultTreeCellRenderer.setLeafIcon(null);
265
                        
266
                        // Root not visible
267
                        fieldsJTree.setRootVisible(false);
268
                        fieldsJTree.setCellRenderer(defaultTreeCellRenderer);
269
                        
270
                        jtreeRoot = new DefaultMutableTreeNode(fieldsJTree.getModel().getRoot());
271
                }
272

    
273
                return fieldsJTree;
274
        }
275
        
276
        /**
277
         * This method initializes valuesJList
278
         *
279
         * @return javax.swing.JList
280
         */
281
        protected javax.swing.JList getValuesJList() {
282
                if (valuesJList == null) {
283
                        valuesJList = new javax.swing.JList(new DefaultListModel());
284
                        
285
                        valuesJList.addMouseListener(new MouseAdapter() {
286
                                /*
287
                                 *  (non-Javadoc)
288
                                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
289
                                 */
290
                                public void mouseClicked(MouseEvent e) {
291
                                        if (e.getClickCount() == 2) {
292
                                                int row = fieldsJTree.getRowForLocation(e.getX(), e.getY());
293
        
294
                                                if (row > -1) {
295
                                                        putSymbol(((DefaultListModel)valuesJList.getModel()).get(row).toString());
296
                                                }
297
                                        }
298
                                }
299
                        });
300
                }
301

    
302
                return valuesJList;
303
        }
304
        
305
    /**
306
     * Inserta una string en otra en la posici?n indicada
307
     *
308
     * @param base String donde se inserta
309
     * @param position posici?n de "base" donde se inserta la String
310
     * @param injerto String que se inserta en "base"
311
     *
312
     * @return String con la inserci?n hecha
313
     */
314
    protected static String insert(String base, int position, String injerto) {
315
        return base.substring(0, position) + injerto +
316
        base.substring(position);
317
    }
318
    
319
        
320
        /**
321
         * This method initializes filterButtonsJPanel
322
         *
323
         * @return javax.swing.JPanel
324
         */
325
        protected JPanel getFilterButtonsJPanel() {
326
                if (filterButtonsJPanel == null) {
327
                        filterButtonsJPanel = new FilterButtonsJPanel();
328
                        filterButtonsJPanel.setPreferredSize(new Dimension(filterButtonsPanelWidth, filterButtonsPanelHeight));
329
                        filterButtonsJPanel.addPropertyChangeListener(new PropertyChangeListener() {
330
                                /*
331
                                 *  (non-Javadoc)
332
                                 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
333
                                 */
334
                                public void propertyChange(PropertyChangeEvent arg0) {
335
                                        if (arg0.getPropertyName().equals(FilterButtonsJPanel.BUTTON_CLICKED_ACTION_COMMAND)) {
336
                                    
337
                                                switch(Integer.parseInt(arg0.getNewValue().toString()))        {
338
                                                        case FilterButtonsJPanel.AND:
339
                                                                putSymbol(" and ");
340
                                                                break;
341
                                                        case FilterButtonsJPanel.DATE:
342
                                                                putSymbol( ((FilterButtonsJPanel)arg0.getSource()).getLastSelectedDate() );
343
                                                                break;
344
                                                        case FilterButtonsJPanel.DISTINCT:
345
                                                                putSymbol(" <> ");
346
                                                                break;
347
                                                        case FilterButtonsJPanel.EQUAL:
348
                                                                putSymbol(" = ");
349
                                                                break;
350
                                                        case FilterButtonsJPanel.EQUALGREATER:
351
                                                                putSymbol(" >= ");
352
                                                                break;
353
                                                        case FilterButtonsJPanel.EQUALSMALLER:
354
                                                                putSymbol(" <= ");
355
                                                                break;
356
                                                        case FilterButtonsJPanel.GREATER:
357
                                                                putSymbol(" > ");
358
                                                                break;
359
                                                        case FilterButtonsJPanel.NOT:
360
                                                                putSymbol(" not ");
361
                                                                break;
362
                                                        case FilterButtonsJPanel.OR:
363
                                                                putSymbol(" or ");
364
                                                                break;
365
                                                        case FilterButtonsJPanel.PARENTHESIS:
366
                                                                putSymbol(" () ");
367
                                                                break;
368
                                                        case FilterButtonsJPanel.SMALLER:
369
                                                                putSymbol(" < ");
370
                                                                break;
371
                                                        default: // do anything
372
                                        }
373
                                        }
374
                                }                        
375
                        });
376
                }
377
                
378
                return filterButtonsJPanel;
379
        }
380
        
381
        /**
382
         * Sets new height to the 'topJPanel', (new Height must be bigger than default, else do nothing)
383
         * 
384
         * @param new_Height New height
385
         */
386
        public void resizeHeight(int new_Height) {
387
                int difference = new_Height - defaultHeight;
388
                
389
                if (difference > 0) {
390
                        this.setPreferredSize(new Dimension(this.getPreferredSize().width, this.getPreferredSize().height + difference));
391
                        topJPanel.setPreferredSize(new Dimension(topJPanel.getPreferredSize().width, topJPanel.getPreferredSize().height + difference));
392
                        
393
                        fieldsJPanel.setPreferredSize(new Dimension(fieldsJPanel.getPreferredSize().width, fieldsJPanel.getPreferredSize().height + difference));
394
                        fieldsJScrollPane.setPreferredSize(new Dimension(fieldsJScrollPane.getPreferredSize().width, fieldsJScrollPane.getPreferredSize().height + difference));
395
                
396
                        valuesJPanel.setPreferredSize(new Dimension(valuesJPanel.getPreferredSize().width, valuesJPanel.getPreferredSize().height + difference));
397
                        valuesJScrollPane.setPreferredSize(new Dimension(valuesJScrollPane.getPreferredSize().width, valuesJScrollPane.getPreferredSize().height + difference));
398
                }
399
        }
400

    
401
        
402
        /**
403
         * This class cointains the data of features of a layer
404
         * 
405
         * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
406
         */
407
        protected class LayerFeaturesFields {
408
                Vector layerFeaturesFields;
409
                Vector layerFeaturesValues;
410
                
411
                /**
412
                 * @see Vector#Vector()
413
                 */
414
                public LayerFeaturesFields() {
415
                        initialize();
416
                }
417

    
418
                /**
419
                 * Initializes the attributes of this class
420
                 */
421
                private void initialize() {
422
                        layerFeaturesFields = new Vector(0, 1);
423
                        layerFeaturesValues = new Vector(0, 1);
424
                }
425

    
426
                /**
427
                 * Adds a new field
428
                 * 
429
                 * @param _field An object
430
                 */
431
                public void addField(Object _field) {
432
                        layerFeaturesFields.add(_field);
433
                        layerFeaturesValues.add(new Vector(0, 1));
434
                }
435
                
436
                /**
437
                 * Adds a new field
438
                 * 
439
                 * @param _field An object
440
                 * @param _values A Vector
441
                 */
442
                public void addFieldAndValues(Object _field, Vector _values) {
443
                        layerFeaturesFields.add(_field);
444
                        layerFeaturesValues.add(_values);
445
                }
446
                
447
                /**
448
                 * Adds a new value for a field
449
                 * 
450
                 * @param _field An object
451
                 * @param _values An object
452
                 */
453
                public void addValue(Object _field, Object _value) {
454
                        Vector values = this.getValues(_field);
455
                        
456
                        if (values != null) {
457
                                values.add(_value);        
458
                        }
459
                }
460

    
461
                /**
462
                 * Adds a values for a field
463
                 * 
464
                 * @param _field An object
465
                 * @param _values A vector
466
                 */
467
                public void addValues(Object _field, Vector _values) {
468
//                        layerFeaturesValues.add(getFieldIndex(_field), _values);
469
                        int index = getFieldIndex(_field);
470
                        
471
                        layerFeaturesValues.remove(index);
472
                        layerFeaturesValues.add(index, _values);
473
                }
474
                
475
                /**
476
                 * Returns a field
477
                 * 
478
                 * @param fieldPosition A position
479
                 * @return An object
480
                 */
481
                public Object getField(int fieldPosition) {
482
                        if (fieldPosition == -1)
483
                                return null;
484
                        else
485
                                return layerFeaturesFields.get(fieldPosition);
486
                }
487
                
488
                /**
489
                 * Returns the values associated to a field
490
                 * 
491
                 * @param fieldPosition A position
492
                 * @return A Vector
493
                 */
494
                public Vector getValues(int fieldPosition) {
495
                        if (fieldPosition == -1)
496
                                return null;
497
                        else
498
                                return (Vector)layerFeaturesValues.get(fieldPosition);
499
                }
500

    
501
                /**
502
                 * Returns the values associated to a field
503
                 * 
504
                 * @param field An object
505
                 * @return A Vector
506
                 */
507
                public Vector getValues(Object field) {
508
                        return (Vector)layerFeaturesValues.get(this.getFieldIndex(field));
509
                }
510

    
511
                
512
                /**
513
                 * Returns the index of a 'field'
514
                 * 
515
                 * @param _field An object
516
                 * @return A position
517
                 */
518
                public int getFieldIndex(Object _field) {
519
                        return layerFeaturesFields.indexOf(_field);
520
                }
521
                
522
                /**
523
                 * Removes a field
524
                 * 
525
                 * @param fieldPosition A position
526
                 */
527
                public void removeField(int fieldPosition) {
528
                        layerFeaturesFields.remove(fieldPosition);
529
                        layerFeaturesValues.remove(fieldPosition);
530
                }
531
                
532
                /**
533
                 * Removes a field
534
                 * 
535
                 * @param field An object
536
                 */
537
                public void removeField(Object field) {
538
                        layerFeaturesValues.remove(this.getFieldIndex(field));
539
                        layerFeaturesFields.remove(field);
540
                }
541
                
542
                /**
543
                 * @see Vector#clear()
544
                 */
545
                public void clear() {
546
                        layerFeaturesFields.clear();
547
                        layerFeaturesValues.clear();
548
                }
549
        }        
550
}