1 13 14 package org.netbeans.modules.java.j2seproject; 15 16 import java.beans.PropertyChangeEvent ; 17 import java.beans.PropertyChangeListener ; 18 import java.beans.PropertyChangeSupport ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.text.Collator ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Comparator ; 26 import java.util.HashMap ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Properties ; 30 import java.util.Set ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 33 import org.netbeans.api.project.ProjectManager; 34 import org.netbeans.modules.java.j2seproject.ui.customizer.CustomizerProviderImpl; 35 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SECompositePanelProvider; 36 import org.netbeans.spi.project.ActionProvider; 37 import org.netbeans.spi.project.ProjectConfiguration; 38 import org.netbeans.spi.project.ProjectConfigurationProvider; 39 import org.netbeans.spi.project.support.ant.EditableProperties; 40 import org.openide.filesystems.FileChangeAdapter; 41 import org.openide.filesystems.FileChangeListener; 42 import org.openide.filesystems.FileEvent; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileRenameEvent; 45 import org.openide.filesystems.FileUtil; 46 import org.openide.util.NbBundle; 47 import org.openide.util.Utilities; 48 49 53 final class J2SEConfigurationProvider implements ProjectConfigurationProvider<J2SEConfigurationProvider.Config> { 54 55 private static final Logger LOGGER = Logger.getLogger(J2SEConfigurationProvider.class.getName()); 56 57 60 public static final String PROP_CONFIG = "config"; 64 public static final String CONFIG_PROPS_PATH = "nbproject/private/config.properties"; 66 public static final class Config implements ProjectConfiguration { 67 68 public final String name; 69 private final String displayName; 70 public Config(String name, String displayName) { 71 this.name = name; 72 this.displayName = displayName; 73 } 74 public String getDisplayName() { 75 return displayName; 76 } 77 public int hashCode() { 78 return name != null ? name.hashCode() : 0; 79 } 80 public boolean equals(Object o) { 81 return (o instanceof Config) && Utilities.compareObjects(name, ((Config) o).name); 82 } 83 public String toString() { 84 return "J2SEConfigurationProvider.Config[" + name + "," + displayName + "]"; } 86 } 87 88 private static final Config DEFAULT = new Config(null, 89 NbBundle.getMessage(J2SEConfigurationProvider.class, "J2SEConfigurationProvider.default.label")); 90 91 private final J2SEProject p; 92 private final PropertyChangeSupport pcs = new PropertyChangeSupport (this); 93 private final FileChangeListener fcl = new FileChangeAdapter() { 94 public void fileFolderCreated(FileEvent fe) { 95 update(fe); 96 } 97 public void fileDataCreated(FileEvent fe) { 98 update(fe); 99 } 100 public void fileDeleted(FileEvent fe) { 101 update(fe); 102 } 103 public void fileRenamed(FileRenameEvent fe) { 104 update(fe); 105 } 106 private void update(FileEvent ev) { 107 LOGGER.log(Level.FINEST, "Received {0}", ev); 108 Set <String > oldConfigs = configs != null ? configs.keySet() : Collections.<String >emptySet(); 109 configDir = p.getProjectDirectory().getFileObject("nbproject/configs"); if (configDir != null) { 111 configDir.removeFileChangeListener(fclWeak); 112 configDir.addFileChangeListener(fclWeak); 113 LOGGER.log(Level.FINEST, "(Re-)added listener to {0}", configDir); 114 } else { 115 LOGGER.log(Level.FINEST, "No nbproject/configs exists"); 116 } 117 calculateConfigs(); 118 Set <String > newConfigs = configs.keySet(); 119 if (!oldConfigs.equals(newConfigs)) { 120 LOGGER.log(Level.FINER, "Firing " + ProjectConfigurationProvider.PROP_CONFIGURATIONS + ": {0} -> {1}", new Object [] {oldConfigs, newConfigs}); 121 pcs.firePropertyChange(ProjectConfigurationProvider.PROP_CONFIGURATIONS, null, null); 122 } 124 } 125 }; 126 private final FileChangeListener fclWeak; 127 private FileObject configDir; 128 private Map <String ,Config> configs; 129 private FileObject nbp; 130 131 public J2SEConfigurationProvider(J2SEProject p) { 132 this.p = p; 133 fclWeak = FileUtil.weakFileChangeListener(fcl, null); 134 nbp = p.getProjectDirectory().getFileObject("nbproject"); if (nbp != null) { 136 nbp.addFileChangeListener(fclWeak); 137 LOGGER.log(Level.FINEST, "Added listener to {0}", nbp); 138 configDir = nbp.getFileObject("configs"); if (configDir != null) { 140 configDir.addFileChangeListener(fclWeak); 141 LOGGER.log(Level.FINEST, "Added listener to {0}", configDir); 142 } 143 } 144 p.evaluator().addPropertyChangeListener(new PropertyChangeListener () { 145 public void propertyChange(PropertyChangeEvent evt) { 146 if (PROP_CONFIG.equals(evt.getPropertyName())) { 147 LOGGER.log(Level.FINER, "Refiring " + PROP_CONFIG + " -> " + ProjectConfigurationProvider.PROP_CONFIGURATION_ACTIVE); 148 pcs.firePropertyChange(ProjectConfigurationProvider.PROP_CONFIGURATION_ACTIVE, null, null); 149 } 150 } 151 }); 152 } 153 154 private void calculateConfigs() { 155 configs = new HashMap <String ,Config>(); 156 if (configDir != null) { 157 for (FileObject kid : configDir.getChildren()) { 158 if (!kid.hasExt("properties")) { 159 continue; 160 } 161 try { 162 InputStream is = kid.getInputStream(); 163 try { 164 Properties p = new Properties (); 165 p.load(is); 166 String name = kid.getName(); 167 String label = p.getProperty("$label"); configs.put(name, new Config(name, label != null ? label : name)); 169 } finally { 170 is.close(); 171 } 172 } catch (IOException x) { 173 LOGGER.log(Level.INFO, null, x); 174 } 175 } 176 } 177 LOGGER.log(Level.FINEST, "Calculated configurations: {0}", configs); 178 } 179 180 public Collection <Config> getConfigurations() { 181 calculateConfigs(); 182 List <Config> l = new ArrayList <Config>(); 183 l.addAll(configs.values()); 184 Collections.sort(l, new Comparator <Config>() { 185 Collator c = Collator.getInstance(); 186 public int compare(Config c1, Config c2) { 187 return c.compare(c1.getDisplayName(), c2.getDisplayName()); 188 } 189 }); 190 l.add(0, DEFAULT); 191 return l; 192 } 193 194 public Config getActiveConfiguration() { 195 calculateConfigs(); 196 String config = p.evaluator().getProperty(PROP_CONFIG); 197 if (config != null && configs.containsKey(config)) { 198 return configs.get(config); 199 } else { 200 return DEFAULT; 201 } 202 } 203 204 public void setActiveConfiguration(Config c) throws IllegalArgumentException , IOException { 205 if (c != DEFAULT && !configs.values().contains(c)) { 206 throw new IllegalArgumentException (); 207 } 208 final String n = c.name; 209 EditableProperties ep = p.getUpdateHelper().getProperties(CONFIG_PROPS_PATH); 210 if (Utilities.compareObjects(n, ep.getProperty(PROP_CONFIG))) { 211 return; 212 } 213 if (n != null) { 214 ep.setProperty(PROP_CONFIG, n); 215 } else { 216 ep.remove(PROP_CONFIG); 217 } 218 p.getUpdateHelper().putProperties(CONFIG_PROPS_PATH, ep); 219 pcs.firePropertyChange(ProjectConfigurationProvider.PROP_CONFIGURATION_ACTIVE, null, null); 220 ProjectManager.getDefault().saveProject(p); 221 assert p.getProjectDirectory().getFileObject(CONFIG_PROPS_PATH) != null; 222 } 223 224 public boolean hasCustomizer() { 225 return true; 226 } 227 228 public void customize() { 229 p.getLookup().lookup(CustomizerProviderImpl.class).showCustomizer(J2SECompositePanelProvider.RUN); 230 } 231 232 public boolean configurationsAffectAction(String command) { 233 return command.equals(ActionProvider.COMMAND_RUN) || 234 command.equals(ActionProvider.COMMAND_DEBUG); 235 } 236 237 public void addPropertyChangeListener(PropertyChangeListener lst) { 238 pcs.addPropertyChangeListener(lst); 239 } 240 241 public void removePropertyChangeListener(PropertyChangeListener lst) { 242 pcs.removePropertyChangeListener(lst); 243 } 244 245 } 246 | Popular Tags |