KickJava   Java API By Example, From Geeks To Geeks.

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


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.CardLayout JavaDoc;
23 import java.awt.GridLayout JavaDoc;
24 import java.awt.Label JavaDoc;
25 import java.awt.Panel JavaDoc;
26 import java.awt.ScrollPane JavaDoc;
27 import java.awt.TextField JavaDoc;
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.awt.event.ActionListener JavaDoc;
30 import java.awt.event.MouseEvent JavaDoc;
31 import java.awt.event.MouseListener JavaDoc;
32
33 import sync4j.foundation.pdi.event.Calendar;
34
35 /**
36  * A form containing all the fields concerning a calendar.
37  * This form will be a subpanel for CalendarModify and CalendarNew
38  *
39  * @author Fabio Maggi @ Funambol
40  * @author Marco Magistrali @ Funambol
41  */

42 public class CalendarForm extends Panel JavaDoc
43 implements ActionListener JavaDoc, MouseListener JavaDoc, ConfigurationParameters {
44     //---------------------------------------------------------- Constants
45

46     //
47
// Note this is NOT the actual size of the TextField.
48
// It's just a way to avoid horizontal scrolling
49
//
50
public static final int TF_COLUMNS = 10;
51
52     //---------------------------------------------------------- Private data
53

54     //
55
// The window containing this panel
56
//
57
private MainWindow mw = null;
58
59     private Language ln = new Language();
60     
61     private TextField JavaDoc tf1 = null;
62     private TextField JavaDoc tf2 = null;
63     private TextField JavaDoc tf3 = null;
64     private TextField JavaDoc tf4 = null;
65     private TextField JavaDoc tf5 = null;
66     private TextField JavaDoc tf6 = null;
67     private TextField JavaDoc tf7 = null;
68
69     private CardLayout JavaDoc cardLayout = null;
70     private Panel JavaDoc fieldPanel = null;
71     private ScrollPane JavaDoc scrollGeneralPane = null;
72
73     //---------------------------------------------------------- Public methods
74
public String JavaDoc getStartDate() {
75         return tf2.getText();
76     }
77     public String JavaDoc getStartTime() {
78         return tf6.getText();
79     }
80     public String JavaDoc getEndDate() {
81         return tf3.getText();
82     }
83     public String JavaDoc getEndTime() {
84         return tf7.getText();
85     }
86
87     /**
88      * Creates the form subpanel.
89      *
90      * @param mw the window containing this panel
91      */

92     public CalendarForm(MainWindow mw) {
93
94         this.mw = mw;
95         
96         Panel JavaDoc generalPanel = new Panel JavaDoc();
97         generalPanel.setLayout(new GridLayout JavaDoc(16, 2));
98
99         //
100
// Subject
101
//
102
Label JavaDoc lb1 = new Label JavaDoc(ln.getString("summary" ));
103         Label JavaDoc lb2 = new Label JavaDoc(ln.getString("start_date"));
104         Label JavaDoc lb3 = new Label JavaDoc(ln.getString("end_date" ));
105         Label JavaDoc lb4 = new Label JavaDoc(ln.getString("location" ));
106         
107         //
108
// body
109
//
110
Label JavaDoc lb5 = new Label JavaDoc(ln.getString("description"));
111         Label JavaDoc lb6 = new Label JavaDoc(ln.getString("start_time" ));
112         Label JavaDoc lb7 = new Label JavaDoc(ln.getString("end_time" ));
113
114         tf1 = new TextField JavaDoc(TF_COLUMNS);
115         tf2 = new TextField JavaDoc(TF_COLUMNS);
116         tf3 = new TextField JavaDoc(TF_COLUMNS);
117         tf4 = new TextField JavaDoc(TF_COLUMNS);
118         tf5 = new TextField JavaDoc(TF_COLUMNS);
119         tf6 = new TextField JavaDoc(TF_COLUMNS);
120         tf7 = new TextField JavaDoc(TF_COLUMNS);
121
122         tf1.addMouseListener(this);
123         tf2.addMouseListener(this);
124         tf3.addMouseListener(this);
125         tf4.addMouseListener(this);
126         tf5.addMouseListener(this);
127         tf6.addMouseListener(this);
128         tf7.addMouseListener(this);
129
130         generalPanel.add(lb1 );
131         generalPanel.add(tf1 );
132         generalPanel.add(lb2 );
133         generalPanel.add(tf2 );
134         generalPanel.add(lb6 );
135         generalPanel.add(tf6 );
136         generalPanel.add(lb3 );
137         generalPanel.add(tf3 );
138         generalPanel.add(lb7 );
139         generalPanel.add(tf7 );
140         generalPanel.add(lb4 );
141         generalPanel.add(tf4 );
142         generalPanel.add(lb5 );
143         generalPanel.add(tf5 );
144         generalPanel.add(new Label JavaDoc());
145         generalPanel.add(new Label JavaDoc());
146         generalPanel.add(new Label JavaDoc());
147         generalPanel.add(new Label JavaDoc());
148         generalPanel.add(new Label JavaDoc());
149         generalPanel.add(new Label JavaDoc());
150         generalPanel.add(new Label JavaDoc());
151         generalPanel.add(new Label JavaDoc());
152         generalPanel.add(new Label JavaDoc());
153         generalPanel.add(new Label JavaDoc());
154         generalPanel.add(new Label JavaDoc());
155         generalPanel.add(new Label JavaDoc());
156         generalPanel.add(new Label JavaDoc());
157         generalPanel.add(new Label JavaDoc());
158         generalPanel.add(new Label JavaDoc());
159         generalPanel.add(new Label JavaDoc());
160         generalPanel.add(new Label JavaDoc());
161         generalPanel.add(new Label JavaDoc());
162
163         scrollGeneralPane = new ScrollPane JavaDoc(ScrollPane.SCROLLBARS_AS_NEEDED);
164         scrollGeneralPane.add(generalPanel);
165
166         fieldPanel = new Panel JavaDoc();
167         fieldPanel.setSize(100,100);
168         cardLayout = new CardLayout JavaDoc();
169         fieldPanel.setLayout(cardLayout);
170         fieldPanel.add(scrollGeneralPane,"general");
171
172         //
173
// Default
174
//
175
cardLayout.show(fieldPanel, "general");
176         
177         setLayout(new BorderLayout JavaDoc());
178         add(fieldPanel,BorderLayout.CENTER);
179     }
180
181     public void actionPerformed(ActionEvent JavaDoc evt) {
182         //do nothing
183
}
184
185     /**
186      * Invoked when the mouse button has been clicked (pressed and released)
187      * on a component.
188      * This method adjusts the scrollpanes so that the selected
189      * textfield is placed on top of the pane.
190      *
191      * @param evt the occurred event
192      */

193     public void mouseClicked(MouseEvent JavaDoc evt) {
194         //do nothing
195
}
196
197     /**
198      * Invoked when a mouse button has been pressed on a component.
199      * Not used.
200      *
201      * @param evt the occurred event
202      */

203     public void mousePressed(MouseEvent JavaDoc evt) {
204         // do nothing
205
}
206
207     /**
208      * Invoked when a mouse button has been released on a component.
209      * Not used.
210      *
211      * @param evt the occurred event
212      */

213     public void mouseReleased(MouseEvent JavaDoc evt) {
214         // do nothing
215
}
216
217     /**
218      * Invoked when the mouse enters a component.
219      * Not used.
220      *
221      * @param evt the occurred event
222      */

223     public void mouseEntered(MouseEvent JavaDoc evt) {
224         // do nothing
225
}
226
227     /**
228      * Invoked when the mouse exits a component.
229      * Not used.
230      *
231      * @param evt the occurred event
232      */

233     public void mouseExited(MouseEvent JavaDoc evt) {
234         // do nothing
235
}
236     //---------------------------------------------------------- Protected methods
237

238     /**
239      * Sets the fields with the values contained
240      * in the specified Calendar object.
241      * Used to display the old value of calendar
242      *
243      * @param calendar the Calendar object which is the source of the values
244      */

245     protected void setFields(Calendar calendar) {
246
247         String JavaDoc dateStart = null;
248         String JavaDoc dateEnd = null;
249         String JavaDoc timeStart = null;
250         String JavaDoc timeEnd = null;
251
252         dateStart = (String JavaDoc)calendar.getEvent().getDtStart().getPropertyValue();
253         dateEnd = (String JavaDoc)calendar.getEvent().getDtEnd().getPropertyValue() ;
254         
255         dateStart = FieldsHelper.convertDateFromUTC(dateStart);
256         if (dateStart != null && dateStart.length() > 10) {
257             timeStart = dateStart.substring(11,16);
258             dateStart = dateStart.substring(0,10);
259         }
260
261         dateEnd = FieldsHelper.convertDateFromUTC(dateEnd);
262         if (dateEnd != null && dateEnd.length() > 10) {
263             timeEnd = dateEnd.substring(11,16);
264             dateEnd = dateEnd.substring(0,10);
265         }
266
267         // General
268
tf1.setText((String JavaDoc)calendar.getEvent()
269                                     .getSummary().getPropertyValue());
270         tf2.setText(dateStart);
271         tf3.setText(dateEnd );
272         tf4.setText((String JavaDoc)calendar.getEvent()
273                                     .getLocation().getPropertyValue());
274         tf5.setText((String JavaDoc)calendar.getEvent()
275                                     .getDescription().getPropertyValue());
276         tf6.setText(timeStart != null ? timeStart : "");
277         tf7.setText(timeEnd != null ? timeEnd : "");
278     }
279
280     /**
281      * Returns a new Calendar object containing the informations set in the textfields.
282      * This method also builds the LABELs from the provided addresses, when at least
283      * street, city, state, postal code and country are specified.
284      *
285      * @return the Calendar object containing the informations set in the textfields
286      */

287     protected Calendar getFields(){
288
289         Calendar calendar = new Calendar();
290
291         String JavaDoc fullStartDate = tf2.getText() + " " + tf6.getText() + ":00";
292         String JavaDoc fullEndDate = tf3.getText() + " " + tf7.getText() + ":00";
293
294         String JavaDoc startDate = FieldsHelper.convertDateToUTC(fullStartDate);
295         String JavaDoc endDate = FieldsHelper.convertDateToUTC(fullEndDate );
296
297         calendar.getEvent().getSummary ().setPropertyValue(tf1.getText());
298         calendar.getEvent().getDtStart ().setPropertyValue(startDate );
299         calendar.getEvent().getDtEnd ().setPropertyValue(endDate );
300         calendar.getEvent().getLocation ().setPropertyValue(tf4.getText());
301         calendar.getEvent().getDescription().setPropertyValue(tf5.getText());
302
303         return calendar;
304     }
305
306     /**
307      * Sets all the textfields to an empty string.
308      */

309     protected void blankFields() {
310         tf1.setText ("");
311         tf2.setText ("");
312         tf3.setText ("");
313         tf4.setText ("");
314         tf5.setText ("");
315         tf6.setText ("");
316         tf7.setText ("");
317     }
318 }
Popular Tags