1 11 package org.eclipse.ui.internal.registry; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.eclipse.core.commands.contexts.Context; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IConfigurationElement; 21 import org.eclipse.core.runtime.IExtension; 22 import org.eclipse.core.runtime.IExtensionPoint; 23 import org.eclipse.core.runtime.Platform; 24 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker; 25 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; 26 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 27 import org.eclipse.ui.PlatformUI; 28 import org.eclipse.ui.contexts.IContextService; 29 import org.eclipse.ui.internal.WorkbenchPlugin; 30 31 34 public class ActionSetRegistry implements IExtensionChangeHandler { 35 36 39 private class ActionSetPartAssociation { 40 44 public ActionSetPartAssociation(String partId, String actionSetId) { 45 this.partId = partId; 46 this.actionSetId = actionSetId; 47 } 48 49 50 String partId; 51 String actionSetId; 52 } 53 54 private ArrayList children = new ArrayList (); 55 56 private Map mapPartToActionSetIds = new HashMap (); 57 58 private Map mapPartToActionSets = new HashMap (); 59 60 private IContextService contextService; 61 62 65 public ActionSetRegistry() { 66 contextService = (IContextService) PlatformUI 67 .getWorkbench().getService(IContextService.class); 68 PlatformUI.getWorkbench().getExtensionTracker().registerHandler( 69 this, 70 ExtensionTracker 71 .createExtensionPointFilter(new IExtensionPoint[] { 72 getActionSetExtensionPoint(), 73 getActionSetPartAssociationExtensionPoint() })); 74 readFromRegistry(); 75 } 76 77 83 private IExtensionPoint getActionSetPartAssociationExtensionPoint() { 84 return Platform 85 .getExtensionRegistry().getExtensionPoint( 86 PlatformUI.PLUGIN_ID, 87 IWorkbenchRegistryConstants.PL_ACTION_SET_PART_ASSOCIATIONS); 88 } 89 90 96 private IExtensionPoint getActionSetExtensionPoint() { 97 return Platform 98 .getExtensionRegistry().getExtensionPoint( 99 PlatformUI.PLUGIN_ID, 100 IWorkbenchRegistryConstants.PL_ACTION_SETS); 101 } 102 103 107 private void addActionSet(ActionSetDescriptor desc) { 108 children.add(desc); 109 Context actionSetContext = contextService.getContext(desc.getId()); 110 if (!actionSetContext.isDefined()) { 111 actionSetContext.define(desc.getLabel(), desc.getDescription(), 112 "org.eclipse.ui.contexts.actionSet"); } 114 } 115 116 121 private void removeActionSet(IActionSetDescriptor desc) { 122 Context actionSetContext = contextService.getContext(desc.getId()); 123 if (actionSetContext.isDefined()) { 124 actionSetContext.undefine(); 125 } 126 children.remove(desc); 127 } 128 129 132 private Object addAssociation(String actionSetId, String partId) { 133 ArrayList actionSets = (ArrayList ) mapPartToActionSetIds.get(partId); 135 if (actionSets == null) { 136 actionSets = new ArrayList (); 137 mapPartToActionSetIds.put(partId, actionSets); 138 } 139 actionSets.add(actionSetId); 140 141 ActionSetPartAssociation association = new ActionSetPartAssociation(partId, actionSetId); 142 return association; 143 } 144 145 152 public IActionSetDescriptor findActionSet(String id) { 153 Iterator i = children.iterator(); 154 while (i.hasNext()) { 155 IActionSetDescriptor desc = (IActionSetDescriptor) i.next(); 156 if (desc.getId().equals(id)) { 157 return desc; 158 } 159 } 160 return null; 161 } 162 163 168 public IActionSetDescriptor[] getActionSets() { 169 return (IActionSetDescriptor []) children.toArray(new IActionSetDescriptor [children.size()]); 170 } 171 172 178 public IActionSetDescriptor[] getActionSetsFor(String partId) { 179 ArrayList actionSets = (ArrayList ) mapPartToActionSets.get(partId); 181 if (actionSets != null) { 182 return (IActionSetDescriptor[]) actionSets 183 .toArray(new IActionSetDescriptor[actionSets.size()]); 184 } 185 186 ArrayList actionSetIds = (ArrayList ) mapPartToActionSetIds.get(partId); 188 if (actionSetIds == null) { 189 return new IActionSetDescriptor[0]; 190 } 191 192 actionSets = new ArrayList (actionSetIds.size()); 194 for (Iterator i = actionSetIds.iterator(); i.hasNext();) { 195 String actionSetId = (String ) i.next(); 196 IActionSetDescriptor actionSet = findActionSet(actionSetId); 197 if (actionSet != null) { 198 actionSets.add(actionSet); 199 } else { 200 WorkbenchPlugin.log("Unable to associate action set with part: " + partId + ". Action set " + actionSetId + " not found."); } 203 } 204 205 mapPartToActionSets.put(partId, actionSets); 206 207 return (IActionSetDescriptor[]) actionSets 208 .toArray(new IActionSetDescriptor[actionSets.size()]); 209 } 210 211 214 private void readFromRegistry() { 215 IExtension[] extensions = getActionSetExtensionPoint().getExtensions(); 216 for (int i = 0; i < extensions.length; i++) { 217 addActionSets(PlatformUI.getWorkbench().getExtensionTracker(), 218 extensions[i]); 219 } 220 221 extensions = getActionSetPartAssociationExtensionPoint() 222 .getExtensions(); 223 for (int i = 0; i < extensions.length; i++) { 224 addActionSetPartAssociations(PlatformUI.getWorkbench() 225 .getExtensionTracker(), extensions[i]); 226 } 227 } 228 229 232 public void addExtension(IExtensionTracker tracker, IExtension extension) { 233 String extensionPointUniqueIdentifier = extension.getExtensionPointUniqueIdentifier(); 234 if (extensionPointUniqueIdentifier.equals(getActionSetExtensionPoint().getUniqueIdentifier())) { 235 addActionSets(tracker, extension); 236 } 237 else if (extensionPointUniqueIdentifier.equals(getActionSetPartAssociationExtensionPoint().getUniqueIdentifier())){ 238 addActionSetPartAssociations(tracker, extension); 239 } 240 } 241 242 246 private void addActionSetPartAssociations(IExtensionTracker tracker, IExtension extension) { 247 IConfigurationElement [] elements = extension.getConfigurationElements(); 248 for (int i = 0; i < elements.length; i++) { 249 IConfigurationElement element = elements[i]; 250 if (element.getName().equals(IWorkbenchRegistryConstants.TAG_ACTION_SET_PART_ASSOCIATION)) { 251 String actionSetId = element.getAttribute(IWorkbenchRegistryConstants.ATT_TARGET_ID); 252 IConfigurationElement[] children = element.getChildren(); 253 for (int j = 0; j < children.length; j++) { 254 IConfigurationElement child = children[j]; 255 if (child.getName().equals(IWorkbenchRegistryConstants.TAG_PART)) { 256 String partId = child.getAttribute(IWorkbenchRegistryConstants.ATT_ID); 257 if (partId != null) { 258 Object trackingObject = addAssociation(actionSetId, partId); 259 if (trackingObject != null) { 260 tracker.registerObject(extension, 261 trackingObject, 262 IExtensionTracker.REF_STRONG); 263 264 } 265 266 } 267 } else { 268 WorkbenchPlugin.log("Unable to process element: " + child.getName() + " in action set part associations extension: " + extension.getUniqueIdentifier()); 271 } 272 } 273 } 274 } 275 276 mapPartToActionSets.clear(); 278 } 279 280 284 private void addActionSets(IExtensionTracker tracker, IExtension extension) { 285 IConfigurationElement [] elements = extension.getConfigurationElements(); 286 for (int i = 0; i < elements.length; i++) { 287 IConfigurationElement element = elements[i]; 288 if (element.getName().equals(IWorkbenchRegistryConstants.TAG_ACTION_SET)) { 289 try { 290 ActionSetDescriptor desc = new ActionSetDescriptor(element); 291 addActionSet(desc); 292 tracker.registerObject(extension, desc, IExtensionTracker.REF_WEAK); 293 294 } catch (CoreException e) { 295 WorkbenchPlugin 297 .log( 298 "Unable to create action set descriptor.", e.getStatus()); } 300 } 301 } 302 303 mapPartToActionSets.clear(); 305 } 306 307 310 public void removeExtension(IExtension extension, Object [] objects) { 311 String extensionPointUniqueIdentifier = extension.getExtensionPointUniqueIdentifier(); 312 if (extensionPointUniqueIdentifier.equals(getActionSetExtensionPoint().getUniqueIdentifier())) { 313 removeActionSets(objects); 314 } 315 else if (extensionPointUniqueIdentifier.equals(getActionSetPartAssociationExtensionPoint().getUniqueIdentifier())){ 316 removeActionSetPartAssociations(objects); 317 } 318 } 319 320 323 private void removeActionSetPartAssociations(Object [] objects) { 324 for (int i = 0; i < objects.length; i++) { 325 Object object = objects[i]; 326 if (object instanceof ActionSetPartAssociation) { 327 ActionSetPartAssociation association = (ActionSetPartAssociation) object; 328 String actionSetId = association.actionSetId; 329 ArrayList actionSets = (ArrayList ) mapPartToActionSetIds.get(association.partId); 330 if (actionSets == null) { 331 return; 332 } 333 actionSets.remove(actionSetId); 334 if (actionSets.isEmpty()) { 335 mapPartToActionSetIds.remove(association.partId); 336 } 337 } 338 } 339 mapPartToActionSets.clear(); 341 342 } 343 344 347 private void removeActionSets(Object [] objects) { 348 for (int i = 0; i < objects.length; i++) { 349 Object object = objects[i]; 350 if (object instanceof IActionSetDescriptor) { 351 IActionSetDescriptor desc = (IActionSetDescriptor) object; 352 removeActionSet(desc); 353 354 for (Iterator j = mapPartToActionSetIds.values().iterator(); j 359 .hasNext();) { 360 ArrayList list = (ArrayList ) j.next(); 361 list.remove(desc.getId()); 362 if (list.isEmpty()) { 363 j.remove(); 364 } 365 } 366 } 367 } 368 mapPartToActionSets.clear(); 370 } 371 } 372 | Popular Tags |