Search in sources :

Example 31 with Document

use of org.eclipse.jface.text.Document in project tdi-studio-se by Talend.

the class TalendJavaSourceViewer method createViewerWithVariables.

public static ReconcilerViewer createViewerWithVariables(Composite composite, int styles, IExpressionDataBean dataBean) {
    IDocument document = new Document();
    StringBuffer buff = new StringBuffer();
    //$NON-NLS-1$
    buff.append("\npackage internal;\n\n");
    buff.append(getImports());
    //$NON-NLS-1$ //$NON-NLS-2$
    buff.append("public class " + VIEWER_CLASS_NAME + currentId + " {\n");
    IRunProcessService runProcessService = getRunProcessService();
    if (runProcessService != null && runProcessService.getSelectedContext() != null) {
        List<IContextParameter> params = runProcessService.getSelectedContext().getContextParameterList();
        buff.append(TEXT_1);
        for (IContextParameter ctxParam : params) {
            buff.append(TEXT_2);
            buff.append(ctxParam.getName());
            buff.append(TEXT_3);
            buff.append(ctxParam.getName());
            buff.append(TEXT_4);
        }
        buff.append(TEXT_5);
        for (IContextParameter ctxParam : params) {
            if (//$NON-NLS-1$ //$NON-NLS-2$
            ctxParam.getType().equals("id_List Of Value") || ctxParam.getType().equals("id_File") || ctxParam.getType().equals("id_Directory") || ctxParam.getType().equals("id_Character")) {
                //$NON-NLS-1$ //$NON-NLS-2$
                buff.append(TEXT_6);
                buff.append(ctxParam.getName());
                buff.append(TEXT_7);
            } else {
                buff.append(TEXT_8);
                buff.append(JavaTypesManager.getTypeToGenerate(ctxParam.getType(), true));
                buff.append(TEXT_9);
                buff.append(ctxParam.getName());
                buff.append(TEXT_10);
            }
        }
        buff.append(TEXT_11);
        //$NON-NLS-1$
        buff.append("\tprivate static ContextProperties context = new ContextProperties();\n");
        //$NON-NLS-1$
        buff.append("\tprivate static final java.util.Map<String, Object> globalMap = new java.util.HashMap<String, Object>();\n");
        if (dataBean != null) {
            buff.append(parseVariables(dataBean.getVariables()));
            //$NON-NLS-1$ //$NON-NLS-2$
            buff.append("\tpublic " + dataBean.getExpressionType() + " myFunction(){\n");
        }
        //$NON-NLS-1$
        buff.append("\t\treturn \n");
    }
    int length = buff.toString().length();
    //$NON-NLS-1$
    String defaultValue = "";
    //$NON-NLS-1$
    buff.append(defaultValue + "\n;\t\n}\n}");
    document.set(buff.toString());
    return initializeViewer(composite, styles, true, document, length);
}
Also used : IRunProcessService(org.talend.designer.runprocess.IRunProcessService) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IContextParameter(org.talend.core.model.process.IContextParameter) IDocument(org.eclipse.jface.text.IDocument)

Example 32 with Document

use of org.eclipse.jface.text.Document in project tdi-studio-se by Talend.

the class TalendJavaSourceViewer method createViewer.

public static ISourceViewer createViewer(Composite composite, int styles, boolean checkCode) {
    /*
         * Special for cProcessor https://jira.talendforge.org/browse/TESB-3687 for cJavaDSLProcessor
         * https://jira.talendforge.org/browse/TESB-7615
         */
    boolean isRouteProcess = isRouteProcess();
    StringBuffer buff = new StringBuffer();
    //$NON-NLS-1$
    buff.append("package internal;\n\n");
    buff.append(getImports());
    // add special imports for RouteBuilder
    if (isRouteProcess) {
        buff.append("import org.apache.camel.*;");
    }
    //$NON-NLS-1$ //$NON-NLS-2$
    buff.append("public class " + VIEWER_CLASS_NAME + currentId + " {\n");
    //$NON-NLS-1$
    buff.append("\tprivate static java.util.Properties context = new java.util.Properties();\n");
    //$NON-NLS-1$
    buff.append("\tprivate static final java.util.Map<String, Object> globalMap = new java.util.HashMap<String, Object>();\n");
    /*
         * Special for cProcessor https://jira.talendforge.org/browse/TESB-3687
         */
    if (isRouteProcess) {
        //$NON-NLS-1$
        buff.append("\tprivate org.apache.camel.Exchange exchange;\n");
        //$NON-NLS-1$
        buff.append("\torg.apache.camel.impl.DefaultCamelContext camelContext;\n");
        //$NON-NLS-1$
        buff.append("\tjavax.jms.ConnectionFactory jmsConnectionFactory;\n");
    }
    // end of https://jira.talendforge.org/browse/TESB-3687
    //$NON-NLS-1$
    buff.append("\tpublic void myFunction(){\n");
    //$NON-NLS-1$
    buff.append("\t  if( \n");
    /*
         * for cJavaDSLProcessor https://jira.talendforge.org/browse/TESB-7615
         */
    if (isRouteProcess) {
        buff.append("new org.apache.camel.model.RouteDefinition()\n");
    }
    // End of https://jira.talendforge.org/browse/TESB-7615
    int documentOffset = buff.toString().length();
    //$NON-NLS-1$
    buff.append("){\n\t}");
    //$NON-NLS-1$
    buff.append("\n\t\n}\n}");
    IDocument document = new Document();
    document.set(buff.toString());
    return initializeViewer(composite, styles, checkCode, document, documentOffset);
}
Also used : Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument)

Example 33 with Document

use of org.eclipse.jface.text.Document in project bndtools by bndtools.

the class FileUtils method readFully.

public static IDocument readFully(IFile file) throws CoreException, IOException {
    if (file.exists()) {
        InputStream stream = file.getContents();
        byte[] bytes = readFully(stream);
        String string = new String(bytes, file.getCharset());
        return new Document(string);
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument)

Example 34 with Document

use of org.eclipse.jface.text.Document in project che by eclipse.

the class JavaContext method evaluateTemplate.

/**
	 * Evaluates a 'java' template in the context of a compilation unit
	 *
	 * @param template the template to be evaluated
	 * @param compilationUnit the compilation unit in which to evaluate the template
	 * @param position the position inside the compilation unit for which to evaluate the template
	 * @return the evaluated template
	 * @throws CoreException in case the template is of an unknown context type
	 * @throws BadLocationException in case the position is invalid in the compilation unit
	 * @throws TemplateException in case the evaluation fails
	 */
public static String evaluateTemplate(Template template, ICompilationUnit compilationUnit, int position) throws CoreException, BadLocationException, TemplateException {
    TemplateContextType contextType = JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(template.getContextTypeId());
    if (!(contextType instanceof CompilationUnitContextType))
        throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.ID_PLUGIN, IStatus.ERROR, JavaTemplateMessages.JavaContext_error_message, null));
    IDocument document = new Document();
    if (compilationUnit != null && compilationUnit.exists())
        document.set(compilationUnit.getSource());
    CompilationUnitContext context = ((CompilationUnitContextType) contextType).createContext(document, position, 0, compilationUnit);
    context.setForceEvaluation(true);
    TemplateBuffer buffer = context.evaluate(template);
    if (buffer == null)
        return null;
    return buffer.getString();
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) CoreException(org.eclipse.core.runtime.CoreException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) IDocument(org.eclipse.jface.text.IDocument)

Example 35 with Document

use of org.eclipse.jface.text.Document in project che by eclipse.

the class ReplaceInvocationsRefactoring method resolveSourceProvider.

private SourceProvider resolveSourceProvider(IMethodBinding methodBinding, RefactoringStatus status) throws JavaModelException {
    final IMethod method = (IMethod) methodBinding.getJavaElement();
    ITypeRoot typeRoot;
    IDocument source;
    CompilationUnit methodDeclarationAstRoot;
    ICompilationUnit methodCu = (method).getCompilationUnit();
    if (methodCu != null) {
        typeRoot = methodCu;
        ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
        parser.setSource(methodCu);
        parser.setFocalPosition(method.getNameRange().getOffset());
        CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
        MethodDeclaration methodDecl = (MethodDeclaration) NodeFinder.perform(compilationUnit, method.getNameRange()).getParent();
        AST ast = compilationUnit.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        Block newBody = ast.newBlock();
        newBody.statements().add(rewrite.createStringPlaceholder(fBody, ASTNode.EMPTY_STATEMENT));
        rewrite.replace(methodDecl.getBody(), newBody, null);
        List<SingleVariableDeclaration> parameters = methodDecl.parameters();
        for (int i = 0; i < parameters.size(); i++) {
            SingleVariableDeclaration parameter = parameters.get(i);
            rewrite.set(parameter.getName(), SimpleName.IDENTIFIER_PROPERTY, fParameterNames[i], null);
        }
        TextEdit textEdit = rewrite.rewriteAST();
        Document document = new Document(methodCu.getBuffer().getContents());
        try {
            textEdit.apply(document);
        } catch (MalformedTreeException e) {
            JavaPlugin.log(e);
        } catch (BadLocationException e) {
            JavaPlugin.log(e);
        }
        source = document;
        methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(source.get(), methodCu, true, true, null);
    } else {
        IClassFile classFile = method.getClassFile();
        //TODO: use source if available?
        StubCreator stubCreator = new StubCreator(true) {

            @Override
            protected void appendMethodBody(IMethod currentMethod) throws JavaModelException {
                if (currentMethod.equals(method)) {
                    fBuffer.append(fBody);
                } else {
                    super.appendMethodBody(currentMethod);
                }
            }

            /*
				 * @see org.eclipse.jdt.internal.corext.refactoring.binary.StubCreator#appendMethodParameterName(org.eclipse.jdt.core.IMethod, int)
				 */
            @Override
            protected void appendMethodParameterName(IMethod currentMethod, int index) {
                if (currentMethod.equals(method)) {
                    fBuffer.append(fParameterNames[index]);
                } else {
                    super.appendMethodParameterName(currentMethod, index);
                }
            }
        };
        String stub = stubCreator.createStub(classFile.getType(), null);
        source = new Document(stub);
        methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(stub, classFile, true, true, null);
        typeRoot = classFile;
    }
    ASTNode node = methodDeclarationAstRoot.findDeclaringNode(methodBinding.getKey());
    if (node instanceof MethodDeclaration) {
        return new SourceProvider(typeRoot, source, (MethodDeclaration) node);
    } else {
        status.addFatalError(RefactoringCoreMessages.ReplaceInvocationsRefactoring_cannot_find_method_declaration);
        return null;
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) IClassFile(org.eclipse.jdt.core.IClassFile) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) StubCreator(org.eclipse.jdt.internal.corext.refactoring.binary.StubCreator) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) IMethod(org.eclipse.jdt.core.IMethod) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

Document (org.eclipse.jface.text.Document)59 IDocument (org.eclipse.jface.text.IDocument)48 BadLocationException (org.eclipse.jface.text.BadLocationException)26 TextEdit (org.eclipse.text.edits.TextEdit)16 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)9 CoreException (org.eclipse.core.runtime.CoreException)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 IRegion (org.eclipse.jface.text.IRegion)8 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)8 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)8 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)7 TemplateException (org.eclipse.jface.text.templates.TemplateException)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)6 Template (org.eclipse.jface.text.templates.Template)6 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)6 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)6 InputStream (java.io.InputStream)5 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)5 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)5