Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 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.commands.AbstractHandler;
 import  org.eclipse.core.commands.ExecutionEvent;
 import  org.eclipse.core.commands.ExecutionException;
 import  org.eclipse.core.runtime.OperationCanceledException;
 import  org.eclipse.jface.dialogs.MessageDialog;
 import  org.eclipse.jface.text.ITextSelection;
 import  org.eclipse.swt.widgets.Display;
 

Author(s):
Jan Koehnlein - Initial contribution and API
Holger Schill
 
 @SuppressWarnings("deprecation")
 public class DefaultRenameElementHandler extends AbstractHandler implements IRenameElementHandler {
 
 
 	
 
 	
 	
 	
 	protected SyncUtil syncUtil;
 	
 	protected static final Logger LOG = Logger.getLogger(DefaultRenameElementHandler.class);
 
 	public Object execute(ExecutionEvent eventthrows ExecutionException {
 		try {
 			final XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
 			if (editor != null) {
 				final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
 				IRenameElementContext renameElementContext = editor.getDocument().priorityReadOnly(
 							public IRenameElementContext exec(XtextResource resourcethrows Exception {
 								EObject selectedElement = .resolveElementAt(resource,
 										selection.getOffset());
 								if (selectedElement != null) {
 											selectedElementeditorselectionresource);
 									if (isRefactoringEnabled(renameElementContextresource))
 										return renameElementContext;
 								}
 								return null;
 							}
 						});
 				if (renameElementContext != null) {
 					startRenameElement(renameElementContext);
 				}
 			}
 		} catch (OperationCanceledException e) {
 			// cancelled by user, ok
 			return null;
 		} catch(InterruptedException e) {
 			// cancelled by user, ok
 			return null;
 		} catch (Exception exc) {
 			.error("Error initializing refactoring"exc);
 			MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error initializing refactoring",
					exc.getMessage() + "\nSee log for details");
		}
		return null;
	}

To maintain binary compatibility only.
	public IRenameElementContext createRenameElementContext(EObject targetElementfinal XtextEditor triggeringEditor,
			final ITextSelection selectionXtextResource triggeringResource) {
		return .createRenameElementContext(targetElementtriggeringEditorselectiontriggeringResource);
	}
	protected boolean isRefactoringEnabled(IRenameElementContext renameElementContextXtextResource resource) {
		ResourceSet resourceSet = resource.getResourceSet();
		if (renameElementContext != null && resourceSet != null) {
			EObject targetElement = resourceSet.getEObject(renameElementContext.getTargetElementURI(), true);
			if (targetElement != null && !targetElement.eIsProxy()) {
				if(targetElement.eResource() == resource && renameElementContext.getTriggeringEditorSelection() instanceof ITextSelection) {
					ITextSelection textSelection = (ITextSelection) renameElementContext.getTriggeringEditorSelection();
					ITextRegion selectedRegion = new TextRegion(textSelection.getOffset(), textSelection.getLength());
					INode crossReferenceNode = .getCrossReferenceNode(resourceselectedRegion);
					if(crossReferenceNode == null) {
						// selection is on the declaration. make sure it's the name
						ITextRegion significantRegion = .getSignificantTextRegion(targetElement);
						if(!significantRegion.contains(selectedRegion)) 
							return false;
					}
				}
				IRenameStrategy.Provider renameStrategyProvider = .findService(targetElement,
				try {
					return renameStrategyProvider.get(targetElementrenameElementContext) != null;
catch (NoSuchStrategyException e) {
					MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Cannot rename element",
				}
			}
		}
		return false;
	}
	protected void startRenameElement(IRenameElementContext renameElementContextthrows InterruptedException {
	}
New to GrepCode? Check out our FAQ X