KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > microedition > lcdui > DateField


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

20  
21 package javax.microedition.lcdui;
22
23 import java.text.SimpleDateFormat;
24 import java.text.ParseException;
25 import java.util.Calendar;
26 import java.util.Date;
27 import java.util.TimeZone;
28
29
30 public class DateField extends Item
31 {
32
33   public static final int DATE = 1;
34   public static final int TIME = 2;
35   public static final int DATE_TIME = 3;
36   
37   Date date;
38   String label;
39   int mode;
40
41   ChoiceGroup dateTime;
42   TextBox dateBox;
43   TextBox timeBox;
44   
45   static SimpleDateFormat dateFormat = new SimpleDateFormat("d.M.yyyy");
46   static SimpleDateFormat timeFormat = new SimpleDateFormat("H.mm");
47   
48   Alert alert = new Alert("Parse Error");
49   
50     static Command saveCommand = new Command("Save", Command.OK, 0);
51     static Command backCommand = new Command("Back", Command.BACK, 0);
52   
53   CommandListener dateTimeListener = new CommandListener()
54   {
55     
56     public void commandAction(Command c, Displayable d)
57     {
58       if (c == backCommand) {
59         getOwner().currentDisplay.setCurrent(owner);
60       } else if (c == saveCommand && d == dateBox) {
61         try {
62           Date tmp = dateFormat.parse(dateBox.getString());
63           if (date == null) {
64             date = tmp;
65           } else {
66             Calendar from = Calendar.getInstance();
67             Calendar to = Calendar.getInstance();
68             from.setTime(tmp);
69             to.setTime(date);
70             to.set(Calendar.DAY_OF_MONTH, from.get(Calendar.DAY_OF_MONTH));
71             to.set(Calendar.MONTH, from.get(Calendar.MONTH));
72             to.set(Calendar.YEAR, from.get(Calendar.YEAR));
73             date = to.getTime();
74           }
75           updateDateTimeString();
76           getOwner().currentDisplay.setCurrent(owner);
77         } catch (ParseException ex) {
78           alert.setString("Date: " + ex.getMessage());
79           getOwner().currentDisplay.setCurrent(alert);
80         }
81       } else if (c == saveCommand && d == timeBox) {
82         try {
83           Date tmp = timeFormat.parse(timeBox.getString());
84           if (date == null) {
85             date = tmp;
86           } else {
87             Calendar from = Calendar.getInstance();
88             Calendar to = Calendar.getInstance();
89             from.setTime(tmp);
90             to.setTime(date);
91             to.set(Calendar.HOUR_OF_DAY, from.get(Calendar.HOUR_OF_DAY));
92             to.set(Calendar.MINUTE, from.get(Calendar.MINUTE));
93             date = to.getTime();
94           }
95           updateDateTimeString();
96           getOwner().currentDisplay.setCurrent(owner);
97         } catch (ParseException ex) {
98           alert.setString("Time: " + ex.getMessage());
99           getOwner().currentDisplay.setCurrent(alert);
100         }
101       }
102     }
103   };
104
105
106   public DateField(String label, int mode)
107   {
108     this(label, mode, null);
109   }
110
111
112   public DateField(String label, int mode, TimeZone timeZone)
113   {
114     super(null);
115     
116     this.label = label;
117
118     setInputMode(mode);
119     
120     dateBox = new TextBox("Date [dd.mm.yyyy]", null, 10, TextField.NUMERIC);
121     dateBox.addCommand(saveCommand);
122     dateBox.addCommand(backCommand);
123     dateBox.setCommandListener(dateTimeListener);
124     timeBox = new TextBox("Time [hh.mm]", null, 5, TextField.NUMERIC);
125     timeBox.addCommand(saveCommand);
126     timeBox.addCommand(backCommand);
127     timeBox.setCommandListener(dateTimeListener);
128   }
129
130
131   public Date getDate()
132   {
133     return date;
134   }
135
136
137   public void setDate(Date date)
138   {
139     this.date = date;
140     updateDateTimeString();
141   }
142
143
144   public int getInputMode()
145   {
146     return mode;
147   }
148
149
150   public void setInputMode(int mode)
151   {
152     if (mode < 1 || mode > 3) {
153       throw new IllegalArgumentException();
154     }
155     
156     this.mode = mode;
157
158     dateTime = new ChoiceGroup(label, Choice.IMPLICIT);
159     if ((mode & DATE) != 0) {
160       dateTime.append("[date]", null);
161     }
162     if ((mode & TIME) != 0) {
163       dateTime.append("[time]", null);
164     }
165   }
166
167
168   public void setLabel(String label)
169   {
170     this.label = label;
171     
172     dateTime.setLabel(label);
173   }
174
175
176     boolean isFocusable()
177     {
178         return true;
179     }
180
181     
182   int getHeight()
183     {
184         return super.getHeight() + dateTime.getHeight();
185     }
186
187
188   int paint(Graphics g)
189   {
190     super.paintContent(g);
191     
192     g.translate(0, super.getHeight());
193         dateTime.paint(g);
194         g.translate(0, -super.getHeight());
195
196     
197     return getHeight();
198   }
199
200   
201     void setFocus(boolean state)
202     {
203     super.setFocus(state);
204
205     dateTime.setFocus(state);
206   }
207
208   
209   boolean select()
210   {
211     dateTime.select();
212
213     if (dateTime.getSelectedIndex() == 0 && (mode & DATE) != 0) {
214       if (date != null) {
215         dateBox.setString(dateFormat.format(date));
216       } else {
217         dateBox.setString("");
218       }
219       getOwner().currentDisplay.setCurrent(dateBox);
220     } else {
221       if (date != null) {
222         timeBox.setString(timeFormat.format(date));
223       } else {
224         timeBox.setString("");
225       }
226       getOwner().currentDisplay.setCurrent(timeBox);
227     }
228       
229     return true;
230   }
231   
232   
233   int traverse(int gameKeyCode, int top, int bottom, boolean action)
234     {
235         return dateTime.traverse(gameKeyCode, top, bottom, action);
236     }
237   
238
239   void updateDateTimeString()
240   {
241     if ((mode & DATE) != 0) {
242       dateTime.set(0, dateFormat.format(date), null);
243     }
244     if ((mode & TIME) != 0) {
245       if ((mode & DATE) != 0) {
246         dateTime.set(1, timeFormat.format(date), null);
247       } else {
248         dateTime.set(0, timeFormat.format(date), null);
249       }
250     }
251   }
252   
253 }
254
Popular Tags