KickJava   Java API By Example, From Geeks To Geeks.

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


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.Cursor JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.event.MouseEvent JavaDoc;
27 import java.awt.event.MouseListener JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import javax.swing.JPanel JavaDoc;
32
33 import org.lucane.applications.calendar.widget.CalendarListener;
34
35 public class FreeBusyRuler extends JPanel JavaDoc
36 implements MouseListener JavaDoc
37 {
38     private int workStart;
39     private int workEnd;
40     private Color JavaDoc unworkedHour;
41     private Color JavaDoc workedHour;
42
43     private Font JavaDoc font = new Font JavaDoc("Verdana", Font.PLAIN, 10);
44     private ArrayList JavaDoc listeners = new ArrayList JavaDoc();
45     
46     public FreeBusyRuler(int workStart, int workEnd, Color JavaDoc unworkedHour, Color JavaDoc workedHour)
47     {
48         this.workStart = workStart;
49         this.workEnd = workEnd;
50         this.unworkedHour = unworkedHour;
51         this.workedHour = workedHour;
52
53         setPreferredSize(new Dimension JavaDoc(24*FreeBusyPanel.HOUR_WIDTH, FreeBusyPanel.HEIGHT));
54
55         setOpaque(true);
56         setBackground(Color.WHITE);
57         setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
58         addMouseListener(this);
59     }
60     
61     public void addCalendarListener(CalendarListener listener)
62     {
63         listeners.add(listener);
64     }
65     
66     public void removeCalendarListener(CalendarListener listener)
67     {
68         listeners.remove(listener);
69     }
70     
71     protected void paintComponent(Graphics JavaDoc g) {
72         super.paintComponent(g);
73         
74         //background
75
g.setColor(unworkedHour);
76         g.fillRect(0, 0, 24*FreeBusyPanel.HOUR_WIDTH, FreeBusyPanel.HEIGHT);
77         g.setColor(workedHour);
78         int x = workStart*FreeBusyPanel.HOUR_WIDTH;
79         int w = (workEnd-workStart)*FreeBusyPanel.HOUR_WIDTH;
80         g.fillRect(x, 0, w, FreeBusyPanel.HEIGHT);
81         
82         //timeline
83
g.setColor(Color.BLACK);
84         for(int i=0;i<24*4;i++)
85         {
86             x = i*FreeBusyPanel.HOUR_WIDTH/4;
87             int h = 2;
88             if(i%2 == 0)
89                 h = 3;
90             if(i%4 == 0)
91                 h = 5;
92             g.drawLine(x, FreeBusyPanel.HEIGHT, x, FreeBusyPanel.HEIGHT-h);
93         }
94
95         //work start & end
96
g.setColor(Color.RED);
97         int xStart = workStart*FreeBusyPanel.HOUR_WIDTH;
98         g.drawLine(xStart, 0, xStart, 2);
99         g.drawLine(xStart, FreeBusyPanel.HEIGHT, xStart, FreeBusyPanel.HEIGHT-5);
100         int xEnd = workEnd*FreeBusyPanel.HOUR_WIDTH;
101         g.drawLine(xEnd, 0, xEnd, 2);
102         g.drawLine(xEnd, FreeBusyPanel.HEIGHT, xEnd, FreeBusyPanel.HEIGHT-5);
103
104         //hour labels
105
g.setColor(Color.BLACK);
106         g.setFont(font);
107         for(int i=0;i<24;i++)
108         {
109             x = i*FreeBusyPanel.HOUR_WIDTH;
110             x = i<10 ? x-3 : x-6;
111             g.drawString(String.valueOf(i), x, 14);
112         }
113     }
114
115     public void mouseEntered(MouseEvent JavaDoc e) {}
116     public void mouseExited(MouseEvent JavaDoc e) {}
117     public void mousePressed(MouseEvent JavaDoc e) {}
118     public void mouseReleased(MouseEvent JavaDoc e) {}
119     public void mouseClicked(MouseEvent JavaDoc e)
120     {
121         int x = e.getPoint().x;
122         
123         int hour = x / FreeBusyPanel.HOUR_WIDTH;
124         int minute = ((x % FreeBusyPanel.HOUR_WIDTH) * 60) / FreeBusyPanel.HOUR_WIDTH;
125         
126         fireHourClick(hour, minute, e.getClickCount());
127     }
128     
129     private void fireHourClick(int hour, int minute, int clickCount)
130     {
131         Iterator JavaDoc i = listeners.iterator();
132         while(i.hasNext())
133         {
134             CalendarListener cl = (CalendarListener)i.next();
135             cl.onHourClick(hour, minute, clickCount);
136         }
137     }
138 }
Popular Tags