KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Calendar JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import javax.swing.*;
29
30 /**
31  * A calendar view by day
32  */

33 public class DayView extends JScrollPane
34 implements MouseListener
35 {
36     private Calendar JavaDoc calendar;
37     
38     //-- colors
39
private Color unworkedHour;
40     private Color workedHour;
41     private int workStart;
42     private int workEnd;
43     
44     //-- attributes
45
private JPanel contentPanel;
46     private JPanel[] background;
47     private JPanel[] border;
48     private ArrayList JavaDoc[] occupied;
49     
50     //-- listeners
51
private ArrayList JavaDoc listeners;
52     
53     /**
54      * Constructor.
55      * Creates an empty day view
56      */

57     public DayView(Calendar JavaDoc calendar, Color unworkedHour, Color workedHour, int workStart, int workEnd)
58     {
59         //-- init
60
this.listeners = new ArrayList JavaDoc();
61         
62         this.calendar = calendar;
63         
64         this.unworkedHour = unworkedHour;
65         this.workedHour = workedHour;
66         this.workStart = workStart;
67         this.workEnd = workEnd;
68         
69         this.contentPanel = new JPanel(new GridBagLayout());
70         this.setViewportView(contentPanel);
71         this.background = new JPanel[48];
72         this.border = new JPanel[48];
73         this.occupied = new ArrayList JavaDoc[48];
74         for(int i=0;i<occupied.length;i++)
75             occupied[i] = new ArrayList JavaDoc();
76         
77         //speed up scrolling !
78
this.getVerticalScrollBar().setUnitIncrement(15);
79                 
80         GridBagConstraints constraints;
81         
82         //-- hours
83
constraints = new GridBagConstraints();
84         constraints.gridx = 0;
85         constraints.weighty = 1;
86         constraints.fill = GridBagConstraints.BOTH;
87         constraints.gridheight = 2;
88         constraints.ipadx = 20;
89         constraints.ipady = 20;
90         for(int i=0;i<24;i++)
91         {
92             constraints.gridy = i*2;
93             
94             JLabel lbl = new JLabel("" + i);
95             lbl.setBorder(BorderFactory.createLineBorder(Color.BLACK));
96             lbl.setHorizontalAlignment(JLabel.CENTER);
97             contentPanel.add(lbl, constraints);
98         }
99     
100         //-- border
101
constraints = new GridBagConstraints();
102         constraints.gridx = 1;
103         constraints.weighty = 1;
104         constraints.fill = GridBagConstraints.VERTICAL;
105
106         for(int i=0;i<24*2;i++)
107         {
108             constraints.gridy = i;
109             JPanel border = new JPanel();
110             border.setBackground(Color.WHITE);
111             this.border[i] = border;
112
113             contentPanel.add(border, constraints);
114         }
115         
116         //-- content
117
constraints = new GridBagConstraints();
118         constraints.gridx = 2;
119         constraints.gridwidth = 10;
120         constraints.weighty = 1;
121         constraints.weightx = 1;
122         constraints.fill = GridBagConstraints.BOTH;
123         for(int i=0;i<24*2;i++)
124         {
125             constraints.gridy = i;
126             JPanel back = new JPanel();
127             back.setName("" + i);
128             back.addMouseListener(this);
129             this.background[i] = back;
130             back.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
131
132             if(i<workStart*2 || i >= (workEnd+1)*2)
133                 back.setBackground(this.unworkedHour);
134             else
135                 back.setBackground(this.workedHour);
136
137             contentPanel.add(back, constraints);
138         }
139     }
140
141     
142     /**
143      * Set the listeners
144      *
145      * @param listeners the CalendarListeners to set
146      */

147     public void setCalendarListeners(ArrayList JavaDoc listeners)
148     {
149         this.listeners = listeners;
150     }
151     
152     
153     /**
154      * Add a listener
155      *
156      * @param listener the CalendarListener to add
157      */

158     public void addCalendarListener(CalendarListener listener)
159     {
160         listeners.add(listener);
161     }
162     
163     /**
164      * Scroll to an hour
165      * @param hour the hour that should be on top
166      */

167     public void scrollToHour(int hour)
168     {
169         getViewport().setViewPosition(new Point(0, hour*40-20));
170     }
171     
172     /**
173      * Add an event to the view
174      *
175      * @param event the Event to add
176      */

177     public void addEvent(BasicEvent event)
178     {
179         //-- create the label
180
EventLabel label = new EventLabel(event);
181         label.setBackground(Color.WHITE);
182         label.setOpaque(true);
183         label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
184         label.addMouseListener(this);
185         
186         //-- translate some infos
187
int start = event.getStartHour() * 2 + (event.getStartMinute() / 30);
188         int end = event.getEndHour() * 2 + (event.getEndMinute() / 30);
189
190         //handle multi day events
191
if(event.isMultiDay())
192         {
193             Calendar JavaDoc x = Calendar.getInstance();
194             x.setTime(event.getStartDate());
195             if(x.get(Calendar.YEAR) < calendar.get(Calendar.YEAR)
196                     || x.get(Calendar.DAY_OF_YEAR) < calendar.get(Calendar.DAY_OF_YEAR))
197             {
198                 start = 0;
199             }
200
201             x.setTime(event.getEndDate());
202             if(x.get(Calendar.YEAR) > calendar.get(Calendar.YEAR)
203                     || x.get(Calendar.DAY_OF_YEAR) > calendar.get(Calendar.DAY_OF_YEAR))
204             {
205                 end = 48;
206             }
207             
208         }
209         
210         int length = end - start;
211         if(length < 1)
212             length = 1;
213         
214         //-- put the label on the panel
215
GridBagConstraints constraints = new GridBagConstraints();
216         constraints.gridx = getFreeIndexFor(start, length);
217         constraints.gridy = start;
218         constraints.gridheight = length;
219         constraints.weightx = 1;
220         constraints.weighty = 1;
221         constraints.fill = GridBagConstraints.BOTH;
222         constraints.ipadx = 5;
223         constraints.ipady = 5;
224         this.contentPanel.add(label, constraints);
225         
226         //-- update attributes
227
for(int i=0;i<length;i++)
228         {
229             this.border[start + i].setBackground(event.getColor());
230             this.background[start + i].setVisible(false);
231             this.occupied[start + i].add(new Integer JavaDoc(constraints.gridx));
232         }
233     }
234     
235     //-- mouse listener
236

237     public void mousePressed(MouseEvent me) {}
238     public void mouseReleased(MouseEvent me) {}
239     public void mouseEntered(MouseEvent me) {}
240     public void mouseExited(MouseEvent me) {}
241     public void mouseClicked(MouseEvent me)
242     {
243         if(me.getSource() instanceof EventLabel)
244         {
245             EventLabel event = (EventLabel)me.getSource();
246             
247             Iterator JavaDoc i = listeners.iterator();
248             while(i.hasNext())
249             {
250                 CalendarListener listener = (CalendarListener)i.next();
251                 listener.onEventClick(event, me.getClickCount());
252             }
253         }
254         else if(me.getSource() instanceof JPanel)
255         {
256             JPanel item = (JPanel)me.getSource();
257             int index = Integer.parseInt(item.getName());
258             int hour = index/2;
259             int minute = (index%2 == 0 ? 0 : 30);
260             
261             Iterator JavaDoc i = listeners.iterator();
262             while(i.hasNext())
263             {
264                 CalendarListener listener = (CalendarListener)i.next();
265                 listener.onHourClick(hour, minute, me.getClickCount());
266             }
267         }
268     }
269     
270     /**
271      * Get the next free index for the gridbag.
272      * Used to avoid collisions if two events occurs at the same time
273      *
274      * @param start the start index
275      * @param length the event length
276      * @return the next free index
277      */

278     private int getFreeIndexFor(int start, int length)
279     {
280         Integer JavaDoc number = new Integer JavaDoc(2);
281         
282         for(int i=0;i<length;i++)
283         {
284             if(occupied[start+i].contains(number))
285             {
286                 number = new Integer JavaDoc(number.intValue()+1);
287                 i =-1;
288             }
289         }
290         
291         return number.intValue();
292     }
293 }
Popular Tags