import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import java.util.*;
import java.rmi.RemoteException;

import helloservice.*;

public class HelloMidlet extends MIDlet implements Runnable, CommandListener {

  Hello_Stub service;
  Display display;
  private Form f;
  private StringItem si;
  private TextField tf;
  private Command sendCommand = new Command("Send", Command.ITEM, 1);
  private Command exitCommand = new Command("Exit", Command.EXIT, 1);

  String name = "";

  public void startApp() {
    display = Display.getDisplay(this);
    f = new Form("Hello Client");
    tf = new TextField("Send:", "", 30, TextField.ANY);
    si = new StringItem("Status:" , " ");
    f.append(tf);
    f.append(si);
    f.addCommand(sendCommand);
    f.addCommand(exitCommand);
    f.setCommandListener(this);
    display.setCurrent(f);
  }

  public void pauseApp() {}

  public void destroyApp(boolean unconditional) {}

  public void commandAction(Command c, Displayable d) {
    if (c == sendCommand) {
      name = tf.getString();
      si.setText("Message sent: " + name);
      /* 
       * Start a new thread so that the remote invocation won't block 
       * the process.
       */
      new Thread(this).start();		
    }
    if (c == exitCommand) {
      notifyDestroyed();
      destroyApp(true);
    }
  }

  public void run() {
    try {
      service = new Hello_Stub();
      service._setProperty(Hello_Stub.SESSION_MAINTAIN_PROPERTY, new Boolean(true));
      String msg = service.getHello(name);
      si.setText("Message Receive: " + msg);
    } catch (Exception exception) {}
  }
}
