Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / lamatek / swingextras / JDateChooser.java @ 6877

History | View | Annotate | Download (11 KB)

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