1 11 package org.eclipse.jdt.internal.ui.text.java; 12 13 import java.net.URL ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.FileLocator; 19 import org.eclipse.core.runtime.IConfigurationElement; 20 import org.eclipse.core.runtime.IExtension; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.InvalidRegistryObjectException; 23 import org.eclipse.core.runtime.Path; 24 import org.eclipse.core.runtime.Platform; 25 import org.eclipse.core.runtime.Status; 26 import org.eclipse.core.runtime.SubProgressMonitor; 27 28 import org.eclipse.jface.action.LegacyActionTools; 29 import org.eclipse.jface.resource.ImageDescriptor; 30 31 import org.eclipse.jdt.internal.corext.util.Messages; 32 33 import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext; 34 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer; 35 36 import org.eclipse.jdt.internal.ui.JavaPlugin; 37 38 import org.osgi.framework.Bundle; 39 40 45 public final class CompletionProposalCategory { 46 47 private static final String ICON= "icon"; 49 private final String fId; 50 private final String fName; 51 private final IConfigurationElement fElement; 52 53 private final ImageDescriptor fImage; 54 55 private boolean fIsSeparateCommand= true; 56 private boolean fIsEnabled= true; 57 private boolean fIsIncluded= true; 58 private final CompletionProposalComputerRegistry fRegistry; 59 60 private int fSortOrder= 0x10000; 61 private String fLastError= null; 62 63 CompletionProposalCategory(IConfigurationElement element, CompletionProposalComputerRegistry registry) { 64 fElement= element; 65 fRegistry= registry; 66 IExtension parent= (IExtension) element.getParent(); 67 fId= parent.getUniqueIdentifier(); 68 checkNotNull(fId, "id"); String name= parent.getLabel(); 70 if (name == null) 71 fName= fId; 72 else 73 fName= name; 74 75 String icon= element.getAttribute(ICON); 76 ImageDescriptor img= null; 77 if (icon != null) { 78 Bundle bundle= getBundle(); 79 if (bundle != null) { 80 Path path= new Path(icon); 81 URL url= FileLocator.find(bundle, path, null); 82 img= ImageDescriptor.createFromURL(url); 83 } 84 } 85 fImage= img; 86 87 } 88 89 CompletionProposalCategory(String id, String name, CompletionProposalComputerRegistry registry) { 90 fRegistry= registry; 91 fId= id; 92 fName= name; 93 fElement= null; 94 fImage= null; 95 } 96 97 private Bundle getBundle() { 98 String namespace= fElement.getDeclaringExtension().getContributor().getName(); 99 Bundle bundle= Platform.getBundle(namespace); 100 return bundle; 101 } 102 103 109 private void checkNotNull(Object obj, String attribute) throws InvalidRegistryObjectException { 110 if (obj == null) { 111 Object [] args= { getId(), fElement.getContributor().getName(), attribute }; 112 String message= Messages.format(JavaTextMessages.CompletionProposalComputerDescriptor_illegal_attribute_message, args); 113 IStatus status= new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, message, null); 114 JavaPlugin.log(status); 115 throw new InvalidRegistryObjectException(); 116 } 117 } 118 119 124 public String getId() { 125 return fId; 126 } 127 128 133 public String getName() { 134 return fName; 135 } 136 137 144 public String getDisplayName() { 145 return LegacyActionTools.removeMnemonics(fName); 146 } 147 148 153 public ImageDescriptor getImageDescriptor() { 154 return fImage; 155 } 156 157 162 public void setSeparateCommand(boolean enabled) { 163 fIsSeparateCommand= enabled; 164 } 165 166 171 public boolean isSeparateCommand() { 172 return fIsSeparateCommand; 173 } 174 175 178 public void setIncluded(boolean included) { 179 fIsIncluded= included; 180 } 181 182 185 public boolean isIncluded() { 186 return fIsIncluded; 187 } 188 189 public boolean isEnabled() { 190 return fIsEnabled; 191 } 192 193 public void setEnabled(boolean isEnabled) { 194 fIsEnabled= isEnabled; 195 } 196 197 204 public boolean hasComputers() { 205 List descriptors= fRegistry.getProposalComputerDescriptors(); 206 for (Iterator it= descriptors.iterator(); it.hasNext();) { 207 CompletionProposalComputerDescriptor desc= (CompletionProposalComputerDescriptor) it.next(); 208 if (desc.getCategory() == this) 209 return true; 210 } 211 return false; 212 } 213 214 222 public boolean hasComputers(String partition) { 223 List descriptors= fRegistry.getProposalComputerDescriptors(partition); 224 for (Iterator it= descriptors.iterator(); it.hasNext();) { 225 CompletionProposalComputerDescriptor desc= (CompletionProposalComputerDescriptor) it.next(); 226 if (desc.getCategory() == this) 227 return true; 228 } 229 return false; 230 } 231 232 235 public int getSortOrder() { 236 return fSortOrder; 237 } 238 239 242 public void setSortOrder(int sortOrder) { 243 fSortOrder= sortOrder; 244 } 245 246 257 public List computeCompletionProposals(ContentAssistInvocationContext context, String partition, SubProgressMonitor monitor) { 258 fLastError= null; 259 List result= new ArrayList (); 260 List descriptors= new ArrayList (fRegistry.getProposalComputerDescriptors(partition)); 261 for (Iterator it= descriptors.iterator(); it.hasNext();) { 262 CompletionProposalComputerDescriptor desc= (CompletionProposalComputerDescriptor) it.next(); 263 if (desc.getCategory() == this) 264 result.addAll(desc.computeCompletionProposals(context, monitor)); 265 if (fLastError == null) 266 fLastError= desc.getErrorMessage(); 267 } 268 return result; 269 } 270 271 282 public List computeContextInformation(ContentAssistInvocationContext context, String partition, SubProgressMonitor monitor) { 283 fLastError= null; 284 List result= new ArrayList (); 285 List descriptors= new ArrayList (fRegistry.getProposalComputerDescriptors(partition)); 286 for (Iterator it= descriptors.iterator(); it.hasNext();) { 287 CompletionProposalComputerDescriptor desc= (CompletionProposalComputerDescriptor) it.next(); 288 if (desc.getCategory() == this) 289 result.addAll(desc.computeContextInformation(context, monitor)); 290 if (fLastError == null) 291 fLastError= desc.getErrorMessage(); 292 } 293 return result; 294 } 295 296 301 public String getErrorMessage() { 302 return fLastError; 303 } 304 305 308 public void sessionStarted() { 309 List descriptors= new ArrayList (fRegistry.getProposalComputerDescriptors()); 310 for (Iterator it= descriptors.iterator(); it.hasNext();) { 311 CompletionProposalComputerDescriptor desc= (CompletionProposalComputerDescriptor) it.next(); 312 if (desc.getCategory() == this) 313 desc.sessionStarted(); 314 if (fLastError == null) 315 fLastError= desc.getErrorMessage(); 316 } 317 } 318 319 322 public void sessionEnded() { 323 List descriptors= new ArrayList (fRegistry.getProposalComputerDescriptors()); 324 for (Iterator it= descriptors.iterator(); it.hasNext();) { 325 CompletionProposalComputerDescriptor desc= (CompletionProposalComputerDescriptor) it.next(); 326 if (desc.getCategory() == this) 327 desc.sessionEnded(); 328 if (fLastError == null) 329 fLastError= desc.getErrorMessage(); 330 } 331 } 332 333 } 334 | Popular Tags |