1 19 20 21 package org.netbeans.modules.web.freeform; 22 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.beans.PropertyChangeSupport ; 26 import java.io.File ; 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.Iterator ; 30 import java.util.List ; 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 ; 54 55 59 public class LookupProviderImpl implements LookupProvider { 60 61 private static final String HELP_ID_FRAGMENT = "web"; 63 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 [] { 80 new ProjectOpenedHookImpl(webcp), new PrivilegedTemplatesImpl(), new WebModules(project, projectHelper, projectEvaluator), new WebFreeFormActionProvider(project, projectHelper, aux), new HelpIDFragmentProviderImpl(), 85 }); 86 } 87 88 public static boolean isMyProject(AuxiliaryConfiguration aux) { 89 return aux.getConfigurationFragment("web-data", WebProjectNature.NS_WEB, true) != null; } 91 92 private static final class HelpIDFragmentProviderImpl implements HelpIDFragmentProvider { 93 public String 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 { 112 113 private ClassPath registeredCP[] = new ClassPath[0]; 114 private List registeredRoots = Collections.EMPTY_LIST; 115 116 private PropertyEvaluator evaluator; 117 private AuxiliaryConfiguration aux; 118 private Project project; 119 120 private PropertyChangeSupport 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 (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 } 157 158 public void propertyChange(PropertyChangeEvent ev) { 159 updateClasspath(); 160 } 161 162 public void addPropertyChangeListener(PropertyChangeListener listener) { 163 pcs.addPropertyChangeListener(listener); 164 } 165 166 public void removePropertyChangeListener(PropertyChangeListener listener) { 167 pcs.removePropertyChangeListener(listener); 168 } 169 170 private synchronized void updateClasspath() { 171 if (!prjClosed) { 172 List 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 getWebRoots(AuxiliaryConfiguration aux, Project proj, PropertyEvaluator evaluator) { 186 Element web = aux.getConfigurationFragment("web-data", WebProjectNature.NS_WEB, true); if (web == null) { 188 return null; 189 } 190 List webModules = Util.findSubElements(web); 191 Iterator it = webModules.iterator(); 192 List roots = new ArrayList (); 193 while (it.hasNext()) { 194 Element webModulesEl = (Element ) it.next(); 195 assert webModulesEl.getLocalName().equals("web-module") : webModulesEl; roots.add(FileUtil.toFileObject(getFile(webModulesEl, "doc-root", proj, evaluator))); } 198 return roots; 199 } 200 201 private File getFile(Element parent, String fileElName, Project proj, PropertyEvaluator evaluator) { 202 Element 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 } 245 246 } 247 248 private static final class PrivilegedTemplatesImpl implements PrivilegedTemplates, RecommendedTemplates { 249 250 private static final String [] PRIVILEGED_NAMES = new String [] { 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 [] RECOMENDED_TYPES = new String [] { 258 "java-classes", "java-main-class", "java-beans", "oasis-XML-catalogs", "XML", "wsdl", "ant-script", "ant-task", "servlet-types", "web-types", "j2ee-types", "junit", "simple-files" }; 272 273 public String [] getPrivilegedTemplates() { 274 return PRIVILEGED_NAMES; 275 } 276 277 public String [] getRecommendedTypes() { 278 return RECOMENDED_TYPES; 279 } 280 } 281 } 282 | Popular Tags |