KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > demo > CalendarList


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.syncclient.demo;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Button JavaDoc;
23 import java.awt.GridLayout JavaDoc;
24 import java.awt.Label JavaDoc;
25 import java.awt.List JavaDoc;
26 import java.awt.Panel JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.awt.event.ItemEvent JavaDoc;
30 import java.awt.event.ItemListener JavaDoc;
31 import java.awt.event.MouseEvent JavaDoc;
32 import java.awt.event.MouseListener JavaDoc;
33
34 import sync4j.foundation.pdi.common.*;
35
36 /**
37  * The calendar list panel.
38  *
39  * @author Fabio Maggi @ Funambol
40  * @author Marco Magistrali @ Funambol
41  */

42 public class CalendarList
43 extends Panel JavaDoc
44 implements ActionListener JavaDoc,
45            ItemListener JavaDoc,
46            MouseListener JavaDoc,
47            ConfigurationParameters {
48
49     //---------------------------------------------------------- Constants
50

51     //
52
// Approximate width of the vertical
53
// scrollbar for the List component
54
//
55
private static final int LIST_SCROLLBAR_WIDTH = 25;
56
57     //---------------------------------------------------------- Private data
58

59     //
60
// The window containing this panel
61
//
62
private MainWindow mw = null ;
63     private List JavaDoc list = null ;
64     private Button JavaDoc butModify = null ;
65     private Button JavaDoc butDelete = null ;
66     private Button JavaDoc butNew = null ;
67
68     private Language ln = new Language() ;
69
70     //---------------------------------------------------------- Public methods
71

72     /**
73      * Creates the panel.
74      *
75      * @param mw the window containing this panel
76      */

77     public CalendarList(MainWindow mw) {
78
79         Label JavaDoc title = null ;
80         Button JavaDoc butSync = null ;
81
82         Panel JavaDoc editButtonPanel = null ;
83         Panel JavaDoc buttonPanel = null ;
84
85         this.mw = mw;
86
87         setLayout(new BorderLayout JavaDoc());
88         title = new Label JavaDoc(ln.getString("calendar_list"));
89         list = new List JavaDoc (10 );
90         list.addItemListener (this ) ;
91         list.addMouseListener (this ) ;
92
93         butNew = new Button JavaDoc (ln.getString ("new") ) ;
94         butNew.setActionCommand ("new" ) ;
95         butNew.addActionListener (this ) ;
96
97         butModify = new Button JavaDoc (ln.getString ("modify") ) ;
98         butModify.setActionCommand ("modify" ) ;
99         butModify.addActionListener (this ) ;
100         butModify.setEnabled (false ) ;
101
102         butDelete = new Button JavaDoc (ln.getString ("delete") ) ;
103         butDelete.setActionCommand ("delete" ) ;
104         butDelete.addActionListener (this ) ;
105         butDelete.setEnabled (false ) ;
106
107         butSync = new Button JavaDoc(ln.getString ("synchronize") );
108         butSync.setActionCommand ("sync" ) ;
109         butSync.addActionListener (this ) ;
110
111         editButtonPanel = new Panel JavaDoc();
112         editButtonPanel.setLayout(new GridLayout JavaDoc(1,3));
113         editButtonPanel.add (butNew ) ;
114         editButtonPanel.add (butModify ) ;
115         editButtonPanel.add (butDelete ) ;
116
117         buttonPanel = new Panel JavaDoc();
118         buttonPanel.setLayout(new GridLayout JavaDoc(2,1));
119         buttonPanel.add (editButtonPanel ) ;
120         buttonPanel.add (butSync ) ;
121
122         add(title , BorderLayout.NORTH ) ;
123         add(list , BorderLayout.CENTER ) ;
124         add(buttonPanel , BorderLayout.SOUTH ) ;
125     }
126
127     /**
128      * Invoked when an action occurs (i.e. a button is pressed).
129      *
130      * @param evt the occurred action
131      */

132     public void actionPerformed(ActionEvent JavaDoc evt) {
133
134         if (evt.getActionCommand().equals("sync") ) {
135             mw.show(KEY_SYNC);
136         } else if (evt.getActionCommand().equals("modify") ) {
137             mw.show(KEY_CALENDARMODIFY);
138         } else if (evt.getActionCommand().equals("delete") ) {
139             mw.deleteCalendar();
140         } else if (evt.getActionCommand().equals("new") ) {
141             mw.show(KEY_CALENDARNEW);
142         }
143     }
144
145     /**
146      * Invoked when an item has been selected or deselected by the user.
147      * This method sets the current index to the index of
148      * the selected idem and
149      * enables the Modify and Delete buttons.
150      *
151      * @param evt the occurred event
152      */

153     public void itemStateChanged(ItemEvent JavaDoc evt) {
154         mw.setCurrentIndex((Integer JavaDoc) evt.getItem());
155         butModify.setEnabled(true);
156         butDelete.setEnabled(true);
157     }
158
159     /**
160      * Invoked when the mouse button has been clicked
161      * (pressed and released) on a component.
162      * This method opens the modification page
163      * for the selected calendar if a double
164      * click is detected.
165      *
166      * @param evt the occurred event
167      */

168     public void mouseClicked(MouseEvent JavaDoc evt) {
169         if (evt.getClickCount()>=2) {
170             if (butModify.isEnabled() &&
171                 evt.getX()<=(getSize().width - LIST_SCROLLBAR_WIDTH) &&
172                 evt.getY() <= (mw.calendars.size() * list.getPreferredSize().height / 10)) {
173                 mw.show(KEY_CALENDARMODIFY);
174             }
175         }
176     }
177
178     /**
179      * Invoked when a mouse button has been pressed on a component.
180      * Not used.
181      *
182      * @param evt the occurred event
183      */

184     public void mousePressed(MouseEvent JavaDoc evt) {
185         // do nothing
186
}
187
188     /**
189      * Invoked when a mouse button has been released on a component.
190      * Not used.
191      *
192      * @param evt the occurred event
193      */

194     public void mouseReleased(MouseEvent JavaDoc evt) {
195         // do nothing
196
}
197
198     /**
199      * Invoked when the mouse enters a component.
200      * Not used.
201      *
202      * @param evt the occurred event
203      */

204     public void mouseEntered(MouseEvent JavaDoc evt) {
205         // do nothing
206
}
207
208     /**
209      * Invoked when the mouse exits a component.
210      * Not used.
211      *
212      * @param evt the occurred event
213      */

214     public void mouseExited(MouseEvent JavaDoc evt) {
215         // do nothing
216
}
217
218     //---------------------------------------------------------- Protected methods
219

220     /**
221      * Fills the list with the loaded calendars.
222      * This method also disables the Modify and Delete buttons.
223      */

224     protected void fillList() {
225         list.removeAll();
226
227         String JavaDoc tmpDisplayName = null ;
228         String JavaDoc tmpDescription = null ;
229         DemoCalendar tmpCalendar = null ;
230
231         for (int i=0, l = mw.calendars.size(); i < l; i++) {
232             tmpCalendar = (DemoCalendar) mw.calendars.elementAt(i);
233             tmpDisplayName = (String JavaDoc) tmpCalendar.getCalendar().
234                     getEvent().getSummary().getPropertyValue();
235
236             tmpDescription = (String JavaDoc) tmpCalendar.getCalendar().
237                     getEvent().getDescription().getPropertyValue();
238
239             if (tmpDisplayName != null && !tmpDisplayName.equals ("")) {
240                 list.add(tmpDisplayName);
241             } else if (tmpDescription != null && !tmpDescription.equals ("")) {
242                 list.add(tmpDescription);
243             } else {
244
245                 tmpDisplayName = "Unknow";
246                 list.add(tmpDisplayName);
247             }
248         }
249
250         //
251
// Disable buttons: no item can be already selected
252
//
253
butModify.setEnabled(false);
254         butDelete.setEnabled(false);
255     }
256
257     //---------------------------------------------------------- Private methods
258

259 }
Popular Tags