Java Code Examples for org.eclipse.core.resources.IWorkspaceRunnable#run()

The following examples show how to use org.eclipse.core.resources.IWorkspaceRunnable#run() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Storage2UriMapperJavaImpl.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private void doInitializeCache() throws CoreException {
	if(!isInitialized) {
		IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
			@Override
			public void run(IProgressMonitor monitor) throws CoreException {
				if(!isInitialized) {
					for(IProject project: workspace.getRoot().getProjects()) {
						if(project.isAccessible() && JavaProject.hasJavaNature(project)) {
							IJavaProject javaProject = JavaCore.create(project);
							updateCache(javaProject);
						}
					}
					isInitialized = true;
				}
			}
		};
		// while the tree is locked, workspace.run may not be used but we are sure that we do already
		// hold the workspace lock - save to just run the action code
		if (workspace.isTreeLocked()) {
			runnable.run(null);
		} else {
			workspace.run(runnable, null, IWorkspace.AVOID_UPDATE, null);
		}
	}
}
 
Example 2
Source File: AbstractBuilderState.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void load() {
	if (!isLoaded) {
		IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
			@Override
			public void run(IProgressMonitor monitor) throws CoreException {
				if (!isLoaded) {
					resourceDescriptionData = new ResourceDescriptionsData(persister.load());
					if(storage2UriMapper instanceof IStorage2UriMapperExtension)
						((IStorage2UriMapperExtension) storage2UriMapper).initializeCache();
					isLoaded = true;
				}
			}
		};
		try {
			switch(workspaceLockAccess.isWorkspaceLockedByCurrentThread(workspace)) {
				case YES: {
					runnable.run(null);
					break;
				}
				case NO: {
					workspace.run(runnable, null, IWorkspace.AVOID_UPDATE, null);
					break;
				}
				case SHUTDOWN: {
					// do not initialize anything if we are about to shutdown.
					return;
				}
			}
		} catch(CoreException e) {
			log.error(e.getMessage(), e);
		}
	}
}
 
Example 3
Source File: BinaryRefactoringHistoryWizard.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates the necessary source code for the refactoring.
 *
 * @param monitor
 *            the progress monitor to use
 * @return
 *            the resulting status
 */
private RefactoringStatus createNecessarySourceCode(final IProgressMonitor monitor) {
	final RefactoringStatus status= new RefactoringStatus();
	try {
		monitor.beginTask(JarImportMessages.JarImportWizard_prepare_import, 240);
		final IPackageFragmentRoot root= getPackageFragmentRoot();
		if (root != null && fSourceFolder != null && fJavaProject != null) {
			try {
				final SubProgressMonitor subMonitor= new SubProgressMonitor(monitor, 40, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL);
				final IJavaElement[] elements= root.getChildren();
				final List<IPackageFragment> list= new ArrayList<IPackageFragment>(elements.length);
				try {
					subMonitor.beginTask(JarImportMessages.JarImportWizard_prepare_import, elements.length);
					for (int index= 0; index < elements.length; index++) {
						final IJavaElement element= elements[index];
						if (!fProcessedFragments.contains(element) && !element.getElementName().equals(META_INF_FRAGMENT))
							list.add((IPackageFragment) element);
						subMonitor.worked(1);
					}
				} finally {
					subMonitor.done();
				}
				if (!list.isEmpty()) {
					fProcessedFragments.addAll(list);
					final URI uri= fSourceFolder.getRawLocationURI();
					if (uri != null) {
						final IPackageFragmentRoot sourceFolder= fJavaProject.getPackageFragmentRoot(fSourceFolder);
						IWorkspaceRunnable runnable= null;
						if (canUseSourceAttachment()) {
							runnable= new SourceCreationOperation(uri, list) {

								private IPackageFragment fFragment= null;

								@Override
								protected final void createCompilationUnit(final IFileStore store, final String name, final String content, final IProgressMonitor pm) throws CoreException {
									fFragment.createCompilationUnit(name, content, true, pm);
								}

								@Override
								protected final void createPackageFragment(final IFileStore store, final String name, final IProgressMonitor pm) throws CoreException {
									fFragment= sourceFolder.createPackageFragment(name, true, pm);
								}
							};
						} else {
							runnable= new StubCreationOperation(uri, list, true) {

								private IPackageFragment fFragment= null;

								@Override
								protected final void createCompilationUnit(final IFileStore store, final String name, final String content, final IProgressMonitor pm) throws CoreException {
									fFragment.createCompilationUnit(name, content, true, pm);
								}

								@Override
								protected final void createPackageFragment(final IFileStore store, final String name, final IProgressMonitor pm) throws CoreException {
									fFragment= sourceFolder.createPackageFragment(name, true, pm);
								}
							};
						}
						try {
							runnable.run(new SubProgressMonitor(monitor, 150, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
						} finally {
							fSourceFolder.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
						}
					}
				}
			} catch (CoreException exception) {
				status.addFatalError(exception.getLocalizedMessage());
			}
		}
	} finally {
		monitor.done();
	}
	return status;
}