SWT ProgressMonitorDialog vs Job

时间:2015-02-19 21:04:39

标签: java eclipse-plugin jface rcp

我想使用非模态的进度监视器,可以缩小到状态栏的右下角。

据我测试,ProgressMonitorDialog正确地向用户显示进度条,可以使用下面的代码进行模态化。但是没有钩子将它缩小到状态栏的右下角。代码类似于:

try {
    ProgressMonitorDialog pmd = new ProgressMonitorDialog(shell){
        @Override
        protected void setShellStyle(int newShellStyle) {           
            super.setShellStyle(SWT.CLOSE | SWT.MODELESS| 
                SWT.BORDER | SWT.TITLE);
            setBlockOnOpen(false);
        }  
    };
    pmd.run(true, true, new MyOperation());
} catch (final InvocationTargetException e) {
    MessageDialog.openError(shell, "Error", e.getMessage());
} catch (final InterruptedException e) {
    MessageDialog.openInformation(shell, "Cancelled", e.getMessage());
}

class MyOperation implements IRunnableWithProgress {
    @Override
    public void run(final IProgressMonitor monitor) throws 
      InvocationTargetException, InterruptedException {
        monitor.beginTask("start task", 10);

        // time consuming work here
        doExpensiveWork(monitor);

        monitor.done();
    }

当我使用Job测试时,作业按预期运行但我没有得到视觉反馈。 我的代码与此类似:

final ProgressBar progressBar = new ProgressBar(shell, SWT.SMOOTH);
progressBar.setBounds(100, 10, 200, 20);

// Setting the progress monitor
final IJobManager manager = Job.getJobManager();

final IProgressMonitor p = new IProgressMonitor() {

    @Override
    public void worked(final int work) {
        sync.syncExec(new Runnable() {
        @Override
        public void run() {
            progressBar.setSelection(progressBar.getSelection() + work);
        }
    });
    //....
}

Job job = new Job("test") {

    @Override
    protected IStatus run(IProgressMonitor monitor) {
        monitor.beginTask("start task", 10);

        // time consuming work here
        doExpensiveWork(monitor);
        // sync with UI
        syncWithUI();

        return Status.OK_STATUS;
    }

};
job.setUser(true);
job.schedule();

当用户最小化标签时,我能够获得可以缩小到状态栏右下角的ProgressMonitorDialog版本,我缺少什么?

1 个答案:

答案 0 :(得分:0)

不知道ProgressDialog如何,但在使用作业时显示进度,你必须设置

IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
setShowProgressIndicator(true);

org.eclipse.ui.application.WorkbenchWindowAdvisor.preWindowOpen()