package helloworld5;

import java.io.IOException;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * MIDlet mit Commands zum Beenden, TextBox löschen und Informationen anzeigen.
 * 
 * @author Ralf Kunze (rkunze@uos.de), Institut fuer Informatik, Universitaet
 *         Osnabrueck
 * @date 29.06.2007
 */
public class HelloWorld extends MIDlet {

    private Display display;

    private TextBox textBox;

    private Image image;

    private Command exitCom;
    
    private Command clearCom;
    
    private Command showCom;
    
    public HelloWorld() {
        try {
            image = Image.createImage("/pics/ralf.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        textBox = new TextBox("Test", null, 100, TextField.ANY);
        exitCom = new Command("Exit", Command.EXIT,1);
        textBox.addCommand(exitCom);
    
        clearCom = new Command("Löschen", "Inhalt Löschen", Command.SCREEN,1);
        textBox.addCommand(clearCom);
        
        showCom = new Command("Bild zeigen", "Bild in Alert anzeigen", Command.SCREEN,2);
        textBox.addCommand(showCom);

        textBox.setCommandListener(new CommandListener() {
            public void commandAction(Command c, Displayable d) {
                if (c==exitCom) {
                    try {
                        destroyApp(false);
                        notifyDestroyed();
                    } catch (MIDletStateChangeException e) {
                        e.printStackTrace();
                    }
                } else if(c == clearCom) {
                    textBox.setString("");
                } else if(c == showCom) {
                    Alert imageAlert;
                    if(image!=null) {
                        imageAlert = new Alert("Image", "", image, AlertType.INFO);
                    } else {
                        imageAlert = new Alert("");
                    }
                    imageAlert.setTimeout(2000);
                    display.setCurrent(imageAlert);
                }
            }
        });
        
        
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

    }

    protected void pauseApp() {
    }

    protected void startApp() throws MIDletStateChangeException {
        if (display == null) {

            display = Display.getDisplay(this);
            
            textBox.setString("Hello World ... ");
            
            display.setCurrent(textBox);
        } else {
            textBox.insert("startApp()", textBox.getString().length());
        }
    }

}
