KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > calendar > gui > CalendarFrame


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.gui;
21
22 import javax.swing.*;
23
24 import org.lucane.client.Client;
25 import org.lucane.client.widgets.DialogBox;
26 import org.lucane.client.widgets.ManagedWindow;
27 import org.lucane.client.widgets.UserSelector;
28 import org.lucane.common.concepts.UserConcept;
29 import org.lucane.applications.calendar.CalendarPlugin;
30 import org.lucane.applications.calendar.Event;
31 import org.lucane.applications.calendar.widget.*;
32
33 import java.awt.*;
34 import java.awt.event.*;
35 import java.util.*;
36
37 public class CalendarFrame extends ManagedWindow
38 implements ActionListener, CalendarListener
39 {
40     private MonthPanel monthPanel;
41     private WeekPanel weekPanel;
42     private DayPanel dayPanel;
43     
44     private JButton newEvent;
45     private JButton goToCurrentMonth;
46     private JButton goToCurrentWeek;
47     private JButton goToCurrentDay;
48     private JButton otherCalendars;
49     private JButton close;
50     
51     private transient CalendarPlugin plugin;
52     
53     public CalendarFrame(CalendarPlugin plugin)
54     {
55         super(plugin, plugin.getTitle());
56         getContentPane().setLayout(new BorderLayout());
57         this.plugin = plugin;
58         
59         monthPanel = new MonthPanel(plugin, this, null);
60         dayPanel = new DayPanel(plugin, this, null);
61         weekPanel = new WeekPanel(plugin, this, null);
62     
63         newEvent = new JButton(tr("btn.newEvent"));
64         goToCurrentMonth = new JButton(tr("btn.thisMonth"));
65         goToCurrentWeek = new JButton(tr("btn.thisWeek"));
66         goToCurrentDay = new JButton(tr("btn.today"));
67         otherCalendars = new JButton(tr("btn.otherCalendars"));
68         close = new JButton(tr("btn.close"));
69         
70         newEvent.setIcon(plugin.getImageIcon("new.png"));
71         goToCurrentMonth.setIcon(plugin.getImageIcon("jumpTo.png"));
72         goToCurrentWeek.setIcon(plugin.getImageIcon("jumpTo.png"));
73         goToCurrentDay.setIcon(plugin.getImageIcon("jumpTo.png"));
74         otherCalendars.setIcon(plugin.getImageIcon("other.png"));
75         close.setIcon(plugin.getImageIcon("close.png"));
76
77         newEvent.addActionListener(this);
78         goToCurrentMonth.addActionListener(this);
79         goToCurrentWeek.addActionListener(this);
80         goToCurrentDay.addActionListener(this);
81         otherCalendars.addActionListener(this);
82         close.addActionListener(this);
83
84         JPanel topbar = new JPanel(new BorderLayout());
85         initTopBar(topbar);
86         
87         getContentPane().add(topbar, BorderLayout.NORTH);
88         getContentPane().add(monthPanel, BorderLayout.CENTER);
89     }
90     
91     private void initTopBar(JPanel bar)
92     {
93         JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT, 1, 1));
94         
95         buttons.add(newEvent);
96         buttons.add(goToCurrentMonth);
97         buttons.add(goToCurrentWeek);
98         buttons.add(goToCurrentDay);
99         buttons.add(otherCalendars);
100         buttons.add(close);
101         
102         bar.add(buttons, BorderLayout.EAST);
103         bar.setBorder(BorderFactory.createEmptyBorder(2, 15, 5, 1));
104     }
105
106     public void actionPerformed(ActionEvent ae)
107     {
108         if(ae.getSource() == newEvent)
109         {
110             //TODO create a better default event
111
long time = Calendar.getInstance().getTimeInMillis();
112             Event e = new Event(-1, "", tr("event.newEvent"), Client.getInstance().getMyInfos().getName(),
113                     true, false, time, time+3600*1000, Event.RECUR_NONE, "");
114             
115             new EventFrame(plugin, e, dayPanel, weekPanel, monthPanel).show();
116         }
117         else if(ae.getSource() == goToCurrentMonth)
118         {
119             monthPanel.showMonth(Calendar.getInstance());
120             if(getContentPane().getComponent(1) != monthPanel)
121             {
122                 getContentPane().remove(dayPanel);
123                 getContentPane().remove(weekPanel);
124                 getContentPane().add(monthPanel, BorderLayout.CENTER);
125                 getContentPane().validate();
126                 getContentPane().repaint();
127             }
128         }
129         else if(ae.getSource() == goToCurrentWeek)
130         {
131             monthPanel.showMonth(Calendar.getInstance());
132             if(getContentPane().getComponent(1) != weekPanel)
133             {
134                 getContentPane().remove(monthPanel);
135                 getContentPane().remove(dayPanel);
136                 getContentPane().add(weekPanel, BorderLayout.CENTER);
137                 getContentPane().validate();
138                 getContentPane().repaint();
139             }
140         }
141         else if(ae.getSource() == goToCurrentDay)
142         {
143             dayPanel.showDay(Calendar.getInstance());
144             if(getContentPane().getComponent(1) != dayPanel)
145             {
146                 getContentPane().remove(monthPanel);
147                 getContentPane().remove(weekPanel);
148                 getContentPane().add(dayPanel, BorderLayout.CENTER);
149                 getContentPane().validate();
150                 getContentPane().repaint();
151             }
152         }
153         else if(ae.getSource() == otherCalendars)
154         {
155             try {
156                 ArrayList groups = plugin.getGroups();
157                 UserSelector select = new UserSelector(null, tr("msg.selectUser"));
158                 select.setRecursive(true);
159                 select.setGroups(groups);
160
161                 UserConcept user = (UserConcept)select.selectItem();
162                 if(user == null)
163                     return;
164                 
165                 CalendarViewer viewer = new CalendarViewer(plugin, user.getName());
166                 viewer.setPreferredSize(new Dimension(780, 550));
167                 viewer.show();
168             } catch (Exception JavaDoc e) {
169                 DialogBox.error(tr("err.unableToFetchUserList"));
170                 e.printStackTrace();
171             }
172
173         }
174         else if(ae.getSource() == close)
175         {
176             this.dispose();
177             plugin.exit();
178         }
179     }
180     
181     public void onDayClick(DayItem day, int clickCount)
182     {
183         int dayOfMonth = day.getDayOfMonth();
184         Calendar cal = monthPanel.getCalendar();
185         cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
186
187         if(clickCount == 1)
188         {
189             dayPanel.showDay(cal);
190             getContentPane().remove(monthPanel);
191             getContentPane().remove(weekPanel);
192             getContentPane().add(dayPanel, BorderLayout.CENTER);
193             getContentPane().validate();
194             getContentPane().repaint();
195         }
196     }
197
198     public void onHourClick(int hour, int minute, int clickCount)
199     {
200         Calendar cal = dayPanel.getCalendar();
201         cal.set(Calendar.HOUR_OF_DAY, hour);
202         cal.set(Calendar.MINUTE, minute);
203
204         if(clickCount == 2)
205         {
206             long time = cal.getTimeInMillis();
207             Event e = new Event(-1, "", tr("event.newEvent"), Client.getInstance().getMyInfos().getName(),
208                     true, false, time, time+3600*1000, Event.RECUR_NONE, "");
209             
210             new EventFrame(plugin, e, dayPanel, weekPanel, monthPanel).show();
211         }
212     }
213     
214     public void onEventClick(EventLabel event, int clickCount)
215     {
216         if(clickCount == 1)
217             new EventFrame(plugin, (Event)event.getEvent(), dayPanel, weekPanel, monthPanel).show();
218     }
219     
220     private String JavaDoc tr(String JavaDoc s)
221     {
222         return plugin.tr(s);
223     }
224 }
Popular Tags