1 19 20 package org.netbeans.modules.ruby.railsprojects.ui.customizer; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.util.Comparator ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Map.Entry; 32 import java.util.Properties ; 33 import java.util.Set ; 34 import java.util.TreeMap ; 35 import java.util.Vector ; 36 import javax.swing.ButtonModel ; 37 import javax.swing.ComboBoxModel ; 38 import javax.swing.DefaultListModel ; 39 import javax.swing.JButton ; 40 import javax.swing.JToggleButton ; 41 import javax.swing.ListCellRenderer ; 42 import javax.swing.table.DefaultTableModel ; 43 import javax.swing.text.BadLocationException ; 44 import javax.swing.text.Document ; 45 import org.netbeans.api.project.ProjectManager; 46 import org.netbeans.modules.ruby.railsprojects.RailsProject; 47 import org.netbeans.modules.ruby.railsprojects.RailsProjectUtil; 48 import org.netbeans.modules.ruby.railsprojects.SourceRoots; 49 import org.netbeans.modules.ruby.railsprojects.UpdateHelper; 50 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper; 51 import org.netbeans.modules.ruby.spi.project.support.rake.EditableProperties; 52 import org.netbeans.modules.ruby.spi.project.support.rake.GeneratedFilesHelper; 53 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyEvaluator; 54 import org.netbeans.modules.ruby.spi.project.support.rake.ReferenceHelper; 55 import org.netbeans.modules.ruby.spi.project.support.rake.ui.StoreGroup; 56 import org.openide.DialogDisplayer; 57 import org.openide.ErrorManager; 58 import org.openide.NotifyDescriptor; 59 import org.openide.filesystems.FileObject; 60 import org.openide.filesystems.FileUtil; 61 import org.openide.util.Mutex; 62 import org.openide.util.MutexException; 63 import org.openide.util.NbBundle; 64 import org.openide.util.Utilities; 65 66 69 public class RailsProjectProperties { 70 public static final String Ruby_PROJECT_NAME = "j2se.project.name"; public static final String JAVA_PLATFORM = "platform.active"; 74 public static final String MAIN_CLASS = "main.file"; public static final String JAVAC_COMPILER_ARG = "javac.compilerargs"; public static final String RUN_JVM_ARGS = "run.jvmargs"; public static final String RUN_WORK_DIR = "work.dir"; public static final String DIST_DIR = "dist.dir"; public static final String DIST_JAR = "dist.jar"; public static final String JAVAC_CLASSPATH = "javac.classpath"; public static final String RUN_CLASSPATH = "run.classpath"; public static final String DEBUG_CLASSPATH = "debug.classpath"; public static final String JAR_COMPRESS = "jar.compress"; public static final String JAVAC_SOURCE = "javac.source"; public static final String JAVAC_TARGET = "javac.target"; public static final String JAVAC_TEST_CLASSPATH = "javac.test.classpath"; public static final String RUN_TEST_CLASSPATH = "run.test.classpath"; public static final String BUILD_DIR = "build.dir"; public static final String BUILD_CLASSES_DIR = "build.classes.dir"; public static final String BUILD_TEST_CLASSES_DIR = "build.test.classes.dir"; public static final String BUILD_TEST_RESULTS_DIR = "build.test.results.dir"; public static final String BUILD_CLASSES_EXCLUDES = "build.classes.excludes"; public static final String DIST_JAVADOC_DIR = "dist.javadoc.dir"; public static final String NO_DEPENDENCIES="no.dependencies"; public static final String DEBUG_TEST_CLASSPATH = "debug.test.classpath"; 99 100 public static final String APPLICATION_ARGS = "application.args"; 103 Document JAVAC_COMPILER_ARG_MODEL; 106 107 Map <String ,Map <String ,String >> RUN_CONFIGS; 109 String activeConfig; 110 111 113 private RailsProject project; 115 private HashMap properties; 116 private UpdateHelper updateHelper; 117 private PropertyEvaluator evaluator; 118 private ReferenceHelper refHelper; 119 private GeneratedFilesHelper genFileHelper; 120 121 private StoreGroup privateGroup; 122 private StoreGroup projectGroup; 123 124 RailsProject getProject() { 125 return project; 126 } 127 128 129 public RailsProjectProperties( RailsProject project, UpdateHelper updateHelper, PropertyEvaluator evaluator, ReferenceHelper refHelper, GeneratedFilesHelper genFileHelper ) { 130 this.project = project; 131 this.updateHelper = updateHelper; 132 this.evaluator = evaluator; 133 this.refHelper = refHelper; 134 this.genFileHelper = genFileHelper; 135 137 privateGroup = new StoreGroup(); 138 projectGroup = new StoreGroup(); 139 140 init(); } 142 143 145 private void init() { 146 JAVAC_COMPILER_ARG_MODEL = projectGroup.createStringDocument( evaluator, JAVAC_COMPILER_ARG ); 147 148 RUN_CONFIGS = readRunConfigs(); 150 activeConfig = evaluator.getProperty("config"); 151 152 } 153 154 public void save() { 155 try { 156 Boolean result = (Boolean ) ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() { 158 final FileObject projectDir = updateHelper.getRakeProjectHelper().getProjectDirectory(); 159 public Object run() throws IOException { 160 if ((genFileHelper.getBuildScriptState(GeneratedFilesHelper.BUILD_IMPL_XML_PATH, 161 RailsProject.class.getResource("resources/build-impl.xsl") ) 163 & GeneratedFilesHelper.FLAG_MODIFIED) == GeneratedFilesHelper.FLAG_MODIFIED) { if (showModifiedMessage (NbBundle.getMessage(RailsProjectProperties.class,"TXT_ModifiedTitle"))) { 165 FileObject fo = projectDir.getFileObject(GeneratedFilesHelper.BUILD_IMPL_XML_PATH); 167 if (fo != null) { 168 fo.delete(); 169 } 170 } 171 else { 172 return Boolean.FALSE; 173 } 174 } 175 storeProperties(); 176 return Boolean.TRUE; 177 } 178 }); 179 if (result == Boolean.TRUE) { 181 ProjectManager.getDefault().saveProject(project); 182 } 183 } 184 catch (MutexException e) { 185 ErrorManager.getDefault().notify((IOException )e.getException()); 186 } 187 catch ( IOException ex ) { 188 ErrorManager.getDefault().notify( ex ); 189 } 190 } 191 192 193 194 private void storeProperties() throws IOException { 195 EditableProperties projectProperties = updateHelper.getProperties( RakeProjectHelper.PROJECT_PROPERTIES_PATH ); 197 EditableProperties privateProperties = updateHelper.getProperties( RakeProjectHelper.PRIVATE_PROPERTIES_PATH ); 198 199 projectGroup.store( projectProperties ); 201 privateGroup.store( privateProperties ); 202 203 storeRunConfigs(RUN_CONFIGS, projectProperties, privateProperties); 204 EditableProperties ep = updateHelper.getProperties("nbproject/private/config.properties"); 205 if (activeConfig == null) { 206 ep.remove("config"); 207 } else { 208 ep.setProperty("config", activeConfig); 209 } 210 updateHelper.putProperties("nbproject/private/config.properties", ep); 211 212 updateHelper.putProperties( RakeProjectHelper.PROJECT_PROPERTIES_PATH, projectProperties ); 214 updateHelper.putProperties( RakeProjectHelper.PRIVATE_PROPERTIES_PATH, privateProperties ); 215 216 } 217 218 private static boolean showModifiedMessage (String title) { 219 String message = NbBundle.getMessage(RailsProjectProperties.class,"TXT_Regenerate"); 220 JButton regenerateButton = new JButton (NbBundle.getMessage(RailsProjectProperties.class,"CTL_RegenerateButton")); 221 regenerateButton.setDefaultCapable(true); 222 regenerateButton.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage(RailsProjectProperties.class,"AD_RegenerateButton")); 223 NotifyDescriptor d = new NotifyDescriptor.Message (message, NotifyDescriptor.WARNING_MESSAGE); 224 d.setTitle(title); 225 d.setOptionType(NotifyDescriptor.OK_CANCEL_OPTION); 226 d.setOptions(new Object [] {regenerateButton, NotifyDescriptor.CANCEL_OPTION}); 227 return DialogDisplayer.getDefault().notify(d) == regenerateButton; 228 } 229 230 233 Map <String ,Map <String ,String >> readRunConfigs() { 234 Map <String ,Map <String ,String >> m = new TreeMap <String ,Map <String ,String >>(new Comparator <String >() { 235 public int compare(String s1, String s2) { 236 return s1 != null ? (s2 != null ? s1.compareTo(s2) : 1) : (s2 != null ? -1 : 0); 237 } 238 }); 239 Map <String ,String > def = new TreeMap <String ,String >(); 240 for (String prop : new String [] {MAIN_CLASS, APPLICATION_ARGS, RUN_JVM_ARGS, RUN_WORK_DIR}) { 241 String v = updateHelper.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(prop); 242 if (v == null) { 243 v = updateHelper.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(prop); 244 } 245 if (v != null) { 246 def.put(prop, v); 247 } 248 } 249 m.put(null, def); 250 FileObject configs = project.getProjectDirectory().getFileObject("nbproject/configs"); 251 if (configs != null) { 252 for (FileObject kid : configs.getChildren()) { 253 if (!kid.hasExt("properties")) { 254 continue; 255 } 256 m.put(kid.getName(), new TreeMap <String ,String >(updateHelper.getProperties(FileUtil.getRelativePath(project.getProjectDirectory(), kid)))); 257 } 258 } 259 configs = project.getProjectDirectory().getFileObject("nbproject/private/configs"); 260 if (configs != null) { 261 for (FileObject kid : configs.getChildren()) { 262 if (!kid.hasExt("properties")) { 263 continue; 264 } 265 Map <String ,String > c = m.get(kid.getName()); 266 if (c == null) { 267 continue; 268 } 269 c.putAll(new HashMap <String ,String >(updateHelper.getProperties(FileUtil.getRelativePath(project.getProjectDirectory(), kid)))); 270 } 271 } 272 return m; 274 } 275 276 279 void storeRunConfigs(Map <String ,Map <String ,String >> configs, 280 EditableProperties projectProperties, EditableProperties privateProperties) throws IOException { 281 Map <String ,String > def = configs.get(null); 283 for (String prop : new String [] {MAIN_CLASS, APPLICATION_ARGS, RUN_JVM_ARGS, RUN_WORK_DIR}) { 284 String v = def.get(prop); 285 EditableProperties ep = (prop.equals(APPLICATION_ARGS) || prop.equals(RUN_WORK_DIR)) ? 286 privateProperties : projectProperties; 287 if (!Utilities.compareObjects(v, ep.getProperty(prop))) { 288 if (v != null && v.length() > 0) { 289 ep.setProperty(prop, v); 290 } else { 291 ep.remove(prop); 292 } 293 } 294 } 295 for (Map.Entry <String ,Map <String ,String >> entry : configs.entrySet()) { 296 String config = entry.getKey(); 297 if (config == null) { 298 continue; 299 } 300 String sharedPath = "nbproject/configs/" + config + ".properties"; String privatePath = "nbproject/private/configs/" + config + ".properties"; Map <String ,String > c = entry.getValue(); 303 if (c == null) { 304 updateHelper.putProperties(sharedPath, null); 305 updateHelper.putProperties(privatePath, null); 306 continue; 307 } 308 for (Map.Entry <String ,String > entry2 : c.entrySet()) { 309 String prop = entry2.getKey(); 310 String v = entry2.getValue(); 311 String path = (prop.equals(APPLICATION_ARGS) || prop.equals(RUN_WORK_DIR)) ? 312 privatePath : sharedPath; 313 EditableProperties ep = updateHelper.getProperties(path); 314 if (!Utilities.compareObjects(v, ep.getProperty(prop))) { 315 if (v != null && (v.length() > 0 || (def.get(prop) != null && def.get(prop).length() > 0))) { 316 ep.setProperty(prop, v); 317 } else { 318 ep.remove(prop); 319 } 320 updateHelper.putProperties(path, ep); 321 } 322 } 323 updateHelper.putProperties(sharedPath, updateHelper.getProperties(sharedPath)); 325 } 326 } 327 } 328 | Popular Tags |