Statistics
| Revision:

root / trunk / libraries / libUI / src / org / gvsig / gui / beans / textBoxWithCalendar / JCalendarDatePanel.java @ 9033

History | View | Annotate | Download (7.79 KB)

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

    
3
import java.io.Serializable;
4
import java.text.SimpleDateFormat;
5
import java.util.Date;
6
import java.util.Locale;
7

    
8
import javax.swing.JButton;
9
import javax.swing.JDialog;
10
import javax.swing.JPanel;
11
import javax.swing.SwingConstants;
12

    
13
import org.freixas.jcalendar.DateEvent;
14
import org.freixas.jcalendar.DateListener;
15
import org.freixas.jcalendar.JCalendar;
16
import org.gvsig.gui.beans.Messages;
17

    
18
import java.awt.Color;
19
import java.awt.Dimension;
20
import java.awt.GridBagConstraints;
21
import java.awt.GridBagLayout;
22
import java.awt.event.ComponentListener;
23
import java.awt.event.MouseAdapter;
24
import java.awt.event.MouseEvent;
25
import java.awt.event.MouseListener;
26

    
27
/**
28
 * Creates a Panel for include in other panels -> this panel allows users to set the date they want
29
 * The difference from this class to the JCalendarDatePanel is that in this class the user can move
30
 *    the calendar with the mouse
31
 * 
32
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
33
 */
34
public class JCalendarDatePanel extends JPanel implements Serializable{
35
        private static final long serialVersionUID = 6851067300202797515L;
36
        private JButton jButton = null;
37
        private JCalendar jCalendar = null;
38
        private JDialog jDialogCalendar = null;
39
        private int defaultWidth = 120;
40
        private int defaultHeight = 19;
41
        private MouseListener mouseListener;        
42

    
43
        /**
44
         * Default Constructor
45
         */
46
        public JCalendarDatePanel() {
47
        
48
                try
49
                {
50
                        this.setPreferredSize(new Dimension(defaultWidth, defaultHeight));
51
                        initialize();                        
52
                }
53
                catch(Exception e)
54
                {
55
                        e.printStackTrace();
56
                }
57
        }
58

    
59
        /**
60
         * This is the default constructor with 2 parameters for set the size
61
         */
62
        public JCalendarDatePanel(int width, int height) {
63
                        
64
                try
65
                {                        
66
                        defaultWidth = width;
67
                        defaultHeight = height;
68
                        
69
                        this.setPreferredSize(new Dimension(defaultWidth, defaultHeight));
70
                        initialize();                        
71
                }
72
                catch(Exception e)
73
                {
74
                        e.printStackTrace();
75
                }
76
        }
77

    
78
        /**
79
         * This method initializes this 
80
         */
81
        private void initialize() {
82
                // Set properties to the current panel
83
                GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
84
                gridBagConstraints1.gridx = 1;
85
                gridBagConstraints1.gridy = 0;                
86
                
87
                GridBagLayout gridBagLayout = new GridBagLayout();
88
                this.setLayout(gridBagLayout);
89
                
90
                // Add components to this panel:
91
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
92
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
93
                gridBagConstraints.gridy = 0;
94
                gridBagConstraints.weightx = 1.0;
95
                gridBagConstraints.gridx = 0;
96
                
97
                // Create the JCalendar Object
98
                this.getJCalendar();
99
                
100
                // Create the JDialog Object
101
                this.getJDialogCalendar();
102

    
103
                // Add the JButton
104
                this.add(getJButton(), gridBagConstraints1);
105
                
106
                // Defines the mouseLIstener
107
                this.createMouseListener();
108
                
109
                // Adds the mouseListener to the jTextField and the jButton
110
                jButton.addMouseListener(mouseListener);
111
        }
112

    
113
        
114
        /**
115
         * Sets the size of this panel
116
         * 
117
         * @param width (the new Width for the panel)
118
         * @param height (the new Height for the panel)
119
         */
120
        public void setPreferredSizeResize(int width, int height)
121
        {
122
                defaultWidth = width;
123
                defaultHeight = height;
124
                this.setPreferredSize(new Dimension(defaultWidth, defaultHeight));
125
                this.revalidate();
126
        }
127
        
128
        /**
129
         * Get the height of this panel
130
         */
131
        public int getHeight()
132
        {
133
                return this.defaultHeight;
134
        }
135
        
136
        /**
137
         * Get the width of this panel
138
         */
139
        public int getWidth()
140
        {
141
                return this.defaultWidth;
142
        }
143

    
144
        /**
145
         * This method initializes jButton        
146
         *         
147
         * @return javax.swing.JButton        
148
         */
149
        private JButton getJButton() {
150
                if (jButton == null) {
151
                        jButton = new JButton();
152
                        jButton.setPreferredSize(new Dimension(defaultWidth, defaultHeight));
153
                        jButton.setHorizontalTextPosition(SwingConstants.LEFT);
154
                        jButton.setBackground(Color.WHITE);
155
                        jButton.setText(this.getFormattedDate(jCalendar.getDate()));
156
                }
157
                return jButton;
158
        }
159
        
160
        /**
161
         * This method initializes jCalendar        
162
         *         
163
         * @return javax.swing.JCalendar        
164
         */
165
        private JCalendar getJCalendar() {
166
                if (jCalendar == null) {
167
                        jCalendar = new JCalendar();
168
                        jCalendar.setToolTipText(Messages.getText("calendarSelectDate"));
169
                        
170
                        try
171
                        {
172
                                jCalendar.setToolTipTextToMonthDecrButton(Messages.getText("calendarBackOneMonth"));
173
                                jCalendar.setToolTipTextToMonthIncrButton(Messages.getText("calendarForwardOneMonth"));
174
                                jCalendar.setToolTipTextToYearDecrButton(Messages.getText("calendarBackOneYear"));
175
                                jCalendar.setToolTipTextToYearIncrButton(Messages.getText("calendarForwardOneYear"));
176

    
177
                                // Adds a date listener calendar
178
                                jCalendar.addDateListener(new DateListener(){
179
                                        /*
180
                                         *  (non-Javadoc)
181
                                         * @see org.freixas.jcalendar.DateListener#dateChanged(org.freixas.jcalendar.DateEvent)
182
                                         */
183
                                        public void dateChanged(DateEvent arg0) {
184
                                                // TODO Auto-generated method stub
185
                                                jButton.setText(getFormattedDate(arg0.getSelectedDate().getTime()));
186
                                                jDialogCalendar.hide();
187
                                        }                                
188
                                });
189
                        }
190
                        catch(Exception e)
191
                        {
192
                                e.printStackTrace();
193
                        }
194
                }
195
                return jCalendar;
196
        }
197
        
198
        /**
199
         * This method initializes jDialogCalendar        
200
         *         
201
         * @return javax.swing.JDialog        
202
         */
203
        private JDialog getJDialogCalendar() {
204
                if (jDialogCalendar == null) {
205
                        jDialogCalendar = new JDialog();
206
                        jDialogCalendar.setSize(jCalendar.getPreferredSize());
207
                        this.setDefaultTitle();
208
                        
209
                        // Adds the jCalendar to this dialog
210
                        jDialogCalendar.getContentPane().add(jCalendar);
211
                }
212
                return jDialogCalendar;
213
        }
214
        
215
        
216
        /**
217
         * Gets the date of the calendar 
218
         * 
219
         * @return Date
220
         */
221
        public Date getDate() {                
222
                return jCalendar.getDate();
223
        }
224
        
225
        /**
226
         * Sets the date to the calendar
227
         * 
228
         * @param Date
229
         */
230
        public void setDate(Date date)
231
        {
232
                jCalendar.setDate(date);
233
        }
234
        
235
        /**
236
         * Allows select the date
237
         */
238
        public void enableCalendar() {
239
                jButton.setEnabled(true);
240
        }
241
        
242
        /**
243
         * Don't allow select the date
244
         */
245
        public void disableCalendar() {
246
                jButton.setEnabled(false);
247
        }
248

    
249
        /**
250
         * Returns the date formatted
251
         * 
252
         * @param Date
253
         * @return String The formatted date
254
         */
255
        private String getFormattedDate(Date d) {
256
                return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(d);
257
        }
258
        
259
        /**
260
         * Returns the date selected, formatted
261
         * 
262
         * @return String The formatted date
263
         */
264
        public String getFormattedDate() {
265
                return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(this.getDate());
266
        }
267
        
268
        /**
269
         * Gets the title of the JDialog with the calendar component. The title is displayed in the JDialog's border.
270
         * @return Title
271
         */
272
        public String getTitle() {
273
                return this.jDialogCalendar.getTitle();
274
        }
275
                
276
        /**
277
         * Sets the title of the JDialog with the calendar component. 
278
         * 
279
         * @param String
280
         */
281
        public void setTitle(String title) {
282
                this.jDialogCalendar.setTitle(title);
283
        }
284

    
285
        /**
286
         * Sets the default title of the JDialog with the calendar component.
287
         */
288
        public void setDefaultTitle() {
289
                this.jDialogCalendar.setTitle(Messages.getText("calendarTitle"));
290
        }        
291
        
292
        /**
293
         * @see java.awt.Dialog#setModal(boolean)
294
         */
295
        public void setModal(boolean b) {
296
                jDialogCalendar.setModal(b);
297
        }
298
        
299
        /**
300
         * Adds a component listener for the inner JDialog that cointins the calendar interface
301
         * 
302
         * @param componentListener
303
         */
304
        public void addComponentListenerForJDialogCalendar(ComponentListener componentListener) {
305
                jDialogCalendar.addComponentListener(componentListener);
306
        }        
307
        
308
        /**
309
         * This method creates a Mouse Listener object
310
         */
311
        private void createMouseListener() {
312
                
313
                mouseListener = new MouseAdapter() {
314

    
315
                        /*
316
                         *  (non-Javadoc)
317
                         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
318
                         */
319
                        public void mouseClicked(MouseEvent e) {
320
                                // TODO Auto-generated method stub
321
                                
322
                                // Show the JDialog
323
                                if (getJButton().isEnabled())
324
                                {
325
                                        jDialogCalendar.setLocationRelativeTo(jButton);
326
                                        jDialogCalendar.show();
327
                                }                                
328
                        }        
329
                };
330
        }
331
        
332
}  //  @jve:decl-index=0:visual-constraint="10,10"