1 11 12 package org.eclipse.ui.internal.commands; 13 14 import java.io.IOException ; 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.IConfigurationElement; 20 import org.eclipse.core.runtime.IExtension; 21 import org.eclipse.core.runtime.IExtensionDelta; 22 import org.eclipse.core.runtime.IExtensionRegistry; 23 import org.eclipse.core.runtime.IRegistryChangeEvent; 24 import org.eclipse.core.runtime.IRegistryChangeListener; 25 import org.eclipse.ui.IMemento; 26 import org.eclipse.ui.commands.IHandler; 27 import org.eclipse.ui.internal.util.ConfigurationElementMemento; 28 29 public final class ExtensionCommandRegistry extends AbstractCommandRegistry { 30 31 private List activeKeyConfigurationDefinitions; 32 private List categoryDefinitions; 33 private List commandDefinitions; 34 private IExtensionRegistry extensionRegistry; 35 41 private List handlers; 42 private List keyConfigurationDefinitions; 43 private List keySequenceBindingDefinitions; 44 45 public ExtensionCommandRegistry(IExtensionRegistry extensionRegistry) { 46 if (extensionRegistry == null) 47 throw new NullPointerException (); 48 49 this.extensionRegistry = extensionRegistry; 50 51 this 52 .extensionRegistry 53 .addRegistryChangeListener(new IRegistryChangeListener() { 54 public void registryChanged(IRegistryChangeEvent registryChangeEvent) { 55 IExtensionDelta[] extensionDeltas = 56 registryChangeEvent.getExtensionDeltas( 57 Persistence.PACKAGE_PREFIX, 58 Persistence.PACKAGE_BASE); 59 60 if (extensionDeltas.length != 0) 61 try { 62 load(); 63 } catch (IOException eIO) { 64 } 66 } 67 }); 68 69 try { 70 load(); 71 } catch (IOException eIO) { 72 } 74 } 75 76 private String getNamespace(IConfigurationElement configurationElement) { 77 String namespace = null; 78 79 if (configurationElement != null) { 80 IExtension extension = configurationElement.getDeclaringExtension(); 81 82 if (extension != null) 83 namespace = extension.getNamespace(); 84 } 85 86 return namespace; 87 } 88 89 private void load() throws IOException { 90 if (activeKeyConfigurationDefinitions == null) 91 activeKeyConfigurationDefinitions = new ArrayList (); 92 else 93 activeKeyConfigurationDefinitions.clear(); 94 95 if (categoryDefinitions == null) 96 categoryDefinitions = new ArrayList (); 97 else 98 categoryDefinitions.clear(); 99 100 if (commandDefinitions == null) 101 commandDefinitions = new ArrayList (); 102 else 103 commandDefinitions.clear(); 104 105 if (handlers == null) { 106 handlers = new ArrayList (); 107 } else { 108 handlers.clear(); 109 } 110 111 if (imageBindingDefinitions == null) 112 imageBindingDefinitions = new ArrayList (); 113 else 114 imageBindingDefinitions.clear(); 115 116 if (keyConfigurationDefinitions == null) 117 keyConfigurationDefinitions = new ArrayList (); 118 else 119 keyConfigurationDefinitions.clear(); 120 121 if (keySequenceBindingDefinitions == null) 122 keySequenceBindingDefinitions = new ArrayList (); 123 else 124 keySequenceBindingDefinitions.clear(); 125 126 IConfigurationElement[] deprecatedConfigurationElements = extensionRegistry.getConfigurationElementsFor("org.eclipse.ui.acceleratorConfigurations"); 129 for (int i = 0; i < deprecatedConfigurationElements.length; i++) { 130 IConfigurationElement deprecatedConfigurationElement = 131 deprecatedConfigurationElements[i]; 132 String name = deprecatedConfigurationElement.getName(); 133 134 if ("acceleratorConfiguration".equals(name)) readKeyConfigurationDefinition(deprecatedConfigurationElement); 136 } 137 138 deprecatedConfigurationElements = extensionRegistry.getConfigurationElementsFor("org.eclipse.ui.acceleratorSets"); 140 for (int i = 0; i < deprecatedConfigurationElements.length; i++) { 141 IConfigurationElement deprecatedConfigurationElement = 142 deprecatedConfigurationElements[i]; 143 String name = deprecatedConfigurationElement.getName(); 144 145 if ("acceleratorSet".equals(name)) { IMemento memento = 147 new ConfigurationElementMemento(deprecatedConfigurationElement); 148 String keyConfigurationId = memento.getString("configurationId"); String scopeId = memento.getString("scopeId"); IConfigurationElement[] deprecatedConfigurationElements2 = deprecatedConfigurationElement.getChildren("accelerator"); 152 for (int j = 0; 153 j < deprecatedConfigurationElements2.length; 154 j++) { 155 IConfigurationElement deprecatedConfigurationElement2 = 156 deprecatedConfigurationElements2[j]; 157 KeySequenceBindingDefinition keySequenceBindingDefinition = 158 Persistence.readKeySequenceBindingDefinition( 159 new ConfigurationElementMemento(deprecatedConfigurationElement2), 160 getNamespace(deprecatedConfigurationElement2)); 161 162 if (keySequenceBindingDefinition != null) { 163 keySequenceBindingDefinition = 164 new KeySequenceBindingDefinition( 165 scopeId, 166 keySequenceBindingDefinition.getCommandId(), 167 keyConfigurationId, 168 keySequenceBindingDefinition.getKeySequence(), 169 keySequenceBindingDefinition.getLocale(), 170 keySequenceBindingDefinition.getPlatform(), 171 keySequenceBindingDefinition.getSourceId()); 172 173 keySequenceBindingDefinitions.add( 174 keySequenceBindingDefinition); 175 } 176 } 177 } 178 } 179 180 deprecatedConfigurationElements = extensionRegistry.getConfigurationElementsFor("org.eclipse.ui.actionDefinitions"); 182 for (int i = 0; i < deprecatedConfigurationElements.length; i++) { 183 IConfigurationElement deprecatedConfigurationElement = 184 deprecatedConfigurationElements[i]; 185 String name = deprecatedConfigurationElement.getName(); 186 187 if ("actionDefinition".equals(name)) readCommandDefinition(deprecatedConfigurationElement); 189 } 190 192 IConfigurationElement[] configurationElements = 193 extensionRegistry.getConfigurationElementsFor( 194 Persistence.PACKAGE_FULL); 195 196 for (int i = 0; i < configurationElements.length; i++) { 197 IConfigurationElement configurationElement = configurationElements[i]; 198 String name = configurationElement.getName(); 199 200 if (Persistence.TAG_ACTIVE_KEY_CONFIGURATION.equals(name)) 201 readActiveKeyConfigurationDefinition(configurationElement); 202 else if (Persistence.TAG_CATEGORY.equals(name)) 203 readCategoryDefinition(configurationElement); 204 else if (Persistence.TAG_COMMAND.equals(name)) 205 readCommandDefinition(configurationElement); 206 else if (Persistence.TAG_HANDLER.equals(name)) { 207 readHandlerSubmissionDefinition(configurationElement); 208 } else if (Persistence.TAG_KEY_CONFIGURATION.equals(name)) 209 readKeyConfigurationDefinition(configurationElement); 210 else if (Persistence.TAG_KEY_SEQUENCE_BINDING.equals(name)) 211 readKeySequenceBindingDefinition(configurationElement); 212 } 213 214 boolean commandRegistryChanged = false; 215 216 if (!activeKeyConfigurationDefinitions 217 .equals(super.activeKeyConfigurationDefinitions)) { 218 super.activeKeyConfigurationDefinitions = 219 Collections.unmodifiableList(activeKeyConfigurationDefinitions); 220 commandRegistryChanged = true; 221 } 222 223 if (!categoryDefinitions.equals(super.categoryDefinitions)) { 224 super.categoryDefinitions = 225 Collections.unmodifiableList(categoryDefinitions); 226 commandRegistryChanged = true; 227 } 228 229 if (!commandDefinitions.equals(super.commandDefinitions)) { 230 super.commandDefinitions = 231 Collections.unmodifiableList(commandDefinitions); 232 commandRegistryChanged = true; 233 } 234 235 if (!handlers.equals(super.handlers)) { 236 super.handlers = Collections.unmodifiableList(handlers); 237 commandRegistryChanged = true; 238 } 239 240 if (!imageBindingDefinitions.equals(super.imageBindingDefinitions)) { 241 super.imageBindingDefinitions = 242 Collections.unmodifiableList(imageBindingDefinitions); 243 commandRegistryChanged = true; 244 } 245 246 if (!keyConfigurationDefinitions 247 .equals(super.keyConfigurationDefinitions)) { 248 super.keyConfigurationDefinitions = 249 Collections.unmodifiableList(keyConfigurationDefinitions); 250 commandRegistryChanged = true; 251 } 252 253 if (!keySequenceBindingDefinitions 254 .equals(super.keySequenceBindingDefinitions)) { 255 super.keySequenceBindingDefinitions = 256 Collections.unmodifiableList(keySequenceBindingDefinitions); 257 commandRegistryChanged = true; 258 } 259 260 if (commandRegistryChanged) 261 fireCommandRegistryChanged(); 262 } 263 264 private void readActiveKeyConfigurationDefinition(IConfigurationElement configurationElement) { 265 ActiveKeyConfigurationDefinition activeKeyConfigurationDefinition = 266 Persistence.readActiveKeyConfigurationDefinition( 267 new ConfigurationElementMemento(configurationElement), 268 getNamespace(configurationElement)); 269 270 if (activeKeyConfigurationDefinition != null) 271 activeKeyConfigurationDefinitions.add( 272 activeKeyConfigurationDefinition); 273 } 274 275 private void readCategoryDefinition(IConfigurationElement configurationElement) { 276 CategoryDefinition categoryDefinition = 277 Persistence.readCategoryDefinition( 278 new ConfigurationElementMemento(configurationElement), 279 getNamespace(configurationElement)); 280 281 if (categoryDefinition != null) 282 categoryDefinitions.add(categoryDefinition); 283 } 284 285 private void readCommandDefinition(IConfigurationElement configurationElement) { 286 CommandDefinition commandDefinition = 287 Persistence.readCommandDefinition( 288 new ConfigurationElementMemento(configurationElement), 289 getNamespace(configurationElement)); 290 291 if (commandDefinition != null) 292 commandDefinitions.add(commandDefinition); 293 } 294 295 303 private final void readHandlerSubmissionDefinition(final IConfigurationElement configurationElement) { 304 final IHandler handler = 305 Persistence.readHandlerSubmissionDefinition(configurationElement); 306 307 if (handler != null) 308 handlers.add(handler); 309 } 310 311 private void readKeyConfigurationDefinition(IConfigurationElement configurationElement) { 312 KeyConfigurationDefinition keyConfigurationDefinition = 313 Persistence.readKeyConfigurationDefinition( 314 new ConfigurationElementMemento(configurationElement), 315 getNamespace(configurationElement)); 316 317 if (keyConfigurationDefinition != null) 318 keyConfigurationDefinitions.add(keyConfigurationDefinition); 319 } 320 321 private void readKeySequenceBindingDefinition(IConfigurationElement configurationElement) { 322 KeySequenceBindingDefinition keySequenceBindingDefinition = 323 Persistence.readKeySequenceBindingDefinition( 324 new ConfigurationElementMemento(configurationElement), 325 getNamespace(configurationElement)); 326 327 if (keySequenceBindingDefinition != null) 328 keySequenceBindingDefinitions.add(keySequenceBindingDefinition); 329 } 330 } 331 | Popular Tags |