001: /*
002: * Copyright (c) 2007, Sun Microsystems, Inc.
003: *
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: * * Neither the name of Sun Microsystems, Inc. nor the names of its
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
023: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
024: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: /*
034: * Converter.java
035: */
036: package converter;
037:
038: import javax.microedition.lcdui.*;
039:
040: /**
041: *
042: */
043: public class Converter extends Form implements CommandListener,
044: ItemStateListener {
045:
046: private ConverterMIDlet midlet;
047:
048: private int[] translate;
049:
050: /**
051: * constructor
052: */
053: public Converter(ConverterMIDlet midlet) {
054: super ("Currency Converter");
055: this .midlet = midlet;
056: this .translate = new int[midlet.currencies.length];
057: int current = 0;
058: for (int i = 0; i < translate.length; i++) {
059: if (midlet.selected[i]) {
060: translate[current++] = i;
061: append(new TextField(midlet.currencies[i], "", 12,
062: TextField.NUMERIC));
063: }
064: }
065: try {
066: // Set up this form to listen to command events
067: setCommandListener(this );
068: // Set up this form to listen to changes in the internal state of its interactive items
069: setItemStateListener(this );
070: // Add the Curreencies command
071: addCommand(new Command("Currencies", Command.OK, 1));
072: // Add the Exit command
073: addCommand(new Command("Exit", Command.EXIT, 1));
074: } catch (Exception e) {
075: e.printStackTrace();
076: }
077: }
078:
079: /**
080: * Called when user action should be handled
081: */
082: public void commandAction(Command command, Displayable displayable) {
083: if (command.getCommandType() == Command.EXIT) {
084: midlet.destroyApp(true);
085: } else if (command.getCommandType() == Command.OK) {
086: midlet.showSettings();
087: }
088: }
089:
090: /**
091: * Called when internal state of any item changed
092: */
093: public void itemStateChanged(Item item) {
094: try {
095: long value = Long.parseLong(((TextField) item).getString());
096: int from = 0;
097: while (get(from) != item)
098: from++;
099: from = translate[from];
100: for (int i = 0; i < size(); i++) {
101: int to = translate[i];
102: if (from != to) {
103: ((TextField) get(i)).setString(String
104: .valueOf(midlet.convert(value, from, to)));
105: }
106: }
107: } catch (NumberFormatException nfe) {
108: for (int i = 0; i < size(); i++) {
109: ((TextField) get(i)).setString("");
110: }
111: }
112: }
113: }
|