svn-gvsig-desktop / trunk / libraries / libUI / src / org / gvsig / gui / beans / textBoxWithCalendar / JCalendarDatePanel.java @ 8092
History | View | Annotate | Download (7.86 KB)
1 |
package org.gvsig.gui.beans.textBoxWithCalendar; |
---|---|
2 |
|
3 |
import java.io.Serializable; |
4 |
import java.text.SimpleDateFormat; |
5 |
import java.util.Date; |
6 |
import java.util.Locale; |
7 |
|
8 |
import javax.swing.JButton; |
9 |
import javax.swing.JDialog; |
10 |
import javax.swing.JPanel; |
11 |
import javax.swing.SwingConstants; |
12 |
|
13 |
import org.freixas.jcalendar.DateEvent; |
14 |
import org.freixas.jcalendar.DateListener; |
15 |
import org.freixas.jcalendar.JCalendar; |
16 |
|
17 |
import com.iver.andami.PluginServices; |
18 |
|
19 |
import java.awt.Color; |
20 |
import java.awt.Dimension; |
21 |
import java.awt.GridBagConstraints; |
22 |
import java.awt.GridBagLayout; |
23 |
import java.awt.event.ComponentListener; |
24 |
import java.awt.event.MouseAdapter; |
25 |
import java.awt.event.MouseEvent; |
26 |
import java.awt.event.MouseListener; |
27 |
|
28 |
/**
|
29 |
* Creates a Panel for include in other panels -> this panel allows users to set the date they want
|
30 |
* The difference from this class to the JCalendarDatePanel is that in this class the user can move
|
31 |
* the calendar with the mouse
|
32 |
*
|
33 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
34 |
*/
|
35 |
public class JCalendarDatePanel extends JPanel implements Serializable{ |
36 |
private static final long serialVersionUID = 6851067300202797515L; |
37 |
private JButton jButton = null; |
38 |
private JCalendar jCalendar = null; |
39 |
private JDialog jDialogCalendar = null; |
40 |
private int defaultWidth = 120; |
41 |
private int defaultHeight = 19; |
42 |
private MouseListener mouseListener; |
43 |
|
44 |
/**
|
45 |
* Default Constructor
|
46 |
*/
|
47 |
public JCalendarDatePanel() {
|
48 |
|
49 |
try
|
50 |
{ |
51 |
this.setPreferredSize(new Dimension(defaultWidth, defaultHeight)); |
52 |
initialize(); |
53 |
} |
54 |
catch(Exception e) |
55 |
{ |
56 |
e.printStackTrace(); |
57 |
} |
58 |
} |
59 |
|
60 |
/**
|
61 |
* This is the default constructor with 2 parameters for set the size
|
62 |
*/
|
63 |
public JCalendarDatePanel(int width, int height) { |
64 |
|
65 |
try
|
66 |
{ |
67 |
defaultWidth = width; |
68 |
defaultHeight = height; |
69 |
|
70 |
this.setPreferredSize(new Dimension(defaultWidth, defaultHeight)); |
71 |
initialize(); |
72 |
} |
73 |
catch(Exception e) |
74 |
{ |
75 |
e.printStackTrace(); |
76 |
} |
77 |
} |
78 |
|
79 |
/**
|
80 |
* This method initializes this
|
81 |
*/
|
82 |
private void initialize() { |
83 |
// Set properties to the current panel
|
84 |
GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
85 |
gridBagConstraints1.gridx = 1;
|
86 |
gridBagConstraints1.gridy = 0;
|
87 |
|
88 |
GridBagLayout gridBagLayout = new GridBagLayout(); |
89 |
this.setLayout(gridBagLayout);
|
90 |
|
91 |
// Add components to this panel:
|
92 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
93 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
94 |
gridBagConstraints.gridy = 0;
|
95 |
gridBagConstraints.weightx = 1.0;
|
96 |
gridBagConstraints.gridx = 0;
|
97 |
|
98 |
// Create the JCalendar Object
|
99 |
this.getJCalendar();
|
100 |
|
101 |
// Create the JDialog Object
|
102 |
this.getJDialogCalendar();
|
103 |
|
104 |
// Add the JButton
|
105 |
this.add(getJButton(), gridBagConstraints1);
|
106 |
|
107 |
// Defines the mouseLIstener
|
108 |
this.createMouseListener();
|
109 |
|
110 |
// Adds the mouseListener to the jTextField and the jButton
|
111 |
jButton.addMouseListener(mouseListener); |
112 |
} |
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 setPreferredSizeResize(int width, int height) |
122 |
{ |
123 |
defaultWidth = width; |
124 |
defaultHeight = height; |
125 |
this.setPreferredSize(new Dimension(defaultWidth, defaultHeight)); |
126 |
this.revalidate();
|
127 |
} |
128 |
|
129 |
/**
|
130 |
* Get the height of this panel
|
131 |
*/
|
132 |
public int getHeight() |
133 |
{ |
134 |
return this.defaultHeight; |
135 |
} |
136 |
|
137 |
/**
|
138 |
* Get the width of this panel
|
139 |
*/
|
140 |
public int getWidth() |
141 |
{ |
142 |
return this.defaultWidth; |
143 |
} |
144 |
|
145 |
/**
|
146 |
* This method initializes jButton
|
147 |
*
|
148 |
* @return javax.swing.JButton
|
149 |
*/
|
150 |
private JButton getJButton() { |
151 |
if (jButton == null) { |
152 |
jButton = new JButton(); |
153 |
jButton.setPreferredSize(new Dimension(defaultWidth, defaultHeight)); |
154 |
jButton.setHorizontalTextPosition(SwingConstants.LEFT);
|
155 |
jButton.setBackground(Color.WHITE);
|
156 |
jButton.setText(this.getFormattedDate(jCalendar.getDate()));
|
157 |
} |
158 |
return jButton;
|
159 |
} |
160 |
|
161 |
/**
|
162 |
* This method initializes jCalendar
|
163 |
*
|
164 |
* @return javax.swing.JCalendar
|
165 |
*/
|
166 |
private JCalendar getJCalendar() {
|
167 |
if (jCalendar == null) { |
168 |
jCalendar = new JCalendar();
|
169 |
jCalendar.setToolTipText(PluginServices.getText(this, "calendarSelectDate")); |
170 |
|
171 |
try
|
172 |
{ |
173 |
jCalendar.setToolTipTextToMonthDecrButton(PluginServices.getText(null, "calendarBackOneMonth")); |
174 |
jCalendar.setToolTipTextToMonthIncrButton(PluginServices.getText(null, "calendarForwardOneMonth")); |
175 |
jCalendar.setToolTipTextToYearDecrButton(PluginServices.getText(null, "calendarBackOneYear")); |
176 |
jCalendar.setToolTipTextToYearIncrButton(PluginServices.getText(null, "calendarForwardOneYear")); |
177 |
|
178 |
// Adds a date listener calendar
|
179 |
jCalendar.addDateListener(new DateListener(){
|
180 |
/*
|
181 |
* (non-Javadoc)
|
182 |
* @see org.freixas.jcalendar.DateListener#dateChanged(org.freixas.jcalendar.DateEvent)
|
183 |
*/
|
184 |
public void dateChanged(DateEvent arg0) { |
185 |
// TODO Auto-generated method stub
|
186 |
jButton.setText(getFormattedDate(arg0.getSelectedDate().getTime())); |
187 |
jDialogCalendar.hide(); |
188 |
} |
189 |
}); |
190 |
} |
191 |
catch(Exception e) |
192 |
{ |
193 |
e.printStackTrace(); |
194 |
} |
195 |
} |
196 |
return jCalendar;
|
197 |
} |
198 |
|
199 |
/**
|
200 |
* This method initializes jDialogCalendar
|
201 |
*
|
202 |
* @return javax.swing.JDialog
|
203 |
*/
|
204 |
private JDialog getJDialogCalendar() { |
205 |
if (jDialogCalendar == null) { |
206 |
jDialogCalendar = new JDialog(); |
207 |
jDialogCalendar.setSize(jCalendar.getPreferredSize()); |
208 |
this.setDefaultTitle();
|
209 |
|
210 |
// Adds the jCalendar to this dialog
|
211 |
jDialogCalendar.getContentPane().add(jCalendar); |
212 |
} |
213 |
return jDialogCalendar;
|
214 |
} |
215 |
|
216 |
|
217 |
/**
|
218 |
* Gets the date of the calendar
|
219 |
*
|
220 |
* @return Date
|
221 |
*/
|
222 |
public Date getDate() { |
223 |
return jCalendar.getDate();
|
224 |
} |
225 |
|
226 |
/**
|
227 |
* Sets the date to the calendar
|
228 |
*
|
229 |
* @param Date
|
230 |
*/
|
231 |
public void setDate(Date date) |
232 |
{ |
233 |
jCalendar.setDate(date); |
234 |
} |
235 |
|
236 |
/**
|
237 |
* Allows select the date
|
238 |
*/
|
239 |
public void enableCalendar() { |
240 |
jButton.setEnabled(true);
|
241 |
} |
242 |
|
243 |
/**
|
244 |
* Don't allow select the date
|
245 |
*/
|
246 |
public void disableCalendar() { |
247 |
jButton.setEnabled(false);
|
248 |
} |
249 |
|
250 |
/**
|
251 |
* Returns the date formatted
|
252 |
*
|
253 |
* @param Date
|
254 |
* @return String The formatted date
|
255 |
*/
|
256 |
private String getFormattedDate(Date d) { |
257 |
return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(d); |
258 |
} |
259 |
|
260 |
/**
|
261 |
* Returns the date selected, formatted
|
262 |
*
|
263 |
* @return String The formatted date
|
264 |
*/
|
265 |
public String getFormattedDate() { |
266 |
return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(this.getDate()); |
267 |
} |
268 |
|
269 |
/**
|
270 |
* Gets the title of the JDialog with the calendar component. The title is displayed in the JDialog's border.
|
271 |
* @return Title
|
272 |
*/
|
273 |
public String getTitle() { |
274 |
return this.jDialogCalendar.getTitle(); |
275 |
} |
276 |
|
277 |
/**
|
278 |
* Sets the title of the JDialog with the calendar component.
|
279 |
*
|
280 |
* @param String
|
281 |
*/
|
282 |
public void setTitle(String title) { |
283 |
this.jDialogCalendar.setTitle(title);
|
284 |
} |
285 |
|
286 |
/**
|
287 |
* Sets the default title of the JDialog with the calendar component.
|
288 |
*/
|
289 |
public void setDefaultTitle() { |
290 |
this.jDialogCalendar.setTitle(PluginServices.getText(this, "calendarTitle")); |
291 |
} |
292 |
|
293 |
/**
|
294 |
* @see java.awt.Dialog#setModal(boolean)
|
295 |
*/
|
296 |
public void setModal(boolean b) { |
297 |
jDialogCalendar.setModal(b); |
298 |
} |
299 |
|
300 |
/**
|
301 |
* Adds a component listener for the inner JDialog that cointins the calendar interface
|
302 |
*
|
303 |
* @param componentListener
|
304 |
*/
|
305 |
public void addComponentListenerForJDialogCalendar(ComponentListener componentListener) { |
306 |
jDialogCalendar.addComponentListener(componentListener); |
307 |
} |
308 |
|
309 |
/**
|
310 |
* This method creates a Mouse Listener object
|
311 |
*/
|
312 |
private void createMouseListener() { |
313 |
|
314 |
mouseListener = new MouseAdapter() { |
315 |
|
316 |
/*
|
317 |
* (non-Javadoc)
|
318 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
319 |
*/
|
320 |
public void mouseClicked(MouseEvent e) { |
321 |
// TODO Auto-generated method stub
|
322 |
|
323 |
// Show the JDialog
|
324 |
if (getJButton().isEnabled())
|
325 |
{ |
326 |
jDialogCalendar.setLocationRelativeTo(jButton); |
327 |
jDialogCalendar.show(); |
328 |
} |
329 |
} |
330 |
}; |
331 |
} |
332 |
|
333 |
} // @jve:decl-index=0:visual-constraint="10,10"
|