KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.lucane.applications.calendar.pdf.WeekExporter;
32
33 public class WeekPanel extends JPanel
34 implements ActionListener
35 {
36     private WeekView view;
37     
38     private JButton previousYear;
39     private JButton previousWeek;
40     
41     private JComboBox week;
42     private JComboBox year;
43     
44     private JButton nextYear;
45     private JButton nextWeek;
46
47     private JButton pdfExport;
48
49     private Calendar calendar;
50     private CalendarListener listener;
51     private String JavaDoc userName;
52     
53     private transient CalendarPlugin plugin;
54     
55     public WeekPanel(CalendarPlugin plugin, CalendarListener listener, String JavaDoc userName)
56     {
57         super(new BorderLayout());
58         this.plugin = plugin;
59         
60         this.listener = listener;
61         this.userName = userName;
62         
63         calendar = Calendar.getInstance();
64         
65         previousYear = new JButton(plugin.getImageIcon("pprevious.png"));
66         previousWeek = new JButton(plugin.getImageIcon("previous.png"));
67         nextYear = new JButton(plugin.getImageIcon("nnext.png"));
68         nextWeek = new JButton(plugin.getImageIcon("next.png"));
69         pdfExport = new JButton(tr("pdf.export"), plugin.getImageIcon("pdf.png"));
70
71         previousYear.addActionListener(this);
72         previousWeek.addActionListener(this);
73         nextYear.addActionListener(this);
74         nextWeek.addActionListener(this);
75         pdfExport.addActionListener(this);
76
77         week = new JComboBox();
78         year = new JComboBox();
79         
80         view = new WeekView(plugin);
81         view.addCalendarListener(listener);
82         
83         JPanel topbar = new JPanel(new BorderLayout());
84         initTopBar(topbar);
85         
86         
87         add(topbar, BorderLayout.NORTH);
88         add(view, BorderLayout.CENTER);
89         
90         refreshView();
91     }
92     
93
94     private void initCombos()
95     {
96         int weekIndex = calendar.get(Calendar.WEEK_OF_YEAR)-1;
97         week.removeActionListener(this);
98         week.removeAllItems();
99         for(int i=1;i<=52;i++)
100             week.addItem(new Integer JavaDoc(i));
101         week.setSelectedIndex(weekIndex);
102         week.addActionListener(this);
103         
104         if(weekIndex == 0)
105             calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
106         else if(weekIndex == 51)
107             calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
108         
109         int currentYear = calendar.get(Calendar.YEAR);
110         year.removeActionListener(this);
111         year.removeAllItems();
112         for(int i=-3;i<4;i++)
113             year.addItem(new Integer JavaDoc(currentYear+i));
114         year.setSelectedIndex(3);
115         year.addActionListener(this);
116     }
117     
118     private void initTopBar(JPanel bar)
119     {
120         JPanel previous = new JPanel(new GridLayout(1, 2));
121         previous.add(previousYear);
122         previous.add(previousWeek);
123         
124         JPanel middle = new JPanel(new BorderLayout());
125         middle.add(week, BorderLayout.CENTER);
126         middle.add(year, BorderLayout.EAST);
127         
128         JPanel next = new JPanel(new GridLayout(1, 2));
129         next.add(nextWeek);
130         next.add(nextYear);
131         next.add(pdfExport);
132
133         bar.add(previous, BorderLayout.WEST);
134         bar.add(middle, BorderLayout.CENTER);
135         bar.add(next, BorderLayout.EAST);
136         bar.setBorder(BorderFactory.createEmptyBorder(2, 1, 3, 1));
137     }
138     
139     public void actionPerformed(ActionEvent ae)
140     {
141         //-- buttons
142
if(ae.getSource() == previousYear)
143             calendar.add(Calendar.YEAR, -1);
144         else if(ae.getSource() == previousWeek)
145             calendar.add(Calendar.WEEK_OF_YEAR, -1);
146         else if(ae.getSource() == nextYear)
147             calendar.add(Calendar.YEAR, 1);
148         else if(ae.getSource() == nextWeek)
149             calendar.add(Calendar.WEEK_OF_YEAR, 1);
150         else if(ae.getSource() == pdfExport)
151         {
152             JFileChooser fc = new JFileChooser();
153             int returnVal = fc.showSaveDialog(null);
154
155             if(returnVal == JFileChooser.APPROVE_OPTION)
156             {
157                 WeekExporter exporter = new WeekExporter(plugin, this.calendar, userName);
158                 try {
159                     exporter.exportWeek(fc.getSelectedFile().getAbsolutePath());
160                     DialogBox.info(tr("pdf.generated"));
161                 } catch (Exception JavaDoc e) {
162                     DialogBox.error(tr("err.pdf.generation"));
163                     e.printStackTrace();
164                 }
165             }
166         }
167
168         //-- combos
169
if(ae.getSource() == week)
170             calendar.set(Calendar.WEEK_OF_YEAR, ((Integer JavaDoc)week.getSelectedItem()).intValue());
171         else if(ae.getSource() == year)
172             calendar.set(Calendar.YEAR, ((Integer JavaDoc)year.getSelectedItem()).intValue());
173         
174         refreshView();
175     }
176     
177     public void refreshView()
178     {
179         calendar.set(Calendar.HOUR_OF_DAY, 0);
180         calendar.set(Calendar.MINUTE, 0);
181         initCombos();
182         view.setDisplayedWeek(calendar.get(Calendar.WEEK_OF_YEAR)+1, calendar.get(Calendar.YEAR));
183         
184         //-- get month interval (in milliseconds)
185
long start, end;
186         Calendar cal = Calendar.getInstance();
187         cal.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
188         cal.set(Calendar.WEEK_OF_YEAR, calendar.get(Calendar.WEEK_OF_YEAR));
189         cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
190         cal.set(Calendar.HOUR_OF_DAY, 0);
191         cal.set(Calendar.MINUTE, 0);
192         start = cal.getTimeInMillis();
193         cal.add(Calendar.DAY_OF_YEAR, 7);
194         end = cal.getTimeInMillis();
195         
196         //-- fetch and display events
197
try {
198             //if no user, show my events
199
ArrayList events;
200             if(this.userName == null)
201                 events = plugin.getMyEvents(start, end);
202             else
203                 events = plugin.getEventsForUser(this.userName, start, end);
204             
205             Calendar startC = Calendar.getInstance();
206             Calendar endC = Calendar.getInstance();
207             Iterator i = events.iterator();
208             
209             while(i.hasNext())
210             {
211                 Event e = (Event)i.next();
212                 startC.setTime(e.getStartDate());
213                 endC.setTime(e.getEndDate());
214                 
215                 calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
216                 calendar.set(Calendar.HOUR_OF_DAY, 0);
217                 calendar.set(Calendar.MINUTE, 0);
218                 Calendar dayStart = Calendar.getInstance();
219                 Calendar dayEnd = Calendar.getInstance();
220                 dayStart.setTime(calendar.getTime());
221                 dayEnd.setTime(calendar.getTime());
222                 dayEnd.add(Calendar.DAY_OF_YEAR, 1);
223                 
224                 for(int d=1;d<=7;d++)
225                 {
226                     if(startC.after(dayStart) && startC.before(dayEnd))
227                         view.addEvent(d, e);
228                     else if(endC.after(dayStart) && endC.before(dayEnd))
229                         view.addEvent(d, e);
230                     else if(startC.before(dayStart) && endC.after(dayEnd))
231                         view.addEvent(d, e);
232                     
233                     dayStart.add(Calendar.DAY_OF_YEAR, 1);
234                     dayEnd.add(Calendar.DAY_OF_YEAR, 1);
235                 }
236             }
237         } catch(Exception JavaDoc e) {
238             DialogBox.error(e.getMessage());
239             e.printStackTrace();
240         }
241         
242         validate();
243     }
244     
245     public void showWeek(Calendar c)
246     {
247         this.calendar = c;
248         refreshView();
249     }
250     
251     public Calendar getCalendar()
252     {
253         return (Calendar)calendar.clone();
254     }
255     
256     private String JavaDoc tr(String JavaDoc s)
257     {
258         return plugin.tr(s);
259     }
260 }
Popular Tags