Show StatusForm.java syntax highlighted
/*
* Copyright (c) 2004
* Helsinki Institute of Physics
* see LICENSE file for details
*
* StatusForm.java
* Created on Mar 18, 2004
*/
package fi.hip.gb.client;
import java.util.Vector;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.StringItem;
import fi.hip.gb.midlet.core.LiteResult;
import fi.hip.gb.midlet.core.LiteStatus;
import fi.hip.gb.midlet.core.LiteStorage;
/**
* Shows the status of the job for the user with a list of available results.
*
* @author Juho Karppinen
*/
public class StatusForm extends Form implements CommandListener {
/** ui component for selecting result from the list of results */
private ChoiceGroup cgResult;
/** container for the data */
private LiteStorage storage;
private final Command cmdView = new Command("View result", Command.ITEM, 1);
private final Command cmdInfo = new Command("View result info", Command.ITEM, 2);
private final Command cmdDownloadAll = new Command("Download all", Command.ITEM, 2);
private final Command cmdRefresh = new Command("Refresh", Command.SCREEN, 3);
private final Command cmdAbort = new Command("Abort", Command.SCREEN, 4);
private final Command cmdRemove = new Command("Remove", Command.SCREEN, 4);
private final Command cmdSave = new Command("Make available offline", Command.ITEM, 5);
/**
* Constructs the form for showing the result
* @param storageContainer container for the data to be shown
*/
public StatusForm(LiteStorage storageContainer) {
super("Status of " + storageContainer.getDescription().getJobname());
this.storage = storageContainer;
LiteStatus status = this.storage.getStatus();
// we use MIDP2 related methods if the device supports it
boolean midp2 = System.getProperty("microedition.profiles").startsWith("MIDP-2");
StringItem item = new StringItem("State: ", LiteStatus.STATES[status.getState()]);
if(midp2) item.setLayout(Item.LAYOUT_NEWLINE_AFTER);
this.append(item);
item = new StringItem("ID: ", this.storage.getDescription().getJobID().toString());
if(midp2) item.setLayout(Item.LAYOUT_NEWLINE_AFTER);
this.append(item);
String url = this.storage.getStatus().getServiceURL();
item = new StringItem("Host: ", url);
if(midp2) item.setLayout(Item.LAYOUT_NEWLINE_AFTER);
this.append(item);
item = new StringItem("Execution time: ", "-");
if(midp2) item.setLayout(Item.LAYOUT_NEWLINE_AFTER);
append(item);
item = new StringItem("Executions done: ",
this.storage.getStatus().getExecModel()[0] + " / " + this.storage.getStatus().getExecModel()[1]);
if(midp2) item.setLayout(Item.LAYOUT_NEWLINE_AFTER);
append(item);
item = new StringItem("Transfer status: ",
status.getTransferModel()[0] + " / " + status.getTransferModel()[1]);
if(midp2) item.setLayout(Item.LAYOUT_NEWLINE_AFTER);
append(item);
update(this.storage);
addCommand(new Command("Back", Command.BACK, 1));
addCommand(cmdRefresh);
setCommandListener(this);
}
/**
* Update the fields
*
* @param storageContainer container for the data
* @return true if job has finished the execution
*/
public boolean update (LiteStorage storageContainer) {
this.storage = storageContainer;
LiteStatus status = this.storage.getStatus();
Vector results = this.storage.getResult();
boolean downloadable = false;
System.out.println("Returned status: " + status.toString());
((StringItem)get(0)).setText(LiteStatus.STATES[status.getState()]);
if(status.getStartTime() != null) {
String execTime = status.getStartTime() + " - ";
if(status.getEndTime() != null)
execTime += status.getEndTime();
((StringItem)get(3)).setText(execTime);
}
((StringItem)get(4)).setText(status.getExecModel()[0]
+ " / " + status.getExecModel()[1]);
((StringItem)get(5)).setText(status.getTransferModel()[0]
+ " / " + status.getTransferModel()[1]);
if(status.getError().length() > 0) {
StringItem item = new StringItem("Exception: ", status.getError());
append(item);
}
if(results.size() > 0) {
if(cgResult == null) {
cgResult = new ChoiceGroup(results.size() + " result(s) available", Choice.EXCLUSIVE);
append(cgResult);
} else {
cgResult.deleteAll();
cgResult.setLabel(results.size() + " result(s) available");
}
for (int i = 0; i < results.size(); i++) {
System.out.println("result #" + i + " " + results.elementAt(i).toString());
LiteResult res = (LiteResult)results.elementAt(i);
String title;
if(res.getSize() < 1000) {
title = res.getPayloadSize() + " / " + res.getSize() + " b";
} else {
title = res.getPayloadSize()/1000 + " / " + res.getSize()/1000 + " kb";
}
int index = cgResult.append("#" + i + " " + res.getName() + " (" + title + ")", null);
cgResult.setSelectedIndex(index, true);
// some result is not yet downloaded
if(res.getPayloadSize() != res.getSize())
downloadable = true;
}
} else if(cgResult != null) {
System.out.println("No resultnames found");
delete(size()-1);
cgResult = null;
}
if(status.getRecordID() != -1) {
// job is already saved into the memory
removeCommand(cmdAbort);
removeCommand(cmdRefresh);
removeCommand(cmdSave);
addCommand(cmdView);
addCommand(cmdInfo);
addCommand(cmdRemove);
return true;
}
//else if(status.isFinished() || results.size() > 0) {
if(results.size() > 0) {
System.out.println("job owns results");
addCommand(cmdView);
addCommand(cmdInfo);
addCommand(cmdSave);
if(downloadable && results.size() > 1) {
// there is still something to download
addCommand(cmdDownloadAll);
}
return true;
}
if(status.isFinished()) {
addCommand(cmdRemove);
removeCommand(cmdAbort);
return true;
}
// continue with the execution
addCommand(cmdAbort);
removeCommand(cmdRemove);
return false;
}
public LiteResult getSelectedResult() {
int selectedResult = cgResult.getSelectedIndex();
return (LiteResult) this.storage.getResult().elementAt(selectedResult);
}
/*
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command c, Displayable disp) {
if (c.getCommandType() == Command.BACK) {
MIDui.back(true);
} else {
try {
// throws exception if not logged in
MIDui.getClient().isLogged();
} catch(Exception e) {
e.printStackTrace();
MIDui.showLogin(e.getClass().getName() + ":" + e.getMessage());
return;
}
if (c == cmdRefresh) {
MIDui.downloadStatus(this.storage.getDescription().getJobID(),
this.storage.getDescription(),
false);
} else if(c == this.cmdDownloadAll) {
String[] query = new String[0];
removeCommand(this.cmdDownloadAll);
MIDui.downloadResult(query,
2,
this.storage,
new Boolean(true),
this);
} else if(c == this.cmdView) {
LiteResult res = getSelectedResult();
if(res.getPayloadSize() < res.getSize()) {
// first download the result, then show
String[] query = new String[]{res.getName(), res.getName() + ".txt"};
MIDui.downloadResult(query,
0,
this.storage,
new Boolean(true),
this);
} else {
MIDui.showResult(res, this.storage.getDescription());
}
} else if(c == this.cmdInfo) {
System.out.println(getSelectedResult());
MIDui.next(new InfoForm(getSelectedResult(), cgResult.getSelectedIndex()));
} else if(c == this.cmdSave) {
MIDui.downloadResult(new String[0],
1,
this.storage,
new Boolean(true),
this);
} else if(c == this.cmdRemove || c == this.cmdAbort) {
MIDui.abortJob(this.storage.getDescription().getJobID(),
c == this.cmdRemove,
true);
}
}
}
}
See more files for this project here