在 RCP 中为作业添加进度条
Add a progress bar to a Job in RCP
我在我的 RCp 应用程序中有一项耗时的工作,我在其中读取了一个大数据库并将其保存在一个文件中。我在一个单独的线程中 运行 这项工作,这样我的应用程序 UI 就不会被阻止,但我不知道如何为这项任务添加进度条
我的代码:
public class MirrorFeatureModel extends Job {
protected class MutexRule implements ISchedulingRule {
public boolean isConflicting(ISchedulingRule rule) {
return rule == this;
}
public boolean contains(ISchedulingRule rule) {
return rule == this;
}
}
private String source;
private String template;
private String target;
public MirrorFeatureModel(String name) {
super(name);
}
public MirrorFeatureModel(String source, String template, String target){
super("Mirroring SWA Model");
this.source = source;
this.template = template;
this.target = target;
}
public void run() {
this.schedule();
}
@Override
protected IStatus run(IProgressMonitor monitor) {
ILog logView = Activator.getDefault().getLog();
String connectionString = this.source;
String emptyEAP = this.template;
String target = this.target;
try{
monitor.beginTask("Copying swa model to local", 1);
new Mirror(connectionString, emptyEAP,target).run();
monitor.worked(1);
}catch(final Exception e) {
logView.log(new Status(Status.ERROR, null , "Failed to create local SAM instance", e));
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openError(PlatformUI.getWorkbench().getDisplay().getActiveShell(), "Failed to create local SAM instance", e.getMessage());
}
});
return Status.CANCEL_STATUS;
}
return Status.OK_STATUS;
}
}
Mirror.run() 方法
public void run(IProgressMonitor monitor) {
try{
if (monitor != null)
monitor.subTask("Creating directory for eap file");
File tempTarget=File.createTempFile("eap-mirror", "eap");
if (monitor != null)
monitor.worked(1);
try {
if (monitor != null)
monitor.subTask("Establising connection");
this.source=DriverManager.getConnection(com.intel.imc.swa.easql.EaDbStringParser.eaDbStringToJdbc(sourceString));
this.source.setReadOnly(true);
if(monitor != null)
monitor.worked(1);
FileUtils.copyFile(new File(templateFileString), tempTarget);
if (monitor != null)
monitor.subTask("Opening database");
this.target=Database.open(tempTarget,false,false);
if(monitor != null)
monitor.worked(1);
if (monitor != null)
monitor.subTask("Mirroring tables");
Collection<String> tables=selectTables(source);
long time=System.currentTimeMillis();
for (String tableName : tables) {
long tTime=System.currentTimeMillis();
Table table=target.getTable(tableName);
System.out.print("Mirroring table "+tableName+"...");
table.setOverrideAutonumber(true);
copyTable(table, source, target);
System.out.println(" took "+(System.currentTimeMillis()-tTime));
}
System.out.println("Done. Overall time: "+(System.currentTimeMillis()-time));
if(monitor != null)
monitor.worked(1);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
如何在此class中添加一个进度条来显示我这项工作的进度
谢谢
通常作业会在进度视图和主窗口底部的状态行中显示进度指示器 window。
如果您在 schedule()
之前调用 setUser(true)
,那么如果作业运行超过几秒,作业将显示一个弹出式进度对话框。
要显示工作进度,您必须在 beginTask
调用
中指定总工作量
monitor.beginTask("...", total work);
然后你必须在每次完成部分工作时调用 monitor.worked(xxx)
。
我在我的 RCp 应用程序中有一项耗时的工作,我在其中读取了一个大数据库并将其保存在一个文件中。我在一个单独的线程中 运行 这项工作,这样我的应用程序 UI 就不会被阻止,但我不知道如何为这项任务添加进度条
我的代码:
public class MirrorFeatureModel extends Job {
protected class MutexRule implements ISchedulingRule {
public boolean isConflicting(ISchedulingRule rule) {
return rule == this;
}
public boolean contains(ISchedulingRule rule) {
return rule == this;
}
}
private String source;
private String template;
private String target;
public MirrorFeatureModel(String name) {
super(name);
}
public MirrorFeatureModel(String source, String template, String target){
super("Mirroring SWA Model");
this.source = source;
this.template = template;
this.target = target;
}
public void run() {
this.schedule();
}
@Override
protected IStatus run(IProgressMonitor monitor) {
ILog logView = Activator.getDefault().getLog();
String connectionString = this.source;
String emptyEAP = this.template;
String target = this.target;
try{
monitor.beginTask("Copying swa model to local", 1);
new Mirror(connectionString, emptyEAP,target).run();
monitor.worked(1);
}catch(final Exception e) {
logView.log(new Status(Status.ERROR, null , "Failed to create local SAM instance", e));
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openError(PlatformUI.getWorkbench().getDisplay().getActiveShell(), "Failed to create local SAM instance", e.getMessage());
}
});
return Status.CANCEL_STATUS;
}
return Status.OK_STATUS;
}
}
Mirror.run() 方法
public void run(IProgressMonitor monitor) {
try{
if (monitor != null)
monitor.subTask("Creating directory for eap file");
File tempTarget=File.createTempFile("eap-mirror", "eap");
if (monitor != null)
monitor.worked(1);
try {
if (monitor != null)
monitor.subTask("Establising connection");
this.source=DriverManager.getConnection(com.intel.imc.swa.easql.EaDbStringParser.eaDbStringToJdbc(sourceString));
this.source.setReadOnly(true);
if(monitor != null)
monitor.worked(1);
FileUtils.copyFile(new File(templateFileString), tempTarget);
if (monitor != null)
monitor.subTask("Opening database");
this.target=Database.open(tempTarget,false,false);
if(monitor != null)
monitor.worked(1);
if (monitor != null)
monitor.subTask("Mirroring tables");
Collection<String> tables=selectTables(source);
long time=System.currentTimeMillis();
for (String tableName : tables) {
long tTime=System.currentTimeMillis();
Table table=target.getTable(tableName);
System.out.print("Mirroring table "+tableName+"...");
table.setOverrideAutonumber(true);
copyTable(table, source, target);
System.out.println(" took "+(System.currentTimeMillis()-tTime));
}
System.out.println("Done. Overall time: "+(System.currentTimeMillis()-time));
if(monitor != null)
monitor.worked(1);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
如何在此class中添加一个进度条来显示我这项工作的进度
谢谢
通常作业会在进度视图和主窗口底部的状态行中显示进度指示器 window。
如果您在 schedule()
之前调用 setUser(true)
,那么如果作业运行超过几秒,作业将显示一个弹出式进度对话框。
要显示工作进度,您必须在 beginTask
调用
monitor.beginTask("...", total work);
然后你必须在每次完成部分工作时调用 monitor.worked(xxx)
。