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 org.eclipse.xtext.util.Strings.*;
 
 import java.util.List;
 
 import  org.eclipse.ltk.core.refactoring.RefactoringStatus;
 import  org.eclipse.text.edits.ReplaceEdit;
 import  org.eclipse.text.edits.TextEdit;
 
Default rename strategy for Xtext based elements that have an EAttribute name.

Author(s):
Jan Koehnlein - Initial contribution and API
Holger Schill
 
 public class DefaultRenameStrategy extends AbstractRenameStrategy {
 
 
 
 
 	protected String nameRuleName;
 
 	public boolean initialize(EObject targetElementIRenameElementContext context) {
 		if (super.initialize(targetElementcontext)) {
 			this. = getNameRuleName(targetElementgetNameAttribute());
 			return true;
 		}
 		return false;
 	}
 
 	public RefactoringStatus validateNewName(String newName) {
 		RefactoringStatus status = super.validateNewName(newName);
 		if( != null) {
 			try {
 				String value = getNameAsValue(newName);
 				String text = getNameAsText(value);
 				if(!equal(textnewName)) {
 					status.addError("Illegal name: '" + newName + "'. Consider using '" + text + "' instead.");
 				}
 			} catch(ValueConverterException vce) {
 				status.addFatalError("Illegal name: " + notNull(vce.getMessage()));
 			}
 		}
 		return status;
 	}
 	
 	protected ITextRegion getOriginalNameRegion(final EObject targetElementEAttribute nameAttribute) {
 		return .getFullTextRegion(targetElementnameAttribute, 0);
 	}
 
 	protected String getNameRuleName(EObject targetElementEAttribute nameAttribute) {
 		List<INodenameNodes = NodeModelUtils.findNodesForFeature(targetElementnameAttribute);
 		if(nameNodes.size() == 1) {
 			EObject grammarElement = nameNodes.get(0).getGrammarElement();
 			if(grammarElement instanceof RuleCall) {
 				AbstractRule nameRule = ((RuleCallgrammarElement).getRule();
 				if(nameRule != null)
 					return nameRule.getName();
 			}
 		}
 		return null;
 	}
 	
 	public void createDeclarationUpdates(String newNameResourceSet resourceSet,
 			IRefactoringUpdateAcceptor updateAcceptor) {
 	}
	protected TextEdit getDeclarationTextEdit(String newName) {
		String text = newName;
		return new ReplaceEdit(.getOffset(), .getLength(), text);
	}
	protected String getNameAsText(String nameAsValue) {
		return ( != null) ? getNameAsText(nameAsValue) : nameAsValue;
	}
	protected String getNameAsText(String nameAsValueString nameRuleName) {
		return (nameRuleName != null) ? .toString(nameAsValuenameRuleName) : nameAsValue;
	}
	protected String getNameAsValue(String nameAsText) {
		return ( != null) ? getNameAsValue(nameAsText) : nameAsText;
	}
	protected String getNameAsValue(String nameAsTextString nameRuleName) {
		return (nameRuleName != null) ? .toValue(nameAsTextnameRuleNamenull).toString()
nameAsText;
	}
	public String getOriginalName() {
	}
	public void applyDeclarationChange(String newNameResourceSet resourceSet) {
		super.applyDeclarationChange(getNameAsValue(newName), resourceSet);
	}
New to GrepCode? Check out our FAQ X