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.bluetooth.demo;
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.lcdui.List;
039: import javax.microedition.midlet.MIDlet;
040:
041: /**
042: * Contains the Bluetooth API demo, that allows to download
043: * the specific images from the other devices.
044: *
045: * @version ,
046: */
047: public final class DemoMIDlet extends MIDlet implements CommandListener {
048: /** The messages are shown in this demo this amount of time. */
049: static final int ALERT_TIMEOUT = 2000;
050:
051: /** A list of menu items */
052: private static final String[] elements = { "Server", "Client" };
053:
054: /** Soft button for exiting the demo. */
055: private final Command EXIT_CMD = new Command("Exit", Command.EXIT,
056: 2);
057:
058: /** Soft button for launching a client or sever. */
059: private final Command OK_CMD = new Command("Ok", Command.SCREEN, 1);
060:
061: /** A menu list instance */
062: private final List menu = new List("Bluetooth Demo", List.IMPLICIT,
063: elements, null);
064:
065: /** A GUI part of server that publishes images. */
066: private GUIImageServer imageServer;
067:
068: /** A GUI part of client that receives image from client */
069: private GUIImageClient imageClient;
070:
071: /** value is true after creating the server/client */
072: private boolean isInit = false;
073:
074: /**
075: * Constructs main screen of the MIDlet.
076: */
077: public DemoMIDlet() {
078: menu.addCommand(EXIT_CMD);
079: menu.addCommand(OK_CMD);
080: menu.setCommandListener(this );
081: }
082:
083: /**
084: * Creates the demo view and action buttons.
085: */
086: public void startApp() {
087: if (!isInit) {
088: show();
089: }
090: }
091:
092: /**
093: * Destroys the application.
094: */
095: protected void destroyApp(boolean unconditional) {
096: if (imageServer != null) {
097: imageServer.destroy();
098: }
099:
100: if (imageClient != null) {
101: imageClient.destroy();
102: }
103: }
104:
105: /**
106: * Does nothing. Redefinition is required by MIDlet class.
107: */
108: protected void pauseApp() {
109: }
110:
111: /**
112: * Responds to commands issued on "client or server" form.
113: *
114: * @param c command object source of action
115: * @param d screen object containing the item action was performed on
116: */
117: public void commandAction(Command c, Displayable d) {
118: if (c == EXIT_CMD) {
119: destroyApp(true);
120: notifyDestroyed();
121:
122: return;
123: }
124:
125: switch (menu.getSelectedIndex()) {
126: case 0:
127: imageServer = new GUIImageServer(this );
128:
129: break;
130:
131: case 1:
132: imageClient = new GUIImageClient(this );
133:
134: break;
135:
136: default:
137: System.err.println("Unexpected choice...");
138:
139: break;
140: }
141:
142: isInit = true;
143: }
144:
145: /** Shows main menu of MIDlet on the screen. */
146: void show() {
147: Display.getDisplay(this ).setCurrent(menu);
148: }
149:
150: /**
151: * Returns the displayable object of this screen -
152: * it is required for Alert construction for the error
153: * cases.
154: */
155: Displayable getDisplayable() {
156: return menu;
157: }
158: } // end of class 'DemoMIDlet' definition
|