Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html /
  
  package org.eclipse.xtext.ui.refactoring.ui;
  
 
 import  org.eclipse.core.resources.IWorkspace;
 import  org.eclipse.core.resources.IncrementalProjectBuilder;
 import  org.eclipse.core.resources.ResourcesPlugin;
 import  org.eclipse.core.runtime.CoreException;
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.core.runtime.OperationCanceledException;
 import  org.eclipse.core.runtime.SubMonitor;
 import  org.eclipse.core.runtime.jobs.Job;
 import  org.eclipse.jface.operation.IRunnableWithProgress;
 import  org.eclipse.swt.widgets.Display;
 import  org.eclipse.ui.IEditorPart;
 import  org.eclipse.ui.IEditorReference;
 import  org.eclipse.ui.IWorkbench;
 import  org.eclipse.ui.IWorkbenchPage;
 import  org.eclipse.ui.IWorkbenchWindow;
 
This class does everything I know to put the IDE into a consistent state before refactoring.

Author(s):
Jan Koehnlein - Initial contribution and API
 
 public class SyncUtil {
 	private static final Logger LOG = Logger.getLogger(SyncUtil.class);
 
 	@Inject(optional = true)
 	private IWorkbench workbench;
 
 	@Inject(optional = true)
 	private IWorkspace workspace;
 
 	public void totalSync(final boolean saveAllboolean useProgressDialogthrows InvocationTargetException,
 		totalSync(saveAlluseProgressDialogtrue);
 	}
 	
 	public void totalSync(final boolean saveAllboolean useProgressDialogboolean forkthrows InvocationTargetException,
 		if (Display.getCurrent() != null &&  != null) {
 			if (useProgressDialog) {
 				.getProgressService().run(forktruenew IRunnableWithProgress() {
 					public void run(IProgressMonitor monitorthrows InvocationTargetExceptionInterruptedException {
 						doReconcileAndBuild(saveAllmonitor);
 					}
 				});
 			} else {
 				doReconcileAndBuild(saveAllnull);
 			}
 		}
 	}
 	
 	private void doReconcileAndBuild(final boolean saveAll, IProgressMonitor monitor)
 		try {
 			SubMonitor progress = SubMonitor.convert(monitor, 6);
 			reconcileAllEditors(saveAllprogress.newChild(1));
 			if (progress.isCanceled()) {
 				throw new InterruptedException();
 			}
 			waitForBuild(progress.newChild(4));
 			if (progress.isCanceled()) {
 				throw new InterruptedException();
 			}
 			yieldToQueuedDisplayJobs(progress.newChild(1));
 		} catch(OperationCanceledException e) {
 			throw new InterruptedException(); 
 		}
 	}
 
 	public void totalSync(final boolean saveAllthrows InvocationTargetExceptionInterruptedException {
 		totalSync(saveAlltrue);
 	}

Deprecated:
use reconcileAllEditors(IWorkbench, boolean, IProgressMonitor) instead
 
 	public void syncAllEditors(IWorkbench workbenchfinal boolean saveAll, IProgressMonitor monitor) {
 		reconcileAllEditors(workbenchsaveAllmonitor);
 	}
	public void reconcileAllEditors(IWorkbench workbenchfinal boolean saveAllfinal IProgressMonitor monitor) {
		for (IWorkbenchWindow window : workbench.getWorkbenchWindows()) {
			for (IWorkbenchPage page : window.getPages()) {
				for (IEditorReference editorReference : page.getEditorReferences()) {
					if (monitor.isCanceled())
						return;
					final IEditorPart editor = editorReference.getEditor(false);
					if (editor != null) {
						if (editor instanceof XtextEditor) {
						}
						if (editor.isDirty() && saveAll) {
							Display display = workbench.getDisplay();
							display.syncExec(new Runnable() {
								public void run() {
									editor.doSave(monitor);
								}
							});
						}
					}
				}
			}
		}
	}
	public void waitForReconciler(XtextEditor editor) {
		try {
				public void process(XtextResource statethrows Exception {
					// this doesn't execute before the reconciler has finished
				}
			});
catch (OperationCanceledException e) {
catch (OperationCanceledError e) {
		}
	}
	public void waitForBuild(IProgressMonitor monitor) {
		try {
			.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor);
catch (CoreException e) {
			throw new OperationCanceledException(e.getMessage());
		}
	}
	public void waitForDirtyStateUpdater(XtextEditor editor) {
		DirtyStateEditorSupport dirtyStateEditorSupport = editor.getDirtyStateEditorSupport();
		try {
			dirtyStateEditorSupport.waitForUpdateEditorJob();
catch (InterruptedException e) {
			// ignore
		}
	}

Deprecated:
we should not rely on auto build to be triggered. Use waitForBuild(IProgressMonitor) instead.
	public void waitForAutoBuild(IProgressMonitor monitor) {
		boolean wasInterrupted = false;
		do {
			try {
				Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, monitor);
				wasInterrupted = false;
catch (OperationCanceledException e) {
				throw e;
catch (InterruptedException e) {
				wasInterrupted = true;
			}
while (wasInterrupted);
	}
	public void yieldToQueuedDisplayJobs(IProgressMonitor monitor) {
		yieldToQueuedDisplayJobs(monitor, 5000);
	}
	public void yieldToQueuedDisplayJobs(IProgressMonitor monitorint maxJobsToYieldTo) {
		int count = 0;
		if (Display.getCurrent() != null) {
			while (count < maxJobsToYieldTo && Display.getCurrent().readAndDispatch()) {
				if (monitor.isCanceled())
					throw new OperationCanceledException();
				++count;
			}
			if (count == maxJobsToYieldTo) {
				.error("maxJobsToYieldTo probably exceeded. Worked: " + count);
			}
		}
	}
New to GrepCode? Check out our FAQ X