--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2000, 2011 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.jdt.internal.ui.text.java;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.swt.graphics.Image;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.contentassist.ICompletionProposal;
+import org.eclipse.jface.text.contentassist.IContextInformation;
+import org.eclipse.jface.text.contentassist.IContextInformationExtension;
+
+import org.eclipse.jdt.core.CompletionRequestor;
+import org.eclipse.jdt.core.ICompilationUnit;
+
+import org.eclipse.jdt.ui.PreferenceConstants;
+import org.eclipse.jdt.ui.text.java.CompletionProposalCollector;
+import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext;
+import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer;
+import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
+
+/**
+ * Computes Java completion proposals and context infos.
+ *
+ * @since 3.2
+ */
+public class JavaCompletionProposalComputer implements IJavaCompletionProposalComputer {
+
+ static final class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
+
+ private final IContextInformation fContextInformation;
+ private int fPosition;
+
+ public ContextInformationWrapper(IContextInformation contextInformation) {
+ fContextInformation= contextInformation;
+ }
+
+ /*
+ * @see IContextInformation#getContextDisplayString()
+ */
+ public String getContextDisplayString() {
+ return fContextInformation.getContextDisplayString();
+ }
+
+ /*
+ * @see IContextInformation#getImage()
+ */
+ public Image getImage() {
+ return fContextInformation.getImage();
+ }
+
+ /*
+ * @see IContextInformation#getInformationDisplayString()
+ */
+ public String getInformationDisplayString() {
+ return fContextInformation.getInformationDisplayString();
+ }
+
+ /*
+ * @see IContextInformationExtension#getContextInformationPosition()
+ */
+ public int getContextInformationPosition() {
+ return fPosition;
+ }
+
+ public void setContextInformationPosition(int position) {
+ fPosition= position;
+ }
+
+ /*
+ * @see org.eclipse.jface.text.contentassist.IContextInformation#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object object) {
+ if (object instanceof ContextInformationWrapper)
+ return fContextInformation.equals(((ContextInformationWrapper) object).fContextInformation);
+ else
+ return fContextInformation.equals(object);
+ }
+
+ /*
+ * @see java.lang.Object#hashCode()
+ * @since 3.5
+ */
+ @Override
+ public int hashCode() {
+ return fContextInformation.hashCode();
+ }
+
+ public void generated_6783350971975576646(int offset, List<IContextInformation> result, List<IContextInformation> anonymousResult, ICompletionProposal proposal) {
+ setContextInformationPosition(offset);
+ if (proposal instanceof AnonymousTypeCompletionProposal)
+ anonymousResult.add(this);
+ else
+ result.add(this);
+ }
+ }
+
+ private static final long JAVA_CODE_ASSIST_TIMEOUT= Long.getLong("org.eclipse.jdt.ui.codeAssistTimeout", 5000).longValue(); // ms //$NON-NLS-1$
+
+ public String fErrorMessage;
+
+ public final IProgressMonitor fTimeoutProgressMonitor;
+
+ public JavaCompletionProposalComputer() {
+ fTimeoutProgressMonitor= createTimeoutProgressMonitor(JAVA_CODE_ASSIST_TIMEOUT);
+ }
+
+ protected int guessContextInformationPosition(ContentAssistInvocationContext context) {
+ return context.getInvocationOffset();
+ }
+
+ public final int guessMethodContextInformationPosition(ContentAssistInvocationContext context) {
+ return context.generated_2271872751418734958();
+ }
+
+ private List<IContextInformation> addContextInformations(JavaContentAssistInvocationContext context, int offset) {
+ List<ICompletionProposal> proposals= internalComputeCompletionProposals(offset, context);
+ List<IContextInformation> result= new ArrayList<IContextInformation>(proposals.size());
+ List<IContextInformation> anonymousResult= new ArrayList<IContextInformation>(proposals.size());
+
+ for (Iterator<ICompletionProposal> it= proposals.iterator(); it.hasNext();) {
+ ICompletionProposal proposal= it.next();
+ IContextInformation contextInformation= proposal.getContextInformation();
+ if (contextInformation != null) {
+ ContextInformationWrapper wrapper= new ContextInformationWrapper(contextInformation);
+ wrapper.generated_6783350971975576646(offset, result, anonymousResult, proposal);
+ }
+ }
+
+ if (result.size() == 0)
+ return anonymousResult;
+ return result;
+
+ }
+
+ /*
+ * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeContextInformation(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public List<IContextInformation> computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) {
+ if (context instanceof JavaContentAssistInvocationContext) {
+ JavaContentAssistInvocationContext javaContext= (JavaContentAssistInvocationContext) context;
+
+ int contextInformationPosition= guessContextInformationPosition(javaContext);
+ List<IContextInformation> result= addContextInformations(javaContext, contextInformationPosition);
+ return result;
+ }
+ return Collections.emptyList();
+ }
+
+ /*
+ * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
+ return context.generated_8495636787821851153(this);
+ }
+
+ public List<ICompletionProposal> internalComputeCompletionProposals(int offset, JavaContentAssistInvocationContext context) {
+ ICompilationUnit unit= context.getCompilationUnit();
+ if (unit == null)
+ return Collections.emptyList();
+
+ ITextViewer viewer= context.getViewer();
+
+ CompletionProposalCollector collector= createCollector(context);
+ collector.setInvocationContext(context);
+
+ // Allow completions for unresolved types - since 3.3
+ List<ICompletionProposal> proposals= collector.generated_1278462159997041060(offset, context, unit, viewer, this);
+ return proposals;
+ }
+
+ /**
+ * Returns a new progress monitor that get cancelled after the given timeout.
+ *
+ * @param timeout the timeout in ms
+ * @return the progress monitor
+ * @since 3.5
+ */
+ private IProgressMonitor createTimeoutProgressMonitor(final long timeout) {
+ return new IProgressMonitor() {
+
+ private long fEndTime;
+
+ public void beginTask(String name, int totalWork) {
+ fEndTime= System.currentTimeMillis() + timeout;
+ }
+ public boolean isCanceled() {
+ return fEndTime <= System.currentTimeMillis();
+ }
+ public void done() {
+ }
+ public void internalWorked(double work) {
+ }
+ public void setCanceled(boolean value) {
+ }
+ public void setTaskName(String name) {
+ }
+ public void subTask(String name) {
+ }
+ public void worked(int work) {
+ }
+ };
+ }
+
+ /**
+ * Returns the array with favorite static members.
+ *
+ * @return the <code>String</code> array with with favorite static members
+ * @see CompletionRequestor#setFavoriteReferences(String[])
+ * @since 3.3
+ */
+ public String[] getFavoriteStaticMembers() {
+ String serializedFavorites= PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS);
+ if (serializedFavorites != null && serializedFavorites.length() > 0)
+ return serializedFavorites.split(";"); //$NON-NLS-1$
+ return new String[0];
+ }
+
+ /**
+ * Creates the collector used to get proposals from core.
+ *
+ * @param context the context
+ * @return the collector
+ */
+ protected CompletionProposalCollector createCollector(JavaContentAssistInvocationContext context) {
+ return context.generated_71050629805439364();
+ }
+
+ /*
+ * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#getErrorMessage()
+ */
+ public String getErrorMessage() {
+ return fErrorMessage;
+ }
+
+ /*
+ * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionStarted()
+ */
+ public void sessionStarted() {
+ }
+
+ /*
+ * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionEnded()
+ */
+ public void sessionEnded() {
+ fErrorMessage= null;
+ }
+}