KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > 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
21 package org.netbeans.modules.web.freeform;
22
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.beans.PropertyChangeSupport JavaDoc;
26 import java.io.File JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import org.netbeans.api.java.classpath.ClassPath;
32 import org.netbeans.api.java.classpath.GlobalPathRegistry;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.modules.ant.freeform.spi.HelpIDFragmentProvider;
35 import org.netbeans.modules.ant.freeform.spi.ProjectAccessor;
36 import org.netbeans.modules.ant.freeform.spi.support.Util;
37 import org.netbeans.spi.java.classpath.ClassPathImplementation;
38 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
39 import org.netbeans.spi.project.AuxiliaryConfiguration;
40 import org.netbeans.spi.project.LookupProvider;
41 import org.netbeans.spi.project.support.ant.AntProjectEvent;
42 import org.netbeans.spi.project.support.ant.AntProjectHelper;
43 import org.netbeans.spi.project.support.ant.AntProjectListener;
44 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
45 import org.netbeans.spi.project.ui.PrivilegedTemplates;
46 import org.netbeans.spi.project.ui.ProjectOpenedHook;
47 import org.netbeans.spi.project.ui.RecommendedTemplates;
48 import org.openide.filesystems.FileObject;
49 import org.openide.filesystems.FileUtil;
50 import org.openide.util.Lookup;
51 import org.openide.util.lookup.Lookups;
52 import org.openide.util.lookup.ProxyLookup;
53 import org.w3c.dom.Element JavaDoc;
54
55 /**
56  *
57  * @author mkleint
58  */

59 public class LookupProviderImpl implements LookupProvider {
60
61     private static final String JavaDoc HELP_ID_FRAGMENT = "web"; // NOI18N
62

63     /** Creates a new instance of LookupProviderImpl */
64     public LookupProviderImpl() {
65     }
66     
67     public Lookup createAdditionalLookup(Lookup baseContext) {
68         Project prj = (Project)baseContext.lookup(Project.class);
69         ProjectAccessor acc = (ProjectAccessor)baseContext.lookup(ProjectAccessor.class);
70         AuxiliaryConfiguration aux = (AuxiliaryConfiguration)baseContext.lookup(AuxiliaryConfiguration.class);
71         assert aux != null;
72         assert prj != null;
73         assert acc != null;
74         return new ProjectLookup(prj, acc.getHelper(), acc.getEvaluator(), aux);
75     }
76
77    private static Lookup initLookup(Project project, AntProjectHelper projectHelper, PropertyEvaluator projectEvaluator, AuxiliaryConfiguration aux) {
78         WebClasspath webcp = new WebClasspath(projectHelper, projectEvaluator, aux, project);
79         return Lookups.fixed(new Object JavaDoc[] {
80             new ProjectOpenedHookImpl(webcp), // register webroots as source classpath
81
new PrivilegedTemplatesImpl(), // List of templates in New action popup
82
new WebModules(project, projectHelper, projectEvaluator), // WebModuleProvider, ClassPathProvider
83
new WebFreeFormActionProvider(project, projectHelper, aux), //ActionProvider
84
new HelpIDFragmentProviderImpl(),
85         });
86     }
87    
88     public static boolean isMyProject(AuxiliaryConfiguration aux) {
89         return aux.getConfigurationFragment("web-data", WebProjectNature.NS_WEB, true) != null; // NOI18N
90
}
91     
92     private static final class HelpIDFragmentProviderImpl implements HelpIDFragmentProvider {
93         public String JavaDoc getHelpIDFragment() {
94             return HELP_ID_FRAGMENT;
95         }
96     }
97     
98     private static class ProjectOpenedHookImpl extends ProjectOpenedHook {
99         private final WebClasspath webcp;
100         public ProjectOpenedHookImpl(WebClasspath wcp) {
101             this.webcp = wcp;
102         }
103         protected void projectOpened() {
104             webcp.prjOpened();
105         }
106         protected void projectClosed() {
107             webcp.prjClosed();
108         }
109     }
110     
111     public static final class WebClasspath implements AntProjectListener, PropertyChangeListener JavaDoc {
112         
113         private ClassPath registeredCP[] = new ClassPath[0];
114         private List JavaDoc/*<FileObject>*/ registeredRoots = Collections.EMPTY_LIST;
115         
116         private PropertyEvaluator evaluator;
117         private AuxiliaryConfiguration aux;
118         private Project project;
119         
120         private PropertyChangeSupport JavaDoc pcs;
121         
122         private boolean prjClosed = false;
123         
124         public WebClasspath(AntProjectHelper helper, PropertyEvaluator evaluator, AuxiliaryConfiguration aux, Project proj) {
125             this.evaluator = evaluator;
126             this.aux = aux;
127             this.project = proj;
128             helper.addAntProjectListener(this);
129             evaluator.addPropertyChangeListener(this);
130             pcs = new PropertyChangeSupport JavaDoc(this);
131         }
132         
133         public void prjOpened() {
134             registeredRoots = getWebRoots(aux, project, evaluator);
135             FileObject fos[] = new FileObject[registeredRoots.size()];
136             ClassPath cp = ClassPathSupport.createClassPath((FileObject[]) registeredRoots.toArray(fos));
137             registeredCP = new ClassPath[] { cp };
138             GlobalPathRegistry.getDefault().register(ClassPath.SOURCE, registeredCP);
139             prjClosed = false;
140         }
141         
142         public void prjClosed() {
143             if (!registeredRoots.isEmpty()) {
144                 GlobalPathRegistry.getDefault().unregister(ClassPath.SOURCE, registeredCP);
145             }
146             registeredRoots = Collections.EMPTY_LIST;
147             prjClosed = true;
148         }
149         
150         public void configurationXmlChanged(AntProjectEvent ev) {
151             updateClasspath();
152         }
153         
154         public void propertiesChanged(AntProjectEvent ev) {
155             // ignore
156
}
157         
158         public void propertyChange(PropertyChangeEvent JavaDoc ev) {
159             updateClasspath();
160         }
161         
162         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
163             pcs.addPropertyChangeListener(listener);
164         }
165         
166         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
167             pcs.removePropertyChangeListener(listener);
168         }
169         
170         private synchronized void updateClasspath() {
171             if (!prjClosed) {
172                 List JavaDoc newRoots = getWebRoots(aux, project, evaluator);
173                 if (!newRoots.equals(registeredRoots)) {
174                     FileObject fos[] = new FileObject[newRoots.size()];
175                     ClassPath cp = ClassPathSupport.createClassPath((FileObject[]) newRoots.toArray(fos));
176                     GlobalPathRegistry.getDefault().unregister(ClassPath.SOURCE, registeredCP);
177                     registeredCP = new ClassPath[] { cp };
178                     registeredRoots = newRoots;
179                     GlobalPathRegistry.getDefault().register(ClassPath.SOURCE, registeredCP);
180                     pcs.firePropertyChange(ClassPathImplementation.PROP_RESOURCES, null, null);
181                 }
182             }
183         }
184         
185         private List JavaDoc/*<FileObject>*/ getWebRoots(AuxiliaryConfiguration aux, Project proj, PropertyEvaluator evaluator) {
186             Element JavaDoc web = aux.getConfigurationFragment("web-data", WebProjectNature.NS_WEB, true); // NOI18N
187
if (web == null) {
188                 return null;
189             }
190             List JavaDoc webModules = Util.findSubElements(web);
191             Iterator JavaDoc it = webModules.iterator();
192             List JavaDoc/*<FileObject>*/ roots = new ArrayList JavaDoc();
193             while (it.hasNext()) {
194                 Element JavaDoc webModulesEl = (Element JavaDoc) it.next();
195                 assert webModulesEl.getLocalName().equals("web-module") : webModulesEl; // NOI18N
196
roots.add(FileUtil.toFileObject(getFile(webModulesEl, "doc-root", proj, evaluator))); // NOI18N
197
}
198             return roots;
199         }
200         
201         private File JavaDoc getFile(Element JavaDoc parent, String JavaDoc fileElName, Project proj, PropertyEvaluator evaluator) {
202             Element JavaDoc el = Util.findElement(parent, fileElName, WebProjectNature.NS_WEB);
203             return Util.resolveFile(evaluator, FileUtil.toFile(proj.getProjectDirectory()), Util.findText(el));
204         }
205         
206     }
207     
208     private static final class ProjectLookup extends ProxyLookup implements AntProjectListener {
209
210         private AntProjectHelper helper;
211         private PropertyEvaluator evaluator;
212         private Project project;
213         private AuxiliaryConfiguration aux;
214         private boolean isMyProject;
215         
216         public ProjectLookup(Project project, AntProjectHelper helper, PropertyEvaluator evaluator, AuxiliaryConfiguration aux) {
217             super(new Lookup[0]);
218             this.project = project;
219             this.helper = helper;
220             this.evaluator = evaluator;
221             this.aux = aux;
222             this.isMyProject = isMyProject(aux);
223             updateLookup();
224             helper.addAntProjectListener(this);
225         }
226         
227         private void updateLookup() {
228             Lookup l = Lookup.EMPTY;
229             if (isMyProject) {
230                 l = initLookup(project, helper, evaluator, aux);
231             }
232             setLookups(new Lookup[]{l});
233         }
234         
235         public void configurationXmlChanged(AntProjectEvent ev) {
236             if (isMyProject(aux) != isMyProject) {
237                 isMyProject = !isMyProject;
238                 updateLookup();
239             }
240         }
241         
242         public void propertiesChanged(AntProjectEvent ev) {
243             // ignore
244
}
245         
246     }
247     
248     private static final class PrivilegedTemplatesImpl implements PrivilegedTemplates, RecommendedTemplates {
249         
250         private static final String JavaDoc[] PRIVILEGED_NAMES = new String JavaDoc[] {
251             "Templates/JSP_Servlet/JSP.jsp",
252             "Templates/JSP_Servlet/Html.html",
253             "Templates/JSP_Servlet/Servlet.java",
254             "Templates/Classes/Class.java",
255         };
256         
257         private static final String JavaDoc[] RECOMENDED_TYPES = new String JavaDoc[] {
258             "java-classes", // NOI18N
259
"java-main-class", // NOI18N
260
"java-beans", // NOI18N
261
"oasis-XML-catalogs", // NOI18N
262
"XML", // NOI18N
263
"wsdl", // NOI18N
264
"ant-script", // NOI18N
265
"ant-task", // NOI18N
266
"servlet-types", // NOI18N
267
"web-types", // NOI18N
268
"j2ee-types", // NOI18N
269
"junit", // NOI18N
270
"simple-files" // NOI18N
271
};
272         
273         public String JavaDoc[] getPrivilegedTemplates() {
274             return PRIVILEGED_NAMES;
275         }
276
277         public String JavaDoc[] getRecommendedTypes() {
278             return RECOMENDED_TYPES;
279         }
280     }
281 }
282
Popular Tags