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