/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MobileCommunication;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;


/**
 * @author i7948162
 */
public class MobileCommunication extends MIDlet implements CommandListener,ItemStateListener {
    private Display display;
    private Form selectionForm;
    private Form clientForm;
    private Form serverForm;
    private ChoiceGroup options;
    private TextField receivedText;
    private TextField transmitText;
    private TextField receivedText1;
    private TextField transmitText1;
    private TextField portNumberServer;
    private TextField portNumberClient;
    private TextField IPaddress;

    static final Command transmittCommand = new Command("Transmit", Command.OK,1);
    static final Command backCommand = new Command("Back", Command.BACK,0);

    public MobileCommunication() {

            selectionForm = new Form("select what to be");
            serverForm = new Form("Server.");
            clientForm = new Form("Client.");
            receivedText = new TextField("Received Text:","",32,TextField.UNEDITABLE);
            transmitText = new TextField("Transmit Text:","",32,TextField.UNEDITABLE);
            receivedText1 = new TextField("Received Text:","",32,TextField.UNEDITABLE);
            transmitText1 = new TextField("Transmit Text:","",32,TextField.UNEDITABLE);
            portNumberServer = new TextField("Port Number:","",32,TextField.UNEDITABLE);
            portNumberClient = new TextField("Port Number:","",32,TextField.UNEDITABLE);
            IPaddress = new TextField("IP Address:","",32,TextField.UNEDITABLE);
            options = new ChoiceGroup("Client or Server",Choice.EXCLUSIVE);
            serverForm.append(receivedText);
            serverForm.append(transmitText);
            serverForm.append(portNumberServer);

            clientForm.append(receivedText1);
            clientForm.append(transmitText1);
            clientForm.append(portNumberClient);
            clientForm.append(IPaddress);

            options.append("Client.",null);
            options.append("Server.",null);
            selectionForm.append(options);
            serverForm.addCommand(backCommand);
            serverForm.addCommand(transmittCommand);
            clientForm.addCommand(backCommand);
            clientForm.addCommand(transmittCommand);
            serverForm.setCommandListener(this);
            clientForm.setCommandListener(this);
            selectionForm.setCommandListener(this);
            selectionForm.setItemStateListener(this);
        }


    public void itemStateChanged (Item item)  {

if (item == options) {
 String selected = options.getString(options.getSelectedIndex());
 if(selected.equalsIgnoreCase("Server.")) {
     System.out.println("You are a server.");
            display.setCurrent(serverForm);

 } else {
     System.out.println("You are a Client.");
     
           display.setCurrent(clientForm);

     
             

 }
}


    }
    public void startApp() {
         display = Display.getDisplay(this);
         display.setCurrent(selectionForm);
         
    }
    
    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    public void commandAction(Command c, Displayable d) {
      
        String event = null;
        event = c.getLabel();

        if (event.equals ("Back"))
            display.setCurrent(selectionForm);







        }


        }

            
