1 19 20 package org.netbeans.core.registry; 21 22 import org.netbeans.api.convertor.Convertors; 23 import org.openide.modules.ModuleInfo; 24 import org.openide.util.Lookup; 25 import org.openide.util.LookupEvent; 26 import org.openide.util.LookupListener; 27 import org.openide.util.WeakListeners; 28 29 import java.beans.PropertyChangeEvent ; 30 import java.beans.PropertyChangeListener ; 31 import java.util.*; 32 33 44 final class StateUpdater { 45 46 private Lookup.Result query; 47 private ArrayList enabledModules; 48 49 private static StateUpdater DEFAULT; 50 51 private WeakHashMap list = new WeakHashMap(); 52 private final ModuleInfoLkpListener lookupListener = new ModuleInfoLkpListener(); 53 private final ConvertorsPropertyChangeListener propertyChangeListener = new ConvertorsPropertyChangeListener(); 54 55 private StateUpdater() { 56 init(); 57 } 58 59 static synchronized StateUpdater getDefault() { 60 if (DEFAULT == null) { 61 DEFAULT= new StateUpdater(); 62 } 63 return DEFAULT; 64 } 65 66 private void init() { 67 query = Lookup.getDefault().lookup(new Lookup.Template(ModuleInfo.class)); 68 enabledModules = getModules(); 69 query.addLookupListener((LookupListener)WeakListeners.create(LookupListener.class, lookupListener, query)); 71 Convertors convertors = Convertors.getDefault(); 72 convertors.addPropertyChangeListener(WeakListeners.propertyChange(propertyChangeListener, convertors)); 73 } 74 75 private ArrayList getModules() { 76 Iterator it = query.allInstances().iterator(); 77 ArrayList m = new ArrayList(); 78 while (it.hasNext()) { 79 ModuleInfo mi = (ModuleInfo)it.next(); 80 if (mi.isEnabled()) { 81 m.add(cutOffVersion(mi.getCodeName())); 82 } 83 } 84 return m; 85 } 86 87 boolean isModuleEnabled(String moduleCodeName) { 88 return enabledModules.contains(cutOffVersion(moduleCodeName)); 89 } 90 91 static String cutOffVersion(String moduleCodeName) { 95 int index = moduleCodeName.lastIndexOf('/'); 96 if (index != -1) { 97 moduleCodeName = moduleCodeName.substring(0, index); 98 } 99 return moduleCodeName; 100 } 101 102 103 void registerRootContext(ContextImpl root) { 104 list.put(root, null); 105 } 106 107 private class ModuleInfoLkpListener implements LookupListener { 108 public void resultChanged(LookupEvent ev) { 109 Set roots = list.keySet(); 113 if (roots.size() == 0) { 114 return; 115 } 116 ArrayList modules = getModules(); 117 ArrayList added = new ArrayList(modules); 118 added.removeAll(enabledModules); 119 ArrayList removed = new ArrayList(enabledModules); 120 removed.removeAll(modules); 121 enabledModules = modules; 122 123 Iterator all = roots.iterator(); 124 while (all.hasNext()) { 125 ContextImpl root = (ContextImpl)all.next(); 126 root.getContextCache().modulesChanged(added, removed); 127 } 128 } 129 } 130 131 private class ConvertorsPropertyChangeListener implements PropertyChangeListener { 132 public void propertyChange(PropertyChangeEvent evt) { 133 Set roots = list.keySet(); 134 if (roots.size() == 0) { 135 return; 136 } 137 if (evt.getPropertyName().equals(Convertors.CONVERTOR_DESCRIPTORS)) { 138 Set oldConvertors = (Set)evt.getOldValue(); 139 Set newConvertors = (Set)evt.getNewValue(); 140 assert oldConvertors != null && newConvertors != null; 143 144 ArrayList added = new ArrayList(newConvertors); 145 added.removeAll(oldConvertors); 146 ArrayList removed = new ArrayList(oldConvertors); 147 removed.removeAll(newConvertors); 148 149 Iterator all = roots.iterator(); 150 while (all.hasNext()) { 151 ContextImpl root = (ContextImpl)all.next(); 152 root.getContextCache().modulesChanged(added, removed); 153 } 154 } 155 } 156 } 157 158 163 164 165 } 166 167 | Popular Tags |