1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.beans.PropertyChangeListener ; 23 import java.io.IOException ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 import javax.swing.Icon ; 27 import javax.swing.ImageIcon ; 28 import org.netbeans.api.project.Project; 29 import org.netbeans.api.project.ProjectInformation; 30 import org.netbeans.api.project.ProjectManager; 31 import org.netbeans.modules.ant.freeform.spi.ProjectNature; 32 import org.netbeans.modules.ant.freeform.spi.support.Util; 33 import org.netbeans.modules.ant.freeform.ui.ProjectCustomizerProvider; 34 import org.netbeans.modules.ant.freeform.ui.View; 35 import org.netbeans.spi.project.AuxiliaryConfiguration; 36 import org.netbeans.spi.project.support.LookupProviderSupport; 37 import org.netbeans.spi.project.support.ant.AntProjectHelper; 38 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 39 import org.netbeans.spi.project.support.ant.PropertyUtils; 40 import org.netbeans.spi.project.ui.support.UILookupMergerSupport; 41 import org.openide.filesystems.FileObject; 42 import org.openide.util.Lookup; 43 import org.openide.util.Mutex; 44 import org.openide.util.Utilities; 45 import org.openide.util.lookup.Lookups; 46 import org.w3c.dom.Element ; 47 import org.w3c.dom.NodeList ; 48 49 53 public final class FreeformProject implements Project { 54 55 public static final Lookup.Result<ProjectNature> PROJECT_NATURES = Lookup.getDefault().lookupResult(ProjectNature.class); 56 57 private final AntProjectHelper helper; 58 private final PropertyEvaluator eval; 59 private final Lookup lookup; 60 private AuxiliaryConfiguration aux; 61 62 public FreeformProject(AntProjectHelper helper) throws IOException { 63 this.helper = helper; 64 eval = new FreeformEvaluator(this); 65 lookup = initLookup(); 66 Logger.getLogger(FreeformProject.class.getName()).log(Level.FINER, "Initializing project in {0} with {1}", new Object [] {helper, lookup}); 67 new ProjectXmlValidator(helper.resolveFileObject(AntProjectHelper.PROJECT_XML_PATH)); 68 } 69 70 public AntProjectHelper helper() { 71 return helper; 72 } 73 74 77 public Element getPrimaryConfigurationData() { 78 return Util.getPrimaryConfigurationData(helper); 79 } 80 81 84 public void putPrimaryConfigurationData(Element data) { 85 Util.putPrimaryConfigurationData(helper, data); 86 } 87 88 private Lookup initLookup() throws IOException { 89 aux = helper().createAuxiliaryConfiguration(); Lookup baseLookup = Lookups.fixed( 91 this, 92 new Info(), new FreeformSources(this), new Actions(this), new View(this), new ProjectCustomizerProvider(this), aux, helper().createCacheDirectoryProvider(), new Subprojects(this), new ArtifactProvider(this), new LookupMergerImpl(), UILookupMergerSupport.createPrivilegedTemplatesMerger(), 103 UILookupMergerSupport.createRecommendedTemplatesMerger(), 104 new FreeformProjectOperations(this), 105 new FreeformSharabilityQuery(helper()), Accessor.DEFAULT.createProjectAccessor(this) ); 108 return LookupProviderSupport.createCompositeLookup(baseLookup, "Projects/org-netbeans-modules-ant-freeform/Lookup"); } 110 111 public FileObject getProjectDirectory() { 112 return helper.getProjectDirectory(); 113 } 114 115 public Lookup getLookup() { 116 return lookup; 117 } 118 119 public PropertyEvaluator evaluator() { 120 return eval; 121 } 122 123 public String toString() { 124 return "FreeformProject[" + getProjectDirectory() + "]"; } 126 127 128 public void setName(final String name) { 129 ProjectManager.mutex().writeAccess(new Mutex.Action<Void >() { 130 public Void run() { 131 Element data = getPrimaryConfigurationData(); 132 NodeList nl = data.getElementsByTagNameNS(FreeformProjectType.NS_GENERAL, "name"); 134 Element nameEl; 135 if (nl.getLength() == 1) { 136 nameEl = (Element ) nl.item(0); 137 NodeList deadKids = nameEl.getChildNodes(); 138 while (deadKids.getLength() > 0) { 139 nameEl.removeChild(deadKids.item(0)); 140 } 141 } else { 142 nameEl = data.getOwnerDocument().createElementNS(FreeformProjectType.NS_GENERAL, "name"); 143 data.insertBefore(nameEl, data.getChildNodes().item(0)); 144 } 145 nameEl.appendChild(data.getOwnerDocument().createTextNode(name)); 146 putPrimaryConfigurationData(data); 147 return null; 148 } 149 }); 150 } 151 152 private final class Info implements ProjectInformation { 153 154 public Info() {} 155 156 public String getName() { 157 return PropertyUtils.getUsablePropertyName(getDisplayName()); 158 } 159 160 public String getDisplayName() { 161 return ProjectManager.mutex().readAccess(new Mutex.Action<String >() { 162 public String run() { 163 Element genldata = getPrimaryConfigurationData(); 164 Element nameEl = Util.findElement(genldata, "name", FreeformProjectType.NS_GENERAL); if (nameEl == null) { 166 return "???"; } 169 return Util.findText(nameEl); 170 } 171 }); 172 } 173 174 public Icon getIcon() { 175 if (usesAntScripting()) { 176 return new ImageIcon (Utilities.loadImage("org/netbeans/modules/ant/freeform/resources/freeform-project.png", true)); } else { 178 return new ImageIcon (Utilities.loadImage("org/netbeans/modules/project/ui/resources/projectTab.gif", true)); } 180 } 181 182 public Project getProject() { 183 return FreeformProject.this; 184 } 185 186 public void addPropertyChangeListener(PropertyChangeListener listener) { 187 } 189 190 public void removePropertyChangeListener(PropertyChangeListener listener) { 191 } 193 194 } 195 196 197 198 207 public boolean usesAntScripting() { 208 return getProjectDirectory().getFileObject("build.xml") != null || evaluator().getProperty("ant.script") != null || Util.getPrimaryConfigurationData(helper).getElementsByTagName("action").getLength() > 0; } 212 213 } 214 | Popular Tags |