Revision 7956

View differences:

trunk/libraries/libUI/src/org/gvsig/gui/beans/textBoxWithCalendar/JCalendarDateDialog.java
1
package org.gvsig.gui.beans.textBoxWithCalendar;
2

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

  
9
import javax.swing.JDialog;
10

  
11
import org.freixas.jcalendar.DateEvent;
12
import org.freixas.jcalendar.DateListener;
13
import org.freixas.jcalendar.JCalendar;
14

  
15
import com.iver.andami.PluginServices;
16

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

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

  
70
	/**
71
	 * Default Constructor
72
	 */
73
	public JCalendarDateDialog() {
74
		super();
75
		try
76
		{
77
			this.setSize(new Dimension(defaultWidth, defaultHeight));
78
			this.initialize();			
79
		}
80
		catch(Exception e)
81
		{
82
			e.printStackTrace();
83
		}
84
	}
85

  
86
	/**
87
	 * This is the default constructor with 2 parameters for set the size
88
	 */
89
	public JCalendarDateDialog(int width, int height) {
90
		super();
91
		try
92
		{			
93
			defaultWidth = width;
94
			defaultHeight = height;
95
			
96
			this.setSize(new Dimension(defaultWidth, defaultHeight));
97
			this.initialize();			
98
		}
99
		catch(Exception e)
100
		{
101
			e.printStackTrace();
102
		}
103
	}
104
	
105
	/**
106
	 * This method initializes this 
107
	 */
108
	private void initialize() {
109
		this.setDefaultTitle();
110
		
111
		// Adds the jCalendar to this dialog
112
		this.getContentPane().add(this.getJCalendar());
113
	}
114

  
115
	/**
116
	 * Sets the size of this panel
117
	 * 
118
	 * @param width (the new Width for the panel)
119
	 * @param height (the new Height for the panel)
120
	 */
121
	public void setSizeResize(int width, int height)
122
	{
123
		defaultWidth = width;
124
		defaultHeight = height;
125
		this.setSize(new Dimension(defaultWidth, defaultHeight));
126
		getJCalendar().revalidate();
127
	}
128
	
129
	/**
130
	 * This method initializes jCalendar	
131
	 * 	
132
	 * @return javax.swing.JCalendar	
133
	 */
134
	private JCalendar getJCalendar() {
135
		if (jCalendar == null) {
136
			jCalendar = new JCalendar();
137
			jCalendar.setToolTipText(PluginServices.getText(this, "calendarSelectDate"));
138
			
139
			try
140
			{
141
				jCalendar.setToolTipTextToMonthDecrButton(PluginServices.getText(null, "calendarBackOneMonth"));
142
				jCalendar.setToolTipTextToMonthIncrButton(PluginServices.getText(null, "calendarForwardOneMonth"));
143
				jCalendar.setToolTipTextToYearDecrButton(PluginServices.getText(null, "calendarBackOneYear"));
144
				jCalendar.setToolTipTextToYearIncrButton(PluginServices.getText(null, "calendarForwardOneYear"));
145

  
146
				// Adds a date listener calendar
147
				jCalendar.addDateListener(new DateListener(){
148
					/*
149
					 *  (non-Javadoc)
150
					 * @see org.freixas.jcalendar.DateListener#dateChanged(org.freixas.jcalendar.DateEvent)
151
					 */
152
					public void dateChanged(DateEvent arg0) {
153
						hide();
154
					}				
155
				});
156
			}
157
			catch(Exception e)
158
			{
159
				e.printStackTrace();
160
			}
161
		}
162
		return jCalendar;
163
	}
164

  
165
	/**
166
	 * Gets the date of the calendar (default or last selected)
167
	 * 
168
	 * @return Date
169
	 */
170
	public Date getDate() {		
171
		return jCalendar.getDate();
172
	}
173

  
174
	/**
175
	 * Sets the date to the calendar
176
	 * 
177
	 * @param Date
178
	 */
179
	public void setDate(Date date)
180
	{
181
		jCalendar.setDate(date);
182
	}
183

  
184
	/**
185
	 * Returns the date formatted
186
	 * 
187
	 * @param Date
188
	 * @return String The formatted date
189
	 */
190
	private String getFormattedDate(Date d) {
191
		return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(d);
192
	}
193
	
194
	/**
195
	 * Returns the date selected, formatted
196
	 * 
197
	 * @return String The formatted date
198
	 */
199
	public String getFormattedDate() {
200
		return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(this.getDate());
201
	}
202

  
203
	/**
204
	 * Gets the title of the JDialog with the calendar component. The title is displayed in the JDialog's border.
205
	 *
206
	 * @return Title
207
	 */
208
	public String getTitle() {
209
		return super.getTitle();
210
	}
211
	
212
	/**
213
	 * Sets the title of the JDialog with the calendar component. 
214
	 * 
215
	 * @param String
216
	 */
217
	public void setTitle(String title) {
218
		super.setTitle(title);
219
	}
220

  
221
	/**
222
	 * Sets the default title of the JDialog with the calendar component.
223
	 */
224
	public void setDefaultTitle() {
225
		this.setTitle(PluginServices.getText(null, "calendarTitle"));
226
	}
227
	
228
	/**
229
	 * @see java.awt.Dialog#setModal(boolean)
230
	 */
231
	public void setModal(boolean b) {
232
		super.setModal(b);
233
	}
234
}
0 235

  

Also available in: Unified diff