KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > calendar > widget > freebusy > FreeBusyItem


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 package org.lucane.applications.calendar.widget.freebusy;
20
21 import java.awt.Color JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.Graphics JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.swing.JPanel JavaDoc;
28
29 import org.lucane.applications.calendar.widget.BasicEvent;
30
31 public class FreeBusyItem extends JPanel JavaDoc
32 {
33     private ArrayList JavaDoc events;
34     private int workStart;
35     private int workEnd;
36     private long startTime;
37     private long endTime;
38
39     public FreeBusyItem(ArrayList JavaDoc events, int workStart, int workEnd, long startTime, long endTime)
40     {
41         this.events = events;
42         this.workStart = workStart;
43         this.workEnd = workEnd;
44         this.startTime = startTime;
45         this.endTime = endTime;
46         
47         setPreferredSize(new Dimension JavaDoc(24*FreeBusyPanel.HOUR_WIDTH, FreeBusyPanel.HEIGHT));
48
49         setOpaque(true);
50         setBackground(Color.WHITE);
51     }
52     
53     protected void paintComponent(Graphics JavaDoc g)
54     {
55         super.paintComponent(g);
56         
57         //hour separators
58
g.setColor(Color.LIGHT_GRAY);
59         for(int i=0;i<24;i++)
60         {
61             int x = i*FreeBusyPanel.HOUR_WIDTH;
62             g.drawLine(x, 0, x, FreeBusyPanel.HEIGHT);
63         }
64         
65         //work start & end
66
g.setColor(Color.RED);
67         int xStart = workStart*FreeBusyPanel.HOUR_WIDTH;
68         g.drawLine(xStart, 0, xStart, FreeBusyPanel.HEIGHT);
69         int xEnd = workEnd*FreeBusyPanel.HOUR_WIDTH;
70         g.drawLine(xEnd, 0, xEnd, FreeBusyPanel.HEIGHT);
71         
72         //events
73
Iterator JavaDoc events = this.events.iterator();
74         while(events.hasNext())
75             paintEvent(g, (BasicEvent)events.next());
76     }
77     
78     private void paintEvent(Graphics JavaDoc g, BasicEvent e)
79     {
80         int hour = e.getStartHour();
81         int minute = e.getStartMinute();
82         int start = hour*FreeBusyPanel.HOUR_WIDTH + minute*FreeBusyPanel.HOUR_WIDTH/60;
83         if(e.getStartDate().getTime() < startTime)
84             start = 0;
85                 
86         hour = e.getEndHour();
87         minute = e.getEndMinute();
88         int end = hour*FreeBusyPanel.HOUR_WIDTH + minute*FreeBusyPanel.HOUR_WIDTH/60;
89         if(e.getEndDate().getTime() > endTime)
90             end = FreeBusyPanel.HOUR_WIDTH*24;
91         
92         g.setColor(e.getColor());
93         g.fillRect(start, 0, end-start, FreeBusyPanel.HEIGHT);
94     }
95 }
Popular Tags