1 11 package org.eclipse.ui.internal.ide.registry; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.resources.IProjectNatureDescriptor; 19 import org.eclipse.core.resources.IWorkspace; 20 import org.eclipse.core.resources.ResourcesPlugin; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IAdaptable; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.Platform; 25 import org.eclipse.ui.internal.ide.Category; 26 import org.eclipse.ui.model.IWorkbenchAdapter; 27 import org.eclipse.ui.model.WorkbenchAdapter; 28 29 33 public class CapabilityRegistry extends WorkbenchAdapter implements IAdaptable { 34 private static final String [] EMPTY_ID_LIST = new String [0]; 35 36 private static final Capability[] EMPTY_CAP_LIST = new Capability[0]; 37 38 private HashMap natureToCapability; 39 40 private ArrayList capabilities; 41 42 private ArrayList categories; 43 44 private Category miscCategory; 45 46 49 public CapabilityRegistry() { 50 capabilities = new ArrayList (30); 51 categories = new ArrayList (15); 52 } 53 54 58 boolean addCapability(Capability capability) { 59 return capabilities.add(capability); 60 } 61 62 66 boolean addCategory(Category category) { 67 return categories.add(category); 68 } 69 70 74 public Capability findCapability(String id) { 75 Iterator itr = capabilities.iterator(); 76 while (itr.hasNext()) { 77 Capability cap = (Capability) itr.next(); 78 if (id.equals(cap.getId())) { 79 return cap; 80 } 81 } 82 return null; 83 } 84 85 89 public Category findCategory(String id) { 90 Iterator itr = categories.iterator(); 91 while (itr.hasNext()) { 92 Category cat = (Category) itr.next(); 93 if (id.equals(cat.getRootPath())) { 94 return cat; 95 } 96 } 97 return null; 98 } 99 100 105 public Capability[] findCapabilities(String [] ids) { 106 int count = capabilities.size(); 107 Capability[] results = new Capability[ids.length]; 108 109 for (int i = 0; i < ids.length; i++) { 110 String id = ids[i]; 111 for (int j = 0; j < count; j++) { 112 Capability cap = (Capability) capabilities.get(j); 113 if (cap.getId().equals(id)) { 114 results[i] = cap; 115 break; 116 } 117 } 118 } 119 120 return results; 121 } 122 123 130 public Category[] findCategories(String [] ids) { 131 int count = categories.size(); 132 Category[] results = new Category[ids.length]; 133 134 for (int i = 0; i < ids.length; i++) { 135 String id = ids[i]; 136 for (int j = 0; j < count; j++) { 137 Category cat = (Category) categories.get(j); 138 if (cat.getId().equals(id)) { 139 results[i] = cat; 140 break; 141 } 142 } 143 } 144 145 return results; 146 } 147 148 151 public Object getAdapter(Class adapter) { 152 if (adapter == IWorkbenchAdapter.class) 153 return this; 154 else 155 return null; 156 } 157 158 163 public ArrayList getUsedCategories() { 164 ArrayList results = new ArrayList (categories.size()); 165 Iterator itr = categories.iterator(); 166 while (itr.hasNext()) { 167 Category cat = (Category) itr.next(); 168 if (cat.hasElements()) 169 results.add(cat); 170 } 171 return results; 172 } 173 174 177 public Capability getCapabilityForNature(String natureId) { 178 return (Capability) natureToCapability.get(natureId); 179 } 180 181 184 public ArrayList getCapabilities() { 185 return capabilities; 186 } 187 188 191 public Object [] getChildren(Object o) { 192 return capabilities.toArray(); 193 } 194 195 199 public String [] getMembershipSetIds(Capability capability) { 200 IProjectNatureDescriptor desc = capability.getNatureDescriptor(); 201 if (desc == null) 202 return EMPTY_ID_LIST; 203 204 return desc.getNatureSetIds(); 205 } 206 207 211 public Category getMiscCategory() { 212 return miscCategory; 213 } 214 215 219 public String [] getPrerequisiteIds(Capability capability) { 220 IProjectNatureDescriptor desc = capability.getNatureDescriptor(); 221 if (desc == null) 222 return EMPTY_ID_LIST; 223 224 String [] natureIds = desc.getRequiredNatureIds(); 225 if (natureIds.length == 0) 226 return EMPTY_ID_LIST; 227 228 ArrayList results = new ArrayList (natureIds.length); 229 for (int i = 0; i < natureIds.length; i++) { 230 Capability cap = (Capability) natureToCapability.get(natureIds[i]); 231 if (cap != null) 232 results.add(cap.getId()); 233 } 234 235 if (results.size() == 0) { 236 return EMPTY_ID_LIST; 237 } else { 238 String [] ids = new String [results.size()]; 239 results.toArray(ids); 240 return ids; 241 } 242 } 243 244 247 public Capability[] getProjectCapabilities(IProject project) { 248 try { 249 String [] natureIds = project.getDescription().getNatureIds(); 250 ArrayList results = new ArrayList (natureIds.length); 251 for (int i = 0; i < natureIds.length; i++) { 252 Capability cap = (Capability) natureToCapability 253 .get(natureIds[i]); 254 if (cap == null) { 255 cap = new Capability(natureIds[i]); 256 mapCapability(cap); 257 } 258 results.add(cap); 259 } 260 261 if (results.size() == 0) { 262 return EMPTY_CAP_LIST; 263 } else { 264 Capability[] caps = new Capability[results.size()]; 265 results.toArray(caps); 266 return caps; 267 } 268 } catch (CoreException e) { 269 return EMPTY_CAP_LIST; 270 } 271 } 272 273 277 public Capability[] getProjectDisabledCapabilities(IProject project) { 278 try { 279 String [] natureIds = project.getDescription().getNatureIds(); 280 ArrayList results = new ArrayList (natureIds.length); 281 for (int i = 0; i < natureIds.length; i++) { 282 if (!project.isNatureEnabled(natureIds[i])) { 283 Capability cap = (Capability) natureToCapability 284 .get(natureIds[i]); 285 if (cap == null) { 286 cap = new Capability(natureIds[i]); 287 mapCapability(cap); 288 } 289 results.add(cap); 290 } 291 } 292 293 if (results.size() == 0) { 294 return EMPTY_CAP_LIST; 295 } else { 296 Capability[] caps = new Capability[results.size()]; 297 results.toArray(caps); 298 return caps; 299 } 300 } catch (CoreException e) { 301 return EMPTY_CAP_LIST; 302 } 303 } 304 305 308 public boolean hasCapabilities() { 309 return !capabilities.isEmpty(); 310 } 311 312 315 public boolean hasPrerequisites(Capability capability) { 316 return getPrerequisiteIds(capability).length > 0; 317 } 318 319 323 public void load() { 324 CapabilityRegistryReader reader = new CapabilityRegistryReader(); 325 reader.read(Platform.getExtensionRegistry(), this); 326 mapCapabilities(); 327 } 328 329 337 void mapCapabilities() { 338 natureToCapability = new HashMap (); 339 340 Iterator itr = capabilities.iterator(); 341 while (itr.hasNext()) 342 mapCapability((Capability) itr.next()); 343 } 344 345 private void mapCapability(Capability cap) { 346 if (!cap.isValid()) { 348 if (miscCategory == null) 349 miscCategory = new Category(); 350 miscCategory.addElement(cap); 351 } else { 352 Category cat = null; 353 String catPath = cap.getCategoryPath(); 354 if (catPath != null) 355 cat = findCategory(catPath); 356 if (cat != null) { 357 cat.addElement(cap); 358 } else { 359 if (miscCategory == null) 360 miscCategory = new Category(); 361 miscCategory.addElement(cap); 362 } 363 } 364 365 natureToCapability.put(cap.getNatureId(), cap); 367 } 368 369 377 public Capability[] pruneCapabilities(Capability[] capabilities) { 378 ArrayList ids = new ArrayList (capabilities.length); 379 for (int i = 0; i < capabilities.length; i++) 380 ids.add(capabilities[i].getId()); 381 382 for (int i = 0; i < capabilities.length; i++) { 383 ArrayList handleIds = capabilities[i].getHandleUIs(); 384 if (handleIds != null) 385 ids.removeAll(handleIds); 386 } 387 388 String [] results = new String [ids.size()]; 389 ids.toArray(results); 390 return findCapabilities(results); 391 } 392 393 404 public IStatus validateCapabilities(Capability[] capabilities) { 405 String natures[] = new String [capabilities.length]; 406 for (int i = 0; i < capabilities.length; i++) 407 natures[i] = capabilities[i].getNatureId(); 408 409 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 410 IStatus status = workspace.validateNatureSet(natures); 411 if (status.isOK()) { 412 natures = workspace.sortNatureSet(natures); 413 for (int i = 0; i < natures.length; i++) 414 capabilities[i] = (Capability) natureToCapability 415 .get(natures[i]); 416 } 417 418 return status; 419 } 420 } 421 | Popular Tags |