| 1 11 package org.eclipse.debug.internal.ui.views.launch; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.ListIterator ; 19 import java.util.Map ; 20 import java.util.Set ; 21 import java.util.StringTokenizer ; 22 import java.util.regex.Pattern ; 23 24 import org.eclipse.core.runtime.IAdaptable; 25 import org.eclipse.core.runtime.IConfigurationElement; 26 import org.eclipse.core.runtime.IExtensionPoint; 27 import org.eclipse.core.runtime.IProgressMonitor; 28 import org.eclipse.core.runtime.IStatus; 29 import org.eclipse.core.runtime.Platform; 30 import org.eclipse.core.runtime.Status; 31 import org.eclipse.debug.core.ILaunch; 32 import org.eclipse.debug.core.model.IDebugElement; 33 import org.eclipse.debug.core.model.IDebugModelProvider; 34 import org.eclipse.debug.core.model.IProcess; 35 import org.eclipse.debug.core.model.IStackFrame; 36 import org.eclipse.debug.internal.ui.DebugUIPlugin; 37 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 38 import org.eclipse.debug.internal.ui.launchConfigurations.PerspectiveManager; 39 import org.eclipse.debug.ui.DebugUITools; 40 import org.eclipse.debug.ui.IDebugUIConstants; 41 import org.eclipse.jface.preference.IPreferenceStore; 42 import org.eclipse.ui.IPerspectiveDescriptor; 43 import org.eclipse.ui.IViewPart; 44 import org.eclipse.ui.IViewReference; 45 import org.eclipse.ui.IWorkbench; 46 import org.eclipse.ui.IWorkbenchPage; 47 import org.eclipse.ui.IWorkbenchPartReference; 48 import org.eclipse.ui.PartInitException; 49 import org.eclipse.ui.PlatformUI; 50 import org.eclipse.ui.activities.ActivityManagerEvent; 51 import org.eclipse.ui.activities.IActivity; 52 import org.eclipse.ui.activities.IActivityManager; 53 import org.eclipse.ui.activities.IActivityManagerListener; 54 import org.eclipse.ui.activities.IActivityPatternBinding; 55 import org.eclipse.ui.activities.IWorkbenchActivitySupport; 56 import org.eclipse.ui.contexts.ContextManagerEvent; 57 import org.eclipse.ui.contexts.EnabledSubmission; 58 import org.eclipse.ui.contexts.IContext; 59 import org.eclipse.ui.contexts.IContextManager; 60 import org.eclipse.ui.contexts.IContextManagerListener; 61 import org.eclipse.ui.contexts.IWorkbenchContextSupport; 62 import org.eclipse.ui.contexts.NotDefinedException; 63 import org.eclipse.ui.progress.UIJob; 64 65 81 public class LaunchViewContextListener implements IContextManagerListener, IActivityManagerListener { 82 83 private static final String DEBUG_MODEL_ACTIVITY_SUFFIX = "/debugModel"; public static final String ID_CONTEXT_VIEW_BINDINGS= "contextViewBindings"; public static final String ID_DEBUG_MODEL_CONTEXT_BINDINGS= "debugModelContextBindings"; public static final String ATTR_CONTEXT_ID= "contextId"; public static final String ATTR_VIEW_ID= "viewId"; public static final String ATTR_DEBUG_MODEL_ID= "debugModelId"; public static final String ATTR_AUTO_OPEN= "autoOpen"; public static final String ATTR_AUTO_CLOSE= "autoClose"; 92 95 private LaunchView launchView; 96 100 private Map modelsToContexts= new HashMap (); 101 102 105 private List modelPatternBindings = new ArrayList (); 106 107 110 private Set enabledActivities; 111 112 116 private Map modelsToActivities= new HashMap (); 117 121 private Map contextViews= new HashMap (); 122 125 private Set managedViewIds= new HashSet (); 126 131 private Set viewIdsToNotOpen= new HashSet (); 132 140 private Map openedViewIds= new HashMap (); 141 142 148 private Map fContextSubmissions= new HashMap (); 149 public static final String DEBUG_CONTEXT= "org.eclipse.debug.ui.debugging"; 157 public static final String PREF_VIEWS_TO_NOT_OPEN= IDebugUIConstants.PLUGIN_ID + ".views_to_not_open"; 166 public static final String PREF_OPENED_VIEWS= IDebugUIConstants.PLUGIN_ID + ".opened_views"; 170 private List lastEnabledIds= new ArrayList (); 171 177 private boolean fIsTrackingPartChanges; 178 182 private List fAutoManagePerspectives= new ArrayList (); 183 184 189 public LaunchViewContextListener(LaunchView view) { 190 launchView= view; 191 loadTrackViews(); 192 loadDebugModelContextExtensions(); 193 loadDebugModelActivityExtensions(); 194 loadContextToViewExtensions(true); 195 loadOpenedViews(); 196 loadViewsToNotOpen(); 197 loadAutoManagePerspectives(); 198 IWorkbench workbench = PlatformUI.getWorkbench(); 199 workbench.getContextSupport().getContextManager().addContextManagerListener(this); 200 IActivityManager activityManager = workbench.getActivitySupport().getActivityManager(); 201 activityManager.addActivityManagerListener(this); 202 enabledActivities = activityManager.getEnabledActivityIds(); 203 } 204 205 209 private void loadContextToViewExtensions(boolean reloadContextMappings) { 210 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), ID_CONTEXT_VIEW_BINDINGS); 211 IConfigurationElement[] configurationElements = extensionPoint.getConfigurationElements(); 212 for (int i = 0; i < configurationElements.length; i++) { 213 IConfigurationElement element = configurationElements[i]; 214 String viewId = getViewId(element); 215 if (reloadContextMappings) { 216 String contextId = element.getAttribute(ATTR_CONTEXT_ID); 217 if (contextId == null || viewId == null) { 218 continue; 219 } 220 List elements= (List ) contextViews.get(contextId); 221 if (elements == null) { 222 elements= new ArrayList (); 223 contextViews.put(contextId, elements); 224 } 225 elements.add(element); 226 } 227 managedViewIds.add(viewId); 228 } 229 } 230 231 241 private void loadDebugModelContextExtensions() { 242 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), ID_DEBUG_MODEL_CONTEXT_BINDINGS); 243 IConfigurationElement[] configurationElements = extensionPoint.getConfigurationElements(); 244 for (int i = 0; i < configurationElements.length; i++) { 245 IConfigurationElement element = configurationElements[i]; 246 String modelIdentifier = element.getAttribute(ATTR_DEBUG_MODEL_ID); 247 String contextId = element.getAttribute(ATTR_CONTEXT_ID); 248 if (modelIdentifier != null && contextId != null) { 249 List contextIds = (List ) modelsToContexts.get(modelIdentifier); 250 if (contextIds == null) { 251 contextIds = new ArrayList (); 252 modelsToContexts.put(modelIdentifier, contextIds); 253 } 254 contextIds.add(contextId); 255 } 256 } 257 } 258 259 264 private void loadDebugModelActivityExtensions() { 265 IActivityManager activityManager = PlatformUI.getWorkbench().getActivitySupport().getActivityManager(); 266 Set activityIds = activityManager.getDefinedActivityIds(); 267 Iterator activityIterator = activityIds.iterator(); 268 while (activityIterator.hasNext()) { 269 String activityId= (String ) activityIterator.next(); 270 IActivity activity = activityManager.getActivity(activityId); 271 if (activity != null) { 272 Set patternBindings = activity.getActivityPatternBindings(); 273 Iterator patternIterator= patternBindings.iterator(); 274 while (patternIterator.hasNext()) { 275 IActivityPatternBinding patternBinding= (IActivityPatternBinding) patternIterator.next(); 276 String pattern = patternBinding.getPattern().pattern(); 277 if (pattern.endsWith(DEBUG_MODEL_ACTIVITY_SUFFIX)) { 278 modelPatternBindings.add(patternBinding); 279 } 280 } 281 } 282 } 283 } 284 285 297 private List getConfigurationElements(String contextId) { 298 List configuredViewIds= new ArrayList (); 301 List allConfigurationElements= new ArrayList (); 302 IContextManager contextManager = PlatformUI.getWorkbench().getContextSupport().getContextManager(); 303 while (contextId != null) { 304 List configurationElements= (List ) contextViews.get(contextId); 305 if (configurationElements != null) { 306 ListIterator iter= configurationElements.listIterator(); 307 while (iter.hasNext()) { 308 IConfigurationElement element= (IConfigurationElement) iter.next(); 313 String viewId = element.getAttribute(ATTR_VIEW_ID); 314 if (viewId != null) { 315 if (!configuredViewIds.contains(viewId)) { 316 allConfigurationElements.add(element); 317 } 318 configuredViewIds.add(viewId); 319 } 320 } 321 } 322 IContext context = contextManager.getContext(contextId); 323 if (context != null) { 324 try { 325 contextId= context.getParentId(); 326 } catch (NotDefinedException e) { 327 contextId= null; 328 } 329 } 330 } 331 return allConfigurationElements; 332 } 333 334 337 private void saveViewsToNotOpen() { 338 saveViewCollection(LaunchViewContextListener.PREF_VIEWS_TO_NOT_OPEN, viewIdsToNotOpen); 339 } 340 341 345 private void saveOpenedViews() { 346 saveViewMap(LaunchViewContextListener.PREF_OPENED_VIEWS, openedViewIds); 347 } 348 349 357 public void saveViewCollection(String attribute, Set collection) { 358 StringBuffer views= new StringBuffer (); 359 Iterator iter= collection.iterator(); 360 while (iter.hasNext()) { 361 views.append((String ) iter.next()).append(','); 362 } 363 if (views.length() > 0) { 364 IPreferenceStore preferenceStore = DebugUITools.getPreferenceStore(); 365 preferenceStore.removePropertyChangeListener(launchView); 366 preferenceStore.setValue(attribute, views.toString()); 367 preferenceStore.addPropertyChangeListener(launchView); 368 } 369 } 370 371 372 389 private void saveViewMap(String attribute, Map map) { 390 StringBuffer views= new StringBuffer (); 391 Iterator iter= map.keySet().iterator(); 392 while (iter.hasNext()) { 393 String perspId = (String ) iter.next(); 394 Set viewIds = (Set )map.get(perspId); 395 views.append("/"); views.append(perspId); 397 if (viewIds != null && !viewIds.isEmpty()) 398 { 399 views.append(":"); Iterator viewsIter = viewIds.iterator(); 401 while (viewsIter.hasNext()) 402 { 403 String viewId = (String )viewsIter.next(); 404 views.append(viewId); 405 views.append(","); } 407 } 408 } 409 if (views.length() > 0) { 410 IPreferenceStore preferenceStore = DebugUITools.getPreferenceStore(); 411 preferenceStore.removePropertyChangeListener(launchView); 412 preferenceStore.setValue(attribute, views.toString()); 413 preferenceStore.addPropertyChangeListener(launchView); 414 } 415 } 416 417 420 public void loadViewsToNotOpen() { 421 loadViewCollection(LaunchViewContextListener.PREF_VIEWS_TO_NOT_OPEN, viewIdsToNotOpen); 422 } 423 424 428 public void loadOpenedViews() { 429 loadViewMap(LaunchViewContextListener.PREF_OPENED_VIEWS, openedViewIds); 430 } 431 432 439 public void loadViewCollection(String attribute, Set collection) { 440 collection.clear(); 441 String views = DebugUITools.getPreferenceStore().getString(attribute); 442 int startIndex= 0; 443 int endIndex= views.indexOf(','); 444 if (endIndex == -1) { 445 endIndex= views.length(); 446 } 447 while (startIndex < views.length() - 1) { 448 String viewId= views.substring(startIndex, endIndex); 449 if (viewId.length() > 0) { 450 collection.add(viewId); 451 } 452 startIndex= endIndex + 1; 453 endIndex= views.indexOf(',', startIndex); 454 } 455 } 456 457 470 private void loadViewMap(String attribute, Map map) { 471 map.clear(); 472 String views = DebugUITools.getPreferenceStore().getString(attribute); 473 474 if (views.startsWith("/")) { 476 String [] viewsStr = views.split("/"); 480 for (int i=0; i<viewsStr.length; i++) 481 { 482 if (viewsStr[i].length() == 0) 483 continue; 484 485 String [] data = viewsStr[i].split(":"); 488 491 if (data.length == 2) 492 { 493 String perspId = data[0]; 494 495 String [] viewIds = data[1].split(","); Set list = new HashSet (); 497 for (int j=0; j<viewIds.length; j++) 498 { 499 list.add(viewIds[j]); 500 } 501 502 openedViewIds.put(perspId, list); 503 } 504 } 505 } 506 } 507 508 511 public void contextManagerChanged(ContextManagerEvent contextManagerEvent) { 512 Set enabled = getNewlyEnabledContexts(contextManagerEvent); 513 Set disabled = getNewlyDisabledContexts(contextManagerEvent); 514 contextEnabled(enabled); 515 contextsDisabled(disabled); 516 } 517 518 private Set getNewlyEnabledContexts(ContextManagerEvent contextManagerEvent) { 519 Set set = new HashSet (contextManagerEvent.getContextManager().getEnabledContextIds()); 520 set.removeAll(contextManagerEvent.getPreviouslyEnabledContextIds()); 521 return set; 522 } 523 524 private Set getNewlyDisabledContexts(ContextManagerEvent contextManagerEvent) { 525 Set set = new HashSet (contextManagerEvent.getPreviouslyEnabledContextIds()); 526 set.removeAll(contextManagerEvent.getContextManager().getEnabledContextIds()); 527 return set; 528 } 529 530 537 public void contextEnabled(final Set contextIds) { 538 if (!isAutoManageViews()) { 539 return; 540 } 541 542 final UIJob openViewsJob = new UIJob("Open Context-Enabled Views") { public IStatus runInUIThread(IProgressMonitor monitor) { 544 IWorkbenchPage page = getPage(); 545 if (page == null) 546 return Status.OK_STATUS; 547 548 if (page.getPerspective() == null) 552 return Status.OK_STATUS; 553 554 contextIds.remove(DEBUG_CONTEXT); 560 if (page == null || contextIds.size() == 0) { 561 return Status.OK_STATUS; 562 } 563 Set viewsToShow = new HashSet (); 564 Set viewsToOpen = new HashSet (); 565 computeViewActivation(contextIds, viewsToOpen, viewsToShow); 566 567 boolean resetTrackingPartChanges = false; 568 if (fIsTrackingPartChanges) { 569 fIsTrackingPartChanges = false; 572 resetTrackingPartChanges = true; 573 } 574 575 Iterator iterator = viewsToOpen.iterator(); 576 577 String id = page.getPerspective().getId(); 578 Set views = (Set ) openedViewIds.get(id); 579 if (views == null) { 580 views = new HashSet (); 581 } 582 583 while (iterator.hasNext()) { 584 String viewId = (String ) iterator.next(); 585 try { 586 IViewPart view = page.showView(viewId, null, 587 IWorkbenchPage.VIEW_CREATE); 588 views.add(viewId); 589 590 viewsToShow.add(view); 591 } catch (PartInitException e) { 592 DebugUIPlugin.log(e.getStatus()); 593 } 594 } 595 596 if (!viewsToOpen.isEmpty()) { 597 openedViewIds.put(id, views); 598 saveOpenedViews(); 599 } 600 iterator = viewsToShow.iterator(); 601 while (iterator.hasNext()) { 602 boolean activate = true; 603 IViewPart view = (IViewPart) iterator.next(); 604 IViewPart[] stackedViews = page.getViewStack(view); 605 if (stackedViews == null) { 606 continue; 607 } 608 for (int i = 0; i < stackedViews.length; i++) { 612 IViewPart stackedView = stackedViews[i]; 613 if (view == stackedView) { 614 break; 615 } else if (viewsToShow.contains(stackedView)) { 616 activate = false; 619 break; 620 } 621 } 622 if (activate) { 623 page.bringToTop(view); 624 } 625 } 626 627 if (resetTrackingPartChanges) 629 loadTrackViews(); 630 631 return Status.OK_STATUS; 632 } 633 }; 634 635 openViewsJob.setSystem(true); 636 637 final PerspectiveManager manager = DebugUIPlugin.getDefault().getPerspectiveManager(); 638 if (isBoundToViews(contextIds)) { 639 manager.schedulePostSwitch(openViewsJob); 640 } 641 } 642 643 648 private boolean isBoundToViews(Set contextIds) 649 { 650 Set possibleViewsToShow = new HashSet (); 651 Iterator iter = contextIds.iterator(); 652 while (iter.hasNext()) 653 { 654 String contextId = (String )iter.next(); 655 Set viewIds = getApplicableViewIds(contextId); 656 possibleViewsToShow.addAll(viewIds); 657 } 658 659 return !possibleViewsToShow.isEmpty(); 660 } 661 662 672 private void computeViewActivation(Set contextIds, Set viewIdsToOpen, Set viewIdsShow) { 673 IWorkbenchPage page = getPage(); 674 if (page == null) { 675 return; 676 } 677 Iterator contexts = contextIds.iterator(); 678 while (contexts.hasNext()) { 679 String contextId = (String ) contexts.next(); 680 Iterator configurationElements= getConfigurationElements(contextId).iterator(); 681 while (configurationElements.hasNext()) { 682 IConfigurationElement element = (IConfigurationElement) configurationElements.next(); 683 String viewId= getViewId(element); 684 if (viewId == null) { 685 continue; 686 } 687 IViewReference reference = page.findViewReference(viewId); 688 if (reference != null && reference
|