/** * 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; import java.util.Date; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.DateField; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Gauge; import javax.microedition.lcdui.Item; import javax.microedition.lcdui.ItemStateListener; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; /** * A simple MIDlet composed of few commonly used UI components. In addition, * the example demonstrates how to show a custom Canvas as a second view. */ public class SimpleTest extends MIDlet implements CommandListener, ItemStateListener { // Constants private static final int MAX_GAUGE_VALUE = 100; private static final int INITIAL_GAUGE_VALUE = 10; // Members private Form myForm = null; private Gauge myGauge = null; private TextField myTextField = null; private DateField myDateField = null; private HelloCanvas myCanvas = null; private int previousGaugeValue = INITIAL_GAUGE_VALUE; private final Command exitCommand = new Command("Exit", Command.EXIT, 1); private final Command backCommand = new Command("Back", Command.BACK, 1); private final Command displayCommand = new Command("Show canvas", Command.SCREEN, 2); /** * Constructor. */ public SimpleTest() { // Create a new form to contain all the UI elements myForm = new Form("Simple Test"); // Create a new date field with the current time myDateField = new DateField("Date", DateField.DATE_TIME); myDateField.setDate(new Date()); // Create a new gauge (slider) component with MAX_GAUGE_VALUE as the // maximum value and INITIAL_GAUGE_VALUE as the initial value myGauge = new Gauge("Gauge value", true, MAX_GAUGE_VALUE, INITIAL_GAUGE_VALUE); // Create a new text field component with maximum size of three // characters, and expect the characters to be numeric myTextField = new TextField("Enter value (0 to " + MAX_GAUGE_VALUE + ")", String.valueOf(INITIAL_GAUGE_VALUE), 3, TextField.NUMERIC); // Append the created components into the form myForm.append(myDateField); myForm.append(myGauge); myForm.append(myTextField); // Add commands to the form and set the form as both the command and // the item listener myForm.addCommand(displayCommand); myForm.addCommand(exitCommand); myForm.setCommandListener(this); myForm.setItemStateListener(this); // Create a custom canvas instance myCanvas = new HelloCanvas(); myCanvas.addCommand(backCommand); myCanvas.setCommandListener(this); } /** * From MIDlet. */ public void startApp() { Display.getDisplay(this).setCurrent(myForm); } /** * From MIDlet. */ public void pauseApp() { } /** * From MIDlet. */ public void destroyApp(boolean unconditional) throws MIDletStateChangeException { } /** * From CommandListener. */ public void commandAction(Command command, Displayable displayable) { if (command == exitCommand) { // Exit the app notifyDestroyed(); } else if (command == backCommand) { // This command was executed in custom Canvas. Return back to the // original form view. Display.getDisplay(this).setCurrent(myForm); } else if (command == displayCommand) { // Show the custom Canvas Display.getDisplay(this).setCurrent(myCanvas); myCanvas.showText(); } } /** * From ItemStateListener. * * If the value of the text field has changed, updates the gauge * accordingly. Similarly, if the value of the gauge has changed, the text * in the text field is updated. */ public void itemStateChanged(Item item) { if (item == myTextField) { String valueString = myTextField.getString(); if (valueString.length() > 0) { final int value = Integer.parseInt(valueString); if (value > MAX_GAUGE_VALUE) { // Invalid value myTextField.setString(String.valueOf(previousGaugeValue)); } else { myGauge.setValue(value); previousGaugeValue = value; } } } else if (item == myGauge) { final int value = myGauge.getValue(); myTextField.setString(String.valueOf(value)); previousGaugeValue = value; } } }