CapitalMIDlet.java

/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */
package com.nokia.example.capitals;

import com.nokia.example.capitals.ui.ErrorScreen;
import com.nokia.example.capitals.ui.WelcomeScreen;
import com.nokia.example.capitals.ui.ResultScreen;
import com.nokia.example.capitals.ui.WaitingScreen;
import com.nokia.example.capitals.ui.MainScreen;
import com.nokia.example.capitals.ui.Screen;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * Main MIDlet class. This class takes care of handling the Display
 * and listen for the result of WebServices requests
 */
public class CapitalMIDlet extends MIDlet implements PosterListener {

    // location of the web service
    private String endPoint;

    protected void startApp() throws MIDletStateChangeException {
        endPoint = getAppProperty("endPoint");
        // create a new WelcomeScreen and set it active
        changeScreen(new WelcomeScreen(this));
    }

    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
    }

    protected void pauseApp() {
    }

    /**
     * Change the currently active screen
     */
    void changeScreen(Screen screen) {
        screen.makeActive();
    }

    /**
     * Called to force termination of MIDlet
     */
    public void quit() {
        notifyDestroyed();
    }

    /**
     * This method calls the web service
     */
    public void requestCapital(String country) {
        changeScreen(new WaitingScreen(this));
        Poster poster = new Poster(this, endPoint);
        poster.requestCapital(country);
    }

    public void showMainScreen() {
        changeScreen(new MainScreen(this));
    }

    /**
     * Called when a valid result is retrieved from the
     * WebService
     */
    public void onCapitalRequestComplete(String nation, String capital) {
        changeScreen(new ResultScreen(this, nation, capital));
    }

    /**
     * Called when an error happens during the WebService call
     */
    public void onCapitalRequestError(String code) {
        changeScreen(new ErrorScreen(this, code));
    }
}