2010-08-17 00:36:24 +02:00
|
|
|
package net.osmand.swing;
|
2010-05-09 00:42:56 +02:00
|
|
|
|
|
|
|
import java.awt.BorderLayout;
|
|
|
|
import java.awt.Component;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
|
|
|
import javax.swing.BorderFactory;
|
|
|
|
import javax.swing.JDialog;
|
|
|
|
import javax.swing.JLabel;
|
|
|
|
import javax.swing.JOptionPane;
|
|
|
|
import javax.swing.JPanel;
|
|
|
|
import javax.swing.JProgressBar;
|
2010-05-09 15:06:13 +02:00
|
|
|
import javax.swing.SwingUtilities;
|
2010-05-09 00:42:56 +02:00
|
|
|
|
2010-08-17 00:36:24 +02:00
|
|
|
import net.osmand.IProgress;
|
|
|
|
import net.osmand.LogUtil;
|
|
|
|
|
2010-06-10 21:36:21 +02:00
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
2010-05-09 00:42:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
public class ProgressDialog extends JDialog implements IProgress {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = -3915486672514402269L;
|
2010-06-10 21:36:21 +02:00
|
|
|
private final static Log log = LogUtil.getLog(ProgressDialog.class);
|
2010-05-09 00:42:56 +02:00
|
|
|
private JProgressBar progressBar;
|
|
|
|
private JLabel label;
|
|
|
|
private Runnable run;
|
|
|
|
private InvocationTargetException exception = null;
|
2010-06-10 21:36:21 +02:00
|
|
|
|
2010-05-09 00:42:56 +02:00
|
|
|
|
|
|
|
private Object result;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Progress variables
|
2010-06-07 20:07:04 +02:00
|
|
|
private static final float deltaToChange = 0.001f;
|
2010-05-09 00:42:56 +02:00
|
|
|
private String taskName;
|
|
|
|
private int deltaWork;
|
2010-05-20 09:59:35 +02:00
|
|
|
private WorkerThread workerThread;
|
2010-06-10 21:36:21 +02:00
|
|
|
private String genProgress;
|
|
|
|
|
|
|
|
private long previousTaskStarted = 0;
|
2010-05-09 00:42:56 +02:00
|
|
|
|
|
|
|
|
2010-05-13 20:02:30 +02:00
|
|
|
public ProgressDialog(Component parent, String name){
|
2010-05-09 00:42:56 +02:00
|
|
|
super(JOptionPane.getFrameForComponent(parent), true);
|
2010-05-13 20:02:30 +02:00
|
|
|
setTitle(name);
|
2010-05-09 00:42:56 +02:00
|
|
|
initDialog();
|
|
|
|
}
|
|
|
|
|
2010-05-18 15:36:42 +02:00
|
|
|
public boolean isInterrupted(){
|
|
|
|
return !isVisible();
|
|
|
|
}
|
|
|
|
|
2010-05-20 09:59:35 +02:00
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public Object run() throws InvocationTargetException, InterruptedException {
|
2010-05-09 15:06:13 +02:00
|
|
|
result = null;
|
2010-05-20 09:59:35 +02:00
|
|
|
workerThread = new WorkerThread();
|
|
|
|
workerThread.start();
|
2010-05-09 15:06:13 +02:00
|
|
|
setVisible(true);
|
2010-05-20 09:59:35 +02:00
|
|
|
if(workerThread.checkIsLive()){
|
|
|
|
// that's really bad solution unless we don't find any problems with that
|
|
|
|
// means monitor objects & we continue to use because otherwise
|
|
|
|
// we should protect all places where it is used regular checks that process is interrupted
|
|
|
|
workerThread.stop();
|
|
|
|
throw new InterruptedException();
|
|
|
|
}
|
2010-05-09 15:06:13 +02:00
|
|
|
if (exception != null) {
|
|
|
|
throw exception;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2010-05-09 00:42:56 +02:00
|
|
|
|
|
|
|
private class WorkerThread extends Thread {
|
2010-05-20 09:59:35 +02:00
|
|
|
private boolean isLive = true;
|
|
|
|
|
2010-05-09 00:42:56 +02:00
|
|
|
|
2010-05-20 09:59:35 +02:00
|
|
|
public boolean checkIsLive(){
|
|
|
|
return isLive;
|
|
|
|
}
|
2010-05-09 00:42:56 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2010-05-09 15:06:13 +02:00
|
|
|
if (run != null) {
|
|
|
|
run.run();
|
2010-05-09 00:42:56 +02:00
|
|
|
}
|
2010-05-09 15:06:13 +02:00
|
|
|
} catch (RuntimeException e) {
|
|
|
|
exception = new InvocationTargetException(e);
|
|
|
|
} finally {
|
2010-05-26 17:47:38 +02:00
|
|
|
isLive = false;
|
2010-05-09 15:06:13 +02:00
|
|
|
}
|
2010-05-26 17:47:38 +02:00
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
setVisible(false);
|
|
|
|
}
|
|
|
|
});
|
2010-05-09 00:42:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initDialog() {
|
|
|
|
JPanel pane = new JPanel(new BorderLayout());
|
|
|
|
pane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
|
|
|
|
progressBar = new JProgressBar();
|
|
|
|
pane.add(progressBar, BorderLayout.SOUTH);
|
|
|
|
label = new JLabel();
|
|
|
|
pane.add(label, BorderLayout.CENTER);
|
|
|
|
add(pane);
|
|
|
|
|
|
|
|
label.setText("Please waiting...");
|
|
|
|
progressBar.setIndeterminate(true);
|
|
|
|
setSize(550, 100);
|
2010-05-15 14:54:26 +02:00
|
|
|
double x = getParent().getBounds().getCenterX();
|
|
|
|
double y = getParent().getBounds().getCenterY();
|
|
|
|
setLocation((int) x - getWidth() / 2, (int) y - getHeight() / 2);
|
2010-05-09 00:42:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Object getResult() {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setResult(Object result) {
|
|
|
|
this.result = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRunnable(Runnable run) {
|
|
|
|
this.run = run;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void progress(int deltaWork) {
|
|
|
|
this.deltaWork += deltaWork;
|
2010-05-14 23:05:18 +02:00
|
|
|
if(change(progressBar.getValue() + this.deltaWork)){
|
|
|
|
progressBar.setValue(progressBar.getValue() + this.deltaWork);
|
|
|
|
this.deltaWork = 0;
|
2010-05-09 00:42:56 +02:00
|
|
|
updateMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateMessage() {
|
|
|
|
if(!progressBar.isIndeterminate()){
|
2010-06-10 21:36:21 +02:00
|
|
|
String format = String.format("\t %.1f %%", progressBar.getValue() * 100f / ((float) progressBar.getMaximum()));
|
|
|
|
label.setText(taskName + format + (genProgress == null ? "" : (" " + genProgress)));
|
2010-05-09 00:42:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean change(int newProgress) {
|
|
|
|
if (newProgress < progressBar.getValue()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((newProgress - progressBar.getValue()) / ((float) progressBar.getMaximum()) < deltaToChange) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void remaining(int remainingWork) {
|
|
|
|
if(change(progressBar.getMaximum() - remainingWork)){
|
|
|
|
progressBar.setValue(progressBar.getMaximum() - remainingWork);
|
|
|
|
updateMessage();
|
|
|
|
}
|
|
|
|
deltaWork = progressBar.getMaximum() - remainingWork - this.progressBar.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isIndeterminate(){
|
|
|
|
return progressBar.isIndeterminate();
|
|
|
|
}
|
2010-06-10 21:36:21 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setGeneralProgress(String genProgress) {
|
|
|
|
this.genProgress = genProgress;
|
|
|
|
|
|
|
|
}
|
2010-05-09 00:42:56 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void startTask(String taskName, int work) {
|
2010-06-10 21:36:21 +02:00
|
|
|
if (log.isDebugEnabled()) {
|
2010-06-11 15:44:37 +02:00
|
|
|
log.debug("Memory before task exec: " + Runtime.getRuntime().totalMemory() + " free : " + Runtime.getRuntime().freeMemory());
|
2010-06-10 21:36:21 +02:00
|
|
|
if (previousTaskStarted == 0) {
|
|
|
|
log.debug(taskName + " started");
|
|
|
|
} else {
|
|
|
|
log.debug(taskName + " started after " + (System.currentTimeMillis() - previousTaskStarted) + " ms");
|
|
|
|
}
|
2010-06-11 15:44:37 +02:00
|
|
|
|
2010-06-10 21:36:21 +02:00
|
|
|
}
|
|
|
|
previousTaskStarted = System.currentTimeMillis();
|
2010-05-09 00:42:56 +02:00
|
|
|
if(taskName == null){
|
|
|
|
taskName = "";
|
|
|
|
}
|
2010-06-10 21:36:21 +02:00
|
|
|
label.setText(taskName + (genProgress == null ? "" : (" "+genProgress)));
|
2010-05-09 00:42:56 +02:00
|
|
|
this.taskName = taskName;
|
|
|
|
startWork(work);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void finishTask() {
|
|
|
|
if (taskName != null) {
|
|
|
|
label.setText(taskName);
|
|
|
|
}
|
|
|
|
progressBar.setIndeterminate(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void startWork(int work) {
|
|
|
|
if(work == 0){
|
|
|
|
work = 1;
|
|
|
|
}
|
2010-05-09 15:06:13 +02:00
|
|
|
final int w = work;
|
|
|
|
SwingUtilities.invokeLater(new Runnable(){
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if(w != -1){
|
|
|
|
progressBar.setMinimum(0);
|
|
|
|
progressBar.setMaximum(w);
|
|
|
|
progressBar.setValue(0);
|
|
|
|
progressBar.setIndeterminate(false);
|
|
|
|
} else {
|
|
|
|
progressBar.setIndeterminate(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-05-09 00:42:56 +02:00
|
|
|
deltaWork = 0;
|
|
|
|
}
|
|
|
|
}
|