svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / swing / textBoxWithCalendar / JCalendarDatePanel.java @ 40561
History | View | Annotate | Download (8.16 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.Color; |
27 |
import java.awt.Dimension; |
28 |
import java.awt.GridBagConstraints; |
29 |
import java.awt.GridBagLayout; |
30 |
import java.awt.event.ComponentAdapter; |
31 |
import java.awt.event.ComponentEvent; |
32 |
import java.awt.event.ComponentListener; |
33 |
import java.awt.event.MouseAdapter; |
34 |
import java.awt.event.MouseEvent; |
35 |
import java.awt.event.MouseListener; |
36 |
import java.io.Serializable; |
37 |
import java.text.SimpleDateFormat; |
38 |
import java.util.Date; |
39 |
import java.util.Locale; |
40 |
|
41 |
import javax.swing.JButton; |
42 |
import javax.swing.JPanel; |
43 |
import javax.swing.JToolTip; |
44 |
import javax.swing.SwingConstants; |
45 |
|
46 |
import org.gvsig.gui.beans.Messages; |
47 |
import org.gvsig.gui.beans.controls.MultiLineToolTip; |
48 |
|
49 |
/**
|
50 |
* Creates a Panel for include in other panels -> this panel allows users to set the date they want
|
51 |
* The difference from this class to the JCalendarDatePanel is that in this class the user can move
|
52 |
* the calendar with the mouse
|
53 |
*
|
54 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
55 |
*/
|
56 |
public class JCalendarDatePanel extends JPanel implements IMethodsForGraphicalCalendarComponents, Serializable{ |
57 |
private static final long serialVersionUID = -5439270916098423256L; |
58 |
|
59 |
private JButton jButton = null; |
60 |
private JCalendarDateDialog jDialogCalendar = null; |
61 |
private final int defaultWidth = 120; |
62 |
private final int defaultHeight = 19; |
63 |
private MouseListener mouseListener; |
64 |
|
65 |
/**
|
66 |
* Default Constructor
|
67 |
*/
|
68 |
public JCalendarDatePanel() {
|
69 |
|
70 |
try
|
71 |
{ |
72 |
this.setPreferredSize(new Dimension(defaultWidth, defaultHeight)); |
73 |
initialize(); |
74 |
} |
75 |
catch(Exception e) |
76 |
{ |
77 |
e.printStackTrace(); |
78 |
} |
79 |
} |
80 |
|
81 |
/**
|
82 |
* This is the default constructor with 2 parameters for set the size of the inner button
|
83 |
*/
|
84 |
public JCalendarDatePanel(int width, int height) { |
85 |
|
86 |
try
|
87 |
{ |
88 |
this.setPreferredSize(new Dimension(defaultWidth, defaultHeight)); |
89 |
initialize(); |
90 |
} |
91 |
catch(Exception e) |
92 |
{ |
93 |
e.printStackTrace(); |
94 |
} |
95 |
} |
96 |
|
97 |
/**
|
98 |
* This method initializes this
|
99 |
*/
|
100 |
private void initialize() { |
101 |
// Set properties to the current panel
|
102 |
GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
103 |
gridBagConstraints1.gridx = 1;
|
104 |
gridBagConstraints1.gridy = 0;
|
105 |
|
106 |
GridBagLayout gridBagLayout = new GridBagLayout(); |
107 |
this.setLayout(gridBagLayout);
|
108 |
|
109 |
// Add components to this panel:
|
110 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
111 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
112 |
gridBagConstraints.gridy = 0;
|
113 |
gridBagConstraints.weightx = 1.0;
|
114 |
gridBagConstraints.gridx = 0;
|
115 |
|
116 |
// Add the JButton
|
117 |
this.add(getJButton(), gridBagConstraints1);
|
118 |
|
119 |
// Defines the mouseLIstener
|
120 |
this.createMouseListener();
|
121 |
|
122 |
// Adds the mouseListener to the jTextField and the jButton
|
123 |
jButton.addMouseListener(mouseListener); |
124 |
} |
125 |
|
126 |
/**
|
127 |
* This method initializes jButton
|
128 |
*
|
129 |
* @return javax.swing.JButton
|
130 |
*/
|
131 |
private JButton getJButton() { |
132 |
if (jButton == null) { |
133 |
jButton = new JButton(); |
134 |
jButton.setPreferredSize(new Dimension(defaultWidth, defaultHeight)); |
135 |
jButton.setHorizontalTextPosition(SwingConstants.LEFT);
|
136 |
jButton.setBackground(Color.WHITE);
|
137 |
jButton.setText(this.getFormattedDate(this.getJCalendarDateDialog().getDate())); |
138 |
} |
139 |
return jButton;
|
140 |
} |
141 |
|
142 |
/**
|
143 |
* This method initializes jDialogCalendar
|
144 |
*
|
145 |
* @return javax.swing.JDialog
|
146 |
*/
|
147 |
public JCalendarDateDialog getJCalendarDateDialog() {
|
148 |
if (jDialogCalendar == null) { |
149 |
jDialogCalendar = new JCalendarDateDialog();
|
150 |
jDialogCalendar.addComponentListener(new ComponentAdapter() { |
151 |
/*
|
152 |
* (non-Javadoc)
|
153 |
* @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
|
154 |
*/
|
155 |
public void componentHidden(ComponentEvent e) { |
156 |
jButton.setText(getFormattedDate(jDialogCalendar.getDate())); |
157 |
} |
158 |
}); |
159 |
} |
160 |
return jDialogCalendar;
|
161 |
} |
162 |
|
163 |
|
164 |
/*
|
165 |
* (non-Javadoc)
|
166 |
* @see org.gvsig.gui.beans.swing.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#getDate()
|
167 |
*/
|
168 |
public Date getDate() { |
169 |
return this.getJCalendarDateDialog().getDate(); |
170 |
} |
171 |
|
172 |
/*
|
173 |
* (non-Javadoc)
|
174 |
* @see org.gvsig.gui.beans.swing.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#setDate(java.util.Date)
|
175 |
*/
|
176 |
public void setDate(Date date) |
177 |
{ |
178 |
this.getJCalendarDateDialog().setDate(date);
|
179 |
} |
180 |
|
181 |
/**
|
182 |
* Allows select the date
|
183 |
*/
|
184 |
public void enableCalendar() { |
185 |
jButton.setEnabled(true);
|
186 |
} |
187 |
|
188 |
/**
|
189 |
* Don't allow select the date
|
190 |
*/
|
191 |
public void disableCalendar() { |
192 |
jButton.setEnabled(false);
|
193 |
} |
194 |
|
195 |
/**
|
196 |
* Returns the date formatted
|
197 |
*
|
198 |
* @param Date
|
199 |
* @return String The formatted date
|
200 |
*/
|
201 |
private String getFormattedDate(Date d) { |
202 |
return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(d); |
203 |
} |
204 |
|
205 |
/*
|
206 |
* (non-Javadoc)
|
207 |
* @see org.gvsig.gui.beans.swing.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#getFormattedDate()
|
208 |
*/
|
209 |
public String getFormattedDate() { |
210 |
return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(this.getDate()); |
211 |
} |
212 |
|
213 |
/**
|
214 |
* Gets the title of the JDialog with the calendar component. The title is displayed in the JDialog's border.
|
215 |
* @return Title
|
216 |
*/
|
217 |
public String getTitle() { |
218 |
return this.getJCalendarDateDialog().getTitle(); |
219 |
} |
220 |
|
221 |
/**
|
222 |
* Sets the title of the JDialog with the calendar component.
|
223 |
*
|
224 |
* @param String
|
225 |
*/
|
226 |
public void setTitle(String title) { |
227 |
this.getJCalendarDateDialog().setTitle(title);
|
228 |
} |
229 |
|
230 |
/**
|
231 |
* Sets the default title of the JDialog with the calendar component.
|
232 |
*/
|
233 |
public void setDefaultTitle() { |
234 |
this.getJCalendarDateDialog().setTitle(Messages.getText("calendarTitle")); |
235 |
} |
236 |
|
237 |
/**
|
238 |
* @see java.awt.Dialog#setModal(boolean)
|
239 |
*/
|
240 |
public void setModal(boolean b) { |
241 |
this.getJCalendarDateDialog().setModal(b);
|
242 |
} |
243 |
|
244 |
/*
|
245 |
* (non-Javadoc)
|
246 |
* @see javax.swing.JComponent#setPreferredSize(java.awt.Dimension)
|
247 |
*/
|
248 |
public void setPreferredSize(Dimension d) { |
249 |
super.setPreferredSize(d);
|
250 |
getJButton().setPreferredSize(d); |
251 |
} |
252 |
|
253 |
/*
|
254 |
* (non-Javadoc)
|
255 |
* @see java.awt.Component#setSize(int, int)
|
256 |
*/
|
257 |
public void setSize(int width, int height) { |
258 |
super.setSize(width, height);
|
259 |
getJButton().setSize(width, height); |
260 |
} |
261 |
|
262 |
/*
|
263 |
* (non-Javadoc)
|
264 |
* @see java.awt.Component#setSize(java.awt.Dimension)
|
265 |
*/
|
266 |
public void setSize(Dimension d) { |
267 |
super.setSize(d);
|
268 |
getJButton().setSize(d); |
269 |
} |
270 |
|
271 |
/**
|
272 |
* Adds a component listener for the inner JDialog that cointins the calendar interface
|
273 |
*
|
274 |
* @param componentListener
|
275 |
*/
|
276 |
public void addComponentListenerForJDialogCalendar(ComponentListener componentListener) { |
277 |
getJCalendarDateDialog().addComponentListener(componentListener); |
278 |
} |
279 |
|
280 |
/**
|
281 |
* This method creates a Mouse Listener object
|
282 |
*/
|
283 |
private void createMouseListener() { |
284 |
|
285 |
mouseListener = new MouseAdapter() { |
286 |
|
287 |
/*
|
288 |
* (non-Javadoc)
|
289 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
290 |
*/
|
291 |
public void mouseClicked(MouseEvent e) { |
292 |
// Show the JDialog
|
293 |
if (getJButton().isEnabled())
|
294 |
{ |
295 |
getJCalendarDateDialog().setLocationRelativeTo(jButton); |
296 |
getJCalendarDateDialog().setVisible(true);
|
297 |
} |
298 |
} |
299 |
}; |
300 |
} |
301 |
|
302 |
/*
|
303 |
* (non-Javadoc)
|
304 |
* @see javax.swing.JComponent#createToolTip()
|
305 |
*/
|
306 |
public JToolTip createToolTip() { |
307 |
// Multiline support
|
308 |
MultiLineToolTip tip = new MultiLineToolTip();
|
309 |
tip.setComponent(this);
|
310 |
return tip;
|
311 |
} |
312 |
} |