/** * Copyright (c) 2013 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.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; /** * The main view of the application. */ public class MainView extends Form implements CommandListener { // Constants 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; // Milliseconds // Members 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; /** * Constructor. * @param title The title of this view. * @param main The Main (MIDlet) instance. */ 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); } /** * @see CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable) */ 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(); } } /** * Shows this view. */ public void displayThis() { Display.getDisplay(main).setCurrent(this); imageCanvas = null; } /** * Displays the given message. * @param alertType The type of the alert (message). * @param message The message to show. */ 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); } }