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