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 list;
033:
034: import javax.microedition.lcdui.*;
035: import javax.microedition.midlet.MIDlet;
036:
037: /**
038: * The MIDlet demonstrates the different types of list supported by MIDP-2.0:
039: * EXCLUSIVE - a choice having exactly one element selected at time.
040: * IMPLICIT - a choice in which the currently focused element is
041: * selected when a Command is initiated.
042: * MULTIPLE - a choice that can have arbitrary number of elements
043: * selected at a time.
044: *
045: * @version 2.0
046: */
047: public class ListDemo extends MIDlet implements CommandListener {
048: private static final Command CMD_EXIT = new Command("Exit",
049: Command.EXIT, 1);
050: private static final Command CMD_BACK = new Command("Back",
051: Command.BACK, 1);
052: private Display display;
053: private List mainList;
054: private List exclusiveList;
055: private List implicitList;
056: private List multipleList;
057: private boolean firstTime;
058:
059: public ListDemo() {
060: display = Display.getDisplay(this );
061:
062: // these are the strings for the choices.
063: String[] stringArray = { "Option A", "Option B", "Option C",
064: "Option D" };
065:
066: // the string elements will have no images
067: Image[] imageArray = null;
068:
069: /*
070: * create samples of the supported types:
071: * "Exclusive", "Implicit" and "Multiple"
072: */
073: exclusiveList = new List("Exclusive", Choice.EXCLUSIVE,
074: stringArray, imageArray);
075: exclusiveList.addCommand(CMD_BACK);
076: exclusiveList.addCommand(CMD_EXIT);
077: exclusiveList.setCommandListener(this );
078:
079: implicitList = new List("Implicit", Choice.IMPLICIT,
080: stringArray, imageArray);
081: implicitList.addCommand(CMD_BACK);
082: implicitList.addCommand(CMD_EXIT);
083: implicitList.setCommandListener(this );
084:
085: multipleList = new List("Multiple", Choice.MULTIPLE,
086: stringArray, imageArray);
087: multipleList.addCommand(CMD_BACK);
088: multipleList.addCommand(CMD_EXIT);
089: multipleList.setCommandListener(this );
090:
091: firstTime = true;
092: }
093:
094: protected void startApp() {
095: if (firstTime) {
096: // these are the images and strings for the choices.
097: Image[] imageArray = null;
098:
099: try {
100: // load the duke image to place in the image array
101: Image icon = Image.createImage("/midp/uidemo/Icon.png");
102:
103: // these are the images and strings for the choices.
104: imageArray = new Image[] { icon, icon, icon };
105: } catch (java.io.IOException err) {
106: // ignore the image loading failure the application can recover.
107: }
108:
109: String[] stringArray = { "Exclusive", "Implicit",
110: "Multiple" };
111: mainList = new List("Choose type", Choice.IMPLICIT,
112: stringArray, imageArray);
113: mainList.addCommand(CMD_EXIT);
114: mainList.setCommandListener(this );
115: display.setCurrent(mainList);
116: firstTime = false;
117: }
118: }
119:
120: protected void destroyApp(boolean unconditional) {
121: }
122:
123: protected void pauseApp() {
124: }
125:
126: public void commandAction(Command c, Displayable d) {
127: if (d.equals(mainList)) {
128: // in the main list
129: if (c == List.SELECT_COMMAND) {
130: if (d.equals(mainList)) {
131: switch (((List) d).getSelectedIndex()) {
132: case 0:
133: display.setCurrent(exclusiveList);
134:
135: break;
136:
137: case 1:
138: display.setCurrent(implicitList);
139:
140: break;
141:
142: case 2:
143: display.setCurrent(multipleList);
144:
145: break;
146: }
147: }
148: }
149: } else {
150: // in one of the sub-lists
151: if (c == CMD_BACK) {
152: display.setCurrent(mainList);
153: }
154: }
155:
156: if (c == CMD_EXIT) {
157: destroyApp(false);
158: notifyDestroyed();
159: }
160: }
161: }
|