KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > freeform > LookupProviderImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.freeform;
21
22 import org.netbeans.api.project.Project;
23 import org.netbeans.modules.ant.freeform.spi.HelpIDFragmentProvider;
24 import org.netbeans.modules.ant.freeform.spi.ProjectAccessor;
25 import org.netbeans.spi.project.AuxiliaryConfiguration;
26 import org.netbeans.spi.project.LookupProvider;
27 import org.netbeans.spi.project.support.ant.AntProjectEvent;
28 import org.netbeans.spi.project.support.ant.AntProjectHelper;
29 import org.netbeans.spi.project.support.ant.AntProjectListener;
30 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
31 import org.netbeans.spi.project.ui.PrivilegedTemplates;
32 import org.netbeans.spi.project.ui.ProjectOpenedHook;
33 import org.netbeans.spi.project.ui.RecommendedTemplates;
34 import org.openide.util.Lookup;
35 import org.openide.util.lookup.Lookups;
36 import org.openide.util.lookup.ProxyLookup;
37 import org.w3c.dom.Comment JavaDoc;
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.NamedNodeMap JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42 import org.w3c.dom.Text JavaDoc;
43
44 /**
45  *
46  * @author mkleint
47  */

48 public class LookupProviderImpl implements LookupProvider {
49      private static final String JavaDoc HELP_ID_FRAGMENT = "java"; // NOI18N
50

51     /** Creates a new instance of LookupProviderImpl */
52     public LookupProviderImpl() {
53     }
54     
55     public Lookup createAdditionalLookup(Lookup baseContext) {
56         Project prj = baseContext.lookup(Project.class);
57         ProjectAccessor acc = baseContext.lookup(ProjectAccessor.class);
58         AuxiliaryConfiguration aux = baseContext.lookup(AuxiliaryConfiguration.class);
59         assert aux != null;
60         assert prj != null;
61         assert acc != null;
62         return new ProjectLookup(prj, acc.getHelper(), acc.getEvaluator(), aux);
63     }
64     
65     private static Lookup initLookup(Project project, AntProjectHelper projectHelper, PropertyEvaluator projectEvaluator, AuxiliaryConfiguration aux) {
66         Classpaths cp = new Classpaths(projectHelper, projectEvaluator, aux);
67         return Lookups.fixed(
68             cp, // ClassPathProvider
69
new SourceLevelQueryImpl(projectHelper, projectEvaluator, aux), // SourceLevelQueryImplementation
70
new SourceForBinaryQueryImpl(projectHelper, projectEvaluator, aux), // SourceForBinaryQueryImplementation
71
new OpenHook(cp), // ProjectOpenedHook
72
new TestQuery(projectHelper, projectEvaluator, aux), // MultipleRootsUnitTestForSourceQueryImplementation
73
new JavadocQuery(projectHelper, projectEvaluator, aux), // JavadocForBinaryQueryImplementation
74
new PrivilegedTemplatesImpl(), // PrivilegedTemplates
75
new JavaActions(project, projectHelper, projectEvaluator, aux), // ActionProvider
76
new LookupMergerImpl(), // LookupMerger
77
new JavaFreeformFileBuiltQuery(project, projectHelper, projectEvaluator, aux), // FileBuiltQueryImplementation
78
new HelpIDFragmentProviderImpl());
79     }
80     
81     public static boolean isMyProject(AuxiliaryConfiguration aux) {
82         return aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true) != null ||
83                aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true) != null;
84     }
85     
86     private static final class HelpIDFragmentProviderImpl implements HelpIDFragmentProvider {
87         public String JavaDoc getHelpIDFragment() {
88             return HELP_ID_FRAGMENT;
89         }
90     }
91     
92     private static class OpenHook extends ProjectOpenedHook {
93         
94         private final Classpaths cp;
95         
96         public OpenHook(Classpaths cp) {
97             this.cp = cp;
98         }
99         
100         protected void projectOpened() {
101             cp.opened();
102         }
103         
104         protected void projectClosed() {
105             cp.closed();
106         }
107         
108     }
109     
110     /**
111      * Transparently handles /1 -> /2 schema upgrade (on read only, not write!).
112      */

113     static final class UpgradingAuxiliaryConfiguration implements AuxiliaryConfiguration {
114         
115         private final AuxiliaryConfiguration delegate;
116         
117         public UpgradingAuxiliaryConfiguration(AuxiliaryConfiguration delegate) {
118             this.delegate = delegate;
119         }
120
121         public Element JavaDoc getConfigurationFragment(String JavaDoc elementName, String JavaDoc namespace, boolean shared) {
122             if (elementName.equals(JavaProjectNature.EL_JAVA) && namespace.equals(JavaProjectNature.NS_JAVA_2) && shared) {
123                 Element JavaDoc nue = delegate.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true);
124                 if (nue == null) {
125                     Element JavaDoc old = delegate.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true);
126                     if (old != null) {
127                         nue = upgradeSchema(old);
128                     }
129                 }
130                 return nue;
131             } else {
132                 return delegate.getConfigurationFragment(elementName, namespace, shared);
133             }
134         }
135
136         public void putConfigurationFragment(Element JavaDoc fragment, boolean shared) throws IllegalArgumentException JavaDoc {
137             delegate.putConfigurationFragment(fragment, shared);
138         }
139         
140         public boolean removeConfigurationFragment(String JavaDoc elementName, String JavaDoc namespace, boolean shared) throws IllegalArgumentException JavaDoc {
141             return delegate.removeConfigurationFragment(elementName, namespace, shared);
142         }
143         
144     }
145     
146     static Element JavaDoc upgradeSchema(Element JavaDoc old) {
147         Document JavaDoc doc = old.getOwnerDocument();
148         Element JavaDoc nue = doc.createElementNS(JavaProjectNature.NS_JAVA_2, JavaProjectNature.EL_JAVA);
149         copyXMLTree(doc, old, nue, JavaProjectNature.NS_JAVA_2);
150         return nue;
151     }
152     // Copied from org.netbeans.modules.java.j2seproject.UpdateHelper with changes; could be an API eventually:
153
private static void copyXMLTree(Document JavaDoc doc, Element JavaDoc from, Element JavaDoc to, String JavaDoc newNamespace) {
154         NodeList JavaDoc nl = from.getChildNodes();
155         int length = nl.getLength();
156         for (int i = 0; i < length; i++) {
157             org.w3c.dom.Node JavaDoc node = nl.item(i);
158             org.w3c.dom.Node JavaDoc newNode;
159             switch (node.getNodeType()) {
160                 case org.w3c.dom.Node.ELEMENT_NODE:
161                     Element JavaDoc oldElement = (Element JavaDoc) node;
162                     newNode = doc.createElementNS(newNamespace, oldElement.getTagName());
163                     NamedNodeMap JavaDoc attrs = oldElement.getAttributes();
164                     int alength = attrs.getLength();
165                     for (int j = 0; j < alength; j++) {
166                         org.w3c.dom.Attr JavaDoc oldAttr = (org.w3c.dom.Attr JavaDoc) attrs.item(j);
167                         ((Element JavaDoc)newNode).setAttributeNS(oldAttr.getNamespaceURI(), oldAttr.getName(), oldAttr.getValue());
168                     }
169                     copyXMLTree(doc, oldElement, (Element JavaDoc) newNode, newNamespace);
170                     break;
171                 case org.w3c.dom.Node.TEXT_NODE:
172                     newNode = doc.createTextNode(((Text JavaDoc) node).getData());
173                     break;
174                 case org.w3c.dom.Node.COMMENT_NODE:
175                     newNode = doc.createComment(((Comment JavaDoc) node).getData());
176                     break;
177                 default:
178                     // Other types (e.g. CDATA) not yet handled.
179
throw new AssertionError JavaDoc(node);
180             }
181             to.appendChild(newNode);
182         }
183     }
184     
185     
186     
187    private static final class ProjectLookup extends ProxyLookup implements AntProjectListener {
188
189         private AntProjectHelper helper;
190         private PropertyEvaluator evaluator;
191         private Project project;
192         private AuxiliaryConfiguration aux;
193         private boolean isMyProject;
194         
195         public ProjectLookup(Project project, AntProjectHelper helper, PropertyEvaluator evaluator, AuxiliaryConfiguration aux) {
196             super(new Lookup[0]);
197             this.project = project;
198             this.helper = helper;
199             this.evaluator = evaluator;
200             this.aux = new UpgradingAuxiliaryConfiguration(aux);
201             this.isMyProject = isMyProject(aux);
202             updateLookup();
203             helper.addAntProjectListener(this);
204         }
205         
206         private void updateLookup() {
207             Lookup l = Lookup.EMPTY;
208             if (isMyProject) {
209                 l = initLookup(project, helper, evaluator, aux);
210             }
211             setLookups(new Lookup[]{l});
212         }
213         
214         public void configurationXmlChanged(AntProjectEvent ev) {
215             if (isMyProject(aux) != isMyProject) {
216                 isMyProject = !isMyProject;
217                 updateLookup();
218             }
219         }
220         
221         public void propertiesChanged(AntProjectEvent ev) {
222             // ignore
223
}
224         
225     }
226    private static final class PrivilegedTemplatesImpl implements PrivilegedTemplates, RecommendedTemplates {
227         
228         private static final String JavaDoc[] PRIVILEGED_NAMES = new String JavaDoc[] {
229             "Templates/Classes/Class.java", // NOI18N
230
"Templates/Classes/Package", // NOI18N
231
"Templates/Classes/Interface.java", // NOI18N
232
};
233         
234         // List of primarily supported templates = J2SEProject.LIBRARY_TYPES + J2SEProject.APPLICATION_TYPES
235
private static final String JavaDoc[] RECOMENDED_TYPES = new String JavaDoc[] {
236             "java-classes", // NOI18N
237
"java-main-class", // NOI18N
238
"java-forms", // NOI18N
239
"gui-java-application", // NOI18N
240
"java-beans", // NOI18N
241
"oasis-XML-catalogs", // NOI18N
242
"XML", // NOI18N
243
"ant-script", // NOI18N
244
"ant-task", // NOI18N
245
"servlet-types", // NOI18N
246
"wsdl", // NOI18N
247
"junit", // NOI18N
248
// "MIDP", // NOI18N
249
"simple-files" // NOI18N
250
};
251         
252         public String JavaDoc[] getRecommendedTypes() {
253             return RECOMENDED_TYPES;
254         }
255         
256         public String JavaDoc[] getPrivilegedTemplates() {
257             return PRIVILEGED_NAMES;
258         }
259         
260     }
261 }
262
Popular Tags