2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.java.freeform;
44
import org.netbeans.api.project.Project;
45
import org.netbeans.modules.ant.freeform.spi.HelpIDFragmentProvider;
46
import org.netbeans.modules.ant.freeform.spi.ProjectAccessor;
47
import org.netbeans.spi.project.AuxiliaryConfiguration;
48
import org.netbeans.spi.project.LookupProvider;
49
import org.netbeans.spi.project.support.ant.AntProjectEvent;
50
import org.netbeans.spi.project.support.ant.AntProjectHelper;
51
import org.netbeans.spi.project.support.ant.AntProjectListener;
52
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
53
import org.netbeans.spi.project.ui.PrivilegedTemplates;
54
import org.netbeans.spi.project.ui.ProjectOpenedHook;
55
import org.netbeans.spi.project.ui.RecommendedTemplates;
56
import org.openide.util.Lookup;
57
import org.openide.util.lookup.Lookups;
58
import org.openide.util.lookup.ProxyLookup;
59
import org.w3c.dom.Comment;
60
import org.w3c.dom.Document;
61
import org.w3c.dom.Element;
62
import org.w3c.dom.NamedNodeMap;
63
import org.w3c.dom.NodeList;
64
import org.w3c.dom.Text;
70
public class LookupProviderImpl implements LookupProvider {
71
private static final String HELP_ID_FRAGMENT = "java"; // NOI18N
73
/** Creates a new instance of LookupProviderImpl */
74
public LookupProviderImpl() {
77
public Lookup createAdditionalLookup(Lookup baseContext) {
78
Project prj = baseContext.lookup(Project.class);
79
ProjectAccessor acc = baseContext.lookup(ProjectAccessor.class);
80
AuxiliaryConfiguration aux = baseContext.lookup(AuxiliaryConfiguration.class);
84
return new ProjectLookup(prj, acc.getHelper(), acc.getEvaluator(), aux);
87
private static Lookup initLookup(Project project, AntProjectHelper projectHelper, PropertyEvaluator projectEvaluator, AuxiliaryConfiguration aux) {
88
Classpaths cp = new Classpaths(projectHelper, projectEvaluator, aux);
90
cp, // ClassPathProvider
91
new SourceLevelQueryImpl(projectHelper, projectEvaluator, aux), // SourceLevelQueryImplementation
92
new SourceForBinaryQueryImpl(projectHelper, projectEvaluator, aux), // SourceForBinaryQueryImplementation
93
new OpenHook(cp), // ProjectOpenedHook
94
new TestQuery(projectHelper, projectEvaluator, aux), // MultipleRootsUnitTestForSourceQueryImplementation
95
new JavadocQuery(projectHelper, projectEvaluator, aux), // JavadocForBinaryQueryImplementation
96
new PrivilegedTemplatesImpl(), // PrivilegedTemplates
97
new JavaActions(project, projectHelper, projectEvaluator, aux), // ActionProvider
98
new LookupMergerImpl(), // LookupMerger
99
new JavaFreeformFileBuiltQuery(project, projectHelper, projectEvaluator, aux), // FileBuiltQueryImplementation
100
new HelpIDFragmentProviderImpl());
103
public static boolean isMyProject(AuxiliaryConfiguration aux) {
104
return aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true) != null ||
105
aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true) != null;
108
private static final class HelpIDFragmentProviderImpl implements HelpIDFragmentProvider {
109
public String getHelpIDFragment() {
110
return HELP_ID_FRAGMENT;
114
private static class OpenHook extends ProjectOpenedHook {
116
private final Classpaths cp;
118
public OpenHook(Classpaths cp) {
122
protected void projectOpened() {
126
protected void projectClosed() {
133
* Transparently handles /1 -> /2 schema upgrade (on read only, not write!).
135
static final class UpgradingAuxiliaryConfiguration implements AuxiliaryConfiguration {
137
private final AuxiliaryConfiguration delegate;
139
public UpgradingAuxiliaryConfiguration(AuxiliaryConfiguration delegate) {
140
this.delegate = delegate;
143
public Element getConfigurationFragment(String elementName, String namespace, boolean shared) {
144
if (elementName.equals(JavaProjectNature.EL_JAVA) && namespace.equals(JavaProjectNature.NS_JAVA_2) && shared) {
145
Element nue = delegate.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true);
147
Element old = delegate.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true);
149
nue = upgradeSchema(old);
154
return delegate.getConfigurationFragment(elementName, namespace, shared);
158
public void putConfigurationFragment(Element fragment, boolean shared) throws IllegalArgumentException {
159
delegate.putConfigurationFragment(fragment, shared);
162
public boolean removeConfigurationFragment(String elementName, String namespace, boolean shared) throws IllegalArgumentException {
163
return delegate.removeConfigurationFragment(elementName, namespace, shared);
168
static Element upgradeSchema(Element old) {
169
Document doc = old.getOwnerDocument();
170
Element nue = doc.createElementNS(JavaProjectNature.NS_JAVA_2, JavaProjectNature.EL_JAVA);
171
copyXMLTree(doc, old, nue, JavaProjectNature.NS_JAVA_2);
174
// Copied from org.netbeans.modules.java.j2seproject.UpdateHelper with changes; could be an API eventually:
175
private static void copyXMLTree(Document doc, Element from, Element to, String newNamespace) {
176
NodeList nl = from.getChildNodes();
177
int length = nl.getLength();
178
for (int i = 0; i < length; i++) {
179
org.w3c.dom.Node node = nl.item(i);
180
org.w3c.dom.Node newNode;
181
switch (node.getNodeType()) {
182
case org.w3c.dom.Node.ELEMENT_NODE:
183
Element oldElement = (Element) node;
184
newNode = doc.createElementNS(newNamespace, oldElement.getTagName());
185
NamedNodeMap attrs = oldElement.getAttributes();
186
int alength = attrs.getLength();
187
for (int j = 0; j < alength; j++) {
188
org.w3c.dom.Attr oldAttr = (org.w3c.dom.Attr) attrs.item(j);
189
((Element)newNode).setAttributeNS(oldAttr.getNamespaceURI(), oldAttr.getName(), oldAttr.getValue());
191
copyXMLTree(doc, oldElement, (Element) newNode, newNamespace);
193
case org.w3c.dom.Node.TEXT_NODE:
194
newNode = doc.createTextNode(((Text) node).getData());
196
case org.w3c.dom.Node.COMMENT_NODE:
197
newNode = doc.createComment(((Comment) node).getData());
200
// Other types (e.g. CDATA) not yet handled.
201
throw new AssertionError(node);
203
to.appendChild(newNode);
209
private static final class ProjectLookup extends ProxyLookup implements AntProjectListener {
211
private AntProjectHelper helper;
212
private PropertyEvaluator evaluator;
213
private Project project;
214
private AuxiliaryConfiguration aux;
215
private boolean isMyProject;
217
public ProjectLookup(Project project, AntProjectHelper helper, PropertyEvaluator evaluator, AuxiliaryConfiguration aux) {
218
super(new Lookup[0]);
219
this.project = project;
220
this.helper = helper;
221
this.evaluator = evaluator;
222
this.aux = new UpgradingAuxiliaryConfiguration(aux);
223
this.isMyProject = isMyProject(aux);
225
helper.addAntProjectListener(this);
228
private void updateLookup() {
229
Lookup l = Lookup.EMPTY;
231
l = initLookup(project, helper, evaluator, aux);
233
setLookups(new Lookup[]{l});
236
public void configurationXmlChanged(AntProjectEvent ev) {
237
if (isMyProject(aux) != isMyProject) {
238
isMyProject = !isMyProject;
243
public void propertiesChanged(AntProjectEvent ev) {
248
private static final class PrivilegedTemplatesImpl implements PrivilegedTemplates, RecommendedTemplates {
250
private static final String[] PRIVILEGED_NAMES = new String[] {
251
"Templates/Classes/Class.java", // NOI18N
252
"Templates/Classes/Package", // NOI18N
253
"Templates/Classes/Interface.java", // NOI18N
256
// List of primarily supported templates = J2SEProject.LIBRARY_TYPES + J2SEProject.APPLICATION_TYPES
257
private static final String[] RECOMENDED_TYPES = new String[] {
258
"java-classes", // NOI18N
259
"java-main-class", // NOI18N
260
"java-forms", // NOI18N
261
"gui-java-application", // NOI18N
262
"java-beans", // NOI18N
263
"oasis-XML-catalogs", // NOI18N
265
"ant-script", // NOI18N
266
"ant-task", // NOI18N
267
"servlet-types", // NOI18N
271
"simple-files" // NOI18N
274
public String[] getRecommendedTypes() {
275
return RECOMENDED_TYPES;
278
public String[] getPrivilegedTemplates() {
279
return PRIVILEGED_NAMES;