[5] | 1 | package de.ugoe.cs.swe.bnftools.ui.refactoring.rename;
|
---|
| 2 |
|
---|
| 3 | import org.eclipse.core.commands.AbstractHandler;
|
---|
| 4 | import org.eclipse.core.commands.ExecutionEvent;
|
---|
| 5 | import org.eclipse.core.commands.ExecutionException;
|
---|
| 6 | import org.eclipse.jface.dialogs.MessageDialog;
|
---|
| 7 | import org.eclipse.jface.text.ITextSelection;
|
---|
| 8 | import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
|
---|
| 9 | import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
|
---|
| 10 | import org.eclipse.swt.widgets.Display;
|
---|
| 11 | import org.eclipse.ui.handlers.HandlerUtil;
|
---|
| 12 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
| 13 | import org.eclipse.xtext.resource.IEObjectDescription;
|
---|
| 14 | import org.eclipse.xtext.resource.IResourceDescriptions;
|
---|
| 15 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
| 16 |
|
---|
| 17 | import com.google.inject.Inject;
|
---|
| 18 |
|
---|
| 19 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
| 20 | import de.ugoe.cs.swe.bnftools.utils.EObjectSelectionResolver;
|
---|
| 21 | import de.ugoe.cs.swe.bnftools.utils.URIFragmentResolver;
|
---|
| 22 |
|
---|
| 23 | public class RenameRefactoringHandler extends AbstractHandler {
|
---|
| 24 |
|
---|
| 25 | @Inject
|
---|
| 26 | private IResourceDescriptions resourceDescriptions;
|
---|
| 27 |
|
---|
| 28 | // --------------------------------------------------------------------------------
|
---|
| 29 |
|
---|
| 30 | public Object execute(ExecutionEvent event) throws ExecutionException {
|
---|
| 31 | try {
|
---|
| 32 | String currentName = "";
|
---|
| 33 |
|
---|
| 34 | XtextEditor editor = (XtextEditor) HandlerUtil
|
---|
| 35 | .getActiveEditor(event);
|
---|
| 36 |
|
---|
| 37 | final ITextSelection selection = (ITextSelection) editor
|
---|
| 38 | .getSelectionProvider().getSelection();
|
---|
| 39 |
|
---|
| 40 | final IEObjectDescription eObjectDescription = editor
|
---|
| 41 | .getDocument()
|
---|
| 42 | .readOnly(
|
---|
| 43 | new EObjectSelectionResolver(selection, resourceDescriptions));
|
---|
| 44 |
|
---|
| 45 | CompositeNode o = editor.getDocument().readOnly(
|
---|
| 46 | new URIFragmentResolver(eObjectDescription.getEObjectURI()
|
---|
| 47 | .fragment()));
|
---|
| 48 |
|
---|
| 49 | if (o.getElement() instanceof Rule) {
|
---|
| 50 | Rule r = (Rule) o.getElement();
|
---|
| 51 | currentName = r.getName();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | RenameProcessor processor = new RenameProcessor(editor,
|
---|
| 55 | resourceDescriptions);
|
---|
| 56 | RenameRefactoring refactoring = new RenameRefactoring(processor);
|
---|
| 57 |
|
---|
| 58 | RenameRefactoringWizard wizard = new RenameRefactoringWizard(
|
---|
| 59 | refactoring, RefactoringWizard.WIZARD_BASED_USER_INTERFACE);
|
---|
| 60 | wizard.setRenameText(currentName);
|
---|
| 61 |
|
---|
| 62 | RefactoringWizardOpenOperation openOperation = new RefactoringWizardOpenOperation(
|
---|
| 63 | wizard);
|
---|
| 64 |
|
---|
| 65 | openOperation.run(Display.getCurrent().getActiveShell(),
|
---|
| 66 | "Refactoring not possible!");
|
---|
| 67 |
|
---|
| 68 | } catch (Exception e) {
|
---|
| 69 | MessageDialog.openInformation(
|
---|
| 70 | Display.getDefault().getActiveShell(),
|
---|
| 71 | "Rename Refactoring",
|
---|
| 72 | "Error while applying refactoring to workbench/wizard: "
|
---|
| 73 | + e.getMessage());
|
---|
| 74 | e.printStackTrace();
|
---|
| 75 | }
|
---|
| 76 | return null;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | }
|
---|