[8] | 1 | package de.ugoe.cs.swe.bnftools.ui.formatter;
|
---|
| 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.core.runtime.NullProgressMonitor;
|
---|
| 7 | import org.eclipse.emf.ecore.EObject;
|
---|
| 8 | import org.eclipse.jface.dialogs.MessageDialog;
|
---|
| 9 | import org.eclipse.ltk.core.refactoring.Change;
|
---|
| 10 | import org.eclipse.ltk.core.refactoring.PerformChangeOperation;
|
---|
| 11 | import org.eclipse.ltk.core.refactoring.RefactoringCore;
|
---|
| 12 | import org.eclipse.ltk.core.refactoring.RefactoringStatus;
|
---|
| 13 | import org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring;
|
---|
| 14 | import org.eclipse.swt.widgets.Display;
|
---|
| 15 | import org.eclipse.ui.PlatformUI;
|
---|
| 16 | import org.eclipse.xtext.parsetree.NodeAdapter;
|
---|
| 17 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
| 18 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
| 19 | import org.eclipse.xtext.ui.editor.model.XtextDocument;
|
---|
| 20 |
|
---|
| 21 | import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
|
---|
| 22 |
|
---|
| 23 | public class FormatterHandler extends AbstractHandler {
|
---|
| 24 |
|
---|
| 25 | public Object execute(ExecutionEvent event) throws ExecutionException {
|
---|
| 26 | XtextEditor editor = (XtextEditor) PlatformUI.getWorkbench()
|
---|
| 27 | .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
|
---|
| 28 |
|
---|
| 29 | // TODO: better error handling
|
---|
| 30 | if (!(editor.getDocument() instanceof XtextDocument)) {
|
---|
| 31 | return null;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | XtextDocument doc = (XtextDocument) editor.getDocument();
|
---|
| 35 | EObject root = doc.readOnly(new RootEObjectFinder());
|
---|
| 36 | NodeAdapter rootNode = NodeUtil.getNodeAdapter(root);
|
---|
| 37 |
|
---|
| 38 | FormatterProcessor processor = new FormatterProcessor(editor,
|
---|
| 39 | rootNode.getParserNode(), doc);
|
---|
| 40 |
|
---|
| 41 | FormatterRefactoring refactoring = new FormatterRefactoring(processor);
|
---|
| 42 |
|
---|
| 43 | try {
|
---|
| 44 | performRefactoring(refactoring);
|
---|
| 45 | } catch (InterruptedException e1) {
|
---|
| 46 | e1.printStackTrace();
|
---|
| 47 | } catch (Exception e) {
|
---|
| 48 | e.printStackTrace();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | return null;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public void performRefactoring(ProcessorBasedRefactoring ref)
|
---|
| 55 | throws Exception {
|
---|
| 56 | NullProgressMonitor nullProgressMonitor = new NullProgressMonitor();
|
---|
| 57 | RefactoringStatus status = ref.checkAllConditions(nullProgressMonitor);
|
---|
| 58 | if (status.isOK()) {
|
---|
| 59 | Change change = ref.createChange(nullProgressMonitor);
|
---|
| 60 | change.initializeValidationData(nullProgressMonitor);
|
---|
| 61 | change.setEnabled(true);
|
---|
| 62 | PerformChangeOperation operation = new PerformChangeOperation(
|
---|
| 63 | change);
|
---|
| 64 | operation.setUndoManager(RefactoringCore.getUndoManager(),
|
---|
| 65 | ref.getProcessor().getProcessorName());
|
---|
| 66 | operation.run(nullProgressMonitor);
|
---|
| 67 | change.dispose();
|
---|
| 68 | } else {
|
---|
| 69 | MessageDialog.openInformation(
|
---|
| 70 | Display.getDefault().getActiveShell(), "Custom Formatter",
|
---|
| 71 | status.getMessageMatchingSeverity(status.getSeverity()));
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | }
|
---|