
/*
 * $Id: MidpClient.java,v 1.2 2003/06/16 13:31:27 tbayer Exp $
 *
 * Copyright (c) 2002 Orientation in Objects GmbH
 * Weinheimer Str. 68, D - 68309 Mannheim, Germany
 * All rights reserved.
 *
 * Reverse Engineering, Aendern und Erweitern dieser Software ist nur
 * mit ausdruecklicher schriftlicher Genehmigung von Orientation in
 * Objects gestattet.
 */

/*
   $Log: MidpClient.java,v $
   Revision 1.2  2003/06/16 13:31:27  tbayer
   Umstellung auf JDK 1.4 Homepage

   Revision 1.1  2002/04/03 14:47:10  tbayer
   *** empty log message ***

 */
package de.oio.zeiterfassung.midp;

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.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStoreException;

/**
 * GUI Komponente eines Zeiterfassungs-MIDlets.
 *
 * @author $Author: tbayer $
 * @version $Revision: 1.2 $<br/>
 * $Date: 2003/06/16 13:31:27 $<br/>
 */
public class MidpClient extends MIDlet implements CommandListener {

    private final String OIO_CLOCKING_ENTRY_SERVLET = "http://localhost:8080/ZeiterfassungServlet/MIDPServlet";
    private Display      display;
    private Command      cleanCommand;
    private Command      exitCommand;
    private Command      inCommand;
    private Command      outCommand;
    private Command      postCommand;
    private Form         inputScreen;
    private StringItem   stringItem;

    public MidpClient() {

        display      = Display.getDisplay(this);
        exitCommand  = new Command("Exit", Command.EXIT, 1);
        inCommand    = new Command("In", Command.OK, 1);
        outCommand   = new Command("Out", Command.OK, 1);
        postCommand  = new Command("Post", Command.OK, 1);
        cleanCommand = new Command("Clean", Command.OK, 1);
    }

    public void startApp() {
        generateInputScreen();
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

    public void commandAction(Command c, Displayable d) {

        try {
            if (d == inputScreen) {
                if (c == exitCommand) {
                    destroyApp(false);
                    notifyDestroyed();
                } else if (c == inCommand) {
                    stringItem.setText(ClockingEntry.getInClockingEntry().toString());
                    ClockingEntryRecordStore.getInstance().addClockingEntry(ClockingEntry.getInClockingEntry());
                } else if (c == outCommand) {
                    stringItem.setText(ClockingEntry.getOutClockingEntry().toString());
                    ClockingEntryRecordStore.getInstance().addClockingEntry(ClockingEntry.getOutClockingEntry());
                } else if (c == postCommand) {
                    ClockingEntryConnectionHelper.getInstance()
                        .setClockingEntries(ClockingEntryRecordStore.getInstance().getAllEntries());
                    ClockingEntryConnectionHelper.getInstance().setURL(OIO_CLOCKING_ENTRY_SERVLET);
                    stringItem.setText(ClockingEntryConnectionHelper.getInstance().postClockingEntries());
                } else if (c == cleanCommand) {
                    ClockingEntryRecordStore.getInstance().removeAllEntries();
                    stringItem.setText("Einträge gelöscht");
                }
            }
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }

    private void generateInputScreen() {

        if (inputScreen == null) {
            inputScreen = new Form("Stechuhr");
            stringItem  = new StringItem("Letzte Stechuhrzeit", "");

            inputScreen.append(stringItem);
            inputScreen.addCommand(exitCommand);
            inputScreen.addCommand(inCommand);
            inputScreen.addCommand(outCommand);
            inputScreen.addCommand(postCommand);
            inputScreen.addCommand(cleanCommand);
            inputScreen.setCommandListener(this);
        }

        display.setCurrent(inputScreen);
    }
}

