|
Lines 1-5
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2007 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2010 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
|
Lines 25-38
Link Here
|
| 25 |
*/ |
25 |
*/ |
| 26 |
public class NatureManager implements ILifecycleListener, IManager { |
26 |
public class NatureManager implements ILifecycleListener, IManager { |
| 27 |
//maps String (nature ID) -> descriptor objects |
27 |
//maps String (nature ID) -> descriptor objects |
| 28 |
protected Map descriptors; |
28 |
private Map descriptors; |
| 29 |
|
29 |
|
| 30 |
//maps IProject -> String[] of enabled natures for that project |
30 |
//maps IProject -> String[] of enabled natures for that project |
| 31 |
protected Map natureEnablements; |
31 |
private Map natureEnablements; |
| 32 |
|
32 |
|
| 33 |
//maps String (builder ID) -> String (nature ID) |
33 |
//maps String (builder ID) -> String (nature ID) |
| 34 |
protected Map buildersToNatures = null; |
34 |
private Map buildersToNatures = null; |
| 35 |
//colour constants used in cycle detection algorithm |
35 |
//color constants used in cycle detection algorithm |
| 36 |
private static final byte WHITE = 0; |
36 |
private static final byte WHITE = 0; |
| 37 |
private static final byte GREY = 1; |
37 |
private static final byte GREY = 1; |
| 38 |
private static final byte BLACK = 2; |
38 |
private static final byte BLACK = 2; |
|
Lines 43-52
Link Here
|
| 43 |
|
43 |
|
| 44 |
/** |
44 |
/** |
| 45 |
* Computes the list of natures that are enabled for the given project. |
45 |
* Computes the list of natures that are enabled for the given project. |
| 46 |
* Enablement computation is subtlely different from nature set |
46 |
* Enablement computation is subtlety different from nature set |
| 47 |
* validation, because it must find and remove all inconsistencies. |
47 |
* validation, because it must find and remove all inconsistencies. |
| 48 |
*/ |
48 |
*/ |
| 49 |
protected String[] computeNatureEnablements(Project project) { |
49 |
private String[] computeNatureEnablements(Project project) { |
| 50 |
final ProjectDescription description = project.internalGetDescription(); |
50 |
final ProjectDescription description = project.internalGetDescription(); |
| 51 |
if (description == null) |
51 |
if (description == null) |
| 52 |
return new String[0];//project deleted concurrently |
52 |
return new String[0];//project deleted concurrently |
|
Lines 136-142
Link Here
|
| 136 |
/** |
136 |
/** |
| 137 |
* Configures the nature with the given ID for the given project. |
137 |
* Configures the nature with the given ID for the given project. |
| 138 |
*/ |
138 |
*/ |
| 139 |
protected void configureNature(final Project project, final String natureID, final MultiStatus errors) { |
139 |
private void configureNature(final Project project, final String natureID, final MultiStatus errors) { |
| 140 |
ISafeRunnable code = new ISafeRunnable() { |
140 |
ISafeRunnable code = new ISafeRunnable() { |
| 141 |
public void run() throws Exception { |
141 |
public void run() throws Exception { |
| 142 |
IProjectNature nature = createNature(project, natureID); |
142 |
IProjectNature nature = createNature(project, natureID); |
|
Lines 164-170
Link Here
|
| 164 |
* new description are removed. Updates the old description so that it reflects |
164 |
* new description are removed. Updates the old description so that it reflects |
| 165 |
* the new set of the natures. Errors are added to the given multi-status. |
165 |
* the new set of the natures. Errors are added to the given multi-status. |
| 166 |
*/ |
166 |
*/ |
| 167 |
public void configureNatures(Project project, ProjectDescription oldDescription, ProjectDescription newDescription, MultiStatus status) { |
167 |
public synchronized void configureNatures(Project project, ProjectDescription oldDescription, ProjectDescription newDescription, MultiStatus status) { |
| 168 |
// Be careful not to rely on much state because (de)configuring a nature |
168 |
// Be careful not to rely on much state because (de)configuring a nature |
| 169 |
// may well result in recursive calls to this method. |
169 |
// may well result in recursive calls to this method. |
| 170 |
HashSet oldNatures = new HashSet(Arrays.asList(oldDescription.getNatureIds(false))); |
170 |
HashSet oldNatures = new HashSet(Arrays.asList(oldDescription.getNatureIds(false))); |
|
Lines 240-246
Link Here
|
| 240 |
/** |
240 |
/** |
| 241 |
* Deconfigures the nature with the given ID for the given project. |
241 |
* Deconfigures the nature with the given ID for the given project. |
| 242 |
*/ |
242 |
*/ |
| 243 |
protected void deconfigureNature(final Project project, final String natureID, final MultiStatus status) { |
243 |
private void deconfigureNature(final Project project, final String natureID, final MultiStatus status) { |
| 244 |
final ProjectInfo info = (ProjectInfo) project.getResourceInfo(false, true); |
244 |
final ProjectInfo info = (ProjectInfo) project.getResourceInfo(false, true); |
| 245 |
IProjectNature existingNature = info.getNature(natureID); |
245 |
IProjectNature existingNature = info.getNature(natureID); |
| 246 |
if (existingNature == null) { |
246 |
if (existingNature == null) { |
|
Lines 275-281
Link Here
|
| 275 |
/** |
275 |
/** |
| 276 |
* Marks all nature descriptors that are involved in cycles |
276 |
* Marks all nature descriptors that are involved in cycles |
| 277 |
*/ |
277 |
*/ |
| 278 |
protected void detectCycles() { |
278 |
private void detectCycles() { |
| 279 |
Collection values = descriptors.values(); |
279 |
Collection values = descriptors.values(); |
| 280 |
ProjectNatureDescriptor[] natures = (ProjectNatureDescriptor[]) values.toArray(new ProjectNatureDescriptor[values.size()]); |
280 |
ProjectNatureDescriptor[] natures = (ProjectNatureDescriptor[]) values.toArray(new ProjectNatureDescriptor[values.size()]); |
| 281 |
for (int i = 0; i < natures.length; i++) |
281 |
for (int i = 0; i < natures.length; i++) |
|
Lines 286-292
Link Here
|
| 286 |
/** |
286 |
/** |
| 287 |
* Returns a status indicating failure to configure natures. |
287 |
* Returns a status indicating failure to configure natures. |
| 288 |
*/ |
288 |
*/ |
| 289 |
protected IStatus failure(String reason) { |
289 |
private IStatus failure(String reason) { |
| 290 |
return new ResourceStatus(IResourceStatus.INVALID_NATURE_SET, reason); |
290 |
return new ResourceStatus(IResourceStatus.INVALID_NATURE_SET, reason); |
| 291 |
} |
291 |
} |
| 292 |
|
292 |
|
|
Lines 294-300
Link Here
|
| 294 |
* Returns the ID of the project nature that claims ownership of the |
294 |
* Returns the ID of the project nature that claims ownership of the |
| 295 |
* builder with the given ID. Returns null if no nature owns that builder. |
295 |
* builder with the given ID. Returns null if no nature owns that builder. |
| 296 |
*/ |
296 |
*/ |
| 297 |
public String findNatureForBuilder(String builderID) { |
297 |
public synchronized String findNatureForBuilder(String builderID) { |
| 298 |
if (buildersToNatures == null) { |
298 |
if (buildersToNatures == null) { |
| 299 |
buildersToNatures = new HashMap(10); |
299 |
buildersToNatures = new HashMap(10); |
| 300 |
IProjectNatureDescriptor[] descs = getNatureDescriptors(); |
300 |
IProjectNatureDescriptor[] descs = getNatureDescriptors(); |
|
Lines 310-316
Link Here
|
| 310 |
return (String) buildersToNatures.get(builderID); |
310 |
return (String) buildersToNatures.get(builderID); |
| 311 |
} |
311 |
} |
| 312 |
|
312 |
|
| 313 |
protected void flushEnablements(IProject project) { |
313 |
private synchronized void flushEnablements(IProject project) { |
| 314 |
if (natureEnablements != null) { |
314 |
if (natureEnablements != null) { |
| 315 |
natureEnablements.remove(project); |
315 |
natureEnablements.remove(project); |
| 316 |
if (natureEnablements.size() == 0) { |
316 |
if (natureEnablements.size() == 0) { |
|
Lines 323-329
Link Here
|
| 323 |
* Returns the cached array of enabled natures for this project, |
323 |
* Returns the cached array of enabled natures for this project, |
| 324 |
* or null if there is nothing in the cache. |
324 |
* or null if there is nothing in the cache. |
| 325 |
*/ |
325 |
*/ |
| 326 |
protected String[] getEnabledNatures(Project project) { |
326 |
protected synchronized String[] getEnabledNatures(Project project) { |
| 327 |
String[] enabled; |
327 |
String[] enabled; |
| 328 |
if (natureEnablements != null) { |
328 |
if (natureEnablements != null) { |
| 329 |
enabled = (String[]) natureEnablements.get(project); |
329 |
enabled = (String[]) natureEnablements.get(project); |
|
Lines 340-346
Link Here
|
| 340 |
* dependencies starting at root i. Returns false otherwise. |
340 |
* dependencies starting at root i. Returns false otherwise. |
| 341 |
* Marks all descriptors that are involved in the cycle as invalid. |
341 |
* Marks all descriptors that are involved in the cycle as invalid. |
| 342 |
*/ |
342 |
*/ |
| 343 |
protected boolean hasCycles(ProjectNatureDescriptor desc) { |
343 |
private boolean hasCycles(ProjectNatureDescriptor desc) { |
| 344 |
if (desc.colour == BLACK) { |
344 |
if (desc.colour == BLACK) { |
| 345 |
//this subgraph has already been traversed, so we know the answer |
345 |
//this subgraph has already been traversed, so we know the answer |
| 346 |
return desc.hasCycle; |
346 |
return desc.hasCycle; |
|
Lines 373-379
Link Here
|
| 373 |
/** |
373 |
/** |
| 374 |
* Returns true if the given project has linked resources, and false otherwise. |
374 |
* Returns true if the given project has linked resources, and false otherwise. |
| 375 |
*/ |
375 |
*/ |
| 376 |
protected boolean hasLinks(IProject project) { |
376 |
private boolean hasLinks(IProject project) { |
| 377 |
try { |
377 |
try { |
| 378 |
IResource[] children = project.members(); |
378 |
IResource[] children = project.members(); |
| 379 |
for (int i = 0; i < children.length; i++) |
379 |
for (int i = 0; i < children.length; i++) |
|
Lines 391-397
Link Here
|
| 391 |
* memberships. Returns the name of one such overlap, or null if |
391 |
* memberships. Returns the name of one such overlap, or null if |
| 392 |
* there is no set overlap. |
392 |
* there is no set overlap. |
| 393 |
*/ |
393 |
*/ |
| 394 |
protected String hasSetOverlap(IProjectNatureDescriptor one, IProjectNatureDescriptor two) { |
394 |
private String hasSetOverlap(IProjectNatureDescriptor one, IProjectNatureDescriptor two) { |
| 395 |
if (one == null || two == null) { |
395 |
if (one == null || two == null) { |
| 396 |
return null; |
396 |
return null; |
| 397 |
} |
397 |
} |
|
Lines 411-417
Link Here
|
| 411 |
/** |
411 |
/** |
| 412 |
* Perform depth-first insertion of the given nature ID into the result list. |
412 |
* Perform depth-first insertion of the given nature ID into the result list. |
| 413 |
*/ |
413 |
*/ |
| 414 |
protected void insert(ArrayList list, Set seen, String id) { |
414 |
private void insert(ArrayList list, Set seen, String id) { |
| 415 |
if (seen.contains(id)) |
415 |
if (seen.contains(id)) |
| 416 |
return; |
416 |
return; |
| 417 |
seen.add(id); |
417 |
seen.add(id); |
|
Lines 443-449
Link Here
|
| 443 |
* Only initialize the descriptor cache when we know it is actually needed. |
443 |
* Only initialize the descriptor cache when we know it is actually needed. |
| 444 |
* Running programs may never need to refer to this cache. |
444 |
* Running programs may never need to refer to this cache. |
| 445 |
*/ |
445 |
*/ |
| 446 |
protected void lazyInitialize() { |
446 |
private synchronized void lazyInitialize() { |
| 447 |
if (descriptors != null) |
447 |
if (descriptors != null) |
| 448 |
return; |
448 |
return; |
| 449 |
IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(ResourcesPlugin.PI_RESOURCES, ResourcesPlugin.PT_NATURES); |
449 |
IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(ResourcesPlugin.PI_RESOURCES, ResourcesPlugin.PT_NATURES); |
|
Lines 467-473
Link Here
|
| 467 |
/** |
467 |
/** |
| 468 |
* Sets the cached array of enabled natures for this project. |
468 |
* Sets the cached array of enabled natures for this project. |
| 469 |
*/ |
469 |
*/ |
| 470 |
protected void setEnabledNatures(IProject project, String[] enablements) { |
470 |
private void setEnabledNatures(IProject project, String[] enablements) { |
| 471 |
if (natureEnablements == null) |
471 |
if (natureEnablements == null) |
| 472 |
natureEnablements = Collections.synchronizedMap(new HashMap(20)); |
472 |
natureEnablements = Collections.synchronizedMap(new HashMap(20)); |
| 473 |
natureEnablements.put(project, enablements); |
473 |
natureEnablements.put(project, enablements); |
|
Lines 513-519
Link Here
|
| 513 |
* @return An OK status if all additions are valid, and an error status |
513 |
* @return An OK status if all additions are valid, and an error status |
| 514 |
* if any of the additions introduce new inconsistencies. |
514 |
* if any of the additions introduce new inconsistencies. |
| 515 |
*/ |
515 |
*/ |
| 516 |
protected IStatus validateAdditions(HashSet newNatures, HashSet additions, IProject project) { |
516 |
private IStatus validateAdditions(HashSet newNatures, HashSet additions, IProject project) { |
| 517 |
Boolean hasLinks = null;//three states: true, false, null (not yet computed) |
517 |
Boolean hasLinks = null;//three states: true, false, null (not yet computed) |
| 518 |
//perform checks in order from least expensive to most expensive |
518 |
//perform checks in order from least expensive to most expensive |
| 519 |
for (Iterator added = additions.iterator(); added.hasNext();) { |
519 |
for (Iterator added = additions.iterator(); added.hasNext();) { |
|
Lines 585-591
Link Here
|
| 585 |
* @return An OK status if all removals are valid, and a not OK status |
585 |
* @return An OK status if all removals are valid, and a not OK status |
| 586 |
* if any of the deletions introduce new inconsistencies. |
586 |
* if any of the deletions introduce new inconsistencies. |
| 587 |
*/ |
587 |
*/ |
| 588 |
protected IStatus validateRemovals(HashSet newNatures, HashSet deletions) { |
588 |
private IStatus validateRemovals(HashSet newNatures, HashSet deletions) { |
| 589 |
//iterate over new nature set, and ensure that none of their prerequisites are being deleted |
589 |
//iterate over new nature set, and ensure that none of their prerequisites are being deleted |
| 590 |
for (Iterator it = newNatures.iterator(); it.hasNext();) { |
590 |
for (Iterator it = newNatures.iterator(); it.hasNext();) { |
| 591 |
String currentID = (String) it.next(); |
591 |
String currentID = (String) it.next(); |