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 alert;
033:
034: import javax.microedition.lcdui.*;
035: import javax.microedition.midlet.MIDlet;
036:
037: /**
038: * The alert demo displays a list of alerts that will be displayed once the
039: * user clicks a list item. These alerts try to present the full range of
040: * alert types supported in MIDP.
041: *
042: * @version 2.0
043: */
044: public class AlertDemo extends MIDlet {
045: private static final Command CMD_EXIT = new Command("Exit",
046: Command.EXIT, 1);
047: private static final Command CMD_SHOW = new Command("Show",
048: Command.SCREEN, 1);
049: private static final String[] typeStrings = { "Alarm",
050: "Confirmation", "Error", "Info", "Warning" };
051: private static final String[] timeoutStrings = { "2 Seconds",
052: "4 Seconds", "8 Seconds", "Forever" };
053: private static final int SECOND = 1000;
054: private Display display;
055: private boolean firstTime;
056: private Form mainForm;
057:
058: public AlertDemo() {
059: firstTime = true;
060: mainForm = new Form("Alert Options");
061: }
062:
063: protected void startApp() {
064: display = Display.getDisplay(this );
065: showOption();
066: }
067:
068: /**
069: * Creates the main display of the MIDlet.
070: * In this form the user will choose the properties of the alert
071: */
072: private void showOption() {
073: if (firstTime) {
074: // choice-group for the type of the alert:
075: // "Alarm", "Confirmation", "Error", "Info" or "Warning"
076: ChoiceGroup types = new ChoiceGroup("Type",
077: ChoiceGroup.POPUP, typeStrings, null);
078: mainForm.append(types);
079:
080: // choice-group for the timeout of the alert:
081: // "2 Seconds", "4 Seconds", "8 Seconds" or "Forever"
082: ChoiceGroup timeouts = new ChoiceGroup("Timeout",
083: ChoiceGroup.POPUP, timeoutStrings, null);
084: mainForm.append(timeouts);
085:
086: // a check-box to add an indicator to the alert
087: String[] optionStrings = { "Show Indicator" };
088: ChoiceGroup options = new ChoiceGroup("Options",
089: Choice.MULTIPLE, optionStrings, null);
090: mainForm.append(options);
091: mainForm.addCommand(CMD_SHOW);
092: mainForm.addCommand(CMD_EXIT);
093: mainForm.setCommandListener(new AlertListener(types,
094: timeouts, options));
095: firstTime = false;
096: }
097:
098: display.setCurrent(mainForm);
099: }
100:
101: protected void destroyApp(boolean unconditional) {
102: }
103:
104: protected void pauseApp() {
105: }
106:
107: /**
108: * Creates the alert's indicator.
109: * If there is no timeout (maxValue == Alert.FOREVER), the indicator will be
110: * an "indefinite-running" gauge.
111: * If there is a timeout, the indicator will be a "non-interactive" gauge
112: * that is updated by a background thread.
113: */
114: private Gauge createIndicator(int maxValue) {
115: if (maxValue == Alert.FOREVER) {
116: return new Gauge(null, false, Gauge.INDEFINITE,
117: Gauge.CONTINUOUS_RUNNING);
118: }
119:
120: final int max = maxValue / SECOND;
121: final Gauge indicator = new Gauge(null, false, max, 0);
122:
123: // if (maxValue != Gauge.INDEFINITE) {
124: new Thread() {
125: public void run() {
126: int value = 0;
127:
128: while (value < max) {
129: indicator.setValue(value);
130: ++value;
131:
132: try {
133: Thread.sleep(1000);
134: } catch (InterruptedException ie) {
135: // ignore
136: }
137: }
138: }
139: }.start();
140:
141: // }
142: return indicator;
143: }
144:
145: private class AlertListener implements CommandListener {
146: AlertType[] alertTypes = { AlertType.ALARM,
147: AlertType.CONFIRMATION, AlertType.ERROR,
148: AlertType.INFO, AlertType.WARNING };
149: ChoiceGroup typesCG;
150: int[] timeouts = { 2 * SECOND, 4 * SECOND, 8 * SECOND,
151: Alert.FOREVER };
152: ChoiceGroup timeoutsCG;
153: ChoiceGroup indicatorCG;
154:
155: public AlertListener(ChoiceGroup types, ChoiceGroup timeouts,
156: ChoiceGroup indicator) {
157: typesCG = types;
158: timeoutsCG = timeouts;
159: indicatorCG = indicator;
160: }
161:
162: public void commandAction(Command c, Displayable d) {
163: if (c == CMD_SHOW) {
164: int typeIndex = typesCG.getSelectedIndex();
165: Alert alert = new Alert("Alert");
166: alert.setType(alertTypes[typeIndex]);
167:
168: int timeoutIndex = timeoutsCG.getSelectedIndex();
169: alert.setTimeout(timeouts[timeoutIndex]);
170: alert.setString(typeStrings[typeIndex]
171: + " Alert, Running "
172: + timeoutStrings[timeoutIndex]);
173:
174: boolean[] SelectedFlags = new boolean[1];
175: indicatorCG.getSelectedFlags(SelectedFlags);
176:
177: if (SelectedFlags[0]) {
178: Gauge indicator = createIndicator(timeouts[timeoutIndex]);
179: alert.setIndicator(indicator);
180: }
181:
182: display.setCurrent(alert);
183: } else if (c == CMD_EXIT) {
184: destroyApp(false);
185: notifyDestroyed();
186: }
187: }
188: }
189: }
|