KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > ProjectNatureDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.resources;
12
13 import java.util.ArrayList JavaDoc;
14 import org.eclipse.core.internal.utils.Messages;
15 import org.eclipse.core.resources.IProjectNatureDescriptor;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.util.NLS;
19
20 /**
21  */

22 public class ProjectNatureDescriptor implements IProjectNatureDescriptor {
23     protected String JavaDoc id;
24     protected String JavaDoc label;
25     protected String JavaDoc[] requiredNatures;
26     protected String JavaDoc[] natureSets;
27     protected String JavaDoc[] builderIds;
28     protected String JavaDoc[] contentTypeIds;
29     protected boolean allowLinking = true;
30
31     //descriptors that are in a dependency cycle are never valid
32
protected boolean hasCycle = false;
33     //colours used by cycle detection algorithm
34
protected byte colour = 0;
35
36     /**
37      * Creates a new descriptor based on the given extension markup.
38      * @exception CoreException if the given nature extension is not correctly formed.
39      */

40     protected ProjectNatureDescriptor(IExtension natureExtension) throws CoreException {
41         readExtension(natureExtension);
42     }
43
44     protected void fail() throws CoreException {
45         fail(NLS.bind(Messages.natures_invalidDefinition, id));
46     }
47
48     protected void fail(String JavaDoc reason) throws CoreException {
49         throw new ResourceException(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, 1, reason, null));
50     }
51
52     /**
53      * Returns the IDs of the incremental builders that this nature claims to
54      * own. These builders do not necessarily exist in the registry.
55      */

56     public String JavaDoc[] getBuilderIds() {
57         return builderIds;
58     }
59
60     /**
61      * Returns the IDs of the content types this nature declares to
62      * have affinity with. These content types do not necessarily exist in the registry.
63      */

64     public String JavaDoc[] getContentTypeIds() {
65         return contentTypeIds;
66     }
67
68     /**
69      * @see IProjectNatureDescriptor#getNatureId()
70      */

71     public String JavaDoc getNatureId() {
72         return id;
73     }
74
75     /**
76      * @see IProjectNatureDescriptor#getLabel()
77      */

78     public String JavaDoc getLabel() {
79         return label;
80     }
81
82     /**
83      * @see IProjectNatureDescriptor#getRequiredNatureIds()
84      */

85     public String JavaDoc[] getRequiredNatureIds() {
86         return requiredNatures;
87     }
88
89     /**
90      * @see IProjectNatureDescriptor#getNatureSetIds()
91      */

92     public String JavaDoc[] getNatureSetIds() {
93         return natureSets;
94     }
95
96     /**
97      * @see IProjectNatureDescriptor#isLinkingAllowed()
98      */

99     public boolean isLinkingAllowed() {
100         return allowLinking;
101     }
102
103     /**
104      * Initialize this nature descriptor based on the provided extension point.
105      */

106     protected void readExtension(IExtension natureExtension) throws CoreException {
107         //read the extension
108
id = natureExtension.getUniqueIdentifier();
109         if (id == null) {
110             fail(Messages.natures_missingIdentifier);
111         }
112         label = natureExtension.getLabel();
113         IConfigurationElement[] elements = natureExtension.getConfigurationElements();
114         int count = elements.length;
115         ArrayList JavaDoc requiredList = new ArrayList JavaDoc(count);
116         ArrayList JavaDoc setList = new ArrayList JavaDoc(count);
117         ArrayList JavaDoc builderList = new ArrayList JavaDoc(count);
118         ArrayList JavaDoc contentTypeList = new ArrayList JavaDoc(count);
119         for (int i = 0; i < count; i++) {
120             IConfigurationElement element = elements[i];
121             String JavaDoc name = element.getName();
122             if (name.equalsIgnoreCase("requires-nature")) { //$NON-NLS-1$
123
String JavaDoc attribute = element.getAttribute("id"); //$NON-NLS-1$
124
if (attribute == null)
125                     fail();
126                 requiredList.add(attribute);
127             } else if (name.equalsIgnoreCase("one-of-nature")) { //$NON-NLS-1$
128
String JavaDoc attribute = element.getAttribute("id"); //$NON-NLS-1$
129
if (attribute == null)
130                     fail();
131                 setList.add(attribute);
132             } else if (name.equalsIgnoreCase("builder")) { //$NON-NLS-1$
133
String JavaDoc attribute = element.getAttribute("id"); //$NON-NLS-1$
134
if (attribute == null)
135                     fail();
136                 builderList.add(attribute);
137             } else if (name.equalsIgnoreCase("content-type")) { //$NON-NLS-1$
138
String JavaDoc attribute = element.getAttribute("id"); //$NON-NLS-1$
139
if (attribute == null)
140                     fail();
141                 contentTypeList.add(attribute);
142             } else if (name.equalsIgnoreCase("options")) { //$NON-NLS-1$
143
String JavaDoc attribute = element.getAttribute("allowLinking"); //$NON-NLS-1$
144
//when in doubt (missing attribute, wrong value) default to allow linking
145
allowLinking = !Boolean.FALSE.toString().equalsIgnoreCase(attribute);
146             }
147         }
148         requiredNatures = (String JavaDoc[]) requiredList.toArray(new String JavaDoc[requiredList.size()]);
149         natureSets = (String JavaDoc[]) setList.toArray(new String JavaDoc[setList.size()]);
150         builderIds = (String JavaDoc[]) builderList.toArray(new String JavaDoc[builderList.size()]);
151         contentTypeIds = (String JavaDoc[]) contentTypeList.toArray(new String JavaDoc[contentTypeList.size()]);
152     }
153
154     /**
155      * Prints out a string representation for debugging purposes only.
156      */

157     public String JavaDoc toString() {
158         return "ProjectNatureDescriptor(" + id + ")"; //$NON-NLS-1$ //$NON-NLS-2$
159
}
160 }
161
Popular Tags