001: /*
002: * Copyright (c) 2007, Sun Microsystems, Inc.
003: *
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: * * Neither the name of Sun Microsystems, Inc. nor the names of its
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
023: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
024: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package l10ndemo;
034:
035: import java.util.Calendar;
036: import javax.microedition.midlet.*;
037: import javax.microedition.lcdui.*;
038:
039: /**
040: * @author tester
041: */
042: public class LocalizationVisualMIDlet extends MIDlet implements
043: CommandListener {
044:
045: private boolean midletPaused = false;
046:
047: //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
048: private List list;
049: private Alert alert;
050: private Alert alert1;
051: private Alert alert2;
052: private Alert alert3;
053: private Command exitCommand;
054:
055: //</editor-fold>//GEN-END:|fields|0|
056:
057: /**
058: * The LocalizationVisualMIDlet constructor.
059: */
060: public LocalizationVisualMIDlet() {
061: }
062:
063: //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
064: //</editor-fold>//GEN-END:|methods|0|
065:
066: //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
067: /**
068: * Initilizes the application.
069: * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
070: */
071: private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
072: // write pre-initialize user code here
073: //GEN-LINE:|0-initialize|1|0-postInitialize
074: // write post-initialize user code here
075: }//GEN-BEGIN:|0-initialize|2|
076:
077: //</editor-fold>//GEN-END:|0-initialize|2|
078:
079: //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
080: /**
081: * Performs an action assigned to the Mobile Device - MIDlet Started point.
082: */
083: public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
084: // write pre-action user code here
085: switchDisplayable(null, getList());//GEN-LINE:|3-startMIDlet|1|3-postAction
086: // write post-action user code here
087: }//GEN-BEGIN:|3-startMIDlet|2|
088:
089: //</editor-fold>//GEN-END:|3-startMIDlet|2|
090:
091: //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
092: /**
093: * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
094: */
095: public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
096: // write pre-action user code here
097: switchDisplayable(null, getList());//GEN-LINE:|4-resumeMIDlet|1|4-postAction
098: // write post-action user code here
099: }//GEN-BEGIN:|4-resumeMIDlet|2|
100:
101: //</editor-fold>//GEN-END:|4-resumeMIDlet|2|
102:
103: //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
104: /**
105: * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
106: * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
107: * @param nextDisplayable the Displayable to be set
108: */
109: public void switchDisplayable(Alert alert,
110: Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
111: // write pre-switch user code here
112: Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
113: if (alert == null) {
114: display.setCurrent(nextDisplayable);
115: } else {
116: display.setCurrent(alert, nextDisplayable);
117: }//GEN-END:|5-switchDisplayable|1|5-postSwitch
118: // write post-switch user code here
119: }//GEN-BEGIN:|5-switchDisplayable|2|
120:
121: //</editor-fold>//GEN-END:|5-switchDisplayable|2|
122:
123: //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
124: /**
125: * Called by a system to indicated that a command has been invoked on a particular displayable.
126: * @param command the Command that was invoked
127: * @param displayable the Displayable where the command was invoked
128: */
129: public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
130: // write pre-action user code here
131: if (displayable == list) {//GEN-BEGIN:|7-commandAction|1|15-preAction
132: if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|1|15-preAction
133: // write pre-action user code here
134: listAction();//GEN-LINE:|7-commandAction|2|15-postAction
135: // write post-action user code here
136: } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|3|31-preAction
137: // write pre-action user code here
138: exitMIDlet();//GEN-LINE:|7-commandAction|4|31-postAction
139: // write post-action user code here
140: }//GEN-BEGIN:|7-commandAction|5|7-postCommandAction
141: }//GEN-END:|7-commandAction|5|7-postCommandAction
142: // write post-action user code here
143: }//GEN-BEGIN:|7-commandAction|6|
144:
145: //</editor-fold>//GEN-END:|7-commandAction|6|
146:
147: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: list ">//GEN-BEGIN:|13-getter|0|13-preInit
148: /**
149: * Returns an initiliazed instance of list component.
150: * @return the initialized component instance
151: */
152: public List getList() {
153: if (list == null) {//GEN-END:|13-getter|0|13-preInit
154: // write pre-init user code here
155: list = new List("list", Choice.IMPLICIT);//GEN-BEGIN:|13-getter|1|13-postInit
156: list.append(LocalizationSupport.getMessage("MENU_ITEM_1"),
157: null);
158: list.append(LocalizationSupport.getMessage("MENU_ITEM_2"),
159: null);
160: list.append(LocalizationSupport.getMessage("MENU_ITEM_3"),
161: null);
162: list.append(LocalizationSupport.getMessage("MENU_ITEM_4"),
163: null);
164: list.addCommand(getExitCommand());
165: list.setCommandListener(this );
166: list.setSelectedFlags(new boolean[] { false, false, false,
167: false });//GEN-END:|13-getter|1|13-postInit
168: // write post-init user code here
169: }//GEN-BEGIN:|13-getter|2|
170: return list;
171: }
172:
173: //</editor-fold>//GEN-END:|13-getter|2|
174:
175: //<editor-fold defaultstate="collapsed" desc=" Generated Method: listAction ">//GEN-BEGIN:|13-action|0|13-preAction
176: /**
177: * Performs an action assigned to the selected list element in the list component.
178: */
179: public void listAction() {//GEN-END:|13-action|0|13-preAction
180: // enter pre-action user code here
181: String __selectedString = getList().getString(
182: getList().getSelectedIndex());//GEN-BEGIN:|13-action|1|18-preAction
183: if (__selectedString != null) {
184: if (__selectedString.equals(LocalizationSupport
185: .getMessage("MENU_ITEM_1"))) {//GEN-END:|13-action|1|18-preAction
186: // we need to update the time in the alert message
187: updateAlertMessage();
188: switchDisplayable(getAlert(), getList());//GEN-LINE:|13-action|2|18-postAction
189: // write post-action user code here
190: } else if (__selectedString.equals(LocalizationSupport
191: .getMessage("MENU_ITEM_2"))) {//GEN-LINE:|13-action|3|19-preAction
192: // write pre-action user code here
193: switchDisplayable(getAlert1(), getList());//GEN-LINE:|13-action|4|19-postAction
194: // write post-action user code here
195: } else if (__selectedString.equals(LocalizationSupport
196: .getMessage("MENU_ITEM_3"))) {//GEN-LINE:|13-action|5|20-preAction
197: // write pre-action user code here
198: switchDisplayable(getAlert2(), getList());//GEN-LINE:|13-action|6|20-postAction
199: // write post-action user code here
200: } else if (__selectedString.equals(LocalizationSupport
201: .getMessage("MENU_ITEM_4"))) {//GEN-LINE:|13-action|7|21-preAction
202: // write pre-action user code here
203: switchDisplayable(getAlert3(), getList());//GEN-LINE:|13-action|8|21-postAction
204: // write post-action user code here
205: }//GEN-BEGIN:|13-action|9|13-postAction
206: }//GEN-END:|13-action|9|13-postAction
207: // enter post-action user code here
208: }//GEN-BEGIN:|13-action|10|
209:
210: //</editor-fold>//GEN-END:|13-action|10|
211:
212: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alert ">//GEN-BEGIN:|22-getter|0|22-preInit
213: /**
214: * Returns an initiliazed instance of alert component.
215: * @return the initialized component instance
216: */
217: public Alert getAlert() {
218: if (alert == null) {//GEN-END:|22-getter|0|22-preInit
219: // write pre-init user code here
220: alert = new Alert(LocalizationSupport
221: .getMessage("A1_TITLE"), null, null, AlertType.INFO);//GEN-BEGIN:|22-getter|1|22-postInit
222: alert.setTimeout(Alert.FOREVER);//GEN-END:|22-getter|1|22-postInit
223: // write post-init user code here
224: }//GEN-BEGIN:|22-getter|2|
225: return alert;
226: }
227:
228: //</editor-fold>//GEN-END:|22-getter|2|
229:
230: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alert1 ">//GEN-BEGIN:|23-getter|0|23-preInit
231: /**
232: * Returns an initiliazed instance of alert1 component.
233: * @return the initialized component instance
234: */
235: public Alert getAlert1() {
236: if (alert1 == null) {//GEN-END:|23-getter|0|23-preInit
237: // write pre-init user code here
238: alert1 = new Alert(LocalizationSupport
239: .getMessage("A2_TITLE"), LocalizationSupport
240: .getMessage("A2_TEXT"), null, AlertType.INFO);//GEN-BEGIN:|23-getter|1|23-postInit
241: alert1.setTimeout(Alert.FOREVER);//GEN-END:|23-getter|1|23-postInit
242: // write post-init user code here
243: }//GEN-BEGIN:|23-getter|2|
244: return alert1;
245: }
246:
247: //</editor-fold>//GEN-END:|23-getter|2|
248:
249: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alert2 ">//GEN-BEGIN:|24-getter|0|24-preInit
250: /**
251: * Returns an initiliazed instance of alert2 component.
252: * @return the initialized component instance
253: */
254: public Alert getAlert2() {
255: if (alert2 == null) {//GEN-END:|24-getter|0|24-preInit
256: // write pre-init user code here
257: alert2 = new Alert(LocalizationSupport
258: .getMessage("A3_TITLE"), LocalizationSupport
259: .getMessage("A3_TITLE"), null, null);//GEN-BEGIN:|24-getter|1|24-postInit
260: alert2.setTimeout(Alert.FOREVER);//GEN-END:|24-getter|1|24-postInit
261: // write post-init user code here
262: }//GEN-BEGIN:|24-getter|2|
263: return alert2;
264: }
265:
266: //</editor-fold>//GEN-END:|24-getter|2|
267:
268: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alert3 ">//GEN-BEGIN:|25-getter|0|25-preInit
269: /**
270: * Returns an initiliazed instance of alert3 component.
271: * @return the initialized component instance
272: */
273: public Alert getAlert3() {
274: if (alert3 == null) {//GEN-END:|25-getter|0|25-preInit
275: // write pre-init user code here
276: alert3 = new Alert(LocalizationSupport
277: .getMessage("A4_TITLE"), LocalizationSupport
278: .getMessage("A4_TEXT"), null, null);//GEN-BEGIN:|25-getter|1|25-postInit
279: alert3.setTimeout(Alert.FOREVER);//GEN-END:|25-getter|1|25-postInit
280: // write post-init user code here
281: }//GEN-BEGIN:|25-getter|2|
282: return alert3;
283: }
284:
285: //</editor-fold>//GEN-END:|25-getter|2|
286:
287: //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|30-getter|0|30-preInit
288: /**
289: * Returns an initiliazed instance of exitCommand component.
290: * @return the initialized component instance
291: */
292: public Command getExitCommand() {
293: if (exitCommand == null) {//GEN-END:|30-getter|0|30-preInit
294: // write pre-init user code here
295: exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|30-getter|1|30-postInit
296: // write post-init user code here
297: }//GEN-BEGIN:|30-getter|2|
298: return exitCommand;
299: }
300:
301: //</editor-fold>//GEN-END:|30-getter|2|
302:
303: /**
304: * Returns a display instance.
305: * @return the display instance.
306: */
307: public Display getDisplay() {
308: return Display.getDisplay(this );
309: }
310:
311: /**
312: * Exits MIDlet.
313: */
314: public void exitMIDlet() {
315: switchDisplayable(null, null);
316: destroyApp(true);
317: notifyDestroyed();
318: }
319:
320: /**
321: * Called when MIDlet is started.
322: * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
323: */
324: public void startApp() {
325: //#ifdef Chinese
326: //# /* This is used only for chinese configuration
327: //# * we want the application to run always in Chinese
328: //# * no matter what the microedition.locale property is set.
329: //# * Otherwise the localization support is initialized
330: //# * when a getMessage() method is called for the first time.
331: //# */
332: //# LocalizationSupport.initLocalizationSupport("zh_CN");
333: //#endif
334:
335: //#ifdef Japanese
336: //# /* This is used only for japanese configuration
337: //# * we want the application to run always in Japanese
338: //# * no matter what the microedition.locale property is set.
339: //# * Otherwise the localization support is initialized
340: //# * when a getMessage() method is called for the first time.
341: //# */
342: //# LocalizationSupport.initLocalizationSupport("ja_JP");
343: //#endif
344:
345: //#ifdef Czech
346: //# /* This is used only for czech configuration
347: //# * we want the application to run always in Czech
348: //# * no matter what the microedition.locale property is set.
349: //# * Otherwise the localization support is initialized
350: //# * when a getMessage() method is called for the first time.
351: //# */
352: //# LocalizationSupport.initLocalizationSupport("cs_CZ");
353: //#endif
354:
355: //#ifdef Deutsch
356: //# /* This is used only for german configuration
357: //# * we want the application to run always in German
358: //# * no matter what the microedition.locale property is set.
359: //# * Otherwise the localization support is initialized
360: //# * when a getMessage() method is called for the first time.
361: //# */
362: //# LocalizationSupport.initLocalizationSupport("de");
363: //#endif
364:
365: //#ifdef Spanish
366: //# /* This is used only for german configuration
367: //# * we want the application to run always in Spanish
368: //# * no matter what the microedition.locale property is set.
369: //# * Otherwise the localization support is initialized
370: //# * when a getMessage() method is called for the first time.
371: //# */
372: //# LocalizationSupport.initLocalizationSupport("es_MX");
373: //#endif
374:
375: if (midletPaused) {
376: resumeMIDlet();
377: } else {
378: initialize();
379: startMIDlet();
380: }
381: midletPaused = false;
382: }
383:
384: /**
385: * Called when MIDlet is paused.
386: */
387: public void pauseApp() {
388: midletPaused = true;
389: }
390:
391: /**
392: * Called to signal the MIDlet to terminate.
393: * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
394: */
395: public void destroyApp(boolean unconditional) {
396: }
397:
398: public void updateAlertMessage() {
399: // get current time
400: Calendar now = Calendar.getInstance();
401: // create the localized sentence with time and timezone (it requires 3 parameters).
402: getAlert().setString(
403: LocalizationSupport.getMessage("A1_TEXT", new Object[] {
404: new Integer(now.get(Calendar.HOUR_OF_DAY)),
405: new Integer(now.get(Calendar.MINUTE)),
406: now.getTimeZone().getID() }));
407: }
408: }
|