1 12 package org.eclipse.debug.internal.ui.contexts; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 import java.util.regex.Pattern ; 22 23 import org.eclipse.core.commands.common.NotDefinedException; 24 import org.eclipse.core.commands.contexts.Context; 25 import org.eclipse.core.runtime.IAdaptable; 26 import org.eclipse.core.runtime.IConfigurationElement; 27 import org.eclipse.core.runtime.IExtensionPoint; 28 import org.eclipse.core.runtime.Platform; 29 import org.eclipse.debug.core.DebugPlugin; 30 import org.eclipse.debug.core.ILaunch; 31 import org.eclipse.debug.core.ILaunchesListener2; 32 import org.eclipse.debug.core.model.IDebugElement; 33 import org.eclipse.debug.core.model.IDebugModelProvider; 34 import org.eclipse.debug.core.model.IStackFrame; 35 import org.eclipse.debug.internal.ui.DebugUIPlugin; 36 import org.eclipse.debug.ui.DebugUITools; 37 import org.eclipse.debug.ui.contexts.DebugContextEvent; 38 import org.eclipse.debug.ui.contexts.IDebugContextListener; 39 import org.eclipse.jface.viewers.ISelection; 40 import org.eclipse.jface.viewers.IStructuredSelection; 41 import org.eclipse.ui.PlatformUI; 42 import org.eclipse.ui.activities.ActivityManagerEvent; 43 import org.eclipse.ui.activities.IActivity; 44 import org.eclipse.ui.activities.IActivityManager; 45 import org.eclipse.ui.activities.IActivityManagerListener; 46 import org.eclipse.ui.activities.IActivityPatternBinding; 47 import org.eclipse.ui.activities.IWorkbenchActivitySupport; 48 import org.eclipse.ui.contexts.IContextActivation; 49 import org.eclipse.ui.contexts.IContextService; 50 51 60 public class DebugModelContextBindingManager implements IDebugContextListener, ILaunchesListener2, IActivityManagerListener { 61 62 66 private Map fModelToContextIds = new HashMap (); 67 68 71 private Map fLaunchToModelIds = new HashMap (); 72 73 76 private Map fLanuchToContextActivations = new HashMap (); 77 78 81 private List fModelPatternBindings = new ArrayList (); 82 83 86 private Map fModelToActivities = new HashMap (); 87 88 92 private Set fModelsEnabledForActivities = new HashSet (); 93 94 public static final String ID_DEBUG_MODEL_CONTEXT_BINDINGS= "debugModelContextBindings"; 97 public static final String ATTR_CONTEXT_ID= "contextId"; public static final String ATTR_DEBUG_MODEL_ID= "debugModelId"; 101 public static final String DEBUG_CONTEXT= "org.eclipse.debug.ui.debugging"; 104 private static final String DEBUG_MODEL_ACTIVITY_SUFFIX = "/debugModel"; 107 private static DebugModelContextBindingManager fgManager; 109 110 private static IContextService fgContextService = (IContextService) PlatformUI.getWorkbench().getAdapter(IContextService.class); 111 112 public static DebugModelContextBindingManager getDefault() { 113 if (fgManager == null) { 114 fgManager = new DebugModelContextBindingManager(); 115 } 116 return fgManager; 117 } 118 119 private DebugModelContextBindingManager() { 120 loadDebugModelContextBindings(); 121 loadDebugModelActivityExtensions(); 122 DebugPlugin.getDefault().getLaunchManager().addLaunchListener(this); 123 DebugUITools.getDebugContextManager().addDebugContextListener(this); 124 IWorkbenchActivitySupport activitySupport = PlatformUI.getWorkbench().getActivitySupport(); 125 activitySupport.getActivityManager().addActivityManagerListener(this); 126 } 127 128 132 private void loadDebugModelContextBindings() { 133 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), ID_DEBUG_MODEL_CONTEXT_BINDINGS); 134 IConfigurationElement[] configurationElements = extensionPoint.getConfigurationElements(); 135 for (int i = 0; i < configurationElements.length; i++) { 136 IConfigurationElement element = configurationElements[i]; 137 String modelIdentifier = element.getAttribute(ATTR_DEBUG_MODEL_ID); 138 String contextId = element.getAttribute(ATTR_CONTEXT_ID); 139 if (modelIdentifier != null && contextId != null) { 140 List contextIds = (List ) fModelToContextIds.get(modelIdentifier); 141 if (contextIds == null) { 142 contextIds = new ArrayList (); 143 fModelToContextIds.put(modelIdentifier, contextIds); 144 } 145 contextIds.add(contextId); 146 } 147 } 148 } 149 150 155 private void loadDebugModelActivityExtensions() { 156 IActivityManager activityManager = PlatformUI.getWorkbench().getActivitySupport().getActivityManager(); 157 Set activityIds = activityManager.getDefinedActivityIds(); 158 Iterator activityIterator = activityIds.iterator(); 159 while (activityIterator.hasNext()) { 160 String activityId= (String ) activityIterator.next(); 161 IActivity activity = activityManager.getActivity(activityId); 162 if (activity != null) { 163 Set patternBindings = activity.getActivityPatternBindings(); 164 Iterator patternIterator= patternBindings.iterator(); 165 while (patternIterator.hasNext()) { 166 IActivityPatternBinding patternBinding= (IActivityPatternBinding) patternIterator.next(); 167 String pattern = patternBinding.getPattern().pattern(); 168 if (pattern.endsWith(DEBUG_MODEL_ACTIVITY_SUFFIX)) { 169 fModelPatternBindings.add(patternBinding); 170 } 171 } 172 } 173 } 174 } 175 176 public void debugContextChanged(DebugContextEvent event) { 177 if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) { 178 ISelection selection = event.getContext(); 179 if (selection instanceof IStructuredSelection) { 180 IStructuredSelection ss = (IStructuredSelection) selection; 181 Iterator iterator = ss.iterator(); 182 while (iterator.hasNext()) { 183 activated(iterator.next()); 184 } 185 } 186 } 187 } 188 189 195 private void activated(Object object) { 196 String [] modelIds = getDebugModelIds(object); 197 if (modelIds == null) { 198 return; 199 } 200 ILaunch launch = getLaunch(object); 201 if (launch == null) { 202 return; 203 } 204 Set alreadyEnabled = (Set ) fLaunchToModelIds.get(launch); 205 if (alreadyEnabled == null) { 206 alreadyEnabled = new HashSet (); 207 fLaunchToModelIds.put(launch, alreadyEnabled); 208 } 209 for (int i = 0; i < modelIds.length; i++) { 210 String id = modelIds[i]; 211 if (!alreadyEnabled.contains(id)) { 212 alreadyEnabled.add(id); 213 activateModel(id, launch); 214 } 215 } 216 enableActivitiesFor(modelIds); 217 } 218 219 226 private void activateModel(String modelId, ILaunch launch) { 227 List contextIds = (List ) fModelToContextIds.get(modelId); 228 if (contextIds == null) { 229 contextIds = new ArrayList (); 233 contextIds.add(DEBUG_CONTEXT); 234 fModelToContextIds.put(modelId, contextIds); 235 } 236 Iterator iterator = contextIds.iterator(); 237 while (iterator.hasNext()) { 238 activateContext((String ) iterator.next(), launch); 239 } 240 } 241 242 248 private void activateContext(String contextId, ILaunch launch) { 249 while (contextId != null) { 250 Context context = fgContextService.getContext(contextId); 251 IContextActivation activation = fgContextService.activateContext(contextId); 252 addActivation(launch, activation); 253 try { 254 if (contextId.equals(DEBUG_CONTEXT)) { 255 break; 257 } 258 contextId = context.getParentId(); 259 } catch (NotDefinedException e) { 260 contextId = null; 261 DebugUIPlugin.log(e); 262 } 263 } 264 } 265 266 272 private void addActivation(ILaunch launch, IContextActivation activation) { 273 List activations = (List ) fLanuchToContextActivations.get(launch); 274 if (activations == null) { 275 activations = new ArrayList (); 276 fLanuchToContextActivations.put(launch, activations); 277 } 278 activations.add(activation); 279 } 280 281 288 private String [] getDebugModelIds(Object object) { 289 if (object instanceof IAdaptable) { 290 IDebugModelProvider modelProvider= (IDebugModelProvider)((IAdaptable)object).getAdapter(IDebugModelProvider.class); 291 if (modelProvider != null) { 292 String [] modelIds= modelProvider.getModelIdentifiers(); 293 if (modelIds != null) { 294 return modelIds; 295 } 296 } 297 } 298 if (object instanceof IStackFrame) { 299 return new String [] { ((IStackFrame) object).getModelIdentifier() }; 300 } 301 return null; 302 } 303 304 311 public static ILaunch getLaunch(Object object) { 312 ILaunch launch = null; 313 if (object instanceof IAdaptable) { 314 launch = (ILaunch) ((IAdaptable)object).getAdapter(ILaunch.class); 315 } 316 if (launch == null && object instanceof IDebugElement) { 317 launch = ((IDebugElement) object).getLaunch(); 318 } 319 return launch; 320 } 321 322 325 public void launchesTerminated(ILaunch[] launches) { 326 for (int i = 0; i < launches.length; i++) { 328 ILaunch launch = launches[i]; 329 final List activations = (List ) fLanuchToContextActivations.remove(launch); 330 fLaunchToModelIds.remove(launch); 331 if (activations != null) { 332 Runnable r = new Runnable () { 333 public void run() { 334 Iterator iterator = activations.iterator(); 335 while (iterator.hasNext()) { 336 IContextActivation activation = (IContextActivation) iterator.next(); 337 activation.getContextService().deactivateContext(activation); 338 } 339 } 340 }; 341 DebugUIPlugin.getStandardDisplay().asyncExec(r); 342 } 343 344 } 345 } 347 348 351 public void launchesRemoved(ILaunch[] launches) {} 352 353 356 public void launchesAdded(ILaunch[] launches) { 357 } 358 359 362 public void launchesChanged(ILaunch[] launches) { 363 } 364 365 371 public List getWorkbenchContextsForDebugContext(Object target) { 372 List workbenchContexts = new ArrayList (); 373 String [] modelIds = getDebugModelIds(target); 374 if (modelIds != null) { 375 for (int i = 0; i < modelIds.length; i++) { 376 String modelId = modelIds[i]; 377 List contextIds = (List ) fModelToContextIds.get(modelId); 378 if (contextIds != null) { 379 Iterator contextIterator = contextIds.iterator(); 380 while (contextIterator.hasNext()) { 381 String contextId = (String ) contextIterator.next(); 382 if (!workbenchContexts.contains(contextId)) { 383 workbenchContexts.add(contextId); 384 } 385 } 386 } 387 } 388 } 389 return workbenchContexts; 390 } 391 392 398 private void enableActivitiesFor(String [] modelIds) { 399 Set activities = null; 400 for (int i = 0; i < modelIds.length; i++) { 401 String id = modelIds[i]; 402 if (!fModelsEnabledForActivities.contains(id)) { 403 Set ids= (Set ) fModelToActivities.get(id); 404 if (ids == null) { 405 ids = new HashSet (); 407 fModelToActivities.put(id, ids); 408 Iterator bindings = fModelPatternBindings.iterator(); 409 while (bindings.hasNext()) { 410 IActivityPatternBinding binding = (IActivityPatternBinding) bindings.next(); 411 String regex = binding.getPattern().pattern(); 412 regex = regex.substring(0, regex.length() - DEBUG_MODEL_ACTIVITY_SUFFIX.length()); 413 if (Pattern.matches(regex, id)) { 414 ids.add(binding.getActivityId()); 415 } 416 } 417 } 418 if (!ids.isEmpty()) { 419 if (activities == null) { 420 activities = new HashSet (); 421 } 422 activities.addAll(ids); 423 } 424 fModelsEnabledForActivities.add(id); 425 } 426 } 427 if (activities != null) { 428 IWorkbenchActivitySupport activitySupport = PlatformUI.getWorkbench().getActivitySupport(); 429 Set enabledActivityIds = activitySupport.getActivityManager().getEnabledActivityIds(); 430 if (!enabledActivityIds.containsAll(activities)) { 431 enabledActivityIds = new HashSet (enabledActivityIds); 432 enabledActivityIds.addAll(activities); 433 activitySupport.setEnabledActivityIds(activities); 434 } 435 } 436 } 437 438 441 public void activityManagerChanged(ActivityManagerEvent activityManagerEvent) { 442 if (activityManagerEvent.haveEnabledActivityIdsChanged()) { 443 fModelsEnabledForActivities.clear(); 444 } 445 } 446 447 } 448 | Popular Tags |