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: * ConverterMIDlet.java
035: */
036: package converter;
037:
038: import java.io.*;
039: import javax.microedition.midlet.*;
040: import javax.microedition.lcdui.*;
041: import javax.microedition.rms.*;
042:
043: /**
044: *
045: */
046: public class ConverterMIDlet extends javax.microedition.midlet.MIDlet {
047:
048: private static String storedDataStr = "ConverterData";
049:
050: public String[] currencies = new String[] { "US $", "Yen \u00a5",
051: "Euro \u20ac" };
052:
053: public boolean[] selected = new boolean[] { true, true, true, true };
054:
055: public long[][] rates = { { 1000000, 117580000, 911079 },
056: { 8504, 1000000, 7749 }, { 1097600, 129056000, 1000000 } };
057:
058: private RecordStore storedData;
059:
060: public void startApp() {
061: try {
062: storedData = RecordStore.openRecordStore(storedDataStr,
063: true);
064: if (storedData.getNumRecords() > 0) {
065: DataInputStream in = new DataInputStream(
066: new ByteArrayInputStream(storedData
067: .getRecord(1)));
068: try {
069: int size = in.readInt();
070: currencies = new String[size];
071: selected = new boolean[size];
072: rates = new long[size][];
073: for (int i = 0; i < size; i++) {
074: currencies[i] = in.readUTF();
075: selected[i] = in.readBoolean();
076: rates[i] = new long[size];
077: for (int j = 0; j < size; j++) {
078: rates[i][j] = in.readLong();
079: }
080: }
081: in.close();
082: } catch (IOException ioe) {
083: }
084: }
085: } catch (RecordStoreException e) {
086: }
087: notifySettingsChanged();
088: }
089:
090: public void pauseApp() {
091: }
092:
093: public void destroyApp(boolean unconditional) {
094: try {
095: ByteArrayOutputStream bytes = new ByteArrayOutputStream();
096: DataOutputStream out = new DataOutputStream(bytes);
097: try {
098: out.writeInt(currencies.length);
099: for (int i = 0; i < currencies.length; i++) {
100: out.writeUTF(currencies[i]);
101: out.writeBoolean(selected[i]);
102: for (int j = 0; j < currencies.length; j++) {
103: out.writeLong(rates[i][j]);
104: }
105: }
106: out.close();
107: if (storedData.getNumRecords() > 0)
108: storedData.setRecord(1, bytes.toByteArray(), 0,
109: bytes.size());
110: else
111: storedData.addRecord(bytes.toByteArray(), 0, bytes
112: .size());
113: } catch (IOException ioe) {
114: ioe.printStackTrace();
115: }
116: } catch (RecordStoreException e) {
117: e.printStackTrace();
118: }
119: notifyDestroyed();
120: }
121:
122: public void showSettings() {
123: Display.getDisplay(this ).setCurrent(
124: new CurrenciesSelector(this ));
125: }
126:
127: public void notifySettingsChanged() {
128: Display.getDisplay(this ).setCurrent(new Converter(this ));
129: }
130:
131: public long convert(long frval, int fridx, int toidx) {
132: return (frval * rates[fridx][toidx]) / 1000000;
133: }
134: }
|