Statistics
| Revision:

root / trunk / libraries / libUI / src / org / gvsig / gui / beans / textBoxWithCalendar / JCalendarDateDialog.java @ 9775

History | View | Annotate | Download (12.3 KB)

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

    
3
import java.awt.Dimension;
4
import java.awt.Toolkit;
5
import java.awt.event.ComponentAdapter;
6
import java.awt.event.ComponentEvent;
7
import java.io.Serializable;
8
import java.text.SimpleDateFormat;
9
import java.util.Date;
10
import java.util.Locale;
11

    
12
import javax.swing.JDialog;
13

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

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

    
60
/**
61
 * Creates a Dialog that allows users to select the date they want
62
 * This class is a version of JCalendarDatePanel
63
 * 
64
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
65
 */
66
public class JCalendarDateDialog extends JDialog implements Serializable {
67
        private static final long serialVersionUID = 6075850667173892632L;
68
        private JCalendar jCalendar = null;
69
        private int defaultWidth = 120;
70
        private int defaultHeight = 19;
71
        private Dimension lastDimensionOfJDialog;
72
        private Dimension minDimensionOfJDialog;
73
        private Dimension maxDimensionOfJDialog;
74

    
75
        /**
76
         * Default Constructor
77
         */
78
        public JCalendarDateDialog() {
79
                super();
80
                
81
                this.initialize();
82
        }
83

    
84
        /**
85
         * This is the default constructor with 2 parameters for set the size
86
         */
87
        public JCalendarDateDialog(int width, int height) {
88
                super();
89

    
90
                this.initialize();                        
91
                        
92
                this.lastDimensionOfJDialog.width = width;
93
                this.lastDimensionOfJDialog.height = height;
94
        }
95
        
96
        /**
97
         * This method initializes this 
98
         */
99
        private void initialize() {
100
                this.setDefaultTitle();
101
                                
102
                // Initialize the attribute 'lastDimensionOfJCalendar'
103
                this.lastDimensionOfJDialog = new Dimension(this.defaultWidth, this.defaultHeight);
104
                
105
                // Adds the jCalendar to this dialog
106
                this.getContentPane().add(this.getJCalendar());
107

    
108
                // By default there isn't maximun neither minimum dimensions
109
                this.maxDimensionOfJDialog = new Dimension(-1, -1);
110
                this.minDimensionOfJDialog = new Dimension(-1, -1);                
111
                
112
                // This allows authomatic revalidation while this component is been resized
113
                Toolkit.getDefaultToolkit().setDynamicLayout(true);
114

    
115
                // Listener for the redimension of this component:
116
                // Dimension must be between the minimum and maximum
117
                this.addComponentListener(new ComponentAdapter() {
118
                        /*
119
                         *  (non-Javadoc)
120
                         * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
121
                         */
122
                        public void componentResized(ComponentEvent e) {
123
                                boolean modified = false;
124
                                int width = getSize().width;
125
                                int height = getSize().height;
126
                                int new_width = 0;
127
                                int new_height = 0;
128
                                
129
                                // If there are a minimum dimension or a maximum dimension, check the current
130
                                //   dimension and resize if it's nessecary
131

    
132
                                // If this component hasn't been initialed yet
133
                                if ((height != 0) && (width != 0)) {
134
                                        if (minDimensionOfJDialog.height != -1) {
135
                                                if (height < minDimensionOfJDialog.height) {
136
                                                        new_width = width;
137
                                                        new_height = minDimensionOfJDialog.height;
138
                                                        modified = true;
139
                                                }
140
                                        }
141
                                        
142
                                        if (minDimensionOfJDialog.width != -1) {
143
                                                if (width < minDimensionOfJDialog.width) {
144
                                                        new_width = minDimensionOfJDialog.width;
145
                                                        new_height = height;
146
                                                        modified = true;
147
                                                }
148
                                        }
149
        
150
                                        if (maxDimensionOfJDialog.height != -1) {
151
                                                if (height > maxDimensionOfJDialog.height) {
152
                                                        new_width = width;
153
                                                        new_height = maxDimensionOfJDialog.height;
154
                                                        modified = true;
155
                                                }
156
                                        }
157
        
158
                                        if (maxDimensionOfJDialog.width != -1) {
159
                                                if (width > maxDimensionOfJDialog.width) {
160
                                                        new_width = maxDimensionOfJDialog.width;
161
                                                        new_height = height;
162
                                                        modified = true;
163
                                                }
164
                                        }
165
                                        
166
                                        if (modified) {
167
                                                setResizable(false);
168
                                                setSize(new_width, new_height);
169
                                                setResizable(true);
170
                                                getJCalendar().revalidate();
171
                                        }
172
                                        
173
                                        lastDimensionOfJDialog = getSize();
174
                                }
175
                        }
176
                });
177
        }
178

    
179
        /**
180
         * Sets the initial size of this panel
181
         * 
182
         * @param width (the new Width for the panel)
183
         * @param height (the new Height for the panel)
184
         */
185
        public void setInitialSize()
186
        {
187
                getContentPane().setSize(this.defaultWidth, this.defaultHeight);
188
                getJCalendar().revalidate();
189
        }
190

    
191
        /**
192
         * Sets the maximum width for this component, according a percentage of the width resolution of the screen
193
         * (If the parameter isn't > 0.0 neither -1.0 neither <= 1.0, this method doesn't apply the changes)
194
         * 
195
         * @param max_width_screen_percentage A float number > 0, or -1 if there is no limit
196
         */
197
        public void setMaximumWidthScreenResolutionPercentage(double max_width_screen_percentage) {
198
                int max_width = (int) Math.ceil(Toolkit.getDefaultToolkit().getScreenSize().width * max_width_screen_percentage);
199
                
200
                if ((max_width > 0) || (max_width == -1)) {
201
                        this.maxDimensionOfJDialog.width = max_width;
202
                }
203
        }
204
        
205
        /**
206
         * Sets the minimum width for this component, according a percentage of the width resolution of the screen
207
         * (If the parameter isn't > 0.0 neither -1.0 neither <= 1.0, this method doesn't apply the changes)
208
         * 
209
         * @param min_width_screen_percentage A float number > 0.0 neither -1.0 neither <= 1.0, or -1 if there is no limit
210
         */
211
        public void setMinimumWidthScreenResolutionPercentage(double min_width_screen_percentage) {
212
                int min_width = (int) Math.ceil(Toolkit.getDefaultToolkit().getScreenSize().width * min_width_screen_percentage);
213
                
214
                if ((min_width > 0) || (min_width == -1))
215
                        this.minDimensionOfJDialog.width = min_width;
216
        }
217

    
218
        /**
219
         * Sets the maximum height for this component, according a percentage of the height resolution of the screen
220
         * (If the parameter isn't > 0.0 neither -1.0 neither <= 1.0, this method doesn't apply the changes)
221
         * 
222
         * @param max_width_screen_percentage A float number > 0.0 neither -1.0 neither <= 1.0, or -1 if there is no limit
223
         */
224
        public void setMaximumHeightScreenResolutionPercentage(double max_height_screen_percentage) {
225
                if (((max_height_screen_percentage > 0.0) && (max_height_screen_percentage <= 1.0)) || (max_height_screen_percentage == -1.0)) {
226
                        int max_height = (int) Math.ceil(Toolkit.getDefaultToolkit().getScreenSize().height * max_height_screen_percentage);
227
                        this.maxDimensionOfJDialog.height = max_height;
228
                }
229
        }
230
        
231
        /**
232
         * Sets the minimum height for this component, according a percentage of the height resolution of the screen
233
         * (If the parameter isn't > 0.0 neither -1.0 neither <= 1.0, this method doesn't apply the changes)
234
         * 
235
         * @param min_width_screen_percentage A float number > 0.0 neither -1.0 neither <= 1.0, or -1 if there is no limit
236
         */
237
        public void setMinimumHeightScreenResolutionPercentage(double min_height_screen_percentage) {
238
                if (((min_height_screen_percentage > 0.0) && (min_height_screen_percentage <= 1.0)) || (min_height_screen_percentage == -1.0)) {
239
                        int min_height = (int) Math.ceil(Toolkit.getDefaultToolkit().getScreenSize().height * min_height_screen_percentage);
240
                        this.minDimensionOfJDialog.height = min_height;
241
                }
242
        }
243
        
244
        /**
245
         * Sets the maximum width for this component
246
         * (If the parameter isn't > 0 neither -1, this method doesn't apply the changes)
247
         * 
248
         * @param max_width A natural number > 0, or -1 if there is no limit
249
         */
250
        public void setMaximumWidth(int max_width) {
251
                if ((max_width > 0) || (max_width == -1)) {
252
                        this.maxDimensionOfJDialog.width = max_width;
253
                }
254
        }
255
        
256
        /**
257
         * Sets the minimum width for this component
258
         *  (If the parameter isn't > 0 neither -1, this method doesn't apply the changes)
259
         * 
260
         * @param min_width A natural number > 0, or -1 if there is no limit
261
         */
262
        public void setMinimumWidth(int min_width) {
263
                if ((min_width > 0) || (min_width == -1))
264
                        this.minDimensionOfJDialog.width = min_width;
265
        }
266

    
267
        /**
268
         * Sets the maximum height for this component
269
         * (If the parameter isn't > 0 neither -1, this method doesn't apply the changes)
270
         * 
271
         * @param max_width A natural number > 0, or -1 if there is no limit
272
         */
273
        public void setMaximumHeight(int max_height) {
274
                if ((max_height > 0) || (max_height == -1))
275
                        this.maxDimensionOfJDialog.height = max_height;
276
        }
277
        
278
        /**
279
         * Sets the minimum height for this component
280
         *  (If the parameter isn't > 0 neither -1, this method doesn't apply the changes)
281
         * 
282
         * @param min_width A natural number > 0, or -1 if there is no limit
283
         */
284
        public void setMinimumHeight(int min_height) {
285
                if ((min_height > 0) || (min_height == -1))
286
                        this.minDimensionOfJDialog.height = min_height;
287
        }
288

    
289
        /**
290
         * Sets the size of this panel
291
         * 
292
         * @param width (the new Width for the panel)
293
         * @param height (the new Height for the panel)
294
         */
295
        public void setSizeResize(int width, int height) {
296
                this.setSize(new Dimension(width, height));
297
                this.lastDimensionOfJDialog = this.getSize();
298
                getJCalendar().revalidate();
299
        }
300
        
301
        /**
302
         * Get the height of this panel
303
         */
304
        public int getHeight()
305
        {
306
                return this.lastDimensionOfJDialog.height;
307
        }
308
        
309
        /**
310
         * Get the width of this panel
311
         */
312
        public int getWidth()
313
        {
314
                return this.lastDimensionOfJDialog.width;
315
        }
316
        
317
        /**
318
         * This method initializes jCalendar        
319
         *         
320
         * @return javax.swing.JCalendar        
321
         */
322
        private JCalendar getJCalendar() {
323
                if (jCalendar == null) {
324
                        jCalendar = new JCalendar();
325
                        jCalendar.setToolTipText(Messages.getText("calendarSelectDate"));
326

    
327
                        try
328
                        {
329
                                jCalendar.setToolTipTextToMonthDecrButton(Messages.getText("calendarBackOneMonth"));
330
                                jCalendar.setToolTipTextToMonthIncrButton(Messages.getText("calendarForwardOneMonth"));
331
                                jCalendar.setToolTipTextToYearDecrButton(Messages.getText("calendarBackOneYear"));
332
                                jCalendar.setToolTipTextToYearIncrButton(Messages.getText("calendarForwardOneYear"));
333

    
334
                                // Adds a date listener calendar
335
                                jCalendar.addDateListener(new DateListener(){
336
                                        /*
337
                                         *  (non-Javadoc)
338
                                         * @see org.freixas.jcalendar.DateListener#dateChanged(org.freixas.jcalendar.DateEvent)
339
                                         */
340
                                        public void dateChanged(DateEvent arg0) {
341
                                                hide();
342
                                        }                                
343
                                });
344
                        }
345
                        catch(Exception e)
346
                        {
347
                                e.printStackTrace();
348
                        }
349
                }
350
                return jCalendar;
351
        }
352

    
353
        /**
354
         * Gets the date of the calendar (default or last selected)
355
         * 
356
         * @return Date
357
         */
358
        public Date getDate() {                
359
                return jCalendar.getDate();
360
        }
361

    
362
        /**
363
         * Sets the date to the calendar
364
         * 
365
         * @param Date
366
         */
367
        public void setDate(Date date)
368
        {
369
                jCalendar.setDate(date);
370
        }
371

    
372
        /**
373
         * Returns the date formatted
374
         * 
375
         * @param Date
376
         * @return String The formatted date
377
         */
378
        private String getFormattedDate(Date d) {
379
                return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(d);
380
        }
381
        
382
        /**
383
         * Returns the date selected, formatted
384
         * 
385
         * @return String The formatted date
386
         */
387
        public String getFormattedDate() {
388
                return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(this.getDate());
389
        }
390

    
391
        /**
392
         * Gets the title of the JDialog with the calendar component. The title is displayed in the JDialog's border.
393
         *
394
         * @return Title
395
         */
396
        public String getTitle() {
397
                return super.getTitle();
398
        }
399
        
400
        /**
401
         * Sets the title of the JDialog with the calendar component. 
402
         * 
403
         * @param String
404
         */
405
        public void setTitle(String title) {
406
                super.setTitle(title);
407
        }
408

    
409
        /**
410
         * Sets the default title of the JDialog with the calendar component.
411
         */
412
        public void setDefaultTitle() {
413
                this.setTitle(Messages.getText("calendarTitle"));
414
        }
415
        
416
        /**
417
         * @see java.awt.Dialog#setModal(boolean)
418
         */
419
        public void setModal(boolean b) {
420
                super.setModal(b);
421
        }
422
}