KickJava   Java API By Example, From Geeks To Geeks.

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


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