KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > calendar > gui > DayPanel


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package org.lucane.applications.calendar.gui;
21
22 import javax.swing.*;
23 import java.awt.*;
24 import java.awt.event.*;
25 import java.util.*;
26
27 import org.lucane.client.widgets.DialogBox;
28 import org.lucane.applications.calendar.CalendarPlugin;
29 import org.lucane.applications.calendar.widget.*;
30 import org.lucane.applications.calendar.Event;
31
32
33 public class DayPanel extends JPanel
34 implements ActionListener
35 {
36     private DayView view;
37     
38     private JButton previousMonth;
39     private JButton previousDay;
40     
41     private JComboBox day;
42     private JComboBox month;
43     private JComboBox year;
44     
45     private JButton nextMonth;
46     private JButton nextDay;
47     
48     private Color unworkedHour = new Color(160, 155, 150);
49     private Color workedHour = new Color(255, 255, 222);
50     private int workStart = 8;
51     private int workEnd = 17;
52     
53     private Calendar calendar;
54     private transient CalendarPlugin plugin;
55     private CalendarListener listener;
56     
57     private String JavaDoc userName;
58     
59     
60     public DayPanel(CalendarPlugin plugin, CalendarListener listener, String JavaDoc userName)
61     {
62         super(new BorderLayout());
63         this.plugin = plugin;
64         this.listener = listener;
65         this.userName = userName;
66         
67         unworkedHour = plugin.getColor("unworked", unworkedHour);
68         workedHour = plugin.getColor("worked", workedHour);
69         workStart = plugin.getLocalConfig().getInt("workStart", workStart);
70         workEnd = plugin.getLocalConfig().getInt("workEnd", workEnd);
71         
72         previousMonth = new JButton(plugin.getImageIcon("pprevious.png"));
73         previousDay = new JButton(plugin.getImageIcon("previous.png"));
74         nextMonth = new JButton(plugin.getImageIcon("nnext.png"));
75         nextDay = new JButton(plugin.getImageIcon("next.png"));
76         
77         previousMonth.addActionListener(this);
78         previousDay.addActionListener(this);
79         nextMonth.addActionListener(this);
80         nextDay.addActionListener(this);
81         
82         day = new JComboBox();
83         month = new JComboBox();
84         year = new JComboBox();
85                 
86         JPanel topbar = new JPanel(new BorderLayout());
87         initTopBar(topbar);
88         
89         
90         add(topbar, BorderLayout.NORTH);
91         
92         calendar = Calendar.getInstance();
93         refreshView();
94     }
95     
96     private void initCombos()
97     {
98         int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
99         int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
100         day.removeActionListener(this);
101         day.removeAllItems();
102         for(int i=1;i<=maxDays;i++)
103             day.addItem(new Integer JavaDoc(i));
104         day.setSelectedIndex(currentDay-1);
105         day.addActionListener(this);
106         
107         int monthIndex = calendar.get(Calendar.MONTH);
108         month.removeActionListener(this);
109         month.removeAllItems();
110         for(int i=1;i<=12;i++)
111             month.addItem(tr("month." + i));
112
113         month.setSelectedIndex(monthIndex);
114         month.addActionListener(this);
115         
116         int currentYear = calendar.get(Calendar.YEAR);
117         year.removeActionListener(this);
118         year.removeAllItems();
119         for(int i=-3;i<4;i++)
120             year.addItem(new Integer JavaDoc(currentYear+i));
121         year.setSelectedIndex(3);
122         year.addActionListener(this);
123     }
124     
125     private void initTopBar(JPanel bar)
126     {
127         JPanel previous = new JPanel(new GridLayout(1, 2));
128         previous.add(previousMonth);
129         previous.add(previousDay);
130
131         
132         JPanel middle = new JPanel(new BorderLayout());
133         middle.add(day, BorderLayout.WEST);
134         middle.add(month, BorderLayout.CENTER);
135         middle.add(year, BorderLayout.EAST);
136         
137         JPanel next = new JPanel(new GridLayout(1, 2));
138         next.add(nextDay);
139         next.add(nextMonth);
140         
141         bar.add(previous, BorderLayout.WEST);
142         bar.add(middle, BorderLayout.CENTER);
143         bar.add(next, BorderLayout.EAST);
144         bar.setBorder(BorderFactory.createEmptyBorder(2, 1, 3, 1));
145     }
146     
147     public void actionPerformed(ActionEvent ae)
148     {
149         //-- buttons
150
if(ae.getSource() == previousMonth)
151             setPreviousMonth();
152         else if(ae.getSource() == previousDay)
153         {
154             int prevDay = calendar.get(Calendar.DAY_OF_MONTH) -1;
155             if(prevDay < 0)
156             {
157                 setPreviousMonth();
158                 prevDay = calendar.getMaximum(Calendar.DAY_OF_MONTH);
159             }
160             calendar.set(Calendar.DAY_OF_MONTH, prevDay);
161         }
162         else if(ae.getSource() == nextMonth)
163             setNextMonth();
164         else if(ae.getSource() == nextDay)
165         {
166             int nextDay = calendar.get(Calendar.DAY_OF_MONTH) +1;
167             if(nextDay > calendar.getMaximum(Calendar.DAY_OF_MONTH))
168             {
169                 setNextMonth();
170                 nextDay = 1;
171             }
172             calendar.set(Calendar.DAY_OF_MONTH, nextDay);
173         }
174             
175         //-- combos
176
else if(ae.getSource() == day)
177             calendar.set(Calendar.DAY_OF_MONTH, day.getSelectedIndex()+1);
178         else if(ae.getSource() == month)
179             calendar.set(Calendar.MONTH, month.getSelectedIndex());
180         else if(ae.getSource() == year)
181             calendar.set(Calendar.YEAR, ((Integer JavaDoc)year.getSelectedItem()).intValue());
182         
183         refreshView();
184     }
185     
186     private void setPreviousMonth()
187     {
188         int prevMonth = calendar.get(Calendar.MONTH) -1;
189         if(prevMonth < 0)
190         {
191             prevMonth = 11;
192             calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR)-1);
193         }
194         calendar.set(Calendar.MONTH, prevMonth);
195     }
196     
197     private void setNextMonth()
198     {
199         int nextMonth = calendar.get(Calendar.MONTH) +1;
200         if(nextMonth > 11)
201         {
202             nextMonth = 0;
203             calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR)+1);
204         }
205         calendar.set(Calendar.MONTH, nextMonth);
206     }
207     
208     public void refreshView()
209     {
210         initCombos();
211         
212         if(view != null)
213             remove(view);
214         view = new DayView(calendar, unworkedHour, workedHour, workStart, workEnd);
215         view.addCalendarListener(listener);
216         view.scrollToHour(workStart-1);
217
218         //-- get day interval (in milliseconds)
219
long start, end;
220         Calendar cal = Calendar.getInstance();
221         cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
222         cal.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
223         cal.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
224         cal.set(Calendar.HOUR_OF_DAY, 0);
225         cal.set(Calendar.MINUTE, 0);
226         start = cal.getTimeInMillis();
227         cal.add(Calendar.HOUR_OF_DAY, 24);
228         end = cal.getTimeInMillis();
229         
230         //-- fetch and display events
231
try {
232             //if no user, show my events
233
ArrayList events;
234             if(this.userName == null)
235                 events = plugin.getMyEvents(start, end);
236             else
237                 events = plugin.getEventsForUser(this.userName, start, end);
238             
239             Iterator i = events.iterator();
240             while(i.hasNext())
241             {
242                 Event e = (Event)i.next();
243                 view.addEvent(e);
244             }
245         } catch(Exception JavaDoc e) {
246             DialogBox.error(e.getMessage());
247             e.printStackTrace();
248         }
249         
250         add(view, BorderLayout.CENTER);
251     }
252     
253     public void showDay(Calendar c)
254     {
255         this.calendar = c;
256         refreshView();
257     }
258     
259     public Calendar getCalendar()
260     {
261         return (Calendar)calendar.clone();
262     }
263     
264     private String JavaDoc tr(String JavaDoc s)
265     {
266         return plugin.tr(s);
267     }
268 }
Popular Tags