RCPでrun in backgroundボタン付きプログレスバーダイアログを実現

2447 ワード


public class TestWithProgress implements IRunnableWithProgress {

		public void run(IProgressMonitor monitor)
				throws InvocationTargetException, InterruptedException {
			monitor.beginTask("Test", IProgressMonitor.UNKNOWN);
			ActivateJob job = new ActivateJob("Test");
			job.addJobChangeListener(new ActivateJobChangeAdapter(Display.getDefault()));
			job.setUser(true);
			job.schedule();
			if (monitor.isCanceled()) {
				throw new InterruptedException(
						"The long running operation was cancelled"); //$NON-NLS-1$
			}
		}
		
		private class ActivateJob extends Job {
			
			public ActivateJob(String name) {
				super(name);
			}

			@Override
			protected IStatus run(IProgressMonitor monitor) {
				
				try {
						...................

					return  new Status(Status.OK, "OK", "OK");
				} catch (Exception ex) {
					return new Status(Status.ERROR, "FAIL", "FAIL");
				}
			}
		}
		
		private class ActivateJobChangeAdapter extends JobChangeAdapter {

				/*
			 * (non-Javadoc)
			 * 
			 * @see
			 * org.eclipse.core.runtime.jobs.JobChangeAdapter#done(org.eclipse.core
			 * .runtime.jobs.IJobChangeEvent)
			 */
			@Override
			public void done(final IJobChangeEvent event) {
					IStatus iStatus = event.getResult();
					String message = iStatus.getMessage();

			
					if (IStatus.OK != iStatus.getSeverity()) {
						......
					}

			}

		}
	}

ProgressMonitorDialog progressMonitorDialog = new ProgressMonitorDialog(
				Display.getCurrent().getActiveShell());
		try {
			progressMonitorDialog.run(false, true,
					new TestWithProgress());
		} catch (InvocationTargetException e) {

		} catch (InterruptedException e) {

		} catch (Throwable e) {

		}

WorkbenchWindowAdvisor.preWindowOpen()にはこのconfigurer.があります.setShowProgressIndicator(true);ステータスバーに進捗バーが表示されていることを確認します
進捗ウィンドウを明示的に表示する場合はjobを使用します.setUser(true).またはIWorkbenchSiteProgressServicesを呼び出す.showInDialog(...)
一部のタスクを完了すると実行します.monitor.worked(進捗フラグメント値);