root / tags / v1_0_2_Build_903 / applications / appgvSIG / src / com / lamatek / swingextras / JDateChooser.java @ 10704
History | View | Annotate | Download (11 KB)
1 |
package com.lamatek.swingextras; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Color; |
5 |
import java.awt.Component; |
6 |
import java.awt.FlowLayout; |
7 |
import java.awt.GridBagConstraints; |
8 |
import java.awt.GridBagLayout; |
9 |
import java.awt.GridLayout; |
10 |
import java.awt.Insets; |
11 |
import java.awt.event.ActionEvent; |
12 |
import java.awt.event.ActionListener; |
13 |
import java.awt.event.MouseAdapter; |
14 |
import java.awt.event.MouseEvent; |
15 |
import java.awt.event.MouseListener; |
16 |
import java.text.DateFormat; |
17 |
import java.util.Calendar; |
18 |
import java.util.Date; |
19 |
import java.util.Vector; |
20 |
|
21 |
import javax.swing.BorderFactory; |
22 |
import javax.swing.JButton; |
23 |
import javax.swing.JLabel; |
24 |
import javax.swing.JPanel; |
25 |
import javax.swing.border.BevelBorder; |
26 |
import javax.swing.border.EtchedBorder; |
27 |
|
28 |
import com.iver.andami.PluginServices; |
29 |
import com.iver.andami.ui.mdiManager.IWindow; |
30 |
import com.iver.andami.ui.mdiManager.WindowInfo; |
31 |
|
32 |
/**
|
33 |
* JDateChooser is a simple Date choosing component with similar functionality
|
34 |
* to JFileChooser and JColorChooser. It can be used as a component, to
|
35 |
* be inserted into a client layout, or can display it's own Dialog
|
36 |
* through use of the {@link #showDialog(Component, String) showDialog} method.
|
37 |
* <p>
|
38 |
* JDateChooser can be initialized to the current date using the no argument
|
39 |
* constructor, or initialized to a predefined date by passing an instance
|
40 |
* of Calendar to the constructor.<p>
|
41 |
* Using the JDateChooser dialog works in a similar manner to JFileChooser
|
42 |
* or JColorChooser. The {@link #showDialog(Component, String) showDialog} method
|
43 |
* returns an int that equates to the public variables ACCEPT_OPTION, CANCEL_OPTION
|
44 |
* or ERROR_OPTION.<p>
|
45 |
* <tt>
|
46 |
* JDateChooser chooser = new JDateChooser();<br>
|
47 |
* if (chooser.showDialog(this, "Select a date...") == JDateChooser.ACCEPT_OPTION) {<br>
|
48 |
* Calendar selectedDate = chooser.getSelectedDate();<br>
|
49 |
* // process date here...<br>
|
50 |
* }<p>
|
51 |
* To use JDateChooser as a component within a GUI, users should subclass
|
52 |
* JDateChooser and override the {@link #acceptSelection() acceptSelection} and
|
53 |
* {@link #cancelSelection() cancelSelection} methods to process the
|
54 |
* corresponding user selection.<p>
|
55 |
* The current date can be retrieved by calling {@link #getSelectedDate() getSelectedDate}
|
56 |
* method.
|
57 |
*/
|
58 |
|
59 |
public class JDateChooser extends JPanel implements ActionListener, DaySelectionListener, IWindow { |
60 |
|
61 |
public static final int CANCEL_OPTION = 4; |
62 |
private int currentDay; |
63 |
private int currentMonth; |
64 |
private int currentYear; |
65 |
private JLabel dateText; |
66 |
private Calendar calendar; |
67 |
private Date date; |
68 |
private JButton previousYear; |
69 |
private JButton previousMonth; |
70 |
private JButton nextMonth; |
71 |
private JButton nextYear; |
72 |
private JButton okay; |
73 |
private JButton cancel; |
74 |
private JPanel days; |
75 |
|
76 |
private DayListener dayListener = new DayListener(); |
77 |
/**
|
78 |
* This constructor creates a new instance of JDateChooser initialized to
|
79 |
* the current date.
|
80 |
*/
|
81 |
public JDateChooser() {
|
82 |
this(Calendar.getInstance()); |
83 |
} |
84 |
|
85 |
/**
|
86 |
* Creates a new instance of JDateChooser initialized to the given Calendar.
|
87 |
*/
|
88 |
public JDateChooser(Calendar c) { |
89 |
super();
|
90 |
this.calendar = c;
|
91 |
this.calendar.setLenient(true); |
92 |
setup(); |
93 |
} |
94 |
|
95 |
private void setup() { |
96 |
GridBagLayout g = new GridBagLayout(); |
97 |
GridBagConstraints c = new GridBagConstraints(); |
98 |
JPanel header = new JPanel(g); |
99 |
c.gridx = 0;
|
100 |
c.gridy = 0;
|
101 |
c.insets = new Insets(2, 0, 2, 0); |
102 |
previousYear = (JButton) header.add(new JButton("<<")); |
103 |
previousYear.addActionListener(this);
|
104 |
previousYear.setToolTipText("A?o anterior");
|
105 |
g.setConstraints(previousYear, c); |
106 |
previousMonth = (JButton) header.add(new JButton("<")); |
107 |
previousMonth.addActionListener(this);
|
108 |
previousMonth.setToolTipText("Mes anterior");
|
109 |
c.gridx++; |
110 |
g.setConstraints(previousMonth, c); |
111 |
dateText = (JLabel) header.add(new JLabel("", JLabel.CENTER)); |
112 |
dateText.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); |
113 |
c.gridx++; |
114 |
c.weightx = 1.0;
|
115 |
c.fill = c.BOTH; |
116 |
g.setConstraints(dateText, c); |
117 |
nextMonth = (JButton) header.add(new JButton(">")); |
118 |
nextMonth.addActionListener(this);
|
119 |
nextMonth.setToolTipText("Mes siguiente");
|
120 |
c.gridx++; |
121 |
c.weightx = 0.0;
|
122 |
c.fill = c.NONE; |
123 |
g.setConstraints(nextMonth, c); |
124 |
nextYear = (JButton) header.add(new JButton(">>")); |
125 |
nextYear.addActionListener(this);
|
126 |
nextYear.setToolTipText("A?o siguiente");
|
127 |
c.gridx++; |
128 |
g.setConstraints(nextYear, c); |
129 |
|
130 |
updateCalendar(calendar); |
131 |
|
132 |
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
133 |
okay = (JButton) buttons.add(new JButton("Ok")); |
134 |
okay.addActionListener(this);
|
135 |
cancel = (JButton) buttons.add(new JButton("Cancelar")); |
136 |
cancel.addActionListener(this);
|
137 |
|
138 |
setLayout(new BorderLayout()); |
139 |
add("North", header);
|
140 |
days.setBackground(Color.WHITE);
|
141 |
days.setOpaque(true);
|
142 |
days.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); |
143 |
add("Center", days);
|
144 |
add("South", buttons);
|
145 |
} |
146 |
|
147 |
private void updateCalendar(Calendar c) { |
148 |
if (days != null) |
149 |
remove(days); |
150 |
currentDay = calendar.get(Calendar.DAY_OF_MONTH);
|
151 |
currentMonth = calendar.get(Calendar.MONTH);
|
152 |
currentYear = calendar.get(Calendar.YEAR);
|
153 |
days = new JPanel(new GridLayout(7, 7)); |
154 |
Calendar setup = (Calendar) calendar.clone(); |
155 |
setup.set(Calendar.DAY_OF_WEEK, setup.getFirstDayOfWeek());
|
156 |
int lastLayoutPosition = 0; |
157 |
for (int i = 0; i < 7; i++) { |
158 |
int dayInt = setup.get(Calendar.DAY_OF_WEEK); |
159 |
if (dayInt == Calendar.MONDAY) |
160 |
days.add(new JLabel("Mon", JLabel.CENTER)); |
161 |
if (dayInt == Calendar.TUESDAY) |
162 |
days.add(new JLabel("Tue", JLabel.CENTER)); |
163 |
if (dayInt == Calendar.WEDNESDAY) |
164 |
days.add(new JLabel("Wed", JLabel.CENTER)); |
165 |
if (dayInt == Calendar.THURSDAY) |
166 |
days.add(new JLabel("Thu", JLabel.CENTER)); |
167 |
if (dayInt == Calendar.FRIDAY) |
168 |
days.add(new JLabel("Fri", JLabel.CENTER)); |
169 |
if (dayInt == Calendar.SATURDAY) |
170 |
days.add(new JLabel("Sat", JLabel.CENTER)); |
171 |
if (dayInt == Calendar.SUNDAY) |
172 |
days.add(new JLabel("Sun", JLabel.CENTER)); |
173 |
setup.roll(Calendar.DAY_OF_WEEK, true); |
174 |
lastLayoutPosition++; |
175 |
} |
176 |
setup = (Calendar) calendar.clone();
|
177 |
setup.set(Calendar.DAY_OF_MONTH, 1); |
178 |
int first = setup.get(Calendar.DAY_OF_WEEK); |
179 |
for (int i = 0; i < (first - 1); i++) { |
180 |
days.add(new JLabel("")); |
181 |
lastLayoutPosition++; |
182 |
} |
183 |
setup.set(Calendar.DAY_OF_MONTH, 1); |
184 |
for (int i = 0; i < setup.getActualMaximum(setup.DAY_OF_MONTH); i++) { |
185 |
DayButton button = new DayButton(setup.get(setup.DAY_OF_MONTH));
|
186 |
if (setup.get(Calendar.DAY_OF_MONTH) == currentDay){ |
187 |
button.setBackground(Color.BLUE);
|
188 |
button.setForeground(Color.WHITE);
|
189 |
dayListener.setDia(button); |
190 |
} |
191 |
button.addDaySelectionListener(this);
|
192 |
button.addMouseListener(dayListener); |
193 |
days.add(button); |
194 |
setup.roll(setup.DAY_OF_MONTH, true);
|
195 |
lastLayoutPosition++; |
196 |
} |
197 |
for (int i = lastLayoutPosition; i < 49; i++) |
198 |
days.add(new JLabel("")); |
199 |
add("Center", days);
|
200 |
validate(); |
201 |
setup = null;
|
202 |
updateLabel(); |
203 |
} |
204 |
|
205 |
private void updateLabel() { |
206 |
Date date = calendar.getTime();
|
207 |
dateText.setText(DateFormat.getInstance().format(date));
|
208 |
} |
209 |
|
210 |
/**
|
211 |
* Returns the currently selected Date in the form of a java.util.Calendar
|
212 |
* object. Typically called adter receipt of an {@link #ACCEPT_OPTION ACCEPT_OPTION}
|
213 |
* (using the {@link #showDialog(Component, String) showDialog} method) or
|
214 |
* within the {@link #acceptSelection() acceptSelection} method (using the
|
215 |
* JDateChooser as a component.)<p>
|
216 |
* @return java.util.Calendar The selected date in the form of a Calendar object.
|
217 |
*/
|
218 |
public Calendar getSelectedDate() { |
219 |
return calendar;
|
220 |
} |
221 |
|
222 |
/**
|
223 |
* Used to process events from the previous month, previous year, next month, next year,
|
224 |
* okay and cancel buttons. Users should call super.actionPerformed(ActionEvent) if overriding
|
225 |
* this method.
|
226 |
*/
|
227 |
public void actionPerformed(ActionEvent e) { |
228 |
if (e.getSource() == okay) {
|
229 |
PluginServices.getMDIManager().closeWindow(this);
|
230 |
} |
231 |
if (e.getSource() == cancel) {
|
232 |
calendar = null;
|
233 |
PluginServices.getMDIManager().closeWindow(this);
|
234 |
} |
235 |
if (e.getSource() == previousYear) {
|
236 |
calendar.roll(Calendar.YEAR, -1); |
237 |
updateCalendar(calendar); |
238 |
} |
239 |
if (e.getSource() == previousMonth) {
|
240 |
calendar.roll(Calendar.MONTH, -1); |
241 |
updateCalendar(calendar); |
242 |
} |
243 |
if (e.getSource() == nextMonth) {
|
244 |
calendar.roll(Calendar.MONTH, 1); |
245 |
updateCalendar(calendar); |
246 |
} |
247 |
if (e.getSource() == nextYear) {
|
248 |
calendar.roll(Calendar.YEAR, 1); |
249 |
updateCalendar(calendar); |
250 |
} |
251 |
} |
252 |
|
253 |
/**
|
254 |
* Used to process day selection events from the user. This method resets
|
255 |
* resets the Calendar object to the selected day. Subclasses should make a call
|
256 |
* to super.daySelected() if overriding this method.
|
257 |
*/
|
258 |
public void daySelected(int d) { |
259 |
calendar.set(Calendar.DAY_OF_MONTH, d);
|
260 |
updateLabel(); |
261 |
currentDay = d; |
262 |
} |
263 |
|
264 |
/**
|
265 |
* @see com.iver.mdiApp.ui.MDIManager.IWindow#getWindowInfo()
|
266 |
*/
|
267 |
public WindowInfo getWindowInfo() {
|
268 |
WindowInfo m_viewinfo=new WindowInfo(WindowInfo.MODALDIALOG);
|
269 |
m_viewinfo.setTitle(PluginServices.getText(this,"seleccione_fecha")); |
270 |
m_viewinfo.setWidth(400);
|
271 |
m_viewinfo.setHeight(200);
|
272 |
|
273 |
return m_viewinfo;
|
274 |
} |
275 |
|
276 |
/**
|
277 |
* @see com.iver.mdiApp.ui.MDIManager.IWindow#windowActivated()
|
278 |
*/
|
279 |
public void viewActivated() { |
280 |
} |
281 |
} |
282 |
|
283 |
class DayListener extends MouseAdapter{ |
284 |
private DayButton btnDia;
|
285 |
|
286 |
public void setDia(DayButton btn){ |
287 |
btnDia = btn; |
288 |
} |
289 |
/**
|
290 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
291 |
*/
|
292 |
public void mouseClicked(MouseEvent e) { |
293 |
if (btnDia != null){ |
294 |
btnDia.setBackground(Color.WHITE);
|
295 |
btnDia.setForeground(Color.BLACK);
|
296 |
} |
297 |
|
298 |
btnDia = (DayButton) e.getSource(); |
299 |
btnDia.setBackground(Color.BLUE);
|
300 |
btnDia.setForeground(Color.WHITE);
|
301 |
|
302 |
} |
303 |
|
304 |
} |
305 |
|
306 |
class DayButton extends JLabel implements MouseListener { |
307 |
|
308 |
private int day; |
309 |
private Vector listeners; |
310 |
|
311 |
public DayButton(int d) { |
312 |
super((new Integer(d)).toString()); |
313 |
this.day = d;
|
314 |
addMouseListener(this);
|
315 |
this.setBackground(Color.WHITE); |
316 |
this.setOpaque(true); |
317 |
this.setHorizontalAlignment(JLabel.CENTER); |
318 |
} |
319 |
|
320 |
public void addDaySelectionListener(DaySelectionListener l) { |
321 |
if (listeners == null) |
322 |
listeners = new Vector(1, 1); |
323 |
listeners.addElement(l); |
324 |
} |
325 |
|
326 |
public void removeDaySelectionListener(DaySelectionListener l) { |
327 |
if (listeners != null) |
328 |
listeners.removeElement(l); |
329 |
} |
330 |
|
331 |
public void removeAllListeners() { |
332 |
listeners = new Vector(1,1); |
333 |
} |
334 |
|
335 |
/**
|
336 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
337 |
*/
|
338 |
public void mouseClicked(MouseEvent e) { |
339 |
if (listeners != null) { |
340 |
for (int i = 0; i < listeners.size(); i++) { |
341 |
((DaySelectionListener) listeners.elementAt(i)).daySelected(day); |
342 |
} |
343 |
} |
344 |
} |
345 |
|
346 |
/**
|
347 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
348 |
*/
|
349 |
public void mouseEntered(MouseEvent arg0) { |
350 |
} |
351 |
|
352 |
/**
|
353 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
354 |
*/
|
355 |
public void mouseExited(MouseEvent arg0) { |
356 |
} |
357 |
|
358 |
/**
|
359 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
360 |
*/
|
361 |
public void mousePressed(MouseEvent arg0) { |
362 |
} |
363 |
|
364 |
/**
|
365 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
366 |
*/
|
367 |
public void mouseReleased(MouseEvent arg0) { |
368 |
} |
369 |
} |
370 |
|
371 |
|