HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Building A Connected MIDlet"
Vol. 9, Issue 3, p. 16

	



Listing 1

private void doEcho() {
Thread thread = new Thread(this);
userInput = getInputUI.getInputText();
runWithProgress(thread, "Working...", true);
}

private void runWithProgress(Thread thread,
String title,
boolean stoppable) {
progressObserverUI.init(title, stoppable);
setCurrent(progressObserverUI);
thread.start();
}

public void run() {
try {
String result = model.echo(userInput);
showOutputUI.setResult(result);
showOutputUI.addFields();
setCurrent(showOutputUI);
} catch (ApplicationException ae) {
switch(ae.getCode()) {
case ErrorMessageCode.ERROR_OPERATION_INTERRUPTED:
showOutputUI.setResult("[operation terminated by user]");
showOutputUI.addFields();
showInfoAlert(null, ae.getMessage(), showOutputUI);
break;

...
}
}
...
}

Listing 2

public String echo(String input) throws ApplicationException {
try {
HttpConnection con = openConnection();
checkAndUpdateProgress();

DataOutputStream dataOutStream = openConnectionOutputStream(con);
dataOutStream.writeUTF(input);
checkAndUpdateProgress();

// Get the response from the server.
DataInputStream dataInStream = openConnectionInputStream(con);
String fromServer = null;
fromServer = dataInStream.readUTF();
checkAndUpdateProgress();

closeConnection(con, dataOutStream, dataInStream); 
return fromServer;
} catch (IOException ioe) {
throw new ApplicationException(ErrorMessageCode.ERROR_COMM_FAILURE);
}
}

Listing 3

public void commandAction(Command command, Displayable displayable) {
if (command == okCommand && displayable == getInputUI) {
if (getInputUI.hasValidInput()) {
doEcho();
}
else {
getInputUI.tryAgain();

...
}

boolean hasValidInput() {
String input = getInputText();
return !(input == null || input.equals(""));
}

Listing 4

/** Use this error code if a more specific one does not exist. */
public static final int ERROR_GENERAL = 0;

/** Indicates a communication failure between the client and remote server. */
public static final int ERROR_COMM_FAILURE = 1;

/** The user did not enter any text in the input screen. */
public static final int ERROR_BLANK_INPUT = 2;

...

Listing 5

public ApplicationException(int code) {
this.code = code;

switch(code) {
case ErrorMessageCode.ERROR_GENERAL: 
setMessage("An ApplicationException has occurred.");
break;

case ErrorMessageCode.ERROR_COMM_FAILURE:
setMessage("A communication error has occurred.");
break;

case ErrorMessageCode.ERROR_OPERATION_INTERRUPTED:
setMessage("The current operation has been interrupted.");
break;

case ErrorMessageCode.ERROR_BLANK_INPUT:
setMessage("User input cannot be blank.");
break;

default:
setMessage("An unidentified error has occurred.");
break;

}
}

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.