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: /*
034: * @(#)LocalizedMIDlet.java 1.0 04/05/18 @(#)
035: */
036: package l10ndemo;
037:
038: import javax.microedition.midlet.*;
039: import javax.microedition.lcdui.*;
040: import java.util.Calendar;
041:
042: /**
043: *
044: * @author breh, lukas
045: * @version
046: */
047: public class LocalizedMIDlet extends MIDlet implements CommandListener {
048:
049: private List mainMenu;
050: private Alert alert1, alert2, alert3, alert4;
051:
052: private Command quitCommand;
053:
054: private Display d;
055:
056: public void startApp() {
057:
058: //#ifdef Chinese
059: //# /* This is used only for chinese configuration
060: //# * we want the application to run always in Chinese
061: //# * no matter what the microedition.locale property is set.
062: //# * Otherwise the localization support is initialized
063: //# * when a getMessage() method is called for the first time.
064: //# */
065: //# LocalizationSupport.initLocalizationSupport("zh_CN");
066: //#endif
067:
068: //#ifdef Japanese
069: //# /* This is used only for japanese configuration
070: //# * we want the application to run always in Japanese
071: //# * no matter what the microedition.locale property is set.
072: //# * Otherwise the localization support is initialized
073: //# * when a getMessage() method is called for the first time.
074: //# */
075: //# LocalizationSupport.initLocalizationSupport("ja_JP");
076: //#endif
077:
078: //#ifdef Czech
079: //# /* This is used only for czech configuration
080: //# * we want the application to run always in Czech
081: //# * no matter what the microedition.locale property is set.
082: //# * Otherwise the localization support is initialized
083: //# * when a getMessage() method is called for the first time.
084: //# */
085: //# LocalizationSupport.initLocalizationSupport("cs_CZ");
086: //#endif
087:
088: //#ifdef Deutsch
089: //# /* This is used only for german configuration
090: //# * we want the application to run always in German
091: //# * no matter what the microedition.locale property is set.
092: //# * Otherwise the localization support is initialized
093: //# * when a getMessage() method is called for the first time.
094: //# */
095: //# LocalizationSupport.initLocalizationSupport("de");
096: //#endif
097:
098: //#ifdef Spanish
099: //# /* This is used only for german configuration
100: //# * we want the application to run always in Spanish
101: //# * no matter what the microedition.locale property is set.
102: //# * Otherwise the localization support is initialized
103: //# * when a getMessage() method is called for the first time.
104: //# */
105: //# LocalizationSupport.initLocalizationSupport("es_MX");
106: //#endif
107:
108: d = Display.getDisplay(this );
109:
110: // prepare our simple gui
111: prepareGUI();
112:
113: // set the menu as the current display
114: d.setCurrent(mainMenu);
115: }
116:
117: public void pauseApp() {
118: }
119:
120: public void destroyApp(boolean unconditional) {
121: }
122:
123: public void updateAlert1() {
124: // get current time
125: Calendar now = Calendar.getInstance();
126: // create the localized sentence with time and timezone (it requires 3 parameters).
127: alert1.setString(LocalizationSupport.getMessage("A1_TEXT",
128: new Object[] {
129: new Integer(now.get(Calendar.HOUR_OF_DAY)),
130: new Integer(now.get(Calendar.MINUTE)),
131: now.getTimeZone().getID() }));
132: }
133:
134: private void prepareGUI() {
135: // create main menu
136: mainMenu = new List(LocalizationSupport
137: .getMessage("MAIN_MENU_TITLE"), List.IMPLICIT);
138: mainMenu.append(LocalizationSupport.getMessage("MENU_ITEM_1"),
139: null);
140: mainMenu.append(LocalizationSupport.getMessage("MENU_ITEM_2"),
141: null);
142: mainMenu.append(LocalizationSupport.getMessage("MENU_ITEM_3"),
143: null);
144: mainMenu.append(LocalizationSupport.getMessage("MENU_ITEM_4"),
145: null);
146:
147: // create quit command and add it to main menu
148: quitCommand = new Command(LocalizationSupport
149: .getMessage("C_QUIT"), Command.EXIT, 1);
150: mainMenu.addCommand(quitCommand);
151: mainMenu.setCommandListener(this );
152:
153: // create an alert1 (called by 1st item from menu)
154: // this only prepares the alert, the actual string will be set just
155: // before it is shown to user
156: alert1 = new Alert(LocalizationSupport.getMessage("A1_TITLE"));
157: alert1.setType(AlertType.INFO);
158: alert1.setTimeout(Alert.FOREVER);
159:
160: // create alert2 (called by 2nd item from menu)
161: // please note, the message requires one parameter, but it is
162: // not supplied - see how it is handled by the support in runtime
163: alert2 = new Alert(LocalizationSupport.getMessage("A2_TITLE"),
164: LocalizationSupport.getMessage("A2_TEXT"), null,
165: AlertType.INFO);
166: alert2.setTimeout(Alert.FOREVER);
167:
168: // create alert3 (called by 3rd item of menu)
169: // the A3_TEXT key does not exists - see how it is handled by the support
170: alert3 = new Alert(LocalizationSupport.getMessage("A3_TITLE"),
171: LocalizationSupport.getMessage("A3_TEXT"), null,
172: AlertType.INFO);
173: alert3.setTimeout(Alert.FOREVER);
174:
175: // create alert4 (called by 4rd item of menu)
176: // just show the current locale - uses message with parameter
177: alert4 = new Alert(LocalizationSupport.getMessage("A4_TITLE"),
178: LocalizationSupport.getMessage("A4_TEXT",
179: new Object[] { System
180: .getProperty("microedition.locale") }),
181: null, AlertType.INFO);
182: alert4.setTimeout(Alert.FOREVER);
183: }
184:
185: public void commandAction(Command c, Displayable displayable) {
186: if (c == quitCommand) {
187: destroyApp(false);
188: notifyDestroyed();
189: } else if ((c == List.SELECT_COMMAND)
190: && (displayable == mainMenu)) {
191: switch (mainMenu.getSelectedIndex()) {
192: case 0:
193: updateAlert1();
194: d.setCurrent(alert1, mainMenu);
195: break;
196: case 1:
197: d.setCurrent(alert2, mainMenu);
198: break;
199: case 2:
200: d.setCurrent(alert3, mainMenu);
201: break;
202: case 3:
203: d.setCurrent(alert4, mainMenu);
204: break;
205:
206: }
207: }
208:
209: }
210: }
|