use of org.eclipse.core.commands.ExecutionException in project xtext-xtend by eclipse.
the class PasteJavaCodeHandler method doPasteJavaCode.
private void doPasteJavaCode(final XtextEditor activeXtextEditor, final String javaCode, final JavaImportData javaImports) throws ExecutionException {
ISourceViewer sourceViewer = activeXtextEditor.getInternalSourceViewer();
final IXtextDocument xtextDocument = activeXtextEditor.getDocument();
IJavaProject project = null;
IEditorInput editorInput = activeXtextEditor.getEditorInput();
if (editorInput instanceof IFileEditorInput) {
IProject iProject = ((IFileEditorInput) editorInput).getFile().getProject();
project = JavaCore.create(iProject);
}
final int selectionOffset = sourceViewer.getSelectedRange().x - 1;
EObject targetElement = xtextDocument.readOnly(new IUnitOfWork<EObject, XtextResource>() {
@Override
public EObject exec(XtextResource state) throws Exception {
IParseResult parseResult = state.getParseResult();
if (parseResult == null) {
return null;
}
ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), selectionOffset);
return leafNode.getSemanticElement();
}
});
JavaConverter javaConverter = javaConverterProvider.get();
final String xtendCode = javaConverter.toXtend(javaCode, javaImports != null ? javaImports.getImports() : null, targetElement, project);
if (!Strings.isEmpty(xtendCode)) {
if (javaImports != null) {
importsUtil.addImports(javaImports.getImports(), javaImports.getStaticImports(), new String[] {}, xtextDocument);
}
Point selection = sourceViewer.getSelectedRange();
try {
xtextDocument.replace(selection.x, selection.y, xtendCode);
} catch (BadLocationException e) {
throw new ExecutionException("Failed to replace content.", e);
}
// TODO enable formatting, when performance became better
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=457814
// doFormat(sourceViewer, xtendCode, selection);
}
}
use of org.eclipse.core.commands.ExecutionException in project xtext-xtend by eclipse.
the class ExtractMethodHandler method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
syncUtil.totalSync(false);
final XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
if (editor != null) {
final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
final IXtextDocument document = editor.getDocument();
XtextResource copiedResource = document.priorityReadOnly(new IUnitOfWork<XtextResource, XtextResource>() {
@Override
public XtextResource exec(XtextResource state) throws Exception {
return resourceCopier.loadIntoNewResourceSet(state);
}
});
List<XExpression> expressions = expressionUtil.findSelectedSiblingExpressions(copiedResource, selection);
if (!expressions.isEmpty()) {
ExtractMethodRefactoring extractMethodRefactoring = refactoringProvider.get();
if (extractMethodRefactoring.initialize(editor, expressions, true)) {
updateSelection(editor, expressions);
ExtractMethodWizard wizard = wizardFactory.create(extractMethodRefactoring);
RefactoringWizardOpenOperation_NonForking openOperation = new RefactoringWizardOpenOperation_NonForking(wizard);
openOperation.run(editor.getSite().getShell(), "Extract Method");
}
}
}
} catch (InterruptedException e) {
return null;
} catch (Exception exc) {
LOG.error("Error during refactoring", exc);
MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error during refactoring", exc.getMessage() + "\nSee log for details");
}
return null;
}
use of org.eclipse.core.commands.ExecutionException in project Palladio-Editors-Sirius by PalladioSimulator.
the class AllocationCreationWizard method finish.
@Override
protected void finish() {
Allocation allocation = (Allocation) modelObject;
Session session = SessionManager.INSTANCE.getSession(modelObject);
TransactionalEditingDomain domain = session.getTransactionalEditingDomain();
String fileString = modelURI.toPlatformString(true);
IPath path = new Path(fileString);
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
List<IFile> list = new ArrayList<IFile>();
list.add(file);
AbstractTransactionalCommand command = new AbstractTransactionalCommand(domain, "Save Allocation model.", list) {
@Override
protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
allocation.setTargetResourceEnvironment_Allocation(resourceEnvironmentSelectorpage.getSelectedResourceEnvironment(session));
allocation.setSystem_Allocation(systemSelectorPage.getSelectedSystem(session));
return CommandResult.newOKCommandResult();
}
};
try {
OperationHistoryFactory.getOperationHistory().execute(command, new NullProgressMonitor(), null);
} catch (ExecutionException e) {
System.out.println("Unable to save allocation model.");
e.printStackTrace();
}
}
use of org.eclipse.core.commands.ExecutionException in project eclipse.platform.text by eclipse.
the class MarkerRulerAction method execute.
/**
* Execute the specified undoable operation.
*
* @param operation the operation to execute
* @since 3.3
*/
private void execute(IUndoableOperation operation) {
final Shell shell = getTextEditor().getSite().getShell();
IAdaptable context = new IAdaptable() {
@SuppressWarnings("unchecked")
@Override
public <T> T getAdapter(Class<T> adapter) {
if (adapter == Shell.class)
return (T) shell;
return null;
}
};
IOperationHistory operationHistory = PlatformUI.getWorkbench().getOperationSupport().getOperationHistory();
try {
operationHistory.execute(operation, null, context);
} catch (ExecutionException e) {
Bundle bundle = Platform.getBundle(PlatformUI.PLUGIN_ID);
ILog log = Platform.getLog(bundle);
// $NON-NLS-2$ //$NON-NLS-1$
String msg = getString(fBundle, fPrefix + "error.dialog.message", fPrefix + "error.dialog.message");
log.log(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.OK, msg, e));
}
}
use of org.eclipse.core.commands.ExecutionException in project AutoRefactor by JnRouvignac.
the class AutoRefactorHandler method execute.
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
try {
Environment environment = getEnvironment();
new PrepareApplyRefactoringsJob(getSelectedJavaElements(event), AllRefactoringRules.getConfiguredRefactoringRules(environment.getPreferences()), environment).schedule();
} catch (Exception e) {
final Shell shell = HandlerUtil.getActiveShell(event);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
showMessage(shell, "An error has occurred:\n\n" + sw.toString());
}
// Use ASTMatcher and do not compare content of expressions, compare just resolvedTypeBinding().
return null;
}
Aggregations