Search in sources :

Example 56 with ExecutionException

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);
    }
}
Also used : XtextResource(org.eclipse.xtext.resource.XtextResource) Point(org.eclipse.swt.graphics.Point) IProject(org.eclipse.core.resources.IProject) Point(org.eclipse.swt.graphics.Point) BadLocationException(org.eclipse.jface.text.BadLocationException) ExecutionException(org.eclipse.core.commands.ExecutionException) IJavaProject(org.eclipse.jdt.core.IJavaProject) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) IFileEditorInput(org.eclipse.ui.IFileEditorInput) EObject(org.eclipse.emf.ecore.EObject) JavaConverter(org.eclipse.xtend.core.javaconverter.JavaConverter) IParseResult(org.eclipse.xtext.parser.IParseResult) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) ExecutionException(org.eclipse.core.commands.ExecutionException) IEditorInput(org.eclipse.ui.IEditorInput) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 57 with ExecutionException

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;
}
Also used : XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XtextResource(org.eclipse.xtext.resource.XtextResource) ITextSelection(org.eclipse.jface.text.ITextSelection) ExecutionException(org.eclipse.core.commands.ExecutionException) RefactoringWizardOpenOperation_NonForking(org.eclipse.xtext.ui.refactoring.ui.RefactoringWizardOpenOperation_NonForking) XExpression(org.eclipse.xtext.xbase.XExpression) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 58 with ExecutionException

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();
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IAdaptable(org.eclipse.core.runtime.IAdaptable) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) AbstractTransactionalCommand(org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) TransactionalEditingDomain(org.eclipse.emf.transaction.TransactionalEditingDomain) Allocation(org.palladiosimulator.pcm.allocation.Allocation) ExecutionException(org.eclipse.core.commands.ExecutionException) Session(org.eclipse.sirius.business.api.session.Session)

Example 59 with ExecutionException

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));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IAdaptable(org.eclipse.core.runtime.IAdaptable) Shell(org.eclipse.swt.widgets.Shell) ResourceBundle(java.util.ResourceBundle) Bundle(org.osgi.framework.Bundle) IOperationHistory(org.eclipse.core.commands.operations.IOperationHistory) ILog(org.eclipse.core.runtime.ILog) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 60 with ExecutionException

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;
}
Also used : Shell(org.eclipse.swt.widgets.Shell) StringWriter(java.io.StringWriter) PrepareApplyRefactoringsJob(org.autorefactor.refactoring.PrepareApplyRefactoringsJob) Environment(org.autorefactor.environment.Environment) CoreException(org.eclipse.core.runtime.CoreException) UnhandledException(org.autorefactor.util.UnhandledException) ExecutionException(org.eclipse.core.commands.ExecutionException) PrintWriter(java.io.PrintWriter)

Aggregations

ExecutionException (org.eclipse.core.commands.ExecutionException)66 CoreException (org.eclipse.core.runtime.CoreException)20 IFile (org.eclipse.core.resources.IFile)15 ArrayList (java.util.ArrayList)13 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)13 IStatus (org.eclipse.core.runtime.IStatus)12 Shell (org.eclipse.swt.widgets.Shell)12 Status (org.eclipse.core.runtime.Status)11 IEditorPart (org.eclipse.ui.IEditorPart)11 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)10 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 ISelection (org.eclipse.jface.viewers.ISelection)8 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)8 File (java.io.File)7 List (java.util.List)7 XLFHandler (net.heartsome.cat.ts.core.file.XLFHandler)7 XLIFFEditorImplWithNatTable (net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable)7 IProject (org.eclipse.core.resources.IProject)7 FileEditorInput (org.eclipse.ui.part.FileEditorInput)7