Show ProgressForm.java syntax highlighted
/**
* Copyright (c) 2004
* Helsinki Institute of Physics
* see LICENSE file for details
*
* ProgressForm.java
* Created on Apr 1, 2004
*/
package fi.hip.gb.client;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.StringItem;
/**
* Shows status of the operation.
*
* @author Juho Karppinen
*/
public class ProgressForm extends Form implements CommandListener {
/** timer used to update progressbar */
private static Timer progressTimer;
/**
* Initializes the progressbar which is shown during
* the network transmissions.
* @param message message shown to the user
*/
public ProgressForm(String message) {
super(message);
StringItem elapsed = new StringItem("", null);
append(elapsed);
Gauge status = new Gauge(null, false, 100, 0);
append(status);
addCommand(new Command("Cancel", Command.CANCEL, 1));
setCommandListener(this);
StatusUpdate updater = new StatusUpdate(elapsed, status);
progressTimer = new Timer();
progressTimer.scheduleAtFixedRate(updater, 1000, 1000);
}
/**
* Close all thread
*/
public static void cancel() {
if(progressTimer != null) {
progressTimer.cancel();
progressTimer = null;
}
}
/**
* A class to increment the value of a Gauge everytime it is run.
* Updates the progressbar which is shown during network transfers.
*/
class StatusUpdate extends TimerTask {
private StringItem elapsed = null;
private Gauge status = null;
private int tick = 0;
public StatusUpdate(StringItem elapsedText, Gauge statusGauge) {
elapsed = elapsedText;
status = statusGauge;
}
public void run() {
//elapsed.setText(client.getCurrentPosition() + " / " + client.getMaximumPosition());
String maximum = (MIDui.getClient().getMaximumPosition() > 100) ?
MIDui.getClient().getMaximumPosition() / 1000 + " kb" :
MIDui.getClient().getMaximumPosition() + " b";
elapsed.setText(MIDui.getClient().getDirection() + " " + maximum
+ ". Elapsed " + Integer.toString(++tick) + "s");
/*
System.out.println("setting " +
MIDui.getClient().getCurrentPosition() + " / " +
MIDui.getClient().getMaximumPosition());
*/
status.setValue(MIDui.getClient().getCurrentPosition());
status.setMaxValue(MIDui.getClient().getMaximumPosition());
if(MIDui.getDisplay().getCurrent() != ProgressForm.this) {
System.out.println("Stopping statusupdates");
this.cancel();
ProgressForm.cancel();
}
}
}
public void commandAction(Command c, Displayable dp) {
MIDui.getClient().cancel();
MIDui.back(true);
}
}
See more files for this project here