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 datagram;
033:
034: import java.io.*;
035:
036: import javax.microedition.io.*;
037: import javax.microedition.lcdui.*;
038: import javax.microedition.midlet.*;
039:
040: public class DatagramMIDlet extends MIDlet implements CommandListener {
041: private static final String SERVER = "Server";
042: private static final String CLIENT = "Client";
043: private static final String[] names = { SERVER, CLIENT };
044: private static Display display;
045: private Form f;
046: ChoiceGroup cg;
047: private boolean isPaused;
048: private Command exitCommand = new Command("Exit", Command.EXIT, 1);
049: private Command startCommand = new Command("Start", Command.ITEM, 1);
050:
051: public DatagramMIDlet() {
052: display = Display.getDisplay(this );
053: f = new Form("Datagram Demo");
054: cg = new ChoiceGroup("Please select peer", Choice.EXCLUSIVE,
055: names, null);
056: f.append(cg);
057:
058: f.addCommand(exitCommand);
059: f.addCommand(startCommand);
060: f.setCommandListener(this );
061:
062: display.setCurrent(f);
063: }
064:
065: public static Display getDisplay() {
066: return display;
067: }
068:
069: public boolean isPaused() {
070: return isPaused;
071: }
072:
073: public void startApp() {
074: isPaused = false;
075: }
076:
077: public void pauseApp() {
078: isPaused = true;
079: }
080:
081: public void destroyApp(boolean unconditional) {
082: }
083:
084: public void commandAction(Command c, Displayable s) {
085: if (c == exitCommand) {
086: destroyApp(true);
087: notifyDestroyed();
088: } else if (c == startCommand) {
089: String name = cg.getString(cg.getSelectedIndex());
090:
091: if (name.equals(SERVER)) {
092: Server server = new Server(this );
093: server.start();
094: } else {
095: Client client = new Client(this);
096: client.start();
097: }
098: }
099: }
100: }
|