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 textbox;
033:
034: import javax.microedition.lcdui.*;
035: import javax.microedition.midlet.MIDlet;
036:
037: /**
038: * The textbox demo displays a list of all the text box types and allows the
039: * user to select a specific type of text box to try.
040: *
041: * @version 2.0
042: */
043: public class TextBoxDemo extends MIDlet implements CommandListener {
044: private static final Command CMD_EXIT = new Command("Exit",
045: Command.EXIT, 1);
046: private static final Command CMD_BACK = new Command("Back",
047: Command.BACK, 1);
048: private static final Command CMD_SHOW = new Command("Show",
049: Command.SCREEN, 1);
050:
051: /**
052: * The labels for the supported textboxs.
053: */
054: static final String[] textBoxLabels = { "Any Character", "E-Mail",
055: "Number", "Decimal", "Phone", "Url" };
056:
057: /**
058: * The supported textbox types.
059: */
060: static final int[] textBoxTypes = { TextField.ANY,
061: TextField.EMAILADDR, TextField.NUMERIC, TextField.DECIMAL,
062: TextField.PHONENUMBER, TextField.URL };
063: private Display display;
064: private ChoiceGroup types;
065: private ChoiceGroup options;
066: private Form mainForm;
067: private boolean firstTime;
068:
069: public TextBoxDemo() {
070: display = Display.getDisplay(this );
071: firstTime = true;
072: }
073:
074: protected void startApp() {
075: if (firstTime) {
076: mainForm = new Form("Select a Text Box Type");
077: mainForm.append("Select a text box type");
078:
079: // the string elements will have no images
080: Image[] imageArray = null;
081:
082: types = new ChoiceGroup("Choose type", Choice.EXCLUSIVE,
083: textBoxLabels, imageArray);
084: mainForm.append(types);
085:
086: // advanced options
087: String[] optionStrings = { "As Password", "Show Ticker" };
088: options = new ChoiceGroup("Options", Choice.MULTIPLE,
089: optionStrings, null);
090: mainForm.append(options);
091: mainForm.addCommand(CMD_SHOW);
092: mainForm.addCommand(CMD_EXIT);
093: mainForm.setCommandListener(this );
094: firstTime = false;
095: }
096:
097: display.setCurrent(mainForm);
098: }
099:
100: protected void destroyApp(boolean unconditional) {
101: }
102:
103: protected void pauseApp() {
104: }
105:
106: public void commandAction(Command c, Displayable d) {
107: if (c == CMD_EXIT) {
108: destroyApp(false);
109: notifyDestroyed();
110: } else if (c == CMD_SHOW) {
111: // these are the images and strings for the choices.
112: Image[] imageArray = null;
113: int index = types.getSelectedIndex();
114: String title = textBoxLabels[index];
115: int choiceType = textBoxTypes[index];
116: boolean[] flags = new boolean[2];
117: options.getSelectedFlags(flags);
118:
119: if (flags[0]) {
120: choiceType |= TextField.PASSWORD;
121: }
122:
123: TextBox textBox = new TextBox(title, "", 50, choiceType);
124:
125: if (flags[1]) {
126: textBox.setTicker(new Ticker("TextBox: " + title));
127: }
128:
129: textBox.addCommand(CMD_BACK);
130: textBox.setCommandListener(this);
131: display.setCurrent(textBox);
132: } else if (c == CMD_BACK) {
133: display.setCurrent(mainForm);
134: }
135: }
136: }
|