Main.java

/*
 * Copyright © 2012 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.paint;

import com.nokia.example.paint.views.DrawArea;
import com.nokia.example.paint.views.Splash;
import com.nokia.example.paint.views.components.FileSaveDialogS60;
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.Gauge;
import javax.microedition.midlet.*;

public class Main extends MIDlet implements CommandListener {

    private Display display;
    private Splash splash;
    private DrawArea view;
    private static Main self;
    private Alert progress = new Alert("Saving", "Saving image", null, AlertType.INFO);
    private Alert error = new Alert("Error!", "Error happened.", null, AlertType.ERROR);
    private Alert exitPrompt = new Alert("Exit?", "Exit, really?", null, AlertType.CONFIRMATION);
    private Command yes = new Command("Yes", Command.OK, 1);
    private Command no = new Command("No", Command.CANCEL, 2);
    private FileSaveDialogS60 dialogS60 = new FileSaveDialogS60();

    public static Main getInstance() {

        return self;
    }

    public void startApp() {
        if (display == null) {
            display = Display.getDisplay(this);
            self = this;
            splash = new Splash();
            display.setCurrent(splash);
        }
    }

    public void initPaint() {
        view = new DrawArea();
        showDrawing();
        splash = null;
        progress.setTimeout(3600000);
        error.setTimeout(Alert.FOREVER);
        error.setCommandListener(this);
        Gauge g = new Gauge(null, false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
        progress.setIndicator(g);
        exitPrompt.addCommand(yes);
        exitPrompt.addCommand(no);
        exitPrompt.setCommandListener(this);
    }

    public void showDrawing() {
        display.setCurrent(view);
    }

    public DrawArea getDrawArea() {
        return view;
    }

    public void showProgressAlert() {
        display.setCurrent(progress);
    }

    public void showError(String errorMessage) {
        error.setString(errorMessage);
        display.setCurrent(error, view);

    }

    public void hideProgressAlert() {
        if (progress.isShown()) {
            display.setCurrent(view);
        }
    }

    public void showSaveDialogS60() {
        display.setCurrent(dialogS60);
    }

    public void showExitPrompt() {
        display.setCurrent(exitPrompt);
    }

    public synchronized void updateProgressText(String newtext) {
        if (newtext != null) {
            progress.setString(newtext);
        }
    }

    public void commandAction(Command cmnd, Displayable dsplbl) {
        if (cmnd == yes) {
            exit();
        }
        else if (cmnd == no) {
            showDrawing();
        }
        else if (cmnd == Alert.DISMISS_COMMAND) {
            display.setCurrent(view);
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void exit() {
        destroyApp(true);
        notifyDestroyed();
    }
}