code example

package com.balbes.comics;
 
import java.rmi.RemoteException;
 
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.xml.rpc.Stub;
 
import com.balbes.web.ArrayOfString;
import com.balbes.web.ComicServiceJ2MESoap_Stub;
 
 
public class ComicMasterMIDlet extends MIDlet implements Runnable, CommandListener, ItemCommandListener {
    private static final int TITLES_FORM_REQUESTED = 0;
    private static final int ISSUES_FORM_REQUESTED = 1;
 
    private int userid = 0;
    private String webServiceURL = "http://localhost/ComicServiceCF/ComicServiceJ2ME.asmx";
    private Command issuesCommand = new Command("Issues", Command.SCREEN, 1);
    private Command backCommand = new Command("Back", Command.BACK, 1);
    private int requestedForm;
    private String selectedTitle;
 
    protected void startApp() throws MIDletStateChangeException {
        Form form = new Form("Titles");
        form.append("Loading...");
        Display.getDisplay(this).setCurrent(form);
        showForm(TITLES_FORM_REQUESTED);
    }
 
    protected void pauseApp() {}
 
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
 
    public void commandAction(Command cmd, Item item) {
        if (item instanceof StringItem) {
            selectedTitle = ((StringItem)item).getText();
            showForm(ISSUES_FORM_REQUESTED);
        }
    }
 
    private void showForm(int requestedForm) {
        this.requestedForm = requestedForm;
        new Thread(this).start();        
    }
    public void run() {
        if (requestedForm == TITLES_FORM_REQUESTED) displayTitlesForm();
        else if (requestedForm == ISSUES_FORM_REQUESTED) displayIssuesForm(); 
    }
 
    private void displayTitlesForm() { 
        Form form = new Form("Titles");
        try {
            ComicServiceJ2MESoap_Stub service = new ComicServiceJ2MESoap_Stub();
            service._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, webServiceURL);
            service._setProperty(Stub.SESSION_MAINTAIN_PROPERTY, Boolean.FALSE);
            ArrayOfString titlesArray = service.getTitles(userid);
            String[] titles = titlesArray.getString();
            for (int i=0; i<titles.length; i++) {
                StringItem item = new StringItem(null, titles[i], Item.BUTTON);
                form.append(item);
                item.setLayout(Item.LAYOUT_2 | Item.LAYOUT_NEWLINE_AFTER);
                item.addCommand(issuesCommand);
                item.setItemCommandListener(this);
            }
        } catch (RemoteException e) {
            form.append("An error occured.");
            form.append(e.getMessage());
        }
        Display.getDisplay(this).setCurrent(form);
    }
 
    private void displayIssuesForm() {
        Form form = new Form(selectedTitle);
        form.addCommand(backCommand);
        form.setCommandListener(this);
        try {
            ComicServiceJ2MESoap_Stub service = new ComicServiceJ2MESoap_Stub();
            service._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, webServiceURL);
            service._setProperty(Stub.SESSION_MAINTAIN_PROPERTY, Boolean.FALSE);
            ArrayOfString issuesArray = service.getConditions(selectedTitle);
            String[] issues = issuesArray.getString();
            for (int i=0; i<issues.length; i++) {
                form.append(Integer.toString(i));
                StringItem item = new StringItem(null, issues[i], Item.PLAIN);
                item.setLayout(Item.LAYOUT_2 | Item.LAYOUT_NEWLINE_AFTER);
                form.append(item);
            }
        } catch (RemoteException e) {
            form.append("An error occured.");
            form.append(e.getMessage());
        }
        Display.getDisplay(this).setCurrent(form);
 
    }
 
    public void commandAction(Command arg0, Displayable arg1) {
        showForm(TITLES_FORM_REQUESTED);
    }
}