Search in sources :

Example 1 with ConversionResult

use of org.eclipse.xtend.core.javaconverter.JavaConverter.ConversionResult in project xtext-xtend by eclipse.

the class ConversionProblemsDialog method handleMemberSelect.

protected void handleMemberSelect(Widget item) {
    Object data = null;
    if (item != null)
        data = item.getData();
    if (data instanceof ICompilationUnit) {
        this.currentCU = (ICompilationUnit) data;
        problemsPane.setImage(CompareUI.getImage(this.currentCU.getResource()));
        String pattern = "Problems in file {0}";
        String title = MessageFormat.format(pattern, new Object[] { this.currentCU.getResource().getName() });
        problemsPane.setText(title);
        if (problemsTable != null) {
            problemsTable.setRedraw(false);
            problemsTable.removeAll();
            ConversionResult conversionResult = input.get(currentCU);
            for (String problem : conversionResult.getProblems()) {
                addProblemItem(problem);
            }
            problemsTable.setRedraw(true);
        }
    } else
        this.currentCU = null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ConversionResult(org.eclipse.xtend.core.javaconverter.JavaConverter.ConversionResult)

Example 2 with ConversionResult

use of org.eclipse.xtend.core.javaconverter.JavaConverter.ConversionResult in project xtext-xtend by eclipse.

the class ConvertJavaCode method doConvert.

private void doConvert(final Set<ICompilationUnit> compilationUnits, IProgressMonitor monitor, Map<ICompilationUnit, ConversionResult> conversionResults) throws ExecutionException, InterruptedException {
    for (ICompilationUnit iCompilationUnit : compilationUnits) {
        if (monitor.isCanceled()) {
            throw new InterruptedException();
        }
        JavaConverter converter = converterProvider.get();
        ConversionResult conversionResult = converter.toXtend(iCompilationUnit);
        monitor.subTask("Working with " + iCompilationUnit.getElementName());
        if (!conversionResult.getProblems().iterator().hasNext()) {
            IFile file = xtendFileToCreate(iCompilationUnit);
            Resource resource = createResource(file, conversionResult.getXtendCode());
            if (!validateResource(resource)) {
                monitor.subTask("Conversion was not successfull. Re-trying with another strategy.");
                conversionResult = converter.useRobustSyntax().toXtend(iCompilationUnit);
            }
        }
        conversionResults.put(iCompilationUnit, conversionResult);
        monitor.worked(1);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ConversionResult(org.eclipse.xtend.core.javaconverter.JavaConverter.ConversionResult) IFile(org.eclipse.core.resources.IFile) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) IResource(org.eclipse.core.resources.IResource) JavaConverter(org.eclipse.xtend.core.javaconverter.JavaConverter)

Example 3 with ConversionResult

use of org.eclipse.xtend.core.javaconverter.JavaConverter.ConversionResult in project xtext-xtend by eclipse.

the class ConvertJavaCode method runJavaConverter.

public void runJavaConverter(final Set<ICompilationUnit> compilationUnits, Shell activeShell) throws ExecutionException {
    Map<ICompilationUnit, ConversionResult> conversionResults = newHashMap();
    boolean canceled = convertAllWithProgress(activeShell, compilationUnits, conversionResults);
    if (canceled) {
        return;
    }
    boolean hasConversionFailures = any(conversionResults.values(), new Predicate<ConversionResult>() {

        @Override
        public boolean apply(ConversionResult input) {
            return input.getProblems().iterator().hasNext();
        }
    });
    if (hasConversionFailures) {
        ConversionProblemsDialog problemsDialog = new ConversionProblemsDialog(activeShell, conversionResults);
        problemsDialog.open();
        if (problemsDialog.getReturnCode() == Window.CANCEL) {
            return;
        }
    }
    final String storedValue = dialogs.getUserDecision(DELETE_JAVA_FILES_AFTER_CONVERSION);
    boolean deleteJavaFiles = false;
    if (MessageDialogWithToggle.PROMPT.equals(storedValue)) {
        int userAnswer = dialogs.askUser("Delete Java source files?", "Xtend converter", DELETE_JAVA_FILES_AFTER_CONVERSION, activeShell);
        if (userAnswer == IDialogConstants.CANCEL_ID) {
            // cancel
            return;
        } else if (userAnswer == IDialogConstants.YES_ID) {
            deleteJavaFiles = true;
        }
    } else if (MessageDialogWithToggle.ALWAYS.equals(storedValue)) {
        deleteJavaFiles = true;
    }
    for (final Entry<ICompilationUnit, ConversionResult> result : conversionResults.entrySet()) {
        ICompilationUnit compilationUnit = result.getKey();
        ConversionResult conversionResult = result.getValue();
        String xtendCode = conversionResult.getXtendCode();
        IFile xtendFileToCreate = xtendFileToCreate(compilationUnit);
        if (!conversionResult.getProblems().iterator().hasNext()) {
            String formattedCode = formatXtendCode(xtendFileToCreate, xtendCode);
            if (formattedCode != null) {
                xtendCode = formattedCode;
            }
        }
        writeToFile(xtendFileToCreate, xtendCode);
        if (deleteJavaFiles) {
            try {
                compilationUnit.delete(true, null);
            } catch (JavaModelException e) {
                handleException("Unable to delete Java file.", e, compilationUnit.getResource());
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ConversionResult(org.eclipse.xtend.core.javaconverter.JavaConverter.ConversionResult) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile)

Example 4 with ConversionResult

use of org.eclipse.xtend.core.javaconverter.JavaConverter.ConversionResult in project xtext-xtend by eclipse.

the class ConversionProblemsDialog method create.

@Override
public void create() {
    super.create();
    if (javaFilesTable != null && !javaFilesTable.isDisposed()) {
        for (Entry<ICompilationUnit, ConversionResult> entry : input.entrySet()) {
            if (entry.getValue().getProblems().iterator().hasNext()) {
                ICompilationUnit cu = entry.getKey();
                IResource resource = cu.getResource();
                TableItem ti = new TableItem(javaFilesTable, SWT.NONE);
                ti.setImage(CompareUI.getImage(resource));
                ti.setText(resource.getName());
                ti.setData(cu);
                ti.setChecked(true);
            }
        }
        if (javaFilesTable.getItems().length > 0) {
            javaFilesTable.select(0);
            handleMemberSelect(javaFilesTable.getItems()[0]);
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ConversionResult(org.eclipse.xtend.core.javaconverter.JavaConverter.ConversionResult) TableItem(org.eclipse.swt.widgets.TableItem) IResource(org.eclipse.core.resources.IResource)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 ConversionResult (org.eclipse.xtend.core.javaconverter.JavaConverter.ConversionResult)4 IFile (org.eclipse.core.resources.IFile)2 IResource (org.eclipse.core.resources.IResource)2 Resource (org.eclipse.emf.ecore.resource.Resource)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 TableItem (org.eclipse.swt.widgets.TableItem)1 JavaConverter (org.eclipse.xtend.core.javaconverter.JavaConverter)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1