Statistics
| Revision:

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

History | View | Annotate | Download (12.6 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 IMethodsForGraphicalCalendarComponents, Serializable {
67
        private static final long serialVersionUID = -4265734997797240482L;
68
        private JCalendar jCalendar = null;
69
        private final int defaultWidth = 350;
70
        private final int defaultHeight = 230;
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
                // Resize this component to its initial size
106
                this.setSizeResize(defaultWidth, defaultHeight);
107
                
108
                // Adds the jCalendar to this dialog
109
                this.getContentPane().add(this.getJCalendar());
110

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

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

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

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

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

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

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

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

    
330
                        try
331
                        {
332
                                jCalendar.setToolTipTextToMonthDecrButton(Messages.getText("calendarBackOneMonth"));
333
                                jCalendar.setToolTipTextToMonthIncrButton(Messages.getText("calendarForwardOneMonth"));
334
                                jCalendar.setToolTipTextToYearDecrButton(Messages.getText("calendarBackOneYear"));
335
                                jCalendar.setToolTipTextToYearIncrButton(Messages.getText("calendarForwardOneYear"));
336

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

    
356
        /*
357
         *  (non-Javadoc)
358
         * @see org.gvsig.gui.beans.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#getDate()
359
         */        
360
        public Date getDate() {                
361
                return jCalendar.getDate();
362
        }
363

    
364
        /*
365
         *  (non-Javadoc)
366
         * @see org.gvsig.gui.beans.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#setDate(java.util.Date)
367
         */
368
        public void setDate(Date date)
369
        {
370
                jCalendar.setDate(date);
371
        }
372

    
373
//        /**
374
//         * Returns the date formatted
375
//         * 
376
//         * @param Date
377
//         * @return String The formatted date
378
//         */
379
//        private String getFormattedDate(Date d) {
380
//                return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(d);
381
//        }
382
        
383
        /*
384
         *  (non-Javadoc)
385
         * @see org.gvsig.gui.beans.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#getFormattedDate()
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
}