Implementing the MIDlet MainView class

To implement the MainView class:

  1. Create the MainView.java class file.

  2. Import the required packages and classes.

    package com.nokia.example.imagescaler;
    
    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.Form;
    import javax.microedition.lcdui.Image;
    import javax.microedition.lcdui.StringItem;
    
    import com.nokia.mid.ui.FileSelect;
    import com.nokia.mid.ui.FileSelectDetail;
  3. Create the MainView class to extend Form and implement CommandListener.

    public class MainView
                extends Form
                implements CommandListener
    {
  4. Create the required constants and variables.

    private static final String LOGO_IMAGE_URI = "/logo.png";
    private static final String INFO_TEXT =
        "This application helps you in downscaling the images using the Image "
        + "Scaling API.";
    private static final int ERROR_DIALOG_TIMEOUT = 5000;
    
    private final Command exitCommand = new Command("Exit", Command.EXIT, 1);
    private final Command browseImagesCommand = new Command("Browse images", Command.OK, 1);
    private Main main = null;
    private ImageCanvas imageCanvas = null;
    private Image logoImage = null;
    private StringItem stringItem = null;
    private String originalImageUrl = null;
  5. Create the MainView class constructor. In the constructor, create the Image and the Commnad Buttons and add it to Screen.

    public MainView(String title, Main main) {
        super(title);
    
        this.main = main;
    
        try {
            logoImage = Image.createImage(LOGO_IMAGE_URI);
        }
        catch (IOException e) {
        }
    
        stringItem = new StringItem("About", INFO_TEXT);
        append(stringItem);
    
        addCommand(browseImagesCommand);
        addCommand(exitCommand);
        setCommandListener(this);
    
    }
  6. Implement commandAction method from the CommandListener interface to capture UI events and handle them to call the respective functions calls.

    public void commandAction(Command command, Displayable displayable) {
        final MainView mainView = this;
    
        if (command == exitCommand) {
            main.quit();
        }
        else if (command == browseImagesCommand) {
            new Thread() {
                public void run() {
                    try {
                        FileSelectDetail selectedFiles[] =
                            FileSelect.launch(Utils.PHOTOS_DIR,
                                              FileSelect.MEDIA_TYPE_PICTURE,
                                              false);
    
                        if (selectedFiles != null && selectedFiles.length > 0) {
                            originalImageUrl = selectedFiles[0].url;
    
                            if (imageCanvas == null) {
                                imageCanvas = new ImageCanvas(mainView);
                            }
    
                            if (imageCanvas.loadImage(originalImageUrl) == true) {
                                Display.getDisplay(main).setCurrent(imageCanvas);
                            }
                        }
                    }
                    catch (Exception e) {
                    }
                }
            }.start();
        }
    }
  7. Create displayThis method, to show this (MainView) view on the Screen.

    public void displayThis() {
        Display.getDisplay(main).setCurrent(this);
        imageCanvas = null;
    }
  8. Create showMessage method to display the given message on the Screen.

    public void showMessage(AlertType alertType, String message) {
        Displayable currentDisplayable = Display.getDisplay(main).getCurrent();
    
        if (currentDisplayable != this || currentDisplayable != imageCanvas) {
            if (imageCanvas != null) {
                Display.getDisplay(main).setCurrent(imageCanvas);
            }
            else {
                Display.getDisplay(main).setCurrent(this);
            }
        }
    
        String title = null;
    
        if (alertType == AlertType.ERROR) {
            title = "Error";
        }
        else {
            title = null;
        }
    
        Alert errorAlert = new Alert(title, message, logoImage, alertType);
        errorAlert.setTimeout(ERROR_DIALOG_TIMEOUT);
        Display.getDisplay(main).setCurrent(errorAlert);
    }
    }