我有一个长时间运行的操作,例如将一个大的 zip 文件复制到临时目录,提取它,然后将它的内容复制到目标路径。在操作进行时,我还会显示一个进度条。
我应该在作为 Java 项目(非 Eclipse/RCP)的独立 SWT UI 中实现这一点。
我了解您需要使用单独的线程在 UI 中执行此任务。我的 UI 中有一个取消按钮,单击该按钮时应回滚执行的任务并优雅地返回 UI/退出而不冻结它。我怎样才能有效地做到这一点。下面是我用来开始的示例代码。任何帮助将不胜感激。
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
public class TestProgressBar {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Display display=new Display();
final Shell shell=new Shell(display,SWT.MIN|SWT.CLOSE);
shell.setSize(500,200);
GridLayout gdLayout=new GridLayout();
gdLayout.numColumns=2;
shell.setLayout(gdLayout);
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(gdLayout);
GridData gd=new GridData();
final Label lab=new Label(composite, 0);
lab.setText("Performing Job :");
gd.horizontalIndent=10;
gd.horizontalAlignment=SWT.FILL;
lab.setLayoutData(gd);
GridData gdPg=new GridData();
gdPg.horizontalAlignment=SWT.FILL;
final ProgressBar bar = new ProgressBar(composite, SWT.NONE);
bar.setLayoutData(gdPg);
final int maximum = bar.getMaximum();
//System.err.println(maximum);
final Button cancelButton=new Button(composite,SWT.NONE);
cancelButton.setText("Cancel Copy");
new Thread() {
public void run() {
for (final int[] i = new int[1]; i[0] <= maximum; i[0]++) {
try {Thread.sleep (10);} catch (Throwable th) {}
copyOperation();
if (display.isDisposed()) return;
display.asyncExec(new Runnable() {
public void run() {
if (bar.isDisposed ()) return;
bar.setSelection(i[0]);
if(i[0]==maximum)
{
lab.setText("Job Finished!!!");
cancelButton.setEnabled(false);
}
}
});
}
}
}.start();
shell.open();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
display.sleep();
}
}
protected static void copyOperation() {
// TODO Auto-generated method stub
File zipFile=new File("D:\\Sample.zip");
try {
FileUtils.copyFileToDirectory(zipFile,new File("C:\\Users\\komail\\Desktop"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}