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.chooser;
033:
034: import javax.microedition.lcdui.Command;
035: import javax.microedition.lcdui.CommandListener;
036: import javax.microedition.lcdui.Display;
037: import javax.microedition.lcdui.Displayable;
038: import javax.microedition.midlet.MIDlet;
039:
040: public class Chooser extends MIDlet implements CommandListener {
041: private Display display; // Our display
042: private FontChooser fonts;
043: private TextSample sample;
044: private ColorChooser colors;
045: private Command exitCommand = new Command("Exit", Command.EXIT, 1);
046: private Command textColorCommand = new Command("Text Color",
047: Command.SCREEN, 3);
048: private Command backgroundColorCommand = new Command(
049: "Background Color", Command.SCREEN, 4);
050: private Command fontsCommand = new Command("Fonts", Command.SCREEN,
051: 11);
052: private Command okCommand = new Command("Ok", Command.SCREEN, 2);
053: private Command okFgCommand = new Command("Ok", Command.SCREEN, 2);
054: private Command okBgCommand = new Command("Ok", Command.SCREEN, 2);
055:
056: public Chooser() {
057: display = Display.getDisplay(this );
058: sample = new TextSample();
059:
060: sample.addCommand(exitCommand);
061: sample.addCommand(textColorCommand);
062: sample.addCommand(backgroundColorCommand);
063: sample.addCommand(fontsCommand);
064: sample.setCommandListener(this );
065: }
066:
067: /**
068: * Create the FontChooser and make it current
069: */
070: public void startApp() {
071: display.setCurrent(sample);
072: }
073:
074: /**
075: * Pause
076: */
077: public void pauseApp() {
078: }
079:
080: /**
081: * Destroy must cleanup everything.
082: */
083: public void destroyApp(boolean unconditional) {
084: }
085:
086: /*
087: * Respond to a commands issued on any Screen
088: */
089: public void commandAction(Command c, Displayable s) {
090: if (c == exitCommand) {
091: destroyApp(true);
092: notifyDestroyed();
093: } else if (c == fontsCommand) {
094: if (fonts == null) {
095: fonts = new FontChooser();
096: fonts.setFace(sample.getFace());
097: fonts.setStyle(sample.getStyle());
098: fonts.setSize(sample.getSize());
099: fonts.addCommand(okCommand);
100: fonts.setCommandListener(this );
101: }
102:
103: display.setCurrent(fonts);
104: } else if (c == backgroundColorCommand) {
105: if (colors == null) {
106: colors = new ColorChooser(display.isColor());
107: colors.setCommandListener(this );
108: }
109:
110: colors.addCommand(okBgCommand);
111: colors.removeCommand(okFgCommand);
112: colors.setColor(sample.getBackgroundColor());
113: display.setCurrent(colors);
114: } else if (c == textColorCommand) {
115: if (colors == null) {
116: colors = new ColorChooser(display.isColor());
117: colors.setCommandListener(this);
118: }
119:
120: colors.addCommand(okFgCommand);
121: colors.removeCommand(okBgCommand);
122:
123: colors.setColor(sample.getForegroundColor());
124: display.setCurrent(colors);
125: } else if (c == okCommand) {
126: if (s == fonts) {
127: sample.setStyle(fonts.getStyle());
128: sample.setFace(fonts.getFace());
129: sample.setSize(fonts.getSize());
130: }
131:
132: display.setCurrent(sample);
133: } else if (c == okFgCommand) {
134: sample.setForegroundColor(colors.getColor());
135: display.setCurrent(sample);
136: } else if (c == okBgCommand) {
137: sample.setBackgroundColor(colors.getColor());
138: display.setCurrent(sample);
139: }
140: }
141: }
|