KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > calendar > widget > MonthView


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2003 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.widget;
21
22 import java.awt.*;
23 import java.awt.event.MouseEvent JavaDoc;
24 import java.awt.event.MouseListener JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Calendar JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import javax.swing.*;
30
31 import org.lucane.applications.calendar.CalendarPlugin;
32 import org.lucane.applications.calendar.util.*;
33
34
35 /**
36  * Month view of a calendar
37  */

38 public class MonthView extends JPanel
39 implements MouseListener JavaDoc
40 {
41     //-- display
42
public JPanel headerPanel;
43     public JPanel contentPanel;
44     
45     //-- listeners
46
private ArrayList JavaDoc listeners;
47     
48     //-- attributes
49
private int displayedMonth;
50     private int displayedYear;
51     
52     private boolean sundayFirst = false;
53
54     /**
55      * Constructor
56      * Create an empty month view and set it to the current month and day
57      */

58     public MonthView(CalendarPlugin plugin)
59     {
60         super(new BorderLayout());
61         
62         this.listeners = new ArrayList JavaDoc();
63         
64         this.headerPanel = new JPanel(new GridLayout(1, 7));
65         this.contentPanel = new JPanel(new GridLayout(6, 7));
66         this.add(headerPanel, BorderLayout.NORTH);
67         this.add(contentPanel, BorderLayout.CENTER);
68         sundayFirst = plugin.getLocalConfig().getInt("sundayFirst", 0) == 1;
69
70         if(sundayFirst)
71             headerPanel.add(new DayHeader(plugin.tr("day.7")));
72         for(int i=1;i<=6;i++)
73             headerPanel.add(new DayHeader(plugin.tr("day."+i)));
74         if(!sundayFirst)
75             headerPanel.add(new DayHeader(plugin.tr("day.7")));
76                 
77         for(int i=0;i<42;i++)
78         {
79             DayItem day = new DayItem(""+i);
80             day.addMouseListener(this);
81             contentPanel.add(day);
82         }
83         
84         Calendar JavaDoc c = Calendar.getInstance();
85         setDisplayedMonth(c.get(Calendar.MONTH)+1, c.get(Calendar.YEAR));
86         setCurrentDay(c.get(Calendar.DAY_OF_MONTH));
87     }
88     
89     /**
90      * Display a month
91      *
92      * @param month the month to display
93      * @param year the year to display
94      */

95     public void setDisplayedMonth(int month, int year)
96     {
97         this.displayedMonth = month;
98         this.displayedYear = year;
99         int curMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
100         int curYear = Calendar.getInstance().get(Calendar.YEAR);
101         
102         int day = this.getFirstDayOfDisplayedMonth();
103         int max = this.getDaysOfDisplayedMonth();
104         for(int i=0;i<day;i++)
105         {
106             DayItem item = getDayItem(i);
107             item.setBackground(Color.LIGHT_GRAY);
108             item.reset("");
109         }
110         for(int i=0;i<max;i++)
111         {
112             DayItem item = getDayItem(i+day);
113             item.setBackground(Color.WHITE);
114             item.reset(""+ (i+1));
115         }
116         for(int i=max+day;i<42;i++)
117         {
118             DayItem item = getDayItem(i);
119             item.setBackground(Color.LIGHT_GRAY);
120             item.reset("");
121         }
122         
123         if(curMonth == month && curYear == year)
124             setCurrentDay(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
125         else
126             setCurrentDay(0);
127     }
128     
129     /**
130      * Add a listener
131      *
132      * @param listener the CalendarListener to add
133      */

134     public void addCalendarListener(CalendarListener listener)
135     {
136         listeners.add(listener);
137     }
138
139     public void addEvent(int dayOfMonth, BasicEvent event, long fakeStart)
140     {
141         long duration = event.getEndDate().getTime() - fakeStart;
142         int dayLength = 24*60*60*1000;
143         int dayMax = getDaysOfDisplayedMonth() + getFirstDayOfDisplayedMonth()-1;
144         for(int i=0;i*dayLength<=duration;i++)
145         {
146             int dayIndex = i + dayOfMonth + getFirstDayOfDisplayedMonth()-1;
147             if(dayIndex > dayMax)
148                 break;
149             
150             getDayItem(dayIndex).addEvent(event, this);
151         }
152     }
153     
154     /**
155      * Highlight the current day
156      *
157      * @param dayOfMonth the day to select
158      */

159     private void setCurrentDay(int dayOfMonth)
160     {
161         for(int i=0;i<42;i++)
162             getDayItem(i).setCurrentDay(false);
163         
164         if(dayOfMonth > 0)
165             getDayItem(dayOfMonth + getFirstDayOfDisplayedMonth()-1).setCurrentDay(true);
166     }
167
168     /**
169      * Get the first day (of week) for the displayed month
170      *
171      * @return the first day of week
172      */

173     private int getFirstDayOfDisplayedMonth()
174     {
175         Calendar JavaDoc c = Calendar.getInstance();
176         c.set(Calendar.MONTH, this.displayedMonth-1);
177         c.set(Calendar.YEAR, this.displayedYear);
178         
179                 return CalendarUtils.getFirstDayOfMonth(c, sundayFirst);
180     }
181     
182     /**
183      * Get the number of days for the displayed month
184      *
185      * @return the number of days
186      */

187     private int getDaysOfDisplayedMonth()
188     {
189         Calendar JavaDoc c = Calendar.getInstance();
190         c.set(Calendar.DAY_OF_MONTH, 1);
191         c.set(Calendar.MONTH, this.displayedMonth-1);
192         c.set(Calendar.YEAR, this.displayedYear);
193         
194         return CalendarUtils.getDaysOfMonth(c);
195     }
196     
197     /**
198      * Get a DayItem associated to its index
199      *
200      * @param index the index (0 - 42)
201      * @return the DayItem
202      */

203     private DayItem getDayItem(int index)
204     {
205         return (DayItem)contentPanel.getComponent(index);
206     }
207         
208     //-- mouse listener implementation
209

210     public void mousePressed(MouseEvent JavaDoc me) {}
211     public void mouseReleased(MouseEvent JavaDoc me) {}
212     public void mouseEntered(MouseEvent JavaDoc me) {}
213     public void mouseExited(MouseEvent JavaDoc me) {}
214     public void mouseClicked(MouseEvent JavaDoc me)
215     {
216         if(me.getSource() instanceof DayItem)
217             doDayClick((DayItem)me.getSource(), me.getClickCount());
218         else if(me.getSource() instanceof EventLabel)
219             doEventClick((EventLabel)me.getSource(), me.getClickCount());
220     }
221     
222     private void doDayClick(DayItem day, int clickCount)
223     {
224         if(day.getDayOfMonth() < 0)
225             return;
226         
227         Iterator JavaDoc i = listeners.iterator();
228         while(i.hasNext())
229         {
230             CalendarListener listener = (CalendarListener)i.next();
231             listener.onDayClick(day, clickCount);
232         }
233     }
234      
235     private void doEventClick(EventLabel event, int clickCount)
236     {
237         Iterator JavaDoc i = listeners.iterator();
238         while(i.hasNext())
239         {
240             CalendarListener listener = (CalendarListener)i.next();
241             listener.onEventClick(event, clickCount);
242         }
243     }
244 }
245
246
247
Popular Tags