1 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 ; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.NamedNodeMap ; 41 import org.w3c.dom.NodeList ; 42 import org.w3c.dom.Text ; 43 44 48 public class LookupProviderImpl implements LookupProvider { 49 private static final String HELP_ID_FRAGMENT = "java"; 51 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, new SourceLevelQueryImpl(projectHelper, projectEvaluator, aux), new SourceForBinaryQueryImpl(projectHelper, projectEvaluator, aux), new OpenHook(cp), new TestQuery(projectHelper, projectEvaluator, aux), new JavadocQuery(projectHelper, projectEvaluator, aux), new PrivilegedTemplatesImpl(), new JavaActions(project, projectHelper, projectEvaluator, aux), new LookupMergerImpl(), new JavaFreeformFileBuiltQuery(project, projectHelper, projectEvaluator, aux), 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 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 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 getConfigurationFragment(String elementName, String namespace, boolean shared) { 122 if (elementName.equals(JavaProjectNature.EL_JAVA) && namespace.equals(JavaProjectNature.NS_JAVA_2) && shared) { 123 Element nue = delegate.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true); 124 if (nue == null) { 125 Element 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 fragment, boolean shared) throws IllegalArgumentException { 137 delegate.putConfigurationFragment(fragment, shared); 138 } 139 140 public boolean removeConfigurationFragment(String elementName, String namespace, boolean shared) throws IllegalArgumentException { 141 return delegate.removeConfigurationFragment(elementName, namespace, shared); 142 } 143 144 } 145 146 static Element upgradeSchema(Element old) { 147 Document doc = old.getOwnerDocument(); 148 Element nue = doc.createElementNS(JavaProjectNature.NS_JAVA_2, JavaProjectNature.EL_JAVA); 149 copyXMLTree(doc, old, nue, JavaProjectNature.NS_JAVA_2); 150 return nue; 151 } 152 private static void copyXMLTree(Document doc, Element from, Element to, String newNamespace) { 154 NodeList nl = from.getChildNodes(); 155 int length = nl.getLength(); 156 for (int i = 0; i < length; i++) { 157 org.w3c.dom.Node node = nl.item(i); 158 org.w3c.dom.Node newNode; 159 switch (node.getNodeType()) { 160 case org.w3c.dom.Node.ELEMENT_NODE: 161 Element oldElement = (Element ) node; 162 newNode = doc.createElementNS(newNamespace, oldElement.getTagName()); 163 NamedNodeMap attrs = oldElement.getAttributes(); 164 int alength = attrs.getLength(); 165 for (int j = 0; j < alength; j++) { 166 org.w3c.dom.Attr oldAttr = (org.w3c.dom.Attr ) attrs.item(j); 167 ((Element )newNode).setAttributeNS(oldAttr.getNamespaceURI(), oldAttr.getName(), oldAttr.getValue()); 168 } 169 copyXMLTree(doc, oldElement, (Element ) newNode, newNamespace); 170 break; 171 case org.w3c.dom.Node.TEXT_NODE: 172 newNode = doc.createTextNode(((Text ) node).getData()); 173 break; 174 case org.w3c.dom.Node.COMMENT_NODE: 175 newNode = doc.createComment(((Comment ) node).getData()); 176 break; 177 default: 178 throw new AssertionError (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 } 224 225 } 226 private static final class PrivilegedTemplatesImpl implements PrivilegedTemplates, RecommendedTemplates { 227 228 private static final String [] PRIVILEGED_NAMES = new String [] { 229 "Templates/Classes/Class.java", "Templates/Classes/Package", "Templates/Classes/Interface.java", }; 233 234 private static final String [] RECOMENDED_TYPES = new String [] { 236 "java-classes", "java-main-class", "java-forms", "gui-java-application", "java-beans", "oasis-XML-catalogs", "XML", "ant-script", "ant-task", "servlet-types", "wsdl", "junit", "simple-files" }; 251 252 public String [] getRecommendedTypes() { 253 return RECOMENDED_TYPES; 254 } 255 256 public String [] getPrivilegedTemplates() { 257 return PRIVILEGED_NAMES; 258 } 259 260 } 261 } 262 | Popular Tags |