root / trunk / libraries / libUI / src / org / gvsig / gui / beans / textBoxWithCalendar / JCalendarCDatePanel.java @ 9033
History | View | Annotate | Download (5.46 KB)
1 |
package org.gvsig.gui.beans.textBoxWithCalendar; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Color; |
5 |
import java.awt.Dimension; |
6 |
|
7 |
import javax.swing.JPanel; |
8 |
|
9 |
import org.freixas.jcalendar.*; |
10 |
import org.gvsig.gui.beans.Messages; |
11 |
|
12 |
import java.text.SimpleDateFormat; |
13 |
import java.util.Calendar; |
14 |
import java.util.Date; |
15 |
import java.util.Locale; |
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 Panel for include in other panels -> this panel allows users to set the date they want
|
60 |
* The difference from this class to the JCalendarDatePanel is that in this class the user can move
|
61 |
* the calendar with the mouse
|
62 |
*
|
63 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
64 |
*/
|
65 |
public class JCalendarCDatePanel extends JPanel implements java.io.Serializable{ |
66 |
private static final long serialVersionUID = 8509184989434648171L; |
67 |
private int defaultWidth = 120; |
68 |
private int defaultHeight = 19; |
69 |
private JCalendarCombo calendar = null; |
70 |
|
71 |
/**
|
72 |
* This is the default constructor: creates the panel with a JCalendar object
|
73 |
*/
|
74 |
public JCalendarCDatePanel() {
|
75 |
|
76 |
try
|
77 |
{ |
78 |
// Set properties to the current panel
|
79 |
this.setPreferredSize(new Dimension(defaultWidth, defaultHeight)); |
80 |
this.setLayout(new BorderLayout()); |
81 |
|
82 |
// Adds the JCalendar to the current panel
|
83 |
this.add(this.getJCalendarCombo()); |
84 |
} |
85 |
catch(Exception e) |
86 |
{ |
87 |
e.printStackTrace(); |
88 |
} |
89 |
} |
90 |
|
91 |
/**
|
92 |
* This is the default constructor with 2 parameters: creates the panel with a JCalendar object
|
93 |
*/
|
94 |
public JCalendarCDatePanel(int width, int height) { |
95 |
|
96 |
try
|
97 |
{ |
98 |
defaultWidth = width; |
99 |
defaultHeight = height; |
100 |
|
101 |
// Set properties to the current panel
|
102 |
this.setPreferredSize(new Dimension(defaultWidth, defaultHeight)); |
103 |
this.setLayout(new BorderLayout()); |
104 |
|
105 |
// Adds the JCalendar to the current panel
|
106 |
this.add(this.getJCalendarCombo()); |
107 |
} |
108 |
catch(Exception e) |
109 |
{ |
110 |
e.printStackTrace(); |
111 |
} |
112 |
} |
113 |
|
114 |
/**
|
115 |
* Returns a reference to the JCalendarCombo private attribute
|
116 |
*
|
117 |
* @return A reference to the calendar
|
118 |
*/
|
119 |
private JCalendarCombo getJCalendarCombo() {
|
120 |
if (this.calendar == null) { |
121 |
// Create a JCalendar
|
122 |
calendar = new JCalendarCombo(Calendar.getInstance(), Locale.getDefault(), JCalendarCombo.DISPLAY_DATE, true); |
123 |
calendar.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); |
124 |
calendar.setPreferredSize(new Dimension(defaultWidth, defaultHeight)); |
125 |
calendar.setEditable(false);
|
126 |
calendar.setBackground(Color.WHITE);
|
127 |
calendar.setToolTipText(Messages.getText("calendarSelectDate"));
|
128 |
calendar.setToolTipTextToMonthDecrButton(Messages.getText("calendarBackOneMonth"));
|
129 |
calendar.setToolTipTextToMonthIncrButton(Messages.getText("calendarForwardOneMonth"));
|
130 |
calendar.setToolTipTextToYearDecrButton(Messages.getText("calendarBackOneYear"));
|
131 |
calendar.setToolTipTextToYearIncrButton(Messages.getText("calendarForwardOneYear"));
|
132 |
} |
133 |
|
134 |
return this.calendar; |
135 |
} |
136 |
|
137 |
/**
|
138 |
* Sets the size of the CalendarDatePanel for resize the JCalendarCommbo and this panel
|
139 |
*
|
140 |
* @param width (the new Width for the panel)
|
141 |
* @param height (the new Height for the panel)
|
142 |
*/
|
143 |
public void setPreferredSizeResize(int width, int height) |
144 |
{ |
145 |
defaultWidth = width; |
146 |
defaultHeight = height; |
147 |
this.setPreferredSize(new Dimension(defaultWidth, defaultHeight)); |
148 |
calendar.setPreferredSize(new Dimension(defaultWidth, defaultHeight)); |
149 |
calendar.setSize(new Dimension(defaultWidth, defaultHeight)); |
150 |
this.revalidate();
|
151 |
} |
152 |
|
153 |
/**
|
154 |
* Get the height of this panel
|
155 |
*/
|
156 |
public int getHeight() |
157 |
{ |
158 |
return this.defaultHeight; |
159 |
} |
160 |
|
161 |
/**
|
162 |
* Get the width of this panel
|
163 |
*/
|
164 |
public int getWidth() |
165 |
{ |
166 |
return this.defaultWidth; |
167 |
} |
168 |
|
169 |
/**
|
170 |
* Sets the date to the calendar
|
171 |
*
|
172 |
* @param date
|
173 |
*/
|
174 |
public void setDate(Date date) |
175 |
{ |
176 |
calendar.setDate(date); |
177 |
} |
178 |
|
179 |
/**
|
180 |
* Gets the date of the calendar
|
181 |
*
|
182 |
* @return Date
|
183 |
*/
|
184 |
public Date getDate() |
185 |
{ |
186 |
return calendar.getDate();
|
187 |
} |
188 |
|
189 |
/**
|
190 |
* Allows select the date
|
191 |
*/
|
192 |
public void enableCalendar() |
193 |
{ |
194 |
calendar.setEnabled(true);
|
195 |
} |
196 |
|
197 |
/**
|
198 |
* Don't allow select the date
|
199 |
*/
|
200 |
public void disableCalendar() |
201 |
{ |
202 |
calendar.setEnabled(false);
|
203 |
} |
204 |
|
205 |
/**
|
206 |
* Returns the date selected, formatted
|
207 |
*
|
208 |
* @return String The formatted date
|
209 |
*/
|
210 |
public String getFormattedDate() { |
211 |
return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(this.getDate()); |
212 |
} |
213 |
} |