Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 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.impl;
  
 import static com.google.common.collect.Iterables.*;
 import static com.google.common.collect.Lists.*;
 import static com.google.common.collect.Sets.*;
 import static org.eclipse.ltk.core.refactoring.RefactoringStatus.*;
 
 import java.util.List;
 import java.util.Set;
 
 import  org.eclipse.core.resources.IProject;
 import  org.eclipse.core.runtime.CoreException;
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.core.runtime.IStatus;
 import  org.eclipse.core.runtime.OperationCanceledException;
 import  org.eclipse.core.runtime.Status;
 import  org.eclipse.core.runtime.SubMonitor;
 import  org.eclipse.ltk.core.refactoring.Change;
 import  org.eclipse.ltk.core.refactoring.CompositeChange;
 import  org.eclipse.ltk.core.refactoring.RefactoringStatus;
 import  org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
 import  org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
 import  org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
 
A rename participant that triggers further rename refactorings based on RenameProcessors.

Author(s):
Jan Koehnlein - Initial contribution and API
 
 public abstract class AbstractProcessorBasedRenameParticipant extends RenameParticipant {
 
 	private static final Logger LOG = Logger.getLogger(AbstractProcessorBasedRenameParticipant.class);
 
 
 
 
 	private String languageName;
 
 	private StatusWrapper status;
 	
 	@Inject 
 	private SyncUtil syncUtil;
 	
 
 	private List<RenameProcessor> wrappedProcessors;
 
 	private Set<ObjectdisabledTargets = newHashSet();
 
 	protected boolean initialize(Object originalTargetElement) {
 		try {
 			 = getRenameProcessors(originalTargetElement);
 			if( != null) {
 				return true;
 			}
 		} catch (InterruptedException e) {
 			return false;
 		} catch (Exception exc) {
 			.add(ERROR, "Error initializing refactoring participant."exc);
 		}
 		return false;
 	}
 
 	protected List<RenameProcessor> getRenameProcessors(Object originalTargetElement) {
 		List<? extends IRenameElementContextparticipantContexts = createRenameElementContexts(originalTargetElement);
		if (participantContexts != null) {
			List<RenameProcessor> processors = newArrayList();
			for (IRenameElementContext participantContext : participantContexts) {
				RenameProcessor renameProcessor = getRenameProcessor(participantContext);
				if (renameProcessor != null) {
					processors.add(renameProcessor);
				}
			}
			return processors;
		}
		return null;
	}
	protected RenameProcessor getRenameProcessor(IRenameElementContext participantContext) {
		IRenameRefactoringProvider renameRefactoringProvider = getRenameRefactoringProvider(participantContext);
		if (renameRefactoringProvider != null)
			return renameRefactoringProvider.getRenameProcessor(participantContext);
		else
			return null;
	}
	}
	public String getName() {
		return ;
	}
	public Object[] getElements() {
		List<Objectelements = newArrayList();
		for (RenameProcessor wrappedProcessor : ) {
			elements.addAll(Arrays.asList(wrappedProcessor.getElements()));
		}
		return toArray(elementsObject.class);
	}
	public void disableFor(Object... elements) {
		.addAll(Arrays.asList(elements));
	}
	public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context)
			throws OperationCanceledException {
		SubMonitor progress = SubMonitor.convert(pm).setWorkRemaining(100 * .size());
		try {
			for (RenameProcessor wrappedProcessor : ) {
				List<ObjecttargetElements = Arrays.asList(wrappedProcessor.getElements());
				if (!.containsAll(targetElements)) {
					setNewName(wrappedProcessorgetNewName());
					.merge(wrappedProcessor.checkInitialConditions(progress.newChild(20)));
					if(!.getRefactoringStatus().hasFatalError())
						.merge(wrappedProcessor.checkFinalConditions(progress.newChild(80), context));
				}
			}
catch (OperationCanceledException e) {
			throw e;
catch (Exception ce) {
			.add(ERROR, "Error checking conditions in refactoring participant: {0}. See log for details"ce);
		}
	}
	protected void setNewName(RenameProcessor processorString newName) {
		((AbstractRenameProcessorprocessor).setNewName(newName);
	}
	public Change createChange(IProgressMonitor pmthrows CoreException, OperationCanceledException {
		CompositeChange compositeChange = null;
		try {
			SubMonitor subMonitor = SubMonitor.convert(pm.size());
			for (RenameProcessor wrappedProcessor : ) {
				if (!.containsAll(Arrays.asList(wrappedProcessor.getElements()))) {
					Change processorChange = wrappedProcessor.createChange(subMonitor.newChild(1));
					if (processorChange != null) {
						if (compositeChange == null)
							compositeChange = new CompositeChange("Changes from participant: " + getName());
						compositeChange.add(processorChange);
					}
else {
					subMonitor.worked(1);
				}
			}
catch (OperationCanceledException e) {
			throw e;
catch (Exception e) {
			throw new CoreException(new Status(IStatus.ERROR, ."Error creating change"e));
finally {
		}
		return compositeChange;
	}

Since:
2.4
	protected void dispose() {
		 = null;
	}
	protected List<? extends IRenameElementContextcreateRenameElementContexts(Object element) {
		if (element instanceof IRenameElementContext) {
			IRenameElementContext triggeringContext = (IRenameElementContextelement;
			IProject project = .getProject(triggeringContext.getTargetElementURI());
			if (project != null) {
				ResourceSet resourceSet = .get(project);
				EObject originalTarget = resourceSet.getEObject(triggeringContext.getTargetElementURI(), true);
				List<EObjectrenamedElements = getRenamedElementsOrProxies(originalTarget);
				if (renamedElements == null || renamedElements.isEmpty())
					return null;
				List<IRenameElementContextcontexts = newArrayListWithCapacity(renamedElements.size());
				for (EObject renamedElement : renamedElements)
					contexts.add(new IRenameElementContext.Impl(EcoreUtil.getURI(renamedElement), renamedElement
							.eClass(), triggeringContext.getTriggeringEditor(), triggeringContext
				return contexts;
			}
		}
		return null;
	}
	protected abstract List<EObjectgetRenamedElementsOrProxies(EObject originalTarget);
	protected String getNewName() {
		return getArguments().getNewName();
	}
	protected StatusWrapper getStatus() {
		return ;
	}
	}
New to GrepCode? Check out our FAQ X