Statistics
| Revision:

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

History | View | Annotate | Download (15.3 KB)

1 7961 ppiqueras
package org.gvsig.gui.beans.filterPanel.filterButtons;
2
3
import java.awt.LayoutManager;
4
import java.awt.event.ActionEvent;
5
import java.awt.event.ActionListener;
6
import java.awt.event.ComponentAdapter;
7
import java.awt.event.ComponentEvent;
8
import java.awt.event.MouseAdapter;
9
import java.awt.event.MouseEvent;
10
import java.beans.PropertyChangeListener;
11
import java.beans.PropertyChangeSupport;
12
import java.io.Serializable;
13
import java.text.DateFormat;
14
import java.util.Date;
15
import java.util.HashMap;
16
17
import javax.swing.JPanel;
18
import org.gvsig.gui.beans.textBoxWithCalendar.JCalendarDateDialog;
19
20 8094 ppiqueras
import com.iver.andami.PluginServices;
21
22 7961 ppiqueras
import java.awt.Dimension;
23
import java.awt.GridBagLayout;
24
import java.awt.GridBagConstraints;
25
26
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
27
 *
28
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
29
 *
30
 * This program is free software; you can redistribute it and/or
31
 * modify it under the terms of the GNU General Public License
32
 * as published by the Free Software Foundation; either version 2
33
 * of the License, or (at your option) any later version.
34
 *
35
 * This program is distributed in the hope that it will be useful,
36
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38
 * GNU General Public License for more details.
39
 *
40
 * You should have received a copy of the GNU General Public License
41
 * along with this program; if not, write to the Free Software
42
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
43
 *
44
 * For more information, contact:
45
 *
46
 *  Generalitat Valenciana
47
 *   Conselleria d'Infraestructures i Transport
48
 *   Av. Blasco Ib??ez, 50
49
 *   46010 VALENCIA
50
 *   SPAIN
51
 *
52
 *      +34 963862235
53
 *   gvsig@gva.es
54
 *      www.gvsig.gva.es
55
 *
56
 *    or
57
 *
58
 *   IVER T.I. S.A
59
 *   Salamanca 50
60
 *   46005 Valencia
61
 *   Spain
62
 *
63
 *   +34 963163400
64
 *   dac@iver.es
65
 */
66
67
/**
68
 * This class is a panel with buttons for filter operations: AND, OR, NOT, >, <, ...
69
 *
70
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
71
 */
72
public class FilterButtonsJPanel extends JPanel implements Serializable {
73 8094 ppiqueras
        private static final long serialVersionUID = -8707991552239900454L;
74
75 7961 ppiqueras
        public static final int default_FilterButtonsJPanelWidth = 190;
76 8034 ppiqueras
        public static final int default_FilterButtonsJPanellHeight = 130;
77 7961 ppiqueras
        private final int buttonsGroupJPanelWidth = 190;
78
        private final int buttonsGroupJPanelHeight = 75;
79
80
        private javax.swing.JButton btnEqual = null;
81
        private javax.swing.JButton btnDistinct = null;
82
        private javax.swing.JButton btnGreater = null;
83
        private javax.swing.JButton btnSmaller = null;
84
        private javax.swing.JButton btnEqualGreater = null;
85
        private javax.swing.JButton btnEqualSmaller = null;
86
        private javax.swing.JButton btnAnd = null;
87
        private javax.swing.JButton btnOr = null;
88
        private javax.swing.JButton btnNot = null;
89
        private javax.swing.JButton btnDate = null;
90
        private javax.swing.JButton btnParenthesis = null;
91
        private javax.swing.JPanel buttonsJPanel = null;
92
        private javax.swing.JPanel referenceToFilterButtonsJPanel = null;
93
94
        // Last selected date
95
        private DateFormat dateFormat = DateFormat.getDateInstance();
96
        private Date lastSelectedDate = null;
97
98
        private JCalendarDateDialog jCalendarDateDialog = null;
99
100
        // Listener for property change support
101
        private PropertyChangeSupport changes = new PropertyChangeSupport(this);
102
103
        // Values of the events fired when has been clicked a button
104
        public static final int DEFAULT = 0;
105
        public static final int EQUAL = 1;
106
        public static final int DISTINCT = 2;
107
        public static final int GREATER = 3;
108
        public static final int SMALLER = 4;
109
        public static final int EQUALGREATER = 5;
110
        public static final int EQUALSMALLER = 6;
111
        public static final int AND = 7;
112
        public static final int OR = 8;
113
        public static final int NOT = 9;
114
        public static final int DATE = 10;
115
        public static final int PARENTHESIS = 11;
116
117
        // Values of the type of event fired
118
        public static final int BUTTON_CLICKED_ACTION_ID = 12;
119
        public static final String BUTTON_CLICKED_ACTION_COMMAND = "Button Clicked";
120
121
        // Hash map for the items
122
        private HashMap map;
123
124
        // Action listener for notify (fire) some events that had happened to this component
125
        private ActionListener actionListener = null;
126
127
        /**
128
         * @see JPanel#JPanel()
129
         */
130
        public FilterButtonsJPanel() {
131
                super();
132
                map = new HashMap();
133
                referenceToFilterButtonsJPanel = this;
134
                initialize();
135
        }
136
137
        /**
138
         * @see JPanel#JPanel(boolean)
139
         */
140
        public FilterButtonsJPanel(boolean isDoubleBuffered) {
141
                super(isDoubleBuffered);
142
                map = new HashMap();
143
                referenceToFilterButtonsJPanel = this;
144
                initialize();
145
        }
146
147
        /**
148
         * @see JPanel#JPanel(java.awt.LayoutManager, boolean)
149
         */
150
        public FilterButtonsJPanel(LayoutManager layout, boolean isDoubleBuffered) {
151
                super(layout, isDoubleBuffered);
152
                map = new HashMap();
153
                referenceToFilterButtonsJPanel = this;
154
                initialize();
155
        }
156
157
        /**
158
         * @see JPanel#JPanel(java.awt.LayoutManager)
159
         */
160
        public FilterButtonsJPanel(LayoutManager layout) {
161
                super(layout);
162
                map = new HashMap();
163
                referenceToFilterButtonsJPanel = this;
164
                initialize();
165
        }
166
167
        /**
168
         * This method initializes this
169
         */
170
        private void initialize() {
171
                this.setPreferredSize(new Dimension(default_FilterButtonsJPanelWidth, default_FilterButtonsJPanellHeight));
172
                this.setLayout(new GridBagLayout());
173
174
                // Vertical center
175
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
176
                gridBagConstraints.fill = GridBagConstraints.BOTH;
177
                gridBagConstraints.anchor = GridBagConstraints.CENTER;
178
179
                this.add(getButtonsJPanel(), gridBagConstraints);
180
        }
181
182
        /**
183
         * This method initializes buttonsJPanel
184
         *
185
         * @return javax.swing.JPanel
186
         */
187
        private javax.swing.JPanel getButtonsJPanel() {
188
                if (buttonsJPanel == null) {
189
                        buttonsJPanel = new JPanel();
190
191
                        buttonsJPanel.setPreferredSize(new Dimension(buttonsGroupJPanelWidth, buttonsGroupJPanelHeight));
192
                        buttonsJPanel.add(getBtnEqual());
193
                        buttonsJPanel.add(getBtnDistinct());
194
                        buttonsJPanel.add(getBtnSmaller());
195
                        buttonsJPanel.add(getBtnGreater());
196
                        buttonsJPanel.add(getBtnEqualSmaller());
197
                        buttonsJPanel.add(getBtnEqualGreater());
198
                        buttonsJPanel.add(getBtnAnd());
199
                        buttonsJPanel.add(getBtnOr());
200
                        buttonsJPanel.add(getBtnNot());
201
                        buttonsJPanel.add(getBtnParenthesis());
202
                        buttonsJPanel.add(getBtnDate());
203
                }
204
205
                return buttonsJPanel;
206
        }
207
208
        /**
209
         * This method initializes btnAnd
210
         *
211
         * @return javax.swing.JButton
212
         */
213
        private javax.swing.JButton getBtnAnd() {
214
                if (btnAnd == null) {
215
                        btnAnd = new javax.swing.JButton();
216
                        btnAnd.setText("And");
217
                        btnAnd.setMargin(new java.awt.Insets(2, 2, 2, 2));
218
                        btnAnd.setPreferredSize(new java.awt.Dimension(40, 20));
219
                        map.put("And", new Integer(FilterButtonsJPanel.AND));
220
221
                        btnAnd.addActionListener(this.getActionListener());
222
                }
223
224
                return btnAnd;
225
        }
226
227
        /**
228
         * This method initializes btnDistinct
229
         *
230
         * @return javax.swing.JButton
231
         */
232
        private javax.swing.JButton getBtnDistinct() {
233
                if (btnDistinct == null) {
234
                        btnDistinct = new javax.swing.JButton();
235
                        btnDistinct.setText("!=");
236
                        btnDistinct.setMargin(new java.awt.Insets(2, 2, 2, 2));
237
                        btnDistinct.setPreferredSize(new java.awt.Dimension(40, 20));
238
                        map.put("!=", new Integer(FilterButtonsJPanel.DISTINCT));
239
240
                        btnDistinct.addActionListener(this.getActionListener());
241
                }
242
243
                return btnDistinct;
244
        }
245
246 8034 ppiqueras
247 7961 ppiqueras
        /**
248
         * This method initializes btnEqua
249
         *
250
         * @return javax.swing.JButton
251
         */
252
        private javax.swing.JButton getBtnEqual() {
253
                if (btnEqual == null) {
254
                        btnEqual = new javax.swing.JButton();
255
                        btnEqual.setText("=");
256
                        btnEqual.setMargin(new java.awt.Insets(2, 2, 2, 2));
257
                        btnEqual.setPreferredSize(new java.awt.Dimension(40, 20));
258
                        map.put("=", new Integer(FilterButtonsJPanel.EQUAL));
259
260
                        btnEqual.addActionListener(this.getActionListener());
261
                }
262
263
                return btnEqual;
264
        }
265
266
        /**
267
         * This method initializes btnGreater
268
         *
269
         * @return javax.swing.JButton
270
         */
271
        private javax.swing.JButton getBtnGreater() {
272
                if (btnGreater == null) {
273
                        btnGreater = new javax.swing.JButton();
274
                        btnGreater.setText(">");
275
                        btnGreater.setMargin(new java.awt.Insets(2, 2, 2, 2));
276
                        btnGreater.setPreferredSize(new java.awt.Dimension(40, 20));
277
                        map.put(">", new Integer(FilterButtonsJPanel.GREATER));
278
279
                        btnGreater.addActionListener(this.getActionListener());
280
                }
281
282
                return btnGreater;
283
        }
284
285
        /**
286
         * This method initializes btnEqualGreater
287
         *
288
         * @return javax.swing.JButton
289
         */
290
        private javax.swing.JButton getBtnEqualGreater() {
291
                if (btnEqualGreater == null) {
292
                        btnEqualGreater = new javax.swing.JButton();
293
                        btnEqualGreater.setText(">=");
294
                        btnEqualGreater.setMargin(new java.awt.Insets(2, 2, 2, 2));
295
                        btnEqualGreater.setPreferredSize(new java.awt.Dimension(40, 20));
296
                        map.put(">=", new Integer(FilterButtonsJPanel.EQUALGREATER));
297
298
                        btnEqualGreater.addActionListener(this.getActionListener());
299
                }
300
301
                return btnEqualGreater;
302
        }
303
304
        /**
305
         * This method initializes btnSmaller
306
         *
307
         * @return javax.swing.JButton
308
         */
309
        private javax.swing.JButton getBtnSmaller() {
310
                if (btnSmaller == null) {
311
                        btnSmaller = new javax.swing.JButton();
312
                        btnSmaller.setText("<");
313
                        btnSmaller.setMargin(new java.awt.Insets(2, 2, 2, 2));
314
                        btnSmaller.setPreferredSize(new java.awt.Dimension(40, 20));
315
                        map.put("<", new Integer(FilterButtonsJPanel.SMALLER));
316
317
                        btnSmaller.addActionListener(this.getActionListener());
318
                }
319
320
                return btnSmaller;
321
        }
322
323
        /**
324
         * This method initializes btnEqualSmaller
325
         *
326
         * @return javax.swing.JButton
327
         */
328
        private javax.swing.JButton getBtnEqualSmaller() {
329
                if (btnEqualSmaller == null) {
330
                        btnEqualSmaller = new javax.swing.JButton();
331
                        btnEqualSmaller.setText("<=");
332
                        btnEqualSmaller.setMargin(new java.awt.Insets(2, 2, 2, 2));
333
                        btnEqualSmaller.setPreferredSize(new java.awt.Dimension(40, 20));
334
                        map.put("<=", new Integer(FilterButtonsJPanel.EQUALSMALLER));
335
336
                        btnEqualSmaller.addActionListener(this.getActionListener());
337
                }
338
339
                return btnEqualSmaller;
340
        }
341
342
        /**
343
         * This method initializes btnDate
344
         *
345
         * @return javax.swing.JButton
346
         */
347
        private javax.swing.JButton getBtnDate() {
348
                if (btnDate == null) {
349
                        btnDate = new javax.swing.JButton();
350
                        btnDate.setText("Date");
351
                        btnDate.setMargin(new java.awt.Insets(2, 2, 2, 2));
352
                        btnDate.setPreferredSize(new java.awt.Dimension(86, 20));
353
                        map.put("Date", new Integer(FilterButtonsJPanel.DATE));
354
355
                        btnDate.addMouseListener(new MouseAdapter() {
356
                                /*
357
                                 *  (non-Javadoc)
358
                                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
359
                                 */
360
                                public void mouseClicked(MouseEvent e) {
361 8094 ppiqueras
                                        getCDD().show();
362 7961 ppiqueras
                                }
363 7997 ppiqueras
                        });
364 7961 ppiqueras
                }
365
366
                return btnDate;
367
        }
368
369
        /**
370
         * This method initializes a JCalendarDateDialog
371
         *
372
         * @return
373
         */
374 8094 ppiqueras
        protected JCalendarDateDialog getCDD() {
375 7961 ppiqueras
                if (jCalendarDateDialog == null) {
376
                        jCalendarDateDialog = new JCalendarDateDialog(350, 230);
377
                        jCalendarDateDialog.setModal(true);
378
                        jCalendarDateDialog.setLocationRelativeTo(btnDate);
379
380
                        // Adds a listener for get the date when the
381
                        jCalendarDateDialog.addComponentListener(new ComponentAdapter() {
382
                                /*
383
                                 *  (non-Javadoc)
384
                                 * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
385
                                 */
386
                                public void componentHidden(ComponentEvent e) {
387
                                        lastSelectedDate = jCalendarDateDialog.getDate();
388
389
                                        actionListener.actionPerformed(new ActionEvent(btnDate, FilterButtonsJPanel.BUTTON_CLICKED_ACTION_ID, FilterButtonsJPanel.BUTTON_CLICKED_ACTION_COMMAND));
390
391
                                }
392
                        });
393
394
                }
395
396
                return jCalendarDateDialog;
397
        }
398
399
400
        /**
401
         * This method initializes btnNot
402
         *
403
         * @return javax.swing.JButton
404
         */
405
        private javax.swing.JButton getBtnNot() {
406
                if (btnNot == null) {
407
                        btnNot = new javax.swing.JButton();
408
                        btnNot.setText("Not");
409
                        btnNot.setMargin(new java.awt.Insets(2, 2, 2, 2));
410
                        btnNot.setPreferredSize(new java.awt.Dimension(40, 20));
411
                        map.put("Not", new Integer(FilterButtonsJPanel.NOT));
412
413
                        btnNot.addMouseListener(new MouseAdapter() {
414
                                /*
415
                                 *  (non-Javadoc)
416
                                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
417
                                 */
418
                                public void mouseClicked(MouseEvent e) {
419
                                        new ActionEvent(this, FilterButtonsJPanel.BUTTON_CLICKED_ACTION_ID, FilterButtonsJPanel.BUTTON_CLICKED_ACTION_COMMAND);
420
                                }
421
                        });
422
423
                        btnNot.addActionListener(this.getActionListener());
424
425
                }
426
427
                return btnNot;
428
        }
429
430
        /**
431
         * This method initializes btnOr
432
         *
433
         * @return javax.swing.JButton
434
         */
435
        private javax.swing.JButton getBtnOr() {
436
                if (btnOr == null) {
437
                        btnOr = new javax.swing.JButton();
438
                        btnOr.setText("Or");
439
                        btnOr.setMargin(new java.awt.Insets(2, 2, 2, 2));
440
                        btnOr.setPreferredSize(new java.awt.Dimension(40, 20));
441
                        map.put("Or", new Integer(FilterButtonsJPanel.OR));
442
443
                        btnOr.addMouseListener(new MouseAdapter() {
444
                                /*
445
                                 *  (non-Javadoc)
446
                                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
447
                                 */
448
                                public void mouseClicked(MouseEvent e) {
449
                                        new ActionEvent(this, FilterButtonsJPanel.BUTTON_CLICKED_ACTION_ID, FilterButtonsJPanel.BUTTON_CLICKED_ACTION_COMMAND);
450
                                }
451
                        });
452
453
                        btnOr.addActionListener(this.getActionListener());
454
                }
455
456
                return btnOr;
457
        }
458
459
        /**
460
         * This method initializes btnParenthesis
461
         *
462
         * @return javax.swing.JButton
463
         */
464
        private javax.swing.JButton getBtnParenthesis() {
465
                if (btnParenthesis == null) {
466
                        btnParenthesis = new javax.swing.JButton();
467
                        btnParenthesis.setText("()");
468
                        btnParenthesis.setMargin(new java.awt.Insets(2, 2, 2, 2));
469
                        btnParenthesis.setPreferredSize(new java.awt.Dimension(40, 20));
470
                        map.put("()", new Integer(FilterButtonsJPanel.PARENTHESIS));
471
472
                        btnParenthesis.addMouseListener(new MouseAdapter() {
473
                                /*
474
                                 *  (non-Javadoc)
475
                                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
476
                                 */
477
                                public void mouseClicked(MouseEvent e) {
478
                                        new ActionEvent(this, FilterButtonsJPanel.BUTTON_CLICKED_ACTION_ID, FilterButtonsJPanel.BUTTON_CLICKED_ACTION_COMMAND);
479
                                }
480
                        });
481
482
                        btnParenthesis.addActionListener(this.getActionListener());                }
483
484
                return btnParenthesis;
485
        }
486
487
        /**
488
         * This method initializes the "actionListener" ActionListener
489
         *
490
         * @return ActionListener
491
         */
492
        private ActionListener getActionListener() {
493
                if (actionListener == null) {
494
                        actionListener = new ActionListener() {
495
                                /*
496
                                 *  (non-Javadoc)
497
                                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
498
                                 */
499
                                public void actionPerformed(ActionEvent event) {
500
                                           // Notifies that has been clicked a button
501
                                       changes.firePropertyChange(FilterButtonsJPanel.BUTTON_CLICKED_ACTION_COMMAND, FilterButtonsJPanel.DEFAULT, ((Integer) map.get( ((javax.swing.JButton)event.getSource()).getText())).intValue());
502
                        }
503
                    };
504
                }
505
                return actionListener;
506
        }
507
508
        /**
509
         * Returns the las selected date, formatted
510
         *
511
         * @return A formatted date
512
         */
513
        public String getLastSelectedDate() {
514
                if (lastSelectedDate == null)
515
                        return "";
516
                else
517
                        return "Date(" + dateFormat.format(lastSelectedDate) + ")";
518
        }
519 8074 ppiqueras
520
        /**
521
         * Returns the 'DateFormat' private attribute
522
         *
523
         * @return A 'DateFormat' reference
524
         */
525
        public DateFormat getDateFormat() {
526
                return dateFormat;
527
        }
528 7961 ppiqueras
529
    /**
530
     * Adds a "Property Change Listener"
531
     */
532
    public void addPropertyChangeListener(PropertyChangeListener l) {
533
            changes.addPropertyChangeListener(l);
534
    }
535
536
    /**
537
     * Removes a "Property Change Listener"
538
     */
539
    public void removePropertyChangeListener(PropertyChangeListener l) {
540
            changes.removePropertyChangeListener(l);
541 7997 ppiqueras
    }
542 7961 ppiqueras
}