KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > gui > CalendarDialog


1 package sellwin.gui;
2
3 import sellwin.utils.*;
4 import javax.swing.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.util.*;
8 import java.text.*;
9
10 // SellWin http://sourceforge.net/projects/sellwincrm
11
//Contact support@open-app.com for commercial help with SellWin
12
//This software is provided "AS IS", without a warranty of any kind.
13

14 /**
15  * This class shows a popup dialog box that contains a
16  * calendar and time selection. The user selects the
17  * day and time using this dialog.
18  */

19 public class CalendarDialog extends javax.swing.JDialog JavaDoc implements GUIChars {
20
21     private static final int HEIGHT = 26;
22     private static final int WIDTH = 25;
23
24     private static final String JavaDoc[] HOURS = {
25         "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
26         "12" };
27     private static final String JavaDoc[] MINUTES = {
28         "00", "01", "02", "03", "04", "05", "06", "07", "08", "09",
29         "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
30         "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
31         "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
32         "40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
33         "50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
34      };
35     private static final String JavaDoc[] AM_PM = { "AM", "PM" };
36
37     private static java.awt.Dimension JavaDoc STANDARD_DIMENSION = new Dimension(HEIGHT, WIDTH);
38
39     private Date date;
40     private Calendar thisMonthCal = Calendar.getInstance();
41     private Calendar currentCal = Calendar.getInstance();
42
43     private JPanel buttonPanel;
44     private JButton previousButton, nextButton;
45     private JLabel monthYearLabel;
46     private static final int MAX_BUTTONS=35;
47     private ArrayList dayButtons = new ArrayList();
48     private JPanel dayButtonPanel;
49     private JPanel timePanel;
50     private JLabel sundayLabel;
51     private JLabel mondayLabel;
52     private JLabel tuesdayLabel;
53     private JLabel wednesdayLabel;
54     private JLabel thursdayLabel;
55     private JLabel fridayLabel;
56     private JLabel saturdayLabel;
57     private JComboBox hoursCombo, minutesCombo, ampmCombo;
58     private Whiteboard wb=null;
59
60     private DateEditorDialogListener myEditor;
61
62     /**
63      * Creates new form CalendarDialog
64      * @param parent the parent to this dialog
65      * @param editor the component that is working with this dialog
66      * @param modal is this dialog modal or not
67      */

68     public CalendarDialog(java.awt.Frame JavaDoc parent, DateEditorDialogListener editor, boolean modal) {
69         super(parent, modal);
70         myEditor = editor;
71         wb = MainWindow.getWhiteboard();
72
73         if (wb.getLang() == null)
74             LogWrite.write("lang is null at this point");
75         setTitle(wb.getLang().getString("calendarSelection"));
76
77         initComponents();
78
79         setSize(290, 190);
80
81         setMonthLabel();
82         updateDayButtons();
83
84         nextButton.addActionListener(
85             new ActionListener() {
86                 public void actionPerformed(ActionEvent e) {
87                     currentCal.add(Calendar.MONTH, 1);
88                     setMonthLabel();
89                     updateDayButtons();
90                 }
91             }
92         );
93
94         previousButton.addActionListener(
95             new ActionListener() {
96                 public void actionPerformed(ActionEvent e) {
97                     currentCal.add(Calendar.MONTH, -1);
98                     setMonthLabel();
99                     updateDayButtons();
100                 }
101             }
102         );
103     }
104
105     /**
106      * This method is called from within the constructor to
107      * initialize the form.
108      */

109     private final void initComponents() {
110         buttonPanel = new JPanel();
111         previousButton = new JButton();
112         monthYearLabel = new JLabel();
113         nextButton = new JButton();
114         dayButtonPanel = new JPanel();
115         timePanel = new JPanel();
116
117         hoursCombo = new JComboBox(HOURS);
118         minutesCombo = new JComboBox(MINUTES);
119         minutesCombo.setMaximumRowCount(10);
120         ampmCombo = new JComboBox(AM_PM);
121
122         sundayLabel = new JLabel("Sun", SwingConstants.CENTER);
123             sundayLabel.setPreferredSize(STANDARD_DIMENSION);
124             sundayLabel.setMinimumSize(STANDARD_DIMENSION);
125             sundayLabel.setMaximumSize(STANDARD_DIMENSION);
126         mondayLabel = new JLabel("Mon", SwingConstants.CENTER);
127             mondayLabel.setPreferredSize(STANDARD_DIMENSION);
128             mondayLabel.setMinimumSize(STANDARD_DIMENSION);
129             mondayLabel.setMaximumSize(STANDARD_DIMENSION);
130         tuesdayLabel = new JLabel("Tue", SwingConstants.CENTER);
131             tuesdayLabel.setPreferredSize(STANDARD_DIMENSION);
132             tuesdayLabel.setMinimumSize(STANDARD_DIMENSION);
133             tuesdayLabel.setMaximumSize(STANDARD_DIMENSION);
134         wednesdayLabel = new JLabel("Wed", SwingConstants.CENTER);
135             wednesdayLabel.setPreferredSize(STANDARD_DIMENSION);
136             wednesdayLabel.setMinimumSize(STANDARD_DIMENSION);
137             wednesdayLabel.setMaximumSize(STANDARD_DIMENSION);
138         thursdayLabel = new JLabel("Thu", SwingConstants.CENTER);
139             thursdayLabel.setPreferredSize(STANDARD_DIMENSION);
140             thursdayLabel.setMinimumSize(STANDARD_DIMENSION);
141             thursdayLabel.setMaximumSize(STANDARD_DIMENSION);
142         fridayLabel = new JLabel("Fri", SwingConstants.CENTER);
143             fridayLabel.setPreferredSize(STANDARD_DIMENSION);
144             fridayLabel.setMinimumSize(STANDARD_DIMENSION);
145             fridayLabel.setMaximumSize(STANDARD_DIMENSION);
146         saturdayLabel = new JLabel("Sat", SwingConstants.CENTER);
147             saturdayLabel.setPreferredSize(STANDARD_DIMENSION);
148             saturdayLabel.setMinimumSize(STANDARD_DIMENSION);
149             saturdayLabel.setMaximumSize(STANDARD_DIMENSION);
150         
151         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
152             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
153                 closeDialog(evt);
154             }
155         });
156         
157         previousButton.setText("<");
158         buttonPanel.add(previousButton);
159         
160         monthYearLabel.setText("Jan 2002");
161         buttonPanel.add(monthYearLabel);
162         
163         nextButton.setText(">");
164         buttonPanel.add(nextButton);
165         
166         getContentPane().add(buttonPanel, java.awt.BorderLayout.NORTH);
167         
168         dayButtonPanel.setLayout(new java.awt.GridLayout JavaDoc(6, 7, 0, 0));
169        
170         dayButtonPanel.add(sundayLabel);
171         dayButtonPanel.add(mondayLabel);
172         dayButtonPanel.add(tuesdayLabel);
173         dayButtonPanel.add(wednesdayLabel);
174         dayButtonPanel.add(thursdayLabel);
175         dayButtonPanel.add(fridayLabel);
176         dayButtonPanel.add(saturdayLabel);
177
178         timePanel.add(hoursCombo);
179         timePanel.add(minutesCombo);
180         timePanel.add(ampmCombo);
181
182         JButton butt;
183         ButtonPressAction buttonPressAction = new ButtonPressAction();
184         for (int i=0;i<MAX_BUTTONS;i++) {
185             butt = new JButton(Integer.toString(i));
186             butt.addActionListener(buttonPressAction);
187             dayButtons.add(butt);
188             butt.setMargin(new Insets(0, 0, 0, 0));
189             butt.setPreferredSize(STANDARD_DIMENSION);
190             butt.setMaximumSize(STANDARD_DIMENSION);
191             butt.setMinimumSize(STANDARD_DIMENSION);
192             dayButtonPanel.add(butt);
193         }
194         
195         
196         getContentPane().add(dayButtonPanel, java.awt.BorderLayout.CENTER);
197         getContentPane().add(timePanel, java.awt.BorderLayout.SOUTH);
198         
199         pack();
200     }
201
202     /**
203      * close the dialog
204      * @param evt the WindowEvent we get when called
205      */

206     private final void closeDialog(java.awt.event.WindowEvent JavaDoc evt) {
207         setVisible(false);
208         dispose();
209     }
210
211
212     /**
213      * set the date displayed by the dialog
214      * not implemented yet
215      * it should set the screen to the passed in month
216      * @param d the date we are to display
217      */

218     public final void setDate(Date d) {
219         this.date = d;
220         Calendar cal = new GregorianCalendar();
221         cal.setTime(d);
222         int hour = cal.get(Calendar.HOUR);
223         hoursCombo.setSelectedItem(Integer.toString(hour));
224         int minute = cal.get(Calendar.MINUTE);
225         minutesCombo.setSelectedItem(Integer.toString(minute));
226         int ampm = cal.get(Calendar.AM_PM);
227         if (ampm == Calendar.PM)
228             ampmCombo.setSelectedItem("PM");
229         else
230             ampmCombo.setSelectedItem("AM");
231     }
232
233     /**
234      * get the date from the dialog
235      * @return the Date
236      */

237     public final Date getDate() {
238         return date;
239     }
240
241     /**
242      * set the month's label to display
243      */

244     private final void setMonthLabel() {
245     
246         Date d = currentCal.getTime();
247         SimpleDateFormat formatter = new SimpleDateFormat("MMM yyyy");
248         monthYearLabel.setText(formatter.format(d));
249     }
250
251     /**
252      * update the day button text labels on the screen
253      */

254     private final void updateDayButtons() {
255         boolean thisMonth=false;
256
257         if ((currentCal.get(Calendar.YEAR) == thisMonthCal.get(Calendar.YEAR)) &&
258             (currentCal.get(Calendar.MONTH) == thisMonthCal.get(Calendar.MONTH)))
259             thisMonth=true;
260     
261         currentCal.set(Calendar.DAY_OF_MONTH, 1); //go to first day of month
262
int day_of_week = currentCal.get(Calendar.DAY_OF_WEEK);
263         int startingAt=0;
264
265         //initialize all day buttons to blank
266
JButton dayButton;
267         for (int i=0;i<dayButtons.size();i++) {
268             dayButton = (JButton)dayButtons.get(i);
269             dayButton.setText("");
270         }
271
272         //figure out which day button we start numbering at
273
switch (day_of_week) {
274             case Calendar.MONDAY:
275                 startingAt = 1;
276                 break;
277             case Calendar.TUESDAY:
278                 startingAt = 2;
279                 break;
280             case Calendar.WEDNESDAY:
281                 startingAt = 3;
282                 break;
283             case Calendar.THURSDAY:
284                 startingAt = 4;
285                 break;
286             case Calendar.FRIDAY:
287                 startingAt = 5;
288                 break;
289             case Calendar.SATURDAY:
290                 startingAt = 6;
291                 break;
292             case Calendar.SUNDAY:
293                 startingAt = 0;
294                 break;
295             default:
296                 Exception JavaDoc e = new Exception JavaDoc();
297                 e.printStackTrace();
298                 System.exit(1);
299         }
300
301         //figure out what the last day of the month is
302
int last_day_in_month = currentCal.getActualMaximum(Calendar.DATE);
303         //set the number on each button
304
int dayNumber=1;
305         for (int j=startingAt;j<MAX_BUTTONS;j++) {
306             dayButton = (JButton)(dayButtons.get(j));
307             dayButton.setText(Integer.toString(dayNumber));
308             if (thisMonth) {
309                 if (dayNumber == thisMonthCal.get(Calendar.DATE))
310                     dayButton.setForeground(Color.red);
311             } else
312                 dayButton.setForeground(Color.black);
313             
314             dayNumber++;
315
316             if (j > last_day_in_month) break;
317         }
318
319     }
320
321     class ButtonPressAction implements ActionListener {
322         public void actionPerformed(ActionEvent e) {
323             JButton button = (JButton)(e.getSource());
324
325             currentCal.set(Calendar.DATE, Integer.parseInt(button.getText()));
326             currentCal.set(Calendar.HOUR, Integer.parseInt((String JavaDoc)hoursCombo.getSelectedItem()));
327             currentCal.set(Calendar.MINUTE, Integer.parseInt((String JavaDoc)minutesCombo.getSelectedItem()));
328             String JavaDoc ampm = (String JavaDoc)(ampmCombo.getSelectedItem());
329             if (ampm.equals("PM"))
330                 currentCal.set(Calendar.AM_PM, Calendar.PM);
331             else
332                 currentCal.set(Calendar.AM_PM, Calendar.AM);
333
334
335             //set the global date so listeners can get the selected date
336
date = currentCal.getTime();
337             myEditor.setDate(date);
338             hide();
339         }
340     }
341
342
343     /**
344      * set the screen's colors
345      */

346     public final void setColors() {
347     }
348
349     /**
350      * set the screen's fonts
351      */

352     public final void setFonts() {
353     }
354
355     /**
356      * set the dialog's language
357      */

358     public final void setLang() {
359         setTitle(wb.getLang().getString("calendarSelection"));
360     }
361
362 }
363
Popular Tags