Search in sources :

Example 41 with IDocument

use of org.eclipse.jface.text.IDocument 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 42 with IDocument

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

the class SQLTextViewer method setDocument.

public void setDocument(IDocument dc) {
    IDocument previous = this.getDocument();
    if (previous != null) {
        partitioner.disconnect();
    }
    super.setDocument(dc);
    if (dc != null) {
        partitioner.connect(dc);
        dc.setDocumentPartitioner(partitioner);
        undoManager.connect(this);
        this.setUndoManager(undoManager);
    }
}
Also used : IDocument(org.eclipse.jface.text.IDocument)

Example 43 with IDocument

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

the class ReconcilerViewer method getAllSnippetsAnnotations.

private Map<ProjectionAnnotation, Position> getAllSnippetsAnnotations() {
    Map<ProjectionAnnotation, Position> annotations = new HashMap<ProjectionAnnotation, Position>();
    IDocument document = getDocument();
    int curOffset = 0;
    FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(document);
    try {
        //$NON-NLS-1$
        IRegion startRegion = frda.find(curOffset, "SNIPPET_START", true, false, false, false);
        while (startRegion != null && startRegion.getOffset() >= curOffset) {
            int startLine = document.getLineOfOffset(startRegion.getOffset());
            int startOffset = document.getLineOffset(startLine);
            curOffset = startOffset + document.getLineLength(startLine);
            //$NON-NLS-1$
            IRegion endRegion = frda.find(startRegion.getOffset(), "SNIPPET_END", true, false, false, false);
            if (endRegion != null) {
                int endLine = document.getLineOfOffset(endRegion.getOffset());
                int endOffset = document.getLineOffset(endLine);
                endOffset += document.getLineLength(endLine);
                curOffset = endOffset;
                String text = document.get(startOffset, endOffset - startOffset);
                ProjectionAnnotation annotation = new ProjectionAnnotation(true);
                annotation.setText(text);
                annotation.setRangeIndication(true);
                annotations.put(annotation, new Position(startOffset, endOffset - startOffset));
            }
            if (curOffset < document.getLength()) {
                //$NON-NLS-1$
                startRegion = frda.find(curOffset, "SNIPPET_START", true, false, false, false);
            }
        }
    } catch (BadLocationException e) {
        ExceptionHandler.process(e);
    }
    return annotations;
}
Also used : ProjectionAnnotation(org.eclipse.jface.text.source.projection.ProjectionAnnotation) HashMap(java.util.HashMap) Position(org.eclipse.jface.text.Position) IDocument(org.eclipse.jface.text.IDocument) FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 44 with IDocument

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

the class JavaProcessor method formatCode.

/**
     * DOC nrousseau Comment method "formatCode".
     *
     * from SourceViewer.doOperation for FORMAT.
     *
     * @param processCode
     * @return
     */
@SuppressWarnings({ "unchecked" })
private String formatCode(String processCode) {
    // we cannot make calls to Ui in headless mode
    if (ProcessorUtilities.isExportConfig() || CommonsPlugin.isHeadless()) {
        // nothing to do
        return processCode;
    }
    final IDocument document = new Document(processCode);
    JavaTextTools tools = JavaPlugin.getDefault().getJavaTextTools();
    tools.setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING);
    IFormattingContext context = null;
    DocumentRewriteSession rewriteSession = null;
    if (document instanceof IDocumentExtension4) {
        rewriteSession = ((IDocumentExtension4) document).startRewriteSession(DocumentRewriteSessionType.SEQUENTIAL);
    }
    try {
        final String rememberedContents = document.get();
        try {
            final MultiPassContentFormatter formatter = new MultiPassContentFormatter(IJavaPartitions.JAVA_PARTITIONING, IDocument.DEFAULT_CONTENT_TYPE);
            formatter.setMasterStrategy(new JavaFormattingStrategy());
            // formatter.setSlaveStrategy(new CommentFormattingStrategy(),
            // IJavaPartitions.JAVA_DOC);
            // formatter.setSlaveStrategy(new CommentFormattingStrategy(),
            // IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
            // formatter.setSlaveStrategy(new CommentFormattingStrategy(),
            // IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
            context = new FormattingContext();
            context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.TRUE);
            Map<String, String> preferences;
            if (this.getTalendJavaProject() == null) {
                preferences = new HashMap<String, String>(JavaCore.getOptions());
            } else {
                // use project options
                preferences = new HashMap<String, String>(this.getTalendJavaProject().getJavaProject().getOptions(true));
            }
            context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, preferences);
            formatter.format(document, context);
        } catch (RuntimeException x) {
            // fire wall for
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=47472
            // if something went wrong we undo the changes we just did
            // TODO to be removed after 3.0 M8
            document.set(rememberedContents);
            throw x;
        }
    } finally {
        if (rewriteSession != null && document instanceof IDocumentExtension4) {
            ((IDocumentExtension4) document).stopRewriteSession(rewriteSession);
        }
        if (context != null) {
            context.dispose();
        }
    }
    return document.get();
}
Also used : IFormattingContext(org.eclipse.jface.text.formatter.IFormattingContext) FormattingContext(org.eclipse.jface.text.formatter.FormattingContext) IFormattingContext(org.eclipse.jface.text.formatter.IFormattingContext) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) JavaTextTools(org.eclipse.jdt.ui.text.JavaTextTools) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) MultiPassContentFormatter(org.eclipse.jface.text.formatter.MultiPassContentFormatter) JavaFormattingStrategy(org.eclipse.jdt.internal.ui.text.java.JavaFormattingStrategy) IDocument(org.eclipse.jface.text.IDocument)

Example 45 with IDocument

use of org.eclipse.jface.text.IDocument in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonDocumentProvider method createDocument.

@Override
protected IDocument createDocument(Object element) throws CoreException {
    IDocument document = super.createDocument(element);
    if (document != null) {
        JsonScanner scanner = new JsonScanner(new ColorManager(), store);
        Set<String> tokens = YAMLToken.VALID_TOKENS.keySet();
        FastPartitioner partitioner = new FastPartitioner(scanner, tokens.toArray(new String[tokens.size()]));
        document.setDocumentPartitioner(partitioner);
        partitioner.connect(document);
    }
    return document;
}
Also used : FastPartitioner(org.eclipse.jface.text.rules.FastPartitioner) ColorManager(org.dadacoalition.yedit.editor.ColorManager) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

IDocument (org.eclipse.jface.text.IDocument)1055 BadLocationException (org.eclipse.jface.text.BadLocationException)379 Document (org.eclipse.jface.text.Document)189 IRegion (org.eclipse.jface.text.IRegion)150 Test (org.junit.Test)107 CoreException (org.eclipse.core.runtime.CoreException)102 Point (org.eclipse.swt.graphics.Point)94 IFile (org.eclipse.core.resources.IFile)90 ArrayList (java.util.ArrayList)85 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)84 Position (org.eclipse.jface.text.Position)74 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)74 IEditorPart (org.eclipse.ui.IEditorPart)62 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)54 IPath (org.eclipse.core.runtime.IPath)54 ITextSelection (org.eclipse.jface.text.ITextSelection)53 TextEdit (org.eclipse.text.edits.TextEdit)49 Region (org.eclipse.jface.text.Region)47 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)44 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)41