001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * 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
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * 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: package example.mia.demo;
033:
034: import java.util.Calendar;
035:
036: import javax.microedition.global.Formatter;
037: import javax.microedition.lcdui.*;
038: import javax.microedition.midlet.*;
039:
040: /**
041: * FormatterDemo MIDlet shows usage of formatting in JSR238
042: * @see http://www.jcp.org/en/jsr/detail?id=238
043: *
044: * MIDlet uses device resources to load number and date/time
045: * formatting rules and symbols. Date/time and numbers are formatted according
046: * locale used by Formatter.
047: *
048: * @version 1.3
049: */
050: public class FormatterDemo extends MIDlet implements CommandListener {
051: private static final String[] LOCALES = { "en-US", "sk-SK",
052: "cs-CZ", "he-IL", "zh-CN", "ja-JP" };
053: private static final String[] CURRENCY_CODES = { "USD", "SKK",
054: "CZK", "ILS", "CNY", "JPY" };
055: private static Formatter defaultFormatter = null;
056: private static Formatter[] formatters = null;
057: private Form dateForm;
058: private Form numberForm1;
059: private Form numberForm2;
060: private Form numberForm3;
061: Command exitCommand = new Command("Exit", Command.EXIT, 1);
062: Command nextCommand = new Command("Next", Command.SCREEN, 1);
063:
064: public FormatterDemo() {
065: defaultFormatter = new Formatter();
066: formatters = new Formatter[LOCALES.length];
067:
068: for (int i = 0; i < LOCALES.length; ++i) {
069: formatters[i] = new Formatter(LOCALES[i]);
070: }
071:
072: dateForm = new Form("Date/time formatting by locales");
073: dateForm.addCommand(nextCommand);
074: dateForm.setCommandListener(this );
075:
076: Calendar calendar = Calendar.getInstance();
077:
078: for (int i = 0; i < LOCALES.length; ++i) {
079: dateForm.append(new StringItem(LOCALES[i] + ":",
080: formatters[i].formatDateTime(calendar,
081: Formatter.DATETIME_LONG)));
082: }
083:
084: numberForm1 = new Form("Number formatting by locales");
085: numberForm1.addCommand(nextCommand);
086: numberForm1.setCommandListener(this );
087:
088: double number = -1234.5678;
089: numberForm1.append("Show number with 2 decimals:");
090:
091: for (int i = 0; i < LOCALES.length; ++i) {
092: numberForm1.append(new StringItem(LOCALES[i] + ":",
093: formatters[i].formatNumber(number, 2)));
094: }
095:
096: numberForm2 = new Form("Percentage formatting by locales");
097: numberForm2.addCommand(nextCommand);
098: numberForm2.setCommandListener(this );
099:
100: numberForm2.append("Show number with 1 decimal:");
101:
102: for (int i = 0; i < LOCALES.length; ++i) {
103: numberForm2.append(new StringItem(LOCALES[i] + ":",
104: formatters[i].formatPercentage((float) number, 1)));
105: }
106:
107: numberForm3 = new Form("Currency formatting");
108: numberForm3.addCommand(nextCommand);
109: numberForm3.addCommand(exitCommand);
110: numberForm3.setCommandListener(this );
111:
112: numberForm3.append("Show number as currency:");
113:
114: for (int i = 0; i < CURRENCY_CODES.length; ++i) {
115: numberForm3.append(new StringItem(CURRENCY_CODES[i] + ":",
116: defaultFormatter.formatCurrency(number,
117: CURRENCY_CODES[i])));
118: }
119: }
120:
121: public void startApp() {
122: Display.getDisplay(this ).setCurrent(dateForm);
123: }
124:
125: public void pauseApp() {
126: }
127:
128: public void destroyApp(boolean unconditional) {
129: }
130:
131: public void commandAction(Command c, Displayable d) {
132: if (c == exitCommand) {
133: destroyApp(false);
134: notifyDestroyed();
135: } else if ((c == nextCommand) && (d == dateForm)) {
136: Display.getDisplay(this).setCurrent(numberForm1);
137: } else if ((c == nextCommand) && (d == numberForm1)) {
138: Display.getDisplay(this).setCurrent(numberForm2);
139: } else if ((c == nextCommand) && (d == numberForm2)) {
140: Display.getDisplay(this).setCurrent(numberForm3);
141: } else if ((c == nextCommand) && (d == numberForm3)) {
142: Display.getDisplay(this).setCurrent(dateForm);
143: }
144: }
145: }
|