1 19 20 package org.netbeans.modules.apisupport.project.ui.customizer; 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.Collection ; 28 import java.util.List ; 29 import java.util.Map ; 30 import org.netbeans.api.java.platform.JavaPlatform; 31 import org.netbeans.api.java.platform.JavaPlatformManager; 32 import org.netbeans.api.project.ProjectManager; 33 import org.netbeans.modules.apisupport.project.NbModuleProjectGenerator; 34 import org.netbeans.modules.apisupport.project.Util; 35 import org.netbeans.modules.apisupport.project.universe.NbPlatform; 36 import org.netbeans.spi.project.support.ant.AntProjectHelper; 37 import org.netbeans.spi.project.support.ant.EditableProperties; 38 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 39 import org.openide.DialogDescriptor; 40 import org.openide.DialogDisplayer; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.util.Mutex; 44 import org.openide.util.MutexException; 45 import org.openide.util.NbBundle; 46 import org.openide.util.RequestProcessor; 47 48 53 public abstract class ModuleProperties { 54 55 public static final String PROPERTIES_REFRESHED = "propertiesRefreshed"; 57 static final RequestProcessor RP = new RequestProcessor(ModuleProperties.class.getName()); 58 59 static boolean runFromTests; 61 62 private final AntProjectHelper helper; 64 private final PropertyEvaluator evaluator; 65 66 67 private EditableProperties projectProperties; 68 69 70 private EditableProperties privateProperties; 71 72 private final PropertyChangeSupport changeSupport = new PropertyChangeSupport (this); 73 74 75 ModuleProperties(AntProjectHelper helper, PropertyEvaluator evaluator) { 76 this.helper = helper; 77 this.evaluator = evaluator; 78 reloadProperties(); 79 } 80 81 protected void reloadProperties() { 82 this.projectProperties = helper.getProperties( 83 AntProjectHelper.PROJECT_PROPERTIES_PATH); 84 this.privateProperties = helper.getProperties( 85 AntProjectHelper.PRIVATE_PROPERTIES_PATH); 86 } 87 88 92 abstract Map <String , String > getDefaultValues(); 93 94 AntProjectHelper getHelper() { 95 return helper; 96 } 97 98 PropertyEvaluator getEvaluator() { 99 return evaluator; 100 } 101 102 EditableProperties getProjectProperties() { 103 return projectProperties; 104 } 105 106 EditableProperties getPrivateProperties() { 107 return privateProperties; 108 } 109 110 final String getProperty(String key) { 111 String value = getProjectProperties().getProperty(key); 112 return value != null ? value : (String ) getDefaultValues().get(key); 113 } 114 115 final boolean getBooleanProperty(String key) { 116 String bValue = getProperty(key); 117 return bValue != null && (bValue.equalsIgnoreCase("true") || bValue.equalsIgnoreCase("yes")); } 120 121 final String removeProperty(String key) { 122 return (String ) getProjectProperties().remove(key); 123 } 124 125 final String removePrivateProperty(String key) { 126 return (String ) getPrivateProperties().remove(key); 127 } 128 129 134 final void setProperty(String key, String value) { 135 String def = (String ) getDefaultValues().get(key); 136 if (def == null) { 137 def = ""; } 139 if (value == null || def.equals(value)) { 140 getProjectProperties().remove(key); 141 } else { 142 getProjectProperties().setProperty(key, value); 143 } 144 firePropertyChange(key, null, value); 145 } 146 147 152 final void setPrivateProperty(String key, String value) { 153 String def = (String ) getDefaultValues().get(key); 154 if (def == null) { 155 def = ""; } 157 if (def.equals(value)) { 158 getPrivateProperties().remove(key); 159 } else { 160 getPrivateProperties().setProperty(key, value); 161 } 162 firePropertyChange(key, null, value); 163 } 164 165 void setProperty(String key, String [] value) { 166 getProjectProperties().setProperty(key, value); 167 firePropertyChange(key, null, null); 168 } 169 170 final void setBooleanProperty(String key, boolean bProp) { 171 setProperty(key, Boolean.toString(bProp)); 172 } 173 174 String getProjectDisplayName() { 175 return Util.getDisplayName(getHelper().getProjectDirectory()); 176 } 177 178 final File getProjectDirectoryFile() { 179 return FileUtil.toFile(getHelper().getProjectDirectory()); 180 } 181 182 final String getProjectDirectory() { 183 return getProjectDirectoryFile().getAbsolutePath(); 184 } 185 186 194 void storeProperties() throws IOException { 195 getHelper().putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, 197 getProjectProperties()); 198 getHelper().putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, 200 getPrivateProperties()); 201 } 202 203 206 static void storePlatform(AntProjectHelper helper, NbPlatform platform) { 207 if (platform != null && platform.getID() != null) { 208 EditableProperties props = helper.getProperties( 210 NbModuleProjectGenerator.PLATFORM_PROPERTIES_PATH); 211 props.put("nbplatform.active", platform.getID()); helper.putProperties( 213 NbModuleProjectGenerator.PLATFORM_PROPERTIES_PATH, props); 214 } 215 } 216 217 public void addPropertyChangeListener(PropertyChangeListener pchl) { 218 changeSupport.addPropertyChangeListener(pchl); 219 } 220 221 public void removePropertyChangeListener(PropertyChangeListener pchl) { 222 changeSupport.removePropertyChangeListener(pchl); 223 } 224 225 protected void firePropertyChange(String propName, Object oldValue, Object newValue) { 226 changeSupport.firePropertyChange(propName, oldValue, newValue); 227 } 228 229 protected void firePropertiesRefreshed() { 230 firePropertyChange(PROPERTIES_REFRESHED, null, null); 231 } 232 233 protected static void reportLostPlatform(final NbPlatform lostPlatform) { 234 String plafText = lostPlatform != null 235 ? '"' + lostPlatform.getLabel() + '"' 236 : NbBundle.getMessage(ModuleProperties.class, "MSG_PreviouslySet"); 237 String message = NbBundle.getMessage(ModuleProperties.class, "MSG_PlatformNotFound", plafText); 238 if (!runFromTests) { 239 DialogDisplayer.getDefault().notify(new DialogDescriptor.Message(message)); 240 } else { 241 System.err.println(message); 242 } 243 } 244 245 static String getPlatformID(JavaPlatform platform) { 246 String id = (String ) platform.getProperties().get("platform.ant.name"); return id != null ? id : "default"; } 251 252 static JavaPlatform findJavaPlatformByID(String id) { 253 if (id == null || id.equals("default")) { return JavaPlatform.getDefault(); 255 } 256 JavaPlatform[] platforms = JavaPlatformManager.getDefault().getInstalledPlatforms(); 257 for (int i = 0; i < platforms.length; i++) { 258 if (id.equals(getPlatformID(platforms[i]))) { 259 return platforms[i]; 260 } 261 } 262 return null; 263 } 264 265 static void storeJavaPlatform(AntProjectHelper helper, PropertyEvaluator eval, JavaPlatform platform, boolean isNetBeansOrg) throws IOException { 266 if (isNetBeansOrg) { 267 final boolean isDefault = platform == null || platform == JavaPlatform.getDefault(); 268 final File home = isDefault ? null : getPlatformLocation(platform); 269 if (home != null || isDefault) { 270 final FileObject nbbuild = helper.resolveFileObject(eval.evaluate("${nb_all}/nbbuild")); if (nbbuild != null) { 272 try { 273 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() { 274 public Object run() throws IOException { 275 FileObject userBuildProperties = nbbuild.getFileObject("user.build.properties"); if (userBuildProperties == null) { 277 userBuildProperties = nbbuild.createData("user.build.properties"); } 279 EditableProperties ep = Util.loadProperties(userBuildProperties); 280 if (isDefault) { 281 ep.remove("nbjdk.home"); 283 } else { 284 ep.setProperty("nbjdk.home", home.getAbsolutePath()); 285 } 286 Util.storeProperties(userBuildProperties, ep); 287 return null; 288 } 289 }); 290 } catch (MutexException e) { 291 throw (IOException ) e.getException(); 292 } 293 } 294 } 295 } else { 296 EditableProperties props = helper.getProperties(NbModuleProjectGenerator.PLATFORM_PROPERTIES_PATH); 297 if (platform == null || platform == JavaPlatform.getDefault()) { 298 if (props.containsKey("nbjdk.active")) { props.put("nbjdk.active", "default"); } 302 } else { 303 props.put("nbjdk.active", getPlatformID(platform)); } 305 helper.putProperties(NbModuleProjectGenerator.PLATFORM_PROPERTIES_PATH, props); 306 } 307 } 308 309 private static File getPlatformLocation(JavaPlatform platform) { 310 Collection <FileObject> installs = platform.getInstallFolders(); 311 if (installs.size() == 1) { 312 return FileUtil.toFile((FileObject) installs.iterator().next()); 313 } else { 314 return null; 315 } 316 } 317 318 public static JavaPlatform findJavaPlatformByLocation(String home) { 319 if (home == null) { 320 return JavaPlatform.getDefault(); 321 } 322 JavaPlatform[] platforms = JavaPlatformManager.getDefault().getInstalledPlatforms(); 323 for (int i = 0; i < platforms.length; i++) { 324 if (new File (home).equals(getPlatformLocation(platforms[i]))) { 325 return platforms[i]; 326 } 327 } 328 return null; 329 } 330 331 332 void addLazyStorage(LazyStorage st) { 333 storages.add(st); 334 } 335 336 void triggerLazyStorages() { 337 for (LazyStorage stor : storages) { 338 stor.store(); 339 } 340 } 341 342 private List <LazyStorage> storages = new ArrayList <LazyStorage>(); 343 347 static interface LazyStorage { 348 349 350 void store(); 351 352 } 353 354 } 355 | Popular Tags |