KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.lucane.applications.calendar.gui;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.text.DateFormat JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Calendar JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JLabel JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35
36 import org.lucane.applications.calendar.Attendee;
37 import org.lucane.applications.calendar.CalendarPlugin;
38 import org.lucane.applications.calendar.Event;
39 import org.lucane.applications.calendar.widget.CalendarListener;
40 import org.lucane.applications.calendar.widget.DayItem;
41 import org.lucane.applications.calendar.widget.EventLabel;
42 import org.lucane.applications.calendar.widget.freebusy.FreeBusyPanel;
43 import org.lucane.client.Client;
44 import org.lucane.client.widgets.DialogBox;
45 import org.lucane.client.widgets.ManagedWindow;
46
47 public class FreeBusyDialog extends ManagedWindow
48 implements ActionListener JavaDoc, CalendarListener
49 {
50     private CalendarPlugin plugin;
51     private Event JavaDoc event;
52     private Calendar JavaDoc calendar;
53     private EventDescriptionPanel description;
54     
55     private JButton JavaDoc previousDay;
56     private JButton JavaDoc nextDay;
57     private JLabel JavaDoc day;
58     private FreeBusyPanel freebusy;
59     
60     private int workStart = 8;
61     private int workEnd = 17;
62     private Color JavaDoc unworkedHour = new Color JavaDoc(160, 155, 150);
63     private Color JavaDoc workedHour = new Color JavaDoc(255, 255, 222);
64
65     public FreeBusyDialog(CalendarPlugin plugin, Event JavaDoc event, EventDescriptionPanel description)
66     {
67         super(plugin, plugin.tr("dialog.findFreeTime"));
68         setName("freebusy");
69         this.plugin = plugin;
70         this.event = event;
71         this.description = description;
72         
73         this.calendar = Calendar.getInstance();
74         calendar.setTime(event.getStartDate());
75         calendar.set(Calendar.HOUR_OF_DAY, 0);
76         calendar.set(Calendar.MINUTE, 0);
77         calendar.set(Calendar.SECOND, 0);
78         calendar.set(Calendar.MILLISECOND, 0);
79         
80         this.workStart = plugin.getLocalConfig().getInt("workStart", workStart);
81         this.workEnd = plugin.getLocalConfig().getInt("workEnd", workEnd);
82         this.unworkedHour = plugin.getColor("unworked", unworkedHour);
83         this.workedHour = plugin.getColor("worked", workedHour);
84
85         this.previousDay = new JButton JavaDoc(plugin.getImageIcon("previous.png"));
86         previousDay.addActionListener(this);
87         this.nextDay = new JButton JavaDoc(plugin.getImageIcon("next.png"));
88         nextDay.addActionListener(this);
89         this.day = new JLabel JavaDoc();
90         this.day.setHorizontalAlignment(JLabel.CENTER);
91         
92         JPanel JavaDoc top = new JPanel JavaDoc(new BorderLayout JavaDoc());
93         top.add(previousDay, BorderLayout.WEST);
94         top.add(day, BorderLayout.CENTER);
95         top.add(nextDay, BorderLayout.EAST);
96         getContentPane().add(top, BorderLayout.NORTH);
97         
98         this.freebusy = new FreeBusyPanel(workStart, workEnd, unworkedHour, workedHour);
99         freebusy.addCalendarListener(this);
100         getContentPane().add(freebusy, BorderLayout.CENTER);
101         
102         setPreferredSize(new Dimension JavaDoc(600, 300));
103         
104         refreshView();
105     }
106     
107     private void refreshView()
108     {
109         long startTime = calendar.getTimeInMillis();
110         long endTime = startTime + 24*60*60*1000;
111
112         DateFormat JavaDoc df = DateFormat.getDateInstance(DateFormat.FULL);
113         this.day.setText(df.format(calendar.getTime()));
114
115         freebusy.resetLines();
116         
117         //get events for organizer
118
String JavaDoc userName = Client.getInstance().getMyInfos().getName();
119         try {
120             ArrayList JavaDoc events = plugin.getMyEvents(startTime, endTime);
121             if(event.getId() >= 0)
122                 events.remove(event);
123             freebusy.addLine(Client.getInstance().getMyInfos().getName(), events, true, startTime, endTime);
124         } catch(Exception JavaDoc e) {
125             DialogBox.error(plugin.tr("err.unableToGetEventsForUser") + userName);
126         }
127         
128         //get events for attendees
129
Iterator JavaDoc i = event.getAttendees();
130         while(i.hasNext())
131         {
132             Attendee a = (Attendee)i.next();
133             try {
134                 ArrayList JavaDoc events = plugin.getEventsForUser(a.getUser(), startTime, endTime);
135                 if(event.getId() >= 0)
136                     events.remove(event);
137                 freebusy.addLine(a.getUser(), events, a.isMandatory(), startTime, endTime);
138             } catch(Exception JavaDoc e) {
139                 DialogBox.error(plugin.tr("err.unableToGetEventsForUser") + a.getUser());
140             }
141         }
142         
143         //auto scroll
144
freebusy.scrollToHour(workStart);
145     }
146     
147     public void actionPerformed(ActionEvent JavaDoc ae)
148     {
149         if(ae.getSource() == previousDay)
150             calendar.add(Calendar.DAY_OF_YEAR, -1);
151         else if(ae.getSource() == nextDay)
152             calendar.add(Calendar.DAY_OF_YEAR, 1);
153         
154         refreshView();
155     }
156
157     public void onDayClick(DayItem day, int clickCount) {}
158     public void onEventClick(EventLabel event, int clickCount) {}
159     public void onHourClick(int hour, int minute, int clickCount)
160     {
161         if(minute < 7)
162             minute = 0;
163         else if(minute < 22)
164             minute = 15;
165         else if(minute < 38)
166             minute = 30;
167         else if(minute < 55)
168             minute = 45;
169         else {
170             hour++;
171             minute = 0;
172         }
173             
174         Calendar JavaDoc cal = Calendar.getInstance();
175         cal.setTimeInMillis(calendar.getTimeInMillis());
176         cal.set(Calendar.HOUR_OF_DAY, hour);
177         cal.set(Calendar.MINUTE, minute);
178         Date JavaDoc start = cal.getTime();
179         
180         long duration = event.getEndDate().getTime() - event.getStartDate().getTime();
181         cal.setTimeInMillis(start.getTime() + duration);
182         Date JavaDoc end = cal.getTime();
183         
184         DateFormat JavaDoc df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT);
185         String JavaDoc msg = plugin.tr("msg.changeEventDate");
186         msg += "\n\n";
187         msg += plugin.tr("event.start") + " : \t" + df.format(start);
188         msg += '\n';
189         msg += plugin.tr("event.end") + " : \t" + df.format(end);
190         msg += '\n';
191         
192         if(DialogBox.question(plugin.tr("dialog.changeEventDate"), msg))
193         {
194             event.setStartDate(start);
195             event.setEndDate(end);
196             
197             description.resetDates();
198             dispose();
199         }
200     }
201 }
Popular Tags