Statistics
| Revision:

root / trunk / libraries / libUI / src / org / gvsig / gui / beans / filterPanel / AbstractFilterQueryJPanel.java @ 8074

History | View | Annotate | Download (15.8 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.awt.event.MouseListener;
9
import java.beans.PropertyChangeEvent;
10
import java.beans.PropertyChangeListener;
11
import java.io.Serializable;
12
import java.util.Vector;
13

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

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

    
23
import com.iver.andami.PluginServices;
24

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

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

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

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

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

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

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

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

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

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

    
245
                txtExpression.setCaretPosition(position);
246
        }
247
        
248
        /**
249
         * This method initializes fieldsJTree
250
         *
251
         * @return javax.swing.JTree
252
         */
253
        /*
254
         *  (non-Javadoc)
255
         * @see org.gvsig.gui.beans.filterPanel.AbstractFilterQueryJPanel#getLstCampos()
256
         */
257
        protected javax.swing.JTree getFieldsJTree() {
258
                if (fieldsJTree == null) {
259
                        fieldsJTree = new javax.swing.JTree(new Vector(0,1));
260
                        
261
                        // Remove icons:
262
                        DefaultTreeCellRenderer defaultTreeCellRenderer = new DefaultTreeCellRenderer();
263
                        defaultTreeCellRenderer.setOpenIcon(null);
264
                        defaultTreeCellRenderer.setClosedIcon(null);
265
                        defaultTreeCellRenderer.setLeafIcon(null);
266
                        
267
                        // Root not visible
268
                        fieldsJTree.setRootVisible(false);
269
                        fieldsJTree.setCellRenderer(defaultTreeCellRenderer);
270
                        
271
                        jtreeRoot = new DefaultMutableTreeNode(fieldsJTree.getModel().getRoot());
272
                        
273
                        fieldsJTree.addMouseListener(new MouseAdapter() {
274
                                /*
275
                                 *  (non-Javadoc)
276
                                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
277
                                 */
278
                                public void mouseClicked(MouseEvent e) {
279
                                        int row = fieldsJTree.getRowForLocation(e.getX(), e.getY());
280
                                        
281
                                        if (row > -1) {
282
                                                Vector values = layerFeaturesFields.getValues(row);
283
                                                
284
                                                ((DefaultListModel)valuesJList.getModel()).clear();
285
                                                
286
                                                for (int i = 0; i < values.size(); i++)
287
                                                        ((DefaultListModel)valuesJList.getModel()).addElement(values.get(i).toString());
288
                                        }
289
                                }
290
                        });
291
                }
292

    
293
                return fieldsJTree;
294
        }
295
        
296
        /**
297
         * This method initializes valuesJList
298
         *
299
         * @return javax.swing.JList
300
         */
301
        protected javax.swing.JList getValuesJList() {
302
                if (valuesJList == null) {
303
                        valuesJList = new javax.swing.JList(new DefaultListModel());
304
                        
305
                        valuesJList.addMouseListener(new MouseAdapter() {
306
                                /*
307
                                 *  (non-Javadoc)
308
                                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
309
                                 */
310
                                public void mouseClicked(MouseEvent e) {
311
                                        if (e.getClickCount() == 2) {
312
                                                int row = fieldsJTree.getRowForLocation(e.getX(), e.getY());
313
        
314
                                                if (row > -1) {
315
                                                        putSymbol(((DefaultListModel)valuesJList.getModel()).get(row).toString());
316
                                                }
317
                                        }
318
                                }
319
                        });
320
                }
321

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

    
421
        
422
        /**
423
         * This class cointains the data of features of a layer
424
         * 
425
         * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
426
         */
427
        protected class LayerFeaturesFields {
428
                Vector layerFeaturesFields;
429
                Vector layerFeaturesValues;
430
                
431
                /**
432
                 * @see Vector#Vector()
433
                 */
434
                public LayerFeaturesFields() {
435
                        initialize();
436
                }
437

    
438
                /**
439
                 * Initializes the attributes of this class
440
                 */
441
                private void initialize() {
442
                        layerFeaturesFields = new Vector(0, 1);
443
                        layerFeaturesValues = new Vector(0, 1);
444
                }
445

    
446
                /**
447
                 * Adds a new field
448
                 * 
449
                 * @param _field An object
450
                 */
451
                public void addField(Object _field) {
452
                        layerFeaturesFields.add(_field);
453
                        layerFeaturesValues.add(new Vector(0, 1));
454
                }
455
                
456
                /**
457
                 * Adds a new field
458
                 * 
459
                 * @param _field An object
460
                 * @param _values A Vector
461
                 */
462
                public void addFieldAndValues(Object _field, Vector _values) {
463
                        layerFeaturesFields.add(_field);
464
                        layerFeaturesValues.add(_values);
465
                }
466
                
467
                /**
468
                 * Adds a new value for a field
469
                 * 
470
                 * @param _field An object
471
                 * @param _values An object
472
                 */
473
                public void addValue(Object _field, Object _value) {
474
                        Vector values = this.getValues(_field);
475
                        
476
                        if (values != null) {
477
                                values.add(_value);        
478
                        }
479
                }
480

    
481
                /**
482
                 * Adds a values for a field
483
                 * 
484
                 * @param _field An object
485
                 * @param _values A vector
486
                 */
487
                public void addValues(Object _field, Vector _values) {
488
//                        layerFeaturesValues.add(getFieldIndex(_field), _values);
489
                        int index = getFieldIndex(_field);
490
                        
491
                        layerFeaturesValues.remove(index);
492
                        layerFeaturesValues.add(index, _values);
493
                }
494
                
495
                /**
496
                 * Returns a field
497
                 * 
498
                 * @param fieldPosition A position
499
                 * @return An object
500
                 */
501
                public Object getField(int fieldPosition) {
502
                        if (fieldPosition == -1)
503
                                return null;
504
                        else
505
                                return layerFeaturesFields.get(fieldPosition);
506
                }
507
                
508
                /**
509
                 * Returns the values associated to a field
510
                 * 
511
                 * @param fieldPosition A position
512
                 * @return A Vector
513
                 */
514
                public Vector getValues(int fieldPosition) {
515
                        if (fieldPosition == -1)
516
                                return null;
517
                        else
518
                                return (Vector)layerFeaturesValues.get(fieldPosition);
519
                }
520

    
521
                /**
522
                 * Returns the values associated to a field
523
                 * 
524
                 * @param field An object
525
                 * @return A Vector
526
                 */
527
                public Vector getValues(Object field) {
528
                        return (Vector)layerFeaturesValues.get(this.getFieldIndex(field));
529
                }
530

    
531
                
532
                /**
533
                 * Returns the index of a 'field'
534
                 * 
535
                 * @param _field An object
536
                 * @return A position
537
                 */
538
                public int getFieldIndex(Object _field) {
539
                        return layerFeaturesFields.indexOf(_field);
540
                }
541
                
542
                /**
543
                 * Removes a field
544
                 * 
545
                 * @param fieldPosition A position
546
                 */
547
                public void removeField(int fieldPosition) {
548
                        layerFeaturesFields.remove(fieldPosition);
549
                        layerFeaturesValues.remove(fieldPosition);
550
                }
551
                
552
                /**
553
                 * Removes a field
554
                 * 
555
                 * @param field An object
556
                 */
557
                public void removeField(Object field) {
558
                        layerFeaturesValues.remove(this.getFieldIndex(field));
559
                        layerFeaturesFields.remove(field);
560
                }
561
                
562
                /**
563
                 * @see Vector#clear()
564
                 */
565
                public void clear() {
566
                        layerFeaturesFields.clear();
567
                        layerFeaturesValues.clear();
568
                }
569
        }        
570
}