sábado, 26 de marzo de 2011

Tarea

/*

* To change this template, choose Tools
Templates
* and open the template in the editor.
*/

import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;

/**
* @author cinthia
*/
public class Saludo extends MIDlet implements CommandListener {

private Display display;
List menu = null;
TextBox input = null;
private String url = "http://jimmy.comxa.com/docs/saludo";

static final Command backCommand = new Command("Back", Command.BACK, 0);
static final Command mainMenuCommand = new Command("Main", Command.SCREEN, 1);
static final Command exitCommand = new Command("Exit", Command.STOP, 2);
String currentMenu = null;

public Saludo() {
display = Display.getDisplay(this);

}

public void startApp() {
menu = new List("Menu Items", Choice.IMPLICIT);
menu.append("Datos", null);
menu.append("X", null);

menu.addCommand(exitCommand);
menu.setCommandListener(this);

mainMenu();


}

private void download (String url) throws IOException {
StringBuffer b = new StringBuffer();
InputStream is = null;
HttpConnection c = null;
TextBox t = null;

try {
long len = 0;
int ch = 0;

c = (HttpConnection)Connector.open(url);

is = c.openInputStream();

while ((ch = is.read()) != -1) {
b.append((char)ch);
}

t = new TextBox("Hola...", b.toString(), 1024, 0);

}finally {
if (is != null)
is.close();
if (c != null)
c.close();
}

display.setCurrent(t);
}


public void pauseApp() {

display = null;
menu = null;
input = null;
}

public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
void mainMenu() {
display.setCurrent(menu);
currentMenu = "Main";
}
public void prepare() {
try {
download(url);
} catch(IOException e) {
System.out.println("IOException: " + e);
}
input.addCommand(backCommand);
input.setCommandListener(this);
display.setCurrent(input);
}
public void prepare2() {

try {
download(url);
} catch(IOException e) {
System.out.println("IOException: " + e);
}
input.addCommand(backCommand);
input.setCommandListener(this);
display.setCurrent(input);
}

public void testItem1() {
prepare();
currentMenu = "item1";
}

public void testItem2() {
prepare2();
currentMenu = "item2";
}
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if (label.equals("Exit")) {
destroyApp(true);
} else if (label.equals("Back")) {
if(currentMenu.equals("item1")

currentMenu.equals("item2")) {
mainMenu();
}

} else {
List down = (List)display.getCurrent();
switch(down.getSelectedIndex()) {
case 0: testItem1();break;
case 1: testItem2();break;


}
}
}

}

No hay comentarios:

Publicar un comentario