2010-05-09 00:42:56 +02:00
|
|
|
package com.osmand.swing;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
import com.osmand.IProgress;
|
|
|
|
|
|
|
|
|
|
|
|
public class ProgressDialog extends JDialog implements IProgress {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = -3915486672514402269L;
|
|
|
|
private JProgressBar progressBar;
|
|
|
|
private JLabel label;
|
|
|
|
private Runnable run;
|
|
|
|
private InvocationTargetException exception = null;
|
|
|
|
|
|
|
|
private Object result;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Progress variables
|
|
|
|
private static final float deltaToChange = 0.01f;
|
|
|
|
private String taskName;
|
|
|
|
private int deltaWork;
|
2010-05-20 09:59:35 +02:00
|
|
|
private WorkerThread workerThread;
|
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-20 09:59:35 +02:00
|
|
|
isLive = false;
|
2010-05-09 15:06:13 +02:00
|
|
|
} catch (RuntimeException e) {
|
|
|
|
exception = new InvocationTargetException(e);
|
|
|
|
} finally {
|
|
|
|
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-05-09 15:06:13 +02:00
|
|
|
label.setText(taskName + String.format("\t %.1f %%", progressBar.getValue() * 100f / ((float) progressBar.getMaximum())));
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void startTask(String taskName, int work) {
|
|
|
|
if(taskName == null){
|
|
|
|
taskName = "";
|
|
|
|
}
|
|
|
|
label.setText(taskName);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|