1 11 package org.eclipse.jdt.internal.ui.text.java; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Collections ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.IProgressMonitor; 20 21 import org.eclipse.swt.graphics.Image; 22 import org.eclipse.swt.graphics.Point; 23 import org.eclipse.swt.widgets.Shell; 24 25 import org.eclipse.jface.dialogs.ErrorDialog; 26 import org.eclipse.jface.dialogs.MessageDialog; 27 28 import org.eclipse.jface.text.ITextViewer; 29 import org.eclipse.jface.text.contentassist.ICompletionProposal; 30 import org.eclipse.jface.text.contentassist.IContextInformation; 31 import org.eclipse.jface.text.contentassist.IContextInformationExtension; 32 33 import org.eclipse.jdt.core.CompletionProposal; 34 import org.eclipse.jdt.core.CompletionRequestor; 35 import org.eclipse.jdt.core.ICompilationUnit; 36 import org.eclipse.jdt.core.JavaModelException; 37 38 import org.eclipse.jdt.ui.PreferenceConstants; 39 import org.eclipse.jdt.ui.text.java.CompletionProposalCollector; 40 import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext; 41 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer; 42 import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext; 43 44 49 public class JavaCompletionProposalComputer implements IJavaCompletionProposalComputer { 50 51 private static final class ContextInformationWrapper implements IContextInformation, IContextInformationExtension { 52 53 private final IContextInformation fContextInformation; 54 private int fPosition; 55 56 public ContextInformationWrapper(IContextInformation contextInformation) { 57 fContextInformation= contextInformation; 58 } 59 60 63 public String getContextDisplayString() { 64 return fContextInformation.getContextDisplayString(); 65 } 66 67 70 public Image getImage() { 71 return fContextInformation.getImage(); 72 } 73 74 77 public String getInformationDisplayString() { 78 return fContextInformation.getInformationDisplayString(); 79 } 80 81 84 public int getContextInformationPosition() { 85 return fPosition; 86 } 87 88 public void setContextInformationPosition(int position) { 89 fPosition= position; 90 } 91 92 95 public boolean equals(Object object) { 96 if (object instanceof ContextInformationWrapper) 97 return fContextInformation.equals(((ContextInformationWrapper) object).fContextInformation); 98 else 99 return fContextInformation.equals(object); 100 } 101 } 102 103 private String fErrorMessage; 104 105 public JavaCompletionProposalComputer() { 106 } 107 108 protected int guessContextInformationPosition(ContentAssistInvocationContext context) { 109 return context.getInvocationOffset(); 110 } 111 112 private List addContextInformations(JavaContentAssistInvocationContext context, int offset, IProgressMonitor monitor) { 113 List proposals= internalComputeCompletionProposals(offset, context, monitor); 114 List result= new ArrayList (proposals.size()); 115 116 for (Iterator it= proposals.iterator(); it.hasNext();) { 117 ICompletionProposal proposal= (ICompletionProposal) it.next(); 118 IContextInformation contextInformation= proposal.getContextInformation(); 119 if (contextInformation != null) { 120 ContextInformationWrapper wrapper= new ContextInformationWrapper(contextInformation); 121 wrapper.setContextInformationPosition(offset); 122 result.add(wrapper); 123 } 124 } 125 return result; 126 } 127 128 131 public List computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) { 132 if (context instanceof JavaContentAssistInvocationContext) { 133 JavaContentAssistInvocationContext javaContext= (JavaContentAssistInvocationContext) context; 134 135 int contextInformationPosition= guessContextInformationPosition(javaContext); 136 List result= addContextInformations(javaContext, contextInformationPosition, monitor); 137 return result; 138 } 139 return Collections.EMPTY_LIST; 140 } 141 142 145 public List computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) { 146 if (context instanceof JavaContentAssistInvocationContext) { 147 JavaContentAssistInvocationContext javaContext= (JavaContentAssistInvocationContext) context; 148 return internalComputeCompletionProposals(context.getInvocationOffset(), javaContext, monitor); 149 } 150 return Collections.EMPTY_LIST; 151 } 152 153 private List internalComputeCompletionProposals(int offset, JavaContentAssistInvocationContext context, IProgressMonitor monitor) { 154 ICompilationUnit unit= context.getCompilationUnit(); 155 if (unit == null) 156 return Collections.EMPTY_LIST; 157 158 ITextViewer viewer= context.getViewer(); 159 160 CompletionProposalCollector collector= createCollector(context); 161 collector.setInvocationContext(context); 162 163 collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_REF, true); 165 collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_IMPORT, true); 166 collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true); 167 168 collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_REF, true); 169 collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_IMPORT, true); 170 collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true); 171 172 collector.setFavoriteReferences(getFavoriteStaticMembers()); 174 175 try { 176 Point selection= viewer.getSelectedRange(); 177 if (selection.y > 0) 178 collector.setReplacementLength(selection.y); 179 180 unit.codeComplete(offset, collector); 181 } catch (JavaModelException x) { 182 Shell shell= viewer.getTextWidget().getShell(); 183 if (x.isDoesNotExist() && !unit.getJavaProject().isOnClasspath(unit)) 184 MessageDialog.openInformation(shell, JavaTextMessages.CompletionProcessor_error_notOnBuildPath_title, JavaTextMessages.CompletionProcessor_error_notOnBuildPath_message); 185 else 186 ErrorDialog.openError(shell, JavaTextMessages.CompletionProcessor_error_accessing_title, JavaTextMessages.CompletionProcessor_error_accessing_message, x.getStatus()); 187 } 188 189 ICompletionProposal[] javaProposals= collector.getJavaCompletionProposals(); 190 int contextInformationOffset= guessContextInformationPosition(context); 191 if (contextInformationOffset != offset) { 192 for (int i= 0; i < javaProposals.length; i++) { 193 if (javaProposals[i] instanceof JavaMethodCompletionProposal) { 194 JavaMethodCompletionProposal jmcp= (JavaMethodCompletionProposal) javaProposals[i]; 195 jmcp.setContextInformationPosition(contextInformationOffset); 196 } 197 } 198 } 199 200 List proposals= new ArrayList (Arrays.asList(javaProposals)); 201 if (proposals.size() == 0) { 202 String error= collector.getErrorMessage(); 203 if (error.length() > 0) 204 fErrorMessage= error; 205 } 206 return proposals; 207 } 208 209 216 private String [] getFavoriteStaticMembers() { 217 String serializedFavorites= PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS); 218 if (serializedFavorites != null && serializedFavorites.length() > 0) 219 return serializedFavorites.split(";"); return new String [0]; 221 } 222 223 226 protected CompletionProposalCollector createCollector(JavaContentAssistInvocationContext context) { 227 if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES)) 228 return new FillArgumentNamesCompletionProposalCollector(context); 229 else 230 return new CompletionProposalCollector(context.getCompilationUnit()); 231 } 232 233 236 public String getErrorMessage() { 237 return fErrorMessage; 238 } 239 240 243 public void sessionStarted() { 244 } 245 246 249 public void sessionEnded() { 250 fErrorMessage= null; 251 } 252 } 253 | Popular Tags |