1 19 20 package org.netbeans.modules.apisupport.project.suite; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 import javax.swing.Icon ; 33 import javax.swing.ImageIcon ; 34 import org.netbeans.api.project.Project; 35 import org.netbeans.api.project.ProjectInformation; 36 import org.netbeans.api.project.ProjectManager; 37 import org.netbeans.modules.apisupport.project.SuiteProvider; 38 import org.netbeans.modules.apisupport.project.Util; 39 import org.netbeans.modules.apisupport.project.ui.SuiteActions; 40 import org.netbeans.modules.apisupport.project.ui.SuiteLogicalView; 41 import org.netbeans.modules.apisupport.project.ui.SuiteOperations; 42 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteCustomizer; 43 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteProperties; 44 import org.netbeans.modules.apisupport.project.universe.NbPlatform; 45 import org.netbeans.spi.project.support.LookupProviderSupport; 46 import org.netbeans.spi.project.support.ant.AntProjectEvent; 47 import org.netbeans.spi.project.support.ant.AntProjectHelper; 48 import org.netbeans.spi.project.support.ant.AntProjectListener; 49 import org.netbeans.spi.project.support.ant.EditableProperties; 50 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper; 51 import org.netbeans.spi.project.support.ant.ProjectXmlSavedHook; 52 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 53 import org.netbeans.spi.project.support.ant.PropertyProvider; 54 import org.netbeans.spi.project.support.ant.PropertyUtils; 55 import org.netbeans.spi.project.ui.PrivilegedTemplates; 56 import org.netbeans.spi.project.ui.ProjectOpenedHook; 57 import org.netbeans.spi.project.ui.RecommendedTemplates; 58 import org.netbeans.spi.project.ui.support.UILookupMergerSupport; 59 import org.openide.ErrorManager; 60 import org.openide.filesystems.FileObject; 61 import org.openide.filesystems.FileUtil; 62 import org.openide.util.Lookup; 63 import org.openide.util.Mutex; 64 import org.openide.util.Utilities; 65 import org.openide.util.lookup.Lookups; 66 import org.w3c.dom.Element ; 67 68 72 public final class SuiteProject implements Project { 73 74 public static final String SUITE_ICON_PATH = 75 "org/netbeans/modules/apisupport/project/suite/resources/suite.gif"; 77 private final AntProjectHelper helper; 78 private Lookup lookup; 79 private final PropertyEvaluator eval; 80 private final GeneratedFilesHelper genFilesHelper; 81 82 public SuiteProject(AntProjectHelper helper) throws IOException { 83 this.helper = helper; 84 eval = createEvaluator(); 85 genFilesHelper = new GeneratedFilesHelper(helper); 86 Util.err.log("Loading suite project in " + getProjectDirectory()); 87 lookup = Lookups.fixed(new Object [] { 88 this, 89 new Info(), 90 helper.createAuxiliaryConfiguration(), 91 helper.createCacheDirectoryProvider(), 92 new SavedHook(), 93 new OpenedHook(), 94 helper.createSharabilityQuery(eval, new String [0], new String [] {"build", "dist"}), new SuiteSubprojectProviderImpl(helper, eval), 96 new SuiteProviderImpl(), 97 new SuiteActions(this), 98 new SuiteLogicalView(this), 99 new SuiteCustomizer(this, helper, eval), 100 new PrivilegedTemplatesImpl(), 101 new SuiteOperations(this), 102 }); 103 lookup = LookupProviderSupport.createCompositeLookup(lookup, "Projects/org-netbeans-modules-apisupport-project-suite/Lookup"); 104 } 105 106 public String toString() { 107 return "SuiteProject[" + getProjectDirectory() + "]"; } 109 110 public Lookup getLookup() { 111 return lookup; 112 } 113 114 public FileObject getProjectDirectory() { 115 return helper.getProjectDirectory(); 116 } 117 118 public File getProjectDirectoryFile() { 119 return FileUtil.toFile(getProjectDirectory()); 120 } 121 122 123 public AntProjectHelper getHelper() { 124 return helper; 125 } 126 127 128 public PropertyEvaluator getEvaluator() { 129 return eval; 130 } 131 132 139 public NbPlatform getPlatform(boolean fallback) { 140 NbPlatform p; 141 String destdir = getEvaluator().getProperty("netbeans.dest.dir"); if (destdir != null) { 144 p = NbPlatform.getPlatformByDestDir(getHelper().resolveFile(destdir)); 145 } else { 146 p = null; 147 } 148 if (fallback && (p == null || !p.isValid())) { 149 p = NbPlatform.getDefaultPlatform(); 150 } 151 return p; 152 } 153 154 private PropertyEvaluator createEvaluator() { 155 PropertyProvider predefs = helper.getStockPropertyPreprovider(); 156 File dir = getProjectDirectoryFile(); 157 List <PropertyProvider> providers = new ArrayList (); 158 providers.add(helper.getPropertyProvider("nbproject/private/platform-private.properties")); providers.add(helper.getPropertyProvider("nbproject/platform.properties")); PropertyEvaluator baseEval = PropertyUtils.sequentialPropertyEvaluator(predefs, (PropertyProvider[]) providers.toArray(new PropertyProvider[providers.size()])); 161 providers.add(new Util.UserPropertiesFileProvider(baseEval, dir)); 162 baseEval = PropertyUtils.sequentialPropertyEvaluator(predefs, (PropertyProvider[]) providers.toArray(new PropertyProvider[providers.size()])); 163 class DestDirProvider extends Util.ComputedPropertyProvider { 164 public DestDirProvider(PropertyEvaluator eval) { 165 super(eval); 166 } 167 protected Map <String ,String > getProperties(Map <String ,String > inputPropertyValues) { 168 String platformS = (String ) inputPropertyValues.get("nbplatform.active"); if (platformS != null) { 170 return Collections.singletonMap("netbeans.dest.dir", "${nbplatform." + platformS + ".netbeans.dest.dir}"); } else { 172 return Collections.EMPTY_MAP; 173 } 174 } 175 protected Set inputProperties() { 176 return Collections.singleton("nbplatform.active"); } 178 } 179 providers.add(new DestDirProvider(baseEval)); 180 providers.add(helper.getPropertyProvider(AntProjectHelper.PRIVATE_PROPERTIES_PATH)); 181 providers.add(helper.getPropertyProvider(AntProjectHelper.PROJECT_PROPERTIES_PATH)); 182 Map <String ,String > fixedProps = new HashMap (); 183 fixedProps.put(SuiteProperties.ENABLED_CLUSTERS_PROPERTY, ""); 185 fixedProps.put(SuiteProperties.DISABLED_CLUSTERS_PROPERTY, ""); 186 fixedProps.put(SuiteProperties.DISABLED_MODULES_PROPERTY, ""); 187 fixedProps.put(BrandingSupport.BRANDING_DIR_PROPERTY, "branding"); providers.add(PropertyUtils.fixedPropertyProvider(fixedProps)); 189 return PropertyUtils.sequentialPropertyEvaluator(predefs, (PropertyProvider[]) providers.toArray(new PropertyProvider[providers.size()])); 190 } 191 192 private final class Info implements ProjectInformation, AntProjectListener { 193 194 private final PropertyChangeSupport pcs = new PropertyChangeSupport (this); 195 196 Info() { 197 helper.addAntProjectListener(this); 198 } 199 200 private String getSimpleName() { 201 Element nameEl = Util.findElement(helper.getPrimaryConfigurationData(true), "name", SuiteProjectType.NAMESPACE_SHARED); String text = (nameEl != null) ? Util.findText(nameEl) : null; 203 return (text != null) ? text : "???"; } 205 206 public String getName() { 207 return PropertyUtils.getUsablePropertyName(getSimpleName()); 208 } 209 210 public String getDisplayName() { 211 String appTitle = getEvaluator().getProperty("app.title"); if (appTitle != null) { 213 return appTitle; 214 } else { 215 return getSimpleName(); 216 } 217 } 218 219 public Icon getIcon() { 220 return new ImageIcon (Utilities.loadImage(SUITE_ICON_PATH)); 221 } 222 223 public Project getProject() { 224 return SuiteProject.this; 225 } 226 227 public void addPropertyChangeListener(PropertyChangeListener listener) { 228 pcs.addPropertyChangeListener(listener); 229 } 230 231 public void removePropertyChangeListener(PropertyChangeListener listener) { 232 pcs.removePropertyChangeListener(listener); 233 } 234 235 public void configurationXmlChanged(AntProjectEvent ev) { 236 fireNameChange(); 237 } 238 239 public void propertiesChanged(AntProjectEvent ev) { 240 fireNameChange(); 241 } 242 243 private void fireNameChange() { 244 pcs.firePropertyChange(ProjectInformation.PROP_NAME, null, getName()); 245 pcs.firePropertyChange(ProjectInformation.PROP_DISPLAY_NAME, null, getDisplayName()); 246 } 247 248 } 249 250 public final class OpenedHook extends ProjectOpenedHook { 251 252 OpenedHook() {} 253 254 public void projectOpened() { 255 ProjectManager.mutex().writeAccess(new Mutex.Action() { 257 public Object run() { 258 String path = "nbproject/private/platform-private.properties"; EditableProperties ep = helper.getProperties(path); 260 File buildProperties = new File (System.getProperty("netbeans.user"), "build.properties"); ep.setProperty("user.properties.file", buildProperties.getAbsolutePath()); helper.putProperties(path, ep); 263 try { 264 ProjectManager.getDefault().saveProject(SuiteProject.this); 265 } catch (IOException e) { 266 ErrorManager.getDefault().notify(e); 267 } 268 return null; 269 } 270 }); 271 try { 273 genFilesHelper.refreshBuildScript( 274 GeneratedFilesHelper.BUILD_IMPL_XML_PATH, 275 SuiteProject.class.getResource("resources/build-impl.xsl"), 276 true); 277 genFilesHelper.refreshBuildScript( 278 GeneratedFilesHelper.BUILD_XML_PATH, 279 SuiteProject.class.getResource("resources/build.xsl"), 280 true); 281 } catch (IOException e) { 282 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 283 } 284 } 285 286 protected void projectClosed() { 287 try { 288 ProjectManager.getDefault().saveProject(SuiteProject.this); 289 } catch (IOException e) { 290 Util.err.notify(e); 291 } 292 } 293 294 } 295 296 private final class SavedHook extends ProjectXmlSavedHook { 297 298 SavedHook() {} 299 300 protected void projectXmlSaved() throws IOException { 301 genFilesHelper.refreshBuildScript( 303 GeneratedFilesHelper.BUILD_IMPL_XML_PATH, 304 SuiteProject.class.getResource("resources/build-impl.xsl"), 305 false); 306 genFilesHelper.refreshBuildScript( 307 GeneratedFilesHelper.BUILD_XML_PATH, 308 SuiteProject.class.getResource("resources/build.xsl"), 309 false); 310 } 311 312 } 313 314 private final class SuiteProviderImpl implements SuiteProvider { 315 316 public File getSuiteDirectory() { 317 return getProjectDirectoryFile(); 318 } 319 320 } 321 322 private static final class PrivilegedTemplatesImpl implements PrivilegedTemplates, RecommendedTemplates { 323 324 private static final String [] PRIVILEGED_NAMES = new String [] { 325 "Templates/Ant/Project.xml", "Templates/Other/properties.properties", }; 328 329 private static final String [] RECOMMENDED_TYPES = new String [] { 330 "oasis-XML-catalogs", "XML", "ant-script", "simple-files", }; 335 336 public String [] getPrivilegedTemplates() { 337 return PRIVILEGED_NAMES; 338 } 339 340 public String [] getRecommendedTypes() { 341 return RECOMMENDED_TYPES; 342 } 343 } 344 345 } 346 | Popular Tags |