Search in sources :

Example 6 with JavaCompletionProposal

use of org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal in project che by eclipse.

the class CompletionProposalCollector method createFieldWithCastedReceiverProposal.

/**
	 * Creates the Java completion proposal for the JDT Core
	 * {@link org.eclipse.jdt.core.CompletionProposal#FIELD_REF_WITH_CASTED_RECEIVER} proposal.
	 *
	 * @param proposal the JDT Core proposal
	 * @return the Java completion proposal
	 * @since 3.4
	 */
private IJavaCompletionProposal createFieldWithCastedReceiverProposal(CompletionProposal proposal) {
    String completion = String.valueOf(proposal.getCompletion());
    //$NON-NLS-1$
    completion = CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, completion, 0, "\n", fJavaProject);
    int start = proposal.getReplaceStart();
    int length = getLength(proposal);
    StyledString label = fLabelProvider.createStyledLabel(proposal);
    Image image = getImage(fLabelProvider.createFieldImageDescriptor(proposal));
    int relevance = computeRelevance(proposal);
    JavaCompletionProposal javaProposal = new JavaFieldWithCastedReceiverCompletionProposal(completion, start, length, image, label, relevance, getContext().isInJavadoc(), getInvocationContext(), proposal);
    if (fJavaProject != null)
        javaProposal.setProposalInfo(new FieldProposalInfo(fJavaProject, proposal));
    javaProposal.setTriggerCharacters(VAR_TRIGGER);
    return javaProposal;
}
Also used : JavaFieldWithCastedReceiverCompletionProposal(org.eclipse.jdt.internal.ui.text.java.JavaFieldWithCastedReceiverCompletionProposal) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) Image(org.eclipse.swt.graphics.Image) JavaCompletionProposal(org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal) LazyJavaCompletionProposal(org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal) FieldProposalInfo(org.eclipse.jdt.internal.ui.text.java.FieldProposalInfo)

Example 7 with JavaCompletionProposal

use of org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal in project che by eclipse.

the class CompletionProposalCollector method createLocalVariableProposal.

private IJavaCompletionProposal createLocalVariableProposal(CompletionProposal proposal) {
    String completion = String.valueOf(proposal.getCompletion());
    int start = proposal.getReplaceStart();
    int length = getLength(proposal);
    Image image = getImage(fLabelProvider.createLocalImageDescriptor(proposal));
    StyledString label = fLabelProvider.createSimpleLabelWithType(proposal);
    int relevance = computeRelevance(proposal);
    final JavaCompletionProposal javaProposal = new JavaCompletionProposal(completion, start, length, image, label, relevance);
    javaProposal.setTriggerCharacters(VAR_TRIGGER);
    return javaProposal;
}
Also used : StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) Image(org.eclipse.swt.graphics.Image) JavaCompletionProposal(org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal) LazyJavaCompletionProposal(org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal)

Example 8 with JavaCompletionProposal

use of org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal in project che by eclipse.

the class CompletionProposalCollector method createAnonymousTypeProposal.

private IJavaCompletionProposal createAnonymousTypeProposal(CompletionProposal proposal, JavaContentAssistInvocationContext invocationContext) {
    if (fCompilationUnit == null || fJavaProject == null)
        return null;
    char[] declarationKey = proposal.getDeclarationKey();
    if (declarationKey == null)
        return null;
    try {
        IJavaElement element = fJavaProject.findElement(new String(declarationKey), null);
        if (!(element instanceof IType))
            return null;
        IType type = (IType) element;
        String completion = String.valueOf(proposal.getCompletion());
        int start = proposal.getReplaceStart();
        int length = getLength(proposal);
        int relevance = computeRelevance(proposal);
        StyledString label = fLabelProvider.createAnonymousTypeLabel(proposal);
        JavaCompletionProposal javaProposal = new AnonymousTypeCompletionProposal(fJavaProject, fCompilationUnit, invocationContext, start, length, completion, label, String.valueOf(proposal.getDeclarationSignature()), type, relevance);
        javaProposal.setProposalInfo(new AnonymousTypeProposalInfo(fJavaProject, proposal));
        return javaProposal;
    } catch (JavaModelException e) {
        return null;
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) AnonymousTypeProposalInfo(org.eclipse.jdt.internal.ui.text.java.AnonymousTypeProposalInfo) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) AnonymousTypeCompletionProposal(org.eclipse.jdt.internal.ui.text.java.AnonymousTypeCompletionProposal) IType(org.eclipse.jdt.core.IType) JavaCompletionProposal(org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal) LazyJavaCompletionProposal(org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal)

Example 9 with JavaCompletionProposal

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

the class TalendJavaCompletionProcessor method sortProposals.

/*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jdt.internal.ui.text.java.JavaCompletionProcessor#filterAndSortProposals(java.util.List,
     * org.eclipse.core.runtime.IProgressMonitor, org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext)
     */
@Override
protected List<ICompletionProposal> sortProposals(List<ICompletionProposal> proposals, IProgressMonitor monitor, ContentAssistInvocationContext context) {
    proposals = super.sortProposals(proposals, monitor, context);
    Map<String, ICompletionProposal> completionProposalMap = new HashMap<String, ICompletionProposal>();
    boolean globalFieldsDone = false;
    for (Object o : proposals) {
        ICompletionProposal proposal = (ICompletionProposal) o;
        String longna = proposal.getDisplayString();
        //$NON-NLS-1$
        int indexna = longna.indexOf("-");
        boolean shouldRemove = false;
        if (indexna > 0) {
            if (longna.substring(indexna + 2, longna.length()).equals(TalendJavaSourceViewer.getClassName())) {
                shouldRemove = true;
            }
        }
        if (proposal instanceof JavaCompletionProposal) {
            JavaCompletionProposal javaProposal = ((JavaCompletionProposal) proposal);
            if (javaProposal.getJavaElement() instanceof SourceField) {
                globalFieldsDone = true;
            }
            if (javaProposal.getJavaElement() == null && globalFieldsDone) {
                shouldRemove = true;
            }
        }
        if (proposal.getDisplayString().startsWith(TalendJavaSourceViewer.VIEWER_CLASS_NAME)) {
            shouldRemove = true;
        }
        if (!shouldRemove) {
            completionProposalMap.put(longna, proposal);
        }
    }
    List<ICompletionProposal> newProposals = new ArrayList<ICompletionProposal>(completionProposalMap.values());
    Collections.sort(newProposals, new Comparator<ICompletionProposal>() {

        @Override
        public int compare(ICompletionProposal arg0, ICompletionProposal arg1) {
            if (arg1 instanceof TalendCompletionProposal && (!(arg0 instanceof TalendCompletionProposal))) {
                return 1;
            } else if (arg0 instanceof TalendCompletionProposal && (!(arg1 instanceof TalendCompletionProposal))) {
                return -1;
            } else if (arg1 instanceof TalendCompletionProposal && (arg0 instanceof TalendCompletionProposal)) {
                TalendCompletionProposal a = (TalendCompletionProposal) arg0;
                TalendCompletionProposal b = (TalendCompletionProposal) arg1;
                return a.getType().compareTo(b.getType());
            }
            return 0;
        }
    });
    return newProposals;
}
Also used : HashMap(java.util.HashMap) SourceField(org.eclipse.jdt.internal.core.SourceField) ArrayList(java.util.ArrayList) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) TalendCompletionProposal(org.talend.designer.core.ui.viewer.proposal.TalendCompletionProposal) JavaCompletionProposal(org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal)

Example 10 with JavaCompletionProposal

use of org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal 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

JavaCompletionProposal (org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal)10 LazyJavaCompletionProposal (org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal)8 StyledString (org.eclipse.jface.viewers.StyledString)8 Image (org.eclipse.swt.graphics.Image)3 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 SourceField (org.eclipse.jdt.internal.core.SourceField)2 FieldProposalInfo (org.eclipse.jdt.internal.ui.text.java.FieldProposalInfo)2 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IType (org.eclipse.jdt.core.IType)1 AnnotationAtttributeProposalInfo (org.eclipse.jdt.internal.ui.text.java.AnnotationAtttributeProposalInfo)1 AnonymousTypeCompletionProposal (org.eclipse.jdt.internal.ui.text.java.AnonymousTypeCompletionProposal)1 AnonymousTypeProposalInfo (org.eclipse.jdt.internal.ui.text.java.AnonymousTypeProposalInfo)1 JavaCompletionProcessor (org.eclipse.jdt.internal.ui.text.java.JavaCompletionProcessor)1 JavaFieldWithCastedReceiverCompletionProposal (org.eclipse.jdt.internal.ui.text.java.JavaFieldWithCastedReceiverCompletionProposal)1 MethodProposalInfo (org.eclipse.jdt.internal.ui.text.java.MethodProposalInfo)1 OverrideCompletionProposal (org.eclipse.jdt.internal.ui.text.java.OverrideCompletionProposal)1 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)1