1 19 20 package org.netbeans.modules.settings.convertors; 21 22 import java.beans.*; 23 import java.util.*; 24 import org.openide.modules.ModuleInfo; 25 import org.openide.util.Lookup; 26 import org.openide.util.LookupListener; 27 import org.openide.util.LookupEvent; 28 29 32 final class ModuleInfoManager { 33 private final static ModuleInfoManager mim = new ModuleInfoManager();; 34 35 36 private HashMap<String , ModuleInfo> modules = null; 37 38 private Lookup.Result<ModuleInfo> modulesResult = null; 39 40 private HashMap<ModuleInfo, PCL> mapOfListeners; 41 42 private ModuleInfoManager() { 43 } 44 45 public final static ModuleInfoManager getDefault() { 46 return mim; 47 } 48 49 53 public ModuleInfo getModule(String codeBaseName) { 54 Collection<? extends ModuleInfo> l = null; 55 if (modules == null) { 56 l = getModulesResult().allInstances(); 57 } 58 synchronized (this) { 59 if (modules == null) fillModules(l); 60 return (ModuleInfo) modules.get(codeBaseName); 61 } 62 } 63 64 private Lookup.Result<ModuleInfo> getModulesResult() { 65 synchronized (this) { 66 if (modulesResult == null) { 67 Lookup lookup = org.netbeans.core.NbTopManager.getModuleLookup(); 68 modulesResult = lookup. 69 lookup(new Lookup.Template<ModuleInfo>(ModuleInfo.class)); 70 modulesResult.addLookupListener(new LookupListener() { 71 public void resultChanged(LookupEvent ev) { 72 Collection<? extends ModuleInfo> l = getModulesResult().allInstances(); 73 XMLSettingsSupport.err.fine("Modules changed: " + l); List reloaded; 75 synchronized (this) { 76 fillModules(l); 77 reloaded = replaceReloadedModules(); 78 XMLSettingsSupport.err.fine("Reloaded modules: " + reloaded); } 80 notifyReloads(reloaded); 81 } 82 }); 83 } 84 return modulesResult; 85 } 86 } 87 88 91 private void notifyReloads(List l) { 92 Iterator it = l.iterator(); 93 while (it.hasNext()) { 94 PCL lsnr = (PCL) it.next(); 95 lsnr.notifyReload(); 96 } 97 } 98 99 102 private void fillModules(Collection<? extends ModuleInfo> l) { 103 HashMap<String , ModuleInfo> m = new HashMap<String , ModuleInfo>((l.size() << 2) / 3 + 1); 104 for (ModuleInfo mi: l) { 105 m.put(mi.getCodeNameBase(), mi); 106 } 107 modules = m; 108 } 109 110 113 private List<PCL> replaceReloadedModules() { 114 if (mapOfListeners == null) return Collections.emptyList(); 115 116 Iterator<ModuleInfo> it = new ArrayList<ModuleInfo>(mapOfListeners.keySet()).iterator(); 117 List<PCL> reloaded = new ArrayList<PCL>(); 118 119 while (it.hasNext()) { 120 ModuleInfo mi = it.next(); 121 ModuleInfo miNew = modules.get(mi.getCodeNameBase()); 122 if (mi != miNew && miNew != null) { 123 PCL lsnr = mapOfListeners.remove(mi); 124 lsnr.setModuleInfo(miNew); 125 reloaded.add(lsnr); 126 mapOfListeners.put(miNew, lsnr); 127 } 128 } 129 130 return reloaded; 131 } 132 133 137 public ModuleInfo getModuleInfo(Class clazz) { 138 Iterator it = getModulesResult().allInstances().iterator(); 139 while (it.hasNext()) { 140 ModuleInfo mi = (ModuleInfo) it.next(); 141 if (mi.owns(clazz)) return mi; 142 } 143 return null; 144 } 145 146 150 public synchronized void registerPropertyChangeListener(SerialDataConvertor sdc, ModuleInfo mi) { 151 if (mapOfListeners == null) { 152 mapOfListeners = new HashMap<ModuleInfo,PCL>(modules.size()); 153 } 154 155 PCL lsnr = mapOfListeners.get(mi); 156 if (lsnr == null) { 157 lsnr = new PCL(mi); 158 mapOfListeners.put(mi, lsnr); 159 } 160 PropertyChangeListener pcl = org.openide.util.WeakListeners.propertyChange(sdc, lsnr); 161 lsnr.addPropertyChangeListener(sdc, pcl); 162 } 163 164 169 public synchronized void unregisterPropertyChangeListener(SerialDataConvertor sdc, ModuleInfo mi) { 170 if (mapOfListeners == null) return; 171 PCL lsnr = (PCL) mapOfListeners.get(mi); 172 if (lsnr != null) { 173 lsnr.removePropertyChangeListener(sdc); 174 } 176 } 177 178 182 public synchronized boolean isReloaded(ModuleInfo mi) { 183 if (mapOfListeners == null) return false; 184 PCL lsnr = (PCL) mapOfListeners.get(mi); 185 return lsnr != null && lsnr.isReloaded(); 186 } 187 188 192 public synchronized boolean isReloaded(String codeBaseName) { 193 if (mapOfListeners == null) return false; 194 return isReloaded(getModule(codeBaseName)); 195 } 196 197 200 private static final class PCL implements PropertyChangeListener { 201 202 private boolean aModuleHasBeenChanged = false; 203 private boolean wasModuleEnabled; 204 private ModuleInfo mi; 205 private PropertyChangeSupport changeSupport; 206 207 private Map<SerialDataConvertor, PropertyChangeListener> origs; 208 209 public PCL(ModuleInfo mi) { 210 this.mi = mi; 211 wasModuleEnabled = mi.isEnabled(); 212 mi.addPropertyChangeListener(this); 213 } 214 215 216 void setModuleInfo(ModuleInfo mi) { 217 this.mi.removePropertyChangeListener(this); 218 aModuleHasBeenChanged = true; 219 this.mi = mi; 220 mi.addPropertyChangeListener(this); 221 } 222 223 224 void notifyReload() { 225 firePropertyChange(); 226 } 227 228 boolean isReloaded() { 229 return aModuleHasBeenChanged; 230 } 231 232 public void propertyChange(java.beans.PropertyChangeEvent evt) { 233 if(ModuleInfo.PROP_ENABLED.equals(evt.getPropertyName())) { 234 235 boolean change; 236 237 if (!Boolean.TRUE.equals (evt.getNewValue ())) { 238 aModuleHasBeenChanged = true; 240 241 change = wasModuleEnabled; 243 } else { 244 change = !wasModuleEnabled; 247 } 248 249 wasModuleEnabled = mi.isEnabled(); 251 252 if (change) { 253 firePropertyChange(); 255 } 256 } 257 } 258 259 260 public void addPropertyChangeListener(SerialDataConvertor sdc, PropertyChangeListener listener) { 261 synchronized (this) { 262 if (changeSupport == null) { 263 changeSupport = new PropertyChangeSupport(this); 264 origs = new WeakHashMap<SerialDataConvertor, PropertyChangeListener>(); 265 } 266 267 PropertyChangeListener old = (PropertyChangeListener) origs.get(sdc); 268 if (old != null) return; 269 origs.put(sdc, listener); 270 } 271 changeSupport.addPropertyChangeListener(listener); 272 } 273 274 public void removePropertyChangeListener(PropertyChangeListener listener) { 275 if (changeSupport != null) { 276 changeSupport.removePropertyChangeListener(listener); 277 } 278 } 279 280 281 public void removePropertyChangeListener(SerialDataConvertor sdc) { 282 synchronized (this) { 283 if (origs == null) return; 284 285 PropertyChangeListener pcl = (PropertyChangeListener) origs.remove(sdc); 286 if (pcl != null) { 287 removePropertyChangeListener(pcl); 288 } 289 } 290 } 291 292 private void firePropertyChange() { 293 if (changeSupport != null) { 294 changeSupport.firePropertyChange(ModuleInfo.PROP_ENABLED, null, null); 295 } 296 } 297 } 298 299 } 300 | Popular Tags |