KickJava   Java API By Example, From Geeks To Geeks.

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


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.widget;
21
22 import java.awt.*;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Calendar JavaDoc;
25
26 import javax.swing.*;
27
28 import org.lucane.applications.calendar.CalendarPlugin;
29
30
31 /**
32  * Week view of a calendar
33  */

34 public class WeekView extends JPanel
35 {
36     //-- display
37
public JPanel headerPanel;
38     public JPanel contentPanel;
39     private JScrollPane contentScrollPane;
40     
41     //-- listeners
42
private ArrayList JavaDoc listeners;
43     
44     private Color unworkedHour = new Color(160, 155, 150);
45     private Color workedHour = new Color(255, 255, 222);
46     private int workStart = 8;
47     private int workEnd = 17;
48     
49     private boolean sundayFirst = false;
50
51     /**
52      * Constructor
53      * Create an empty wek view and set it to the current month and day
54      */

55     public WeekView(CalendarPlugin plugin)
56     {
57         super(new BorderLayout());
58         
59         this.listeners = new ArrayList JavaDoc();
60         
61         unworkedHour = plugin.getColor("unworked", unworkedHour);
62         workedHour = plugin.getColor("worked", workedHour);
63         workStart = plugin.getLocalConfig().getInt("workStart", workStart);
64         workEnd = plugin.getLocalConfig().getInt("workEnd", workEnd);
65         sundayFirst = plugin.getLocalConfig().getInt("sundayFirst", 0) == 1;
66         
67         this.contentScrollPane = new JScrollPane();
68         this.headerPanel = new JPanel(new GridLayout(1, 7));
69         this.contentPanel = new JPanel(new GridLayout(1, 7)){
70             public Dimension getPreferredSize() {
71                 Dimension base = super.getPreferredSize();
72                 base.width = contentScrollPane.getSize().width;
73                 base.width -= contentScrollPane.getVerticalScrollBar().getSize().width;
74                 return base;
75             }
76         };
77         contentScrollPane.setViewportView(contentPanel);
78         contentScrollPane.setColumnHeaderView(headerPanel);
79         contentScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
80         this.add(contentScrollPane, BorderLayout.CENTER);
81         
82         if(sundayFirst)
83             headerPanel.add(new DayHeader(plugin.tr("day.7")));
84         for(int i=1;i<=6;i++)
85             headerPanel.add(new DayHeader(plugin.tr("day."+i)));
86         if(!sundayFirst)
87             headerPanel.add(new DayHeader(plugin.tr("day.7")));
88                 
89         Calendar JavaDoc c = Calendar.getInstance();
90         setDisplayedWeek(c.get(Calendar.WEEK_OF_YEAR)+1, c.get(Calendar.YEAR));
91     }
92     
93     /**
94      * Display a week
95      *
96      * @param week the week to display
97      * @param year the year to display
98      */

99     public void setDisplayedWeek(int week, int year)
100     {
101         Calendar JavaDoc calendar = Calendar.getInstance();
102         calendar.set(Calendar.YEAR, year);
103         calendar.set(Calendar.WEEK_OF_YEAR, week-1);
104         calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
105
106         contentPanel.removeAll();
107         for(int i=0;i<7;i++)
108         {
109             DayView day = new DayView((Calendar JavaDoc)calendar.clone(), unworkedHour, workedHour, workStart, workEnd);
110             day.setCalendarListeners(listeners);
111             contentPanel.add(day);
112             calendar.add(Calendar.DAY_OF_YEAR, 1);
113         }
114         scrollToHour(workStart-1);
115     }
116     
117     /**
118      * Scroll to an hour
119      * @param hour the hour that should be on top
120      */

121     public void scrollToHour(int hour)
122     {
123         contentScrollPane.getViewport().setViewPosition(new Point(0, hour*40));
124     }
125     
126     /**
127      * Add a listener
128      *
129      * @param listener the CalendarListener to add
130      */

131     public void addCalendarListener(CalendarListener listener)
132     {
133         listeners.add(listener);
134     }
135
136     public void addEvent(int dayOfWeek, BasicEvent event)
137     {
138         if(!sundayFirst)
139             dayOfWeek = (dayOfWeek == 0 || dayOfWeek == 7 )? 6 : dayOfWeek-1;
140         else if(dayOfWeek > 7)
141             dayOfWeek = 0;
142
143         getDayView(dayOfWeek).addEvent(event);
144     }
145     
146     /**
147      * Get a DayView associated to its index
148      *
149      * @param index the index (0 - 7)
150      * @return the DayView
151      */

152     private DayView getDayView(int index)
153     {
154         return (DayView)contentPanel.getComponent(index);
155     }
156 }
157
158
159
Popular Tags