home *** CD-ROM | disk | FTP | other *** search
- import java.io.DataInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Vector;
- import javax.microedition.io.Connector;
- import javax.microedition.io.HttpConnection;
- import javax.microedition.io.StreamConnection;
- 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.List;
- import javax.microedition.lcdui.TextField;
- import javax.microedition.midlet.MIDlet;
-
- public class ImageViewer extends MIDlet implements CommandListener {
- final String LABEL_COMMAND_EXIT = "Exit";
- final String LABEL_COMMAND_MENU = "Menu";
- final String LABEL_COMMAND_NEW = "New URL";
- final String LABEL_COMMAND_EDIT = "Edit URL";
- final String LABEL_COMMAND_DELETE = "Delete URL";
- final String LABEL_COMMAND_OK = "Ok";
- final String LABEL_COMMAND_CANCEL = "Cancel";
- final String LABEL_NEWURL = "file:// New URL";
- protected Display display;
- protected List urlList;
- protected Form editURLForm;
- protected ImageCanvas canvas;
- protected TextField labelTextField = new TextField("Label:", (String)null, 32, 0);
- protected TextField urlTextField = new TextField("URL:", (String)null, 250, 4);
- protected int editURLIndex;
- protected Database urlDB;
- protected Vector urls = new Vector();
-
- protected void showURL(String var1) {
- byte[] var2 = null;
- if (var1.startsWith("http:")) {
- HttpConnection var3 = null;
- DataInputStream var4 = null;
-
- try {
- var3 = (HttpConnection)Connector.open(var1, 1);
- int var5 = var3.getResponseCode();
- if (var5 != 200) {
- throw new IOException("HTTP Response Code = " + var5);
- }
-
- int var6 = (int)var3.getLength();
- if (var6 <= 0) {
- throw new IOException("Content length is missing");
- }
-
- var4 = var3.openDataInputStream();
- var2 = new byte[var6];
- var4.readFully(var2);
- } catch (IOException var38) {
- this.display.setCurrent(new Alert("Error", "Unable to read from URL!", (Image)null, AlertType.ERROR));
- } finally {
- try {
- if (var4 != null) {
- var4.close();
- }
-
- if (var3 != null) {
- var3.close();
- }
- } catch (IOException var34) {
- }
-
- }
- } else {
- StreamConnection var40 = null;
- InputStream var42 = null;
-
- try {
- var40 = (StreamConnection)Connector.open(var1, 1);
- var42 = var40.openInputStream();
- if (var42.available() > 0) {
- var2 = new byte[var42.available()];
- var42.read(var2);
- }
- } catch (IOException var36) {
- this.display.setCurrent(new Alert("Error", "Unable to read from URL!", (Image)null, AlertType.ERROR));
- } finally {
- try {
- if (var42 != null) {
- var42.close();
- }
-
- if (var40 != null) {
- var40.close();
- }
- } catch (IOException var33) {
- }
-
- }
- }
-
- if (var2 != null) {
- try {
- Image var41 = Image.createImage(var2, 0, var2.length);
- if (this.canvas == null) {
- this.canvas = new ImageCanvas();
- this.canvas.addCommand(new Command("Exit", 7, 1));
- this.canvas.addCommand(new Command("Menu", 2, 2));
- this.canvas.setCommandListener(this);
- }
-
- this.canvas.setImage(var41);
- this.display.setCurrent(this.canvas);
- } catch (Exception var35) {
- this.display.setCurrent(new Alert("Error", "Unable to decode image!", (Image)null, AlertType.ERROR));
- }
- }
-
- }
-
- public void commandAction(Command var1, Displayable var2) {
- String var3 = var1.getLabel();
- int var4 = var1.getCommandType();
- if (var3 == "Exit") {
- this.destroyApp(false);
- } else if (var1 == List.SELECT_COMMAND) {
- this.showURL(this.getURL(this.urlList.getSelectedIndex())[1]);
- } else if (var3 == "Menu") {
- this.showURLList();
- } else if (var3 == "New URL") {
- this.urlDB.addRecord("file:// New URL".getBytes());
- this.urls.addElement("file:// New URL");
- this.editURLIndex = this.urls.size() - 1;
- this.showEditURLForm();
- } else if (var3 == "Edit URL") {
- this.editURLIndex = this.urlList.getSelectedIndex();
- this.showEditURLForm();
- } else if (var3 == "Ok") {
- String var5 = this.urlTextField.getString().trim() + ' ' + this.labelTextField.getString();
- this.urlDB.setRecord(this.urlDB.getId(this.editURLIndex), var5.getBytes());
- this.urls.setElementAt(var5, this.editURLIndex);
- this.showURLList();
- } else if (var3 == "Cancel") {
- this.showURLList();
- } else if (var3 == "Delete URL") {
- this.urlDB.deleteRecord(this.urlDB.getId(this.urlList.getSelectedIndex()));
- this.urls.removeElementAt(this.urlList.getSelectedIndex());
- this.showURLList();
- }
-
- }
-
- String[] getURL(int var1) {
- String var2 = (String)this.urls.elementAt(var1);
- int var3 = var2.indexOf(32);
- String[] var4 = new String[]{var2.substring(var3 + 1), var2.substring(0, var3)};
- return var4;
- }
-
- void showEditURLForm() {
- if (this.editURLForm == null) {
- this.editURLForm = new Form("Edit URL");
- this.editURLForm.append(this.labelTextField);
- this.editURLForm.append(this.urlTextField);
- this.editURLForm.addCommand(new Command("Ok", 4, 0));
- this.editURLForm.addCommand(new Command("Cancel", 2, 1));
- this.editURLForm.setCommandListener(this);
- }
-
- String[] var1 = this.getURL(this.editURLIndex);
- this.labelTextField.setString(var1[0]);
- this.urlTextField.setString(var1[1]);
- this.display.setCurrent(this.editURLForm);
- }
-
- void showURLList() {
- this.urlList = new List("Image Viewer", 3);
-
- for(int var1 = 0; var1 < this.urls.size(); ++var1) {
- this.urlList.append(this.getURL(var1)[0], (Image)null);
- }
-
- this.urlList.addCommand(new Command("Exit", 2, 0));
- this.urlList.addCommand(new Command("New URL", 1, 1));
- this.urlList.addCommand(new Command("Edit URL", 8, 2));
- this.urlList.addCommand(new Command("Delete URL", 8, 3));
- this.urlList.setCommandListener(this);
- this.display.setCurrent(this.urlList);
- }
-
- protected void startApp() {
- this.display = Display.getDisplay(this);
- this.urlDB = new Database("URLs");
-
- for(int var1 = 0; var1 < this.urlDB.numRecords(); ++var1) {
- this.urls.addElement(new String(this.urlDB.getRecord(this.urlDB.getId(var1))));
- }
-
- this.showURLList();
- }
-
- protected void pauseApp() {
- }
-
- protected void destroyApp(boolean var1) {
- this.urlDB.close();
- this.display.setCurrent((Displayable)null);
- ((MIDlet)this).notifyDestroyed();
- }
- }
-