Search in sources :

Example 1 with JavaCompletionProcessor

use of org.eclipse.jdt.internal.ui.text.java.JavaCompletionProcessor in project tdi-studio-se by Talend.

the class TalendJavaSourceViewer method createViewerForComponent.

public static ISourceViewer createViewerForComponent(Composite composite, int styles, CompilationUnitEditor editor, List<Variable> variableList, String uniqueName, String codePart) {
    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    String selectedPart = "[" + uniqueName + " " + codePart + " ] start";
    IDocument originalDocument = editor.getDocumentProvider().getDocument(editor.getEditorInput());
    int documentOffset = -1;
    //$NON-NLS-1$
    String globalFields = "";
    //$NON-NLS-1$
    String localFields = "";
    //$NON-NLS-1$
    globalFields = "\tprivate static java.util.Properties context = new java.util.Properties();\n";
    //$NON-NLS-1$
    globalFields += "\tprivate static final java.util.Map<String, Object> globalMap = new java.util.HashMap<String, Object>();\n";
    IDocument newDoc = new Document();
    boolean checkCode = false;
    FindReplaceDocumentAdapter frda = null;
    // hywang modified for bug 9180
    String documentText = originalDocument.get();
    if (documentText != null && !documentText.equals("") && documentText.length() > 0) {
        //$NON-NLS-1$
        frda = new FindReplaceDocumentAdapter(originalDocument);
    }
    try {
        Region region = null;
        if (frda != null) {
            region = (Region) frda.find(0, selectedPart, true, false, false, false);
        }
        if (region != null) {
            checkCode = true;
            documentOffset = region.getOffset();
            documentOffset = originalDocument.getLineOffset(originalDocument.getLineOfOffset(documentOffset) + 2);
            JavaCompletionProcessor processor = new JavaCompletionProcessor(editor, new ContentAssistant(), IDocument.DEFAULT_CONTENT_TYPE);
            boolean globalFieldsDone = false;
            //$NON-NLS-1$
            globalFields = "";
            String className = editor.getPartName().substring(0, editor.getPartName().indexOf('.') + 1);
            ICompletionProposal[] proposals = processor.computeCompletionProposals(editor.getViewer(), documentOffset);
            for (ICompletionProposal curProposal : proposals) {
                if (curProposal instanceof JavaCompletionProposal) {
                    JavaCompletionProposal javaProposal = ((JavaCompletionProposal) curProposal);
                    if (javaProposal.getJavaElement() instanceof SourceField) {
                        globalFieldsDone = true;
                        SourceField sourceField = (SourceField) javaProposal.getJavaElement();
                        //$NON-NLS-1$ //$NON-NLS-2$
                        globalFields += "\t" + sourceField.getSource() + "\n";
                    // System.out.println();
                    }
                    if (javaProposal.getJavaElement() == null && !globalFieldsDone) {
                        //$NON-NLS-1$
                        String[] str = javaProposal.getSortString().split(" ");
                        //$NON-NLS-1$
                        String variable = "";
                        for (int i = str.length - 1; i >= 0; i--) {
                            //$NON-NLS-1$
                            variable += str[i].length() == 0 ? " " : str[i];
                        }
                        if (variable.contains(className)) {
                            continue;
                        }
                        //$NON-NLS-1$
                        variable += ";";
                        //$NON-NLS-1$ //$NON-NLS-2$
                        localFields += "\t\t" + variable + "\n";
                    // System.out.println(variable);
                    }
                }
            }
        }
    } catch (BadLocationException e) {
        ExceptionHandler.process(e);
    } catch (JavaModelException e) {
        // e.printStackTrace();
        ExceptionHandler.process(e);
    }
    StringBuffer buff = new StringBuffer();
    //$NON-NLS-1$
    buff.append("package internal;\n\n");
    buff.append(getImports());
    //$NON-NLS-1$ //$NON-NLS-2$
    buff.append("public class " + VIEWER_CLASS_NAME + currentId + " {\n");
    buff.append(globalFields);
    //$NON-NLS-1$
    buff.append("\tpublic void myFunction(){\n");
    buff.append(localFields);
    documentOffset = buff.toString().length();
    //$NON-NLS-1$
    buff.append("\n\t\n}\n}");
    newDoc.set(buff.toString());
    return initializeViewer(composite, styles, checkCode, newDoc, documentOffset);
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) SourceField(org.eclipse.jdt.internal.core.SourceField) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) ContentAssistant(org.eclipse.jface.text.contentassist.ContentAssistant) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Region(org.eclipse.jface.text.Region) JavaCompletionProcessor(org.eclipse.jdt.internal.ui.text.java.JavaCompletionProcessor) IDocument(org.eclipse.jface.text.IDocument) FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter) BadLocationException(org.eclipse.jface.text.BadLocationException) JavaCompletionProposal(org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal)

Aggregations

JavaModelException (org.eclipse.jdt.core.JavaModelException)1 SourceField (org.eclipse.jdt.internal.core.SourceField)1 JavaCompletionProcessor (org.eclipse.jdt.internal.ui.text.java.JavaCompletionProcessor)1 JavaCompletionProposal (org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 Document (org.eclipse.jface.text.Document)1 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)1 IDocument (org.eclipse.jface.text.IDocument)1 Region (org.eclipse.jface.text.Region)1 ContentAssistant (org.eclipse.jface.text.contentassist.ContentAssistant)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1