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.ui;
  
 import static org.eclipse.xtext.util.Strings.*;
 
 
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.core.runtime.OperationCanceledException;
 import  org.eclipse.jface.operation.IRunnableWithProgress;
 import  org.eclipse.jface.text.ITextSelection;
 import  org.eclipse.jface.text.link.LinkedPositionGroup;
 import  org.eclipse.jface.viewers.ISelection;
 import  org.eclipse.swt.widgets.Display;
 import  org.eclipse.ui.IEditorPart;
 import  org.eclipse.ui.IWorkbench;
 
Handles the various UI states of a refactoring, such as linked mode, dialog, and direct refactoring.

Author(s):
Jan Koehnlein - Initial contribution and API
 
 public class RenameRefactoringController {
 
 	@Inject(optional = true)
 	private IWorkbench workbench;
 
 
 
 
 	
 	
 	
 
 
 
 	private String newName;
 	
 	public void initialize(IRenameElementContext renameElementContext) {
 		if ( != null) {
 			if (.isSameRenameElementContext(renameElementContext)) {
 				return;
 			} else {
 			}
 		}
 		this. = renameElementContext;
 		this. = null;
 	}
 	
 	public void startRefactoring(IRenameElementContext renameElementContext) {
 		initialize(renameElementContext);
 		else 
 	}
 
 	public void startRefactoring(RefactoringType refactoringType) {
 		ViewFreezer freezer = null;
 		try {
 			if (refactoringType == .) {
				return;
			}
			if ( != null)
			freezer.freeze();
			// Cancel undoable right now because the freezer would show the old
			// state and not the new one
			if ( != null) {
				 = null;
			}
			switch (refactoringType) {
					break;
					break;
					break;
				default:
					throw new IllegalStateException("Invalid refactoring type " + refactoringType.toString());
			}
catch (InterruptedException exc) {
			// canceling by the user is ok
finally {
			if (freezer != null)
				freezer.release();
		}
	}
	protected void startLinkedEditing() throws InterruptedException {
		if ( != null
		try {
			final XtextEditor xtextEditor = getXtextEditor();
			if (xtextEditor != null) {
				.getProgressService().run(truetruenew IRunnableWithProgress() {
					public void run(final IProgressMonitor monitorthrows InvocationTargetExceptionInterruptedException {
						try {
							final Provider<LinkedPositionGroup> provider = .getLinkedPositionGroup(monitor);
							Display display = .getDisplay();
							display.syncExec(new Runnable() {
								public void run() {
									if (newLinkedMode.start(providermonitor)) {
										 = newLinkedMode;
									}
								}
							});
catch(OperationCanceledException e) {
							throw new InterruptedException();
						}
					}
				});
				if ( == null) {
				}
			}
catch (InterruptedException e) {
			throw e;
catch (Exception exc) {
			// unwrap invocation target exceptions
			if (exc.getCause() instanceof RuntimeException)
				throw (RuntimeExceptionexc.getCause();
			if (exc instanceof RuntimeException)
				throw (RuntimeExceptionexc;
			throw new WrappedException(exc);
		}
	}
	}

Linked mode is unrecoverable canceled.
	public void cancelLinkedMode() {
		if ( != null) {
		}
	}
	protected void startDirectRefactoring() throws InterruptedException {
		if (Strings.isEmpty()) {
else {
			if(!equal(originalName)) {
				if(renameSupport != null
					renameSupport.startDirectRefactoring();
			}
		}
	}
	protected void startRefactoringWithDialog(final boolean previewOnlythrows InterruptedException {
		if (Strings.isEmpty())
		if (Strings.isEmpty()) 
		else {
			if(renameSupport != null) {
				renameSupport.startRefactoringWithDialog(previewOnly);
			}
		}
	}
	protected String getOriginalName(final XtextEditor xtextEditor) {
		return xtextEditor.getDocument().readOnly(new IUnitOfWork<StringXtextResource>() {
			public String exec(XtextResource statethrows Exception {
				try {
						false);
					if (targetElement == null) {
						return null;
					}
					IRenameStrategy.Provider strategyProvider = .findService(targetElement,
					if (strategyProvider != null) {
						IRenameStrategy strategy = strategyProvider.get(targetElement);
						if (strategy != null)
							return strategy.getOriginalName();
					}
					// null
				}
				return null;
			}
		});
	}
	}
	protected void restoreOriginalSelection() {
		ISelection originalSelection = .getTriggeringEditorSelection();
		if (originalSelection instanceof ITextSelection) {
					((ITextSelection) originalSelection).getOffset(), ((ITextSelection) originalSelection).getLength());
		}
	}
	protected XtextEditor getXtextEditor() {
		if (part instanceof XtextEditor) {
			return (XtextEditorpart;
		}
		return null;
	}
New to GrepCode? Check out our FAQ X