Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / swing / textBoxWithCalendar / JCalendarDateDialog.java @ 40561

History | View | Annotate | Download (13.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gui.beans.swing.textBoxWithCalendar;
25

    
26
import java.awt.Dimension;
27
import java.awt.Toolkit;
28
import java.awt.event.ComponentAdapter;
29
import java.awt.event.ComponentEvent;
30
import java.io.Serializable;
31
import java.text.SimpleDateFormat;
32
import java.util.Date;
33
import java.util.Locale;
34

    
35
import javax.swing.JDialog;
36

    
37
import org.freixas.jcalendar.DateEvent;
38
import org.freixas.jcalendar.DateListener;
39
import org.freixas.jcalendar.JCalendar;
40
import org.gvsig.gui.beans.Messages;
41

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

    
83
/**
84
 * Creates a Dialog that allows users to select the date they want
85
 * This class is a version of JCalendarDatePanel
86
 * 
87
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
88
 */
89
public class JCalendarDateDialog extends JDialog implements IMethodsForGraphicalCalendarComponents, Serializable {
90
        private static final long serialVersionUID = -4265734997797240482L;
91
        private JCalendar jCalendar = null;
92
        private final int defaultWidth = 350;
93
        private final int defaultHeight = 230;
94
        private Dimension lastDimensionOfJDialog;
95
        private Dimension minDimensionOfJDialog;
96
        private Dimension maxDimensionOfJDialog;
97

    
98
        /**
99
         * Default Constructor
100
         */
101
        public JCalendarDateDialog() {
102
                super();
103
                
104
                this.initialize();
105
        }
106

    
107
        /**
108
         * This is the default constructor with 2 parameters for set the size
109
         */
110
        public JCalendarDateDialog(int width, int height) {
111
                super();
112

    
113
                this.initialize();                        
114
                        
115
                this.lastDimensionOfJDialog.width = width;
116
                this.lastDimensionOfJDialog.height = height;
117
        }
118
        
119
        /**
120
         * This method initializes this 
121
         */
122
        private void initialize() {
123
                this.setDefaultTitle();
124
                                
125
                // Initialize the attribute 'lastDimensionOfJCalendar'
126
                this.lastDimensionOfJDialog = new Dimension(this.defaultWidth, this.defaultHeight);
127
                
128
                // Resize this component to its initial size
129
                this.setSizeResize(defaultWidth, defaultHeight);
130
                
131
                // Adds the jCalendar to this dialog
132
                this.getContentPane().add(this.getJCalendar());
133

    
134
                // By default there isn't maximun neither minimum dimensions
135
                this.maxDimensionOfJDialog = new Dimension(-1, -1);
136
                this.minDimensionOfJDialog = new Dimension(-1, -1);                
137
                
138
                // This allows authomatic revalidation while this component is been resized
139
                Toolkit.getDefaultToolkit().setDynamicLayout(true);
140

    
141
                // Listener for the redimension of this component:
142
                // Dimension must be between the minimum and maximum
143
                this.addComponentListener(new ComponentAdapter() {
144
                        /*
145
                         *  (non-Javadoc)
146
                         * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
147
                         */
148
                        public void componentResized(ComponentEvent e) {
149
                                boolean modified = false;
150
                                int width = getSize().width;
151
                                int height = getSize().height;
152
                                int new_width = 0;
153
                                int new_height = 0;
154
                                
155
                                // If there are a minimum dimension or a maximum dimension, check the current
156
                                //   dimension and resize if it's nessecary
157

    
158
                                // If this component hasn't been initialed yet
159
                                if ((height != 0) && (width != 0)) {
160
                                        if (minDimensionOfJDialog.height != -1) {
161
                                                if (height < minDimensionOfJDialog.height) {
162
                                                        new_width = width;
163
                                                        new_height = minDimensionOfJDialog.height;
164
                                                        modified = true;
165
                                                }
166
                                        }
167
                                        
168
                                        if (minDimensionOfJDialog.width != -1) {
169
                                                if (width < minDimensionOfJDialog.width) {
170
                                                        new_width = minDimensionOfJDialog.width;
171
                                                        new_height = height;
172
                                                        modified = true;
173
                                                }
174
                                        }
175
        
176
                                        if (maxDimensionOfJDialog.height != -1) {
177
                                                if (height > maxDimensionOfJDialog.height) {
178
                                                        new_width = width;
179
                                                        new_height = maxDimensionOfJDialog.height;
180
                                                        modified = true;
181
                                                }
182
                                        }
183
        
184
                                        if (maxDimensionOfJDialog.width != -1) {
185
                                                if (width > maxDimensionOfJDialog.width) {
186
                                                        new_width = maxDimensionOfJDialog.width;
187
                                                        new_height = height;
188
                                                        modified = true;
189
                                                }
190
                                        }
191
                                        
192
                                        if (modified) {
193
                                                setResizable(false);
194
                                                setSize(new_width, new_height);
195
                                                setResizable(true);
196
                                                getJCalendar().revalidate();
197
                                        }
198
                                        
199
                                        lastDimensionOfJDialog = getSize();
200
                                }
201
                        }
202
                });
203
        }
204

    
205
        /**
206
         * Sets the initial size of this panel
207
         * 
208
         * @param width (the new Width for the panel)
209
         * @param height (the new Height for the panel)
210
         */
211
        public void resizeToInitialSize()
212
        {
213
                getContentPane().setSize(this.defaultWidth, this.defaultHeight);
214
                getJCalendar().revalidate();
215
        }
216

    
217
        /**
218
         * Sets the maximum width for this component, according a percentage of the width resolution of the screen
219
         * (If the parameter isn't > 0.0 neither -1.0 neither <= 1.0, this method doesn't apply the changes)
220
         * 
221
         * @param max_width_screen_percentage A float number > 0, or -1 if there is no limit
222
         */
223
        public void setMaximumWidthScreenResolutionPercentage(double max_width_screen_percentage) {
224
                int max_width = (int) Math.ceil(Toolkit.getDefaultToolkit().getScreenSize().width * max_width_screen_percentage);
225
                
226
                if ((max_width > 0) || (max_width == -1)) {
227
                        this.maxDimensionOfJDialog.width = max_width;
228
                }
229
        }
230
        
231
        /**
232
         * Sets the minimum width for this component, according a percentage of the width 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 setMinimumWidthScreenResolutionPercentage(double min_width_screen_percentage) {
238
                int min_width = (int) Math.ceil(Toolkit.getDefaultToolkit().getScreenSize().width * min_width_screen_percentage);
239
                
240
                if ((min_width > 0) || (min_width == -1))
241
                        this.minDimensionOfJDialog.width = min_width;
242
        }
243

    
244
        /**
245
         * Sets the maximum height for this component, according a percentage of the height resolution of the screen
246
         * (If the parameter isn't > 0.0 neither -1.0 neither <= 1.0, this method doesn't apply the changes)
247
         * 
248
         * @param max_width_screen_percentage A float number > 0.0 neither -1.0 neither <= 1.0, or -1 if there is no limit
249
         */
250
        public void setMaximumHeightScreenResolutionPercentage(double max_height_screen_percentage) {
251
                if (((max_height_screen_percentage > 0.0) && (max_height_screen_percentage <= 1.0)) || (max_height_screen_percentage == -1.0)) {
252
                        int max_height = (int) Math.ceil(Toolkit.getDefaultToolkit().getScreenSize().height * max_height_screen_percentage);
253
                        this.maxDimensionOfJDialog.height = max_height;
254
                }
255
        }
256
        
257
        /**
258
         * Sets the minimum height for this component, according a percentage of the height resolution of the screen
259
         * (If the parameter isn't > 0.0 neither -1.0 neither <= 1.0, this method doesn't apply the changes)
260
         * 
261
         * @param min_width_screen_percentage A float number > 0.0 neither -1.0 neither <= 1.0, or -1 if there is no limit
262
         */
263
        public void setMinimumHeightScreenResolutionPercentage(double min_height_screen_percentage) {
264
                if (((min_height_screen_percentage > 0.0) && (min_height_screen_percentage <= 1.0)) || (min_height_screen_percentage == -1.0)) {
265
                        int min_height = (int) Math.ceil(Toolkit.getDefaultToolkit().getScreenSize().height * min_height_screen_percentage);
266
                        this.minDimensionOfJDialog.height = min_height;
267
                }
268
        }
269
        
270
        /**
271
         * Sets the maximum width for this component
272
         * (If the parameter isn't > 0 neither -1, this method doesn't apply the changes)
273
         * 
274
         * @param max_width A natural number > 0, or -1 if there is no limit
275
         */
276
        public void setMaximumWidth(int max_width) {
277
                if ((max_width > 0) || (max_width == -1)) {
278
                        this.maxDimensionOfJDialog.width = max_width;
279
                }
280
        }
281
        
282
        /**
283
         * Sets the minimum width for this component
284
         *  (If the parameter isn't > 0 neither -1, this method doesn't apply the changes)
285
         * 
286
         * @param min_width A natural number > 0, or -1 if there is no limit
287
         */
288
        public void setMinimumWidth(int min_width) {
289
                if ((min_width > 0) || (min_width == -1))
290
                        this.minDimensionOfJDialog.width = min_width;
291
        }
292

    
293
        /**
294
         * Sets the maximum height for this component
295
         * (If the parameter isn't > 0 neither -1, this method doesn't apply the changes)
296
         * 
297
         * @param max_width A natural number > 0, or -1 if there is no limit
298
         */
299
        public void setMaximumHeight(int max_height) {
300
                if ((max_height > 0) || (max_height == -1))
301
                        this.maxDimensionOfJDialog.height = max_height;
302
        }
303
        
304
        /**
305
         * Sets the minimum height for this component
306
         *  (If the parameter isn't > 0 neither -1, this method doesn't apply the changes)
307
         * 
308
         * @param min_width A natural number > 0, or -1 if there is no limit
309
         */
310
        public void setMinimumHeight(int min_height) {
311
                if ((min_height > 0) || (min_height == -1))
312
                        this.minDimensionOfJDialog.height = min_height;
313
        }
314

    
315
        /**
316
         * Sets the size of this panel
317
         * 
318
         * @param width (the new Width for the panel)
319
         * @param height (the new Height for the panel)
320
         */
321
        public void setSizeResize(int width, int height) {
322
                this.setSize(new Dimension(width, height));
323
                this.lastDimensionOfJDialog = this.getSize();
324
                getJCalendar().revalidate();
325
        }
326
        
327
        /**
328
         * Get the height of this panel
329
         */
330
        public int getHeight()
331
        {
332
                return this.lastDimensionOfJDialog.height;
333
        }
334
        
335
        /**
336
         * Get the width of this panel
337
         */
338
        public int getWidth()
339
        {
340
                return this.lastDimensionOfJDialog.width;
341
        }
342
        
343
        /**
344
         * This method initializes jCalendar        
345
         *         
346
         * @return javax.swing.JCalendar        
347
         */
348
        private JCalendar getJCalendar() {
349
                if (jCalendar == null) {
350
                        jCalendar = new JCalendar();
351
                        jCalendar.setToolTipText(Messages.getText("calendarSelectDate"));
352

    
353
                        try
354
                        {
355
                                jCalendar.setToolTipTextToMonthDecrButton(Messages.getText("calendarBackOneMonth"));
356
                                jCalendar.setToolTipTextToMonthIncrButton(Messages.getText("calendarForwardOneMonth"));
357
                                jCalendar.setToolTipTextToYearDecrButton(Messages.getText("calendarBackOneYear"));
358
                                jCalendar.setToolTipTextToYearIncrButton(Messages.getText("calendarForwardOneYear"));
359

    
360
                                // Adds a date listener calendar
361
                                jCalendar.addDateListener(new DateListener(){
362
                                        /*
363
                                         *  (non-Javadoc)
364
                                         * @see org.freixas.jcalendar.DateListener#dateChanged(org.freixas.jcalendar.DateEvent)
365
                                         */
366
                                        public void dateChanged(DateEvent arg0) {
367
                                                setVisible(false);
368
                                        }                                
369
                                });
370
                        }
371
                        catch(Exception e)
372
                        {
373
                                e.printStackTrace();
374
                        }
375
                }
376
                return jCalendar;
377
        }
378

    
379
        /*
380
         *  (non-Javadoc)
381
         * @see org.gvsig.gui.beans.swing.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#getDate()
382
         */        
383
        public Date getDate() {                
384
                return jCalendar.getDate();
385
        }
386

    
387
        /*
388
         *  (non-Javadoc)
389
         * @see org.gvsig.gui.beans.swing.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#setDate(java.util.Date)
390
         */
391
        public void setDate(Date date)
392
        {
393
                jCalendar.setDate(date);
394
        }
395

    
396
//        /**
397
//         * Returns the date formatted
398
//         * 
399
//         * @param Date
400
//         * @return String The formatted date
401
//         */
402
//        private String getFormattedDate(Date d) {
403
//                return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(d);
404
//        }
405
        
406
        /*
407
         *  (non-Javadoc)
408
         * @see org.gvsig.gui.beans.swing.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#getFormattedDate()
409
         */
410
        public String getFormattedDate() {
411
                return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(this.getDate());
412
        }
413

    
414
        /**
415
         * Gets the title of the JDialog with the calendar component. The title is displayed in the JDialog's border.
416
         *
417
         * @return Title
418
         */
419
        public String getTitle() {
420
                return super.getTitle();
421
        }
422
        
423
        /**
424
         * Sets the title of the JDialog with the calendar component. 
425
         * 
426
         * @param String
427
         */
428
        public void setTitle(String title) {
429
                super.setTitle(title);
430
        }
431

    
432
        /**
433
         * Sets the default title of the JDialog with the calendar component.
434
         */
435
        public void setDefaultTitle() {
436
                this.setTitle(Messages.getText("calendarTitle"));
437
        }
438
        
439
        /**
440
         * @see java.awt.Dialog#setModal(boolean)
441
         */
442
        public void setModal(boolean b) {
443
                super.setModal(b);
444
        }
445
}