KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > launch > LaunchViewContextListener


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.views.launch;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.ListIterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22 import java.util.regex.Pattern JavaDoc;
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 /**
66  * A context listener which automatically opens/closes/activates views in
67  * response to debug context changes and automatically enables activities
68  * as appropriate.
69  *
70  * The context listener updates for selection changes in the LaunchView,
71  * enabling/disabling contexts and enabling activities based on the
72  * org.eclipse.debug.ui.debugModelContextBindings and
73  * org.eclipse.ui.activities extension points.
74  *
75  * Activity pattern bindings with patterns of the form:
76  * <debug model identifier>/debugModel
77  * are treated as bindings between a debug model and an activity. When
78  * an element with the specified debug model identifier is selected,
79  * the specified activity will be enabled.
80  */

81 public class LaunchViewContextListener implements IContextManagerListener, IActivityManagerListener {
82
83     private static final String JavaDoc DEBUG_MODEL_ACTIVITY_SUFFIX = "/debugModel"; //$NON-NLS-1$
84
public static final String JavaDoc ID_CONTEXT_VIEW_BINDINGS= "contextViewBindings"; //$NON-NLS-1$
85
public static final String JavaDoc ID_DEBUG_MODEL_CONTEXT_BINDINGS= "debugModelContextBindings"; //$NON-NLS-1$
86
public static final String JavaDoc ATTR_CONTEXT_ID= "contextId"; //$NON-NLS-1$
87
public static final String JavaDoc ATTR_VIEW_ID= "viewId"; //$NON-NLS-1$
88
public static final String JavaDoc ATTR_DEBUG_MODEL_ID= "debugModelId"; //$NON-NLS-1$
89
public static final String JavaDoc ATTR_AUTO_OPEN= "autoOpen"; //$NON-NLS-1$
90
public static final String JavaDoc ATTR_AUTO_CLOSE= "autoClose"; //$NON-NLS-1$
91

92     /**
93      * The launch view that this context listener works for
94      */

95     private LaunchView launchView;
96     /**
97      * A mapping of debug models IDs (String) to a collection
98      * of context IDs (List<String>).
99      */

100     private Map JavaDoc modelsToContexts= new HashMap JavaDoc();
101     
102     /**
103      * A cache of activity pattern bindings for debug models.
104      */

105     private List JavaDoc modelPatternBindings = new ArrayList JavaDoc();
106     
107     /**
108      * Currently enabled activities
109      */

110     private Set JavaDoc enabledActivities;
111     
112     /**
113      * A mapping of debug model IDs (String) to a collection
114      * of resolved activity IDs (Set<String>).
115      */

116     private Map JavaDoc modelsToActivities= new HashMap JavaDoc();
117     /**
118      * A mapping of context IDs (Strings) to a collection
119      * of context-view bindings (IConfigurationElements).
120      */

121     private Map JavaDoc contextViews= new HashMap JavaDoc();
122     /**
123      * Collection of all views that might be opened or closed automatically.
124      */

125     private Set JavaDoc managedViewIds= new HashSet JavaDoc();
126     /**
127      * Collection of views which have been manually closed by the
128      * user. Views which are in this collection should not be
129      * automatically opened.
130      */

131     private Set JavaDoc viewIdsToNotOpen= new HashSet JavaDoc();
132     /**
133      * Map of views which have been automatically opened.
134      * Only views which are in this collection should be automatically
135      * closed.
136      *
137      * Key: perspective id
138      * Value: HashSet of view ids open in a perspective
139      */

140     private Map JavaDoc openedViewIds= new HashMap JavaDoc();
141     
142     /**
143      * Map of ILaunch objects to the List of EnabledSubmissions that were
144      * submitted for them.
145      * Key: ILaunch
146      * Value: List <EnabledSubmission>
147      */

148     private Map JavaDoc fContextSubmissions= new HashMap JavaDoc();
149     public static final String JavaDoc DEBUG_CONTEXT= "org.eclipse.debug.ui.debugging"; //$NON-NLS-1$
150
/**
151      * String preference specifying which views should not be
152      * automatically opened by the launch view.
153      * The value is a comma-separated list of view identifiers.
154      *
155      * @since 3.0
156      */

157     public static final String JavaDoc PREF_VIEWS_TO_NOT_OPEN= IDebugUIConstants.PLUGIN_ID + ".views_to_not_open"; //$NON-NLS-1$
158
/**
159      * String preference specifying which views have been
160      * automatically opened by the launch view. Only views which
161      * have been automatically opened will be automatically closed.
162      * The value is a comma-separated list of view identifiers.
163      *
164      * @since 3.0
165      */

166     public static final String JavaDoc PREF_OPENED_VIEWS= IDebugUIConstants.PLUGIN_ID + ".opened_views"; //$NON-NLS-1$
167
/**
168      * The collection of context ids which were most recently enabled.
169      */

170     private List JavaDoc lastEnabledIds= new ArrayList JavaDoc();
171     /**
172      * Boolean flag controlling whether or not this listener is
173      * tracking part changes. This is necessary since this class
174      * doesn't implement its own listener, but it informed of
175      * perspective change events by the LaunchView.
176      */

177     private boolean fIsTrackingPartChanges;
178     /**
179      * Collection of perspectives in which views should be
180      * automatically opened and closed.
181      */

182     private List JavaDoc fAutoManagePerspectives= new ArrayList JavaDoc();
183     
184     /**
185      * Creates a fully initialized context listener.
186      *
187      * @param view a fully initialized launch view
188      */

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     /**
206      * Loads extensions which map context ids to views. This information
207      * is used to open the appropriate views when a context is activated.
208      */

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 JavaDoc viewId = getViewId(element);
215             if (reloadContextMappings) {
216                 String JavaDoc contextId = element.getAttribute(ATTR_CONTEXT_ID);
217                 if (contextId == null || viewId == null) {
218                     continue;
219                 }
220                 List JavaDoc elements= (List JavaDoc) contextViews.get(contextId);
221                 if (elements == null) {
222                     elements= new ArrayList JavaDoc();
223                     contextViews.put(contextId, elements);
224                 }
225                 elements.add(element);
226             }
227             managedViewIds.add(viewId);
228         }
229     }
230     
231     /**
232      * Loads the extensions which map debug model identifiers
233      * to context ids. This information is used to activate the
234      * appropriate context when a debug element is selected.
235      *
236      * When a context associated with a debug model is enabled, we
237      * also activate all parent contexts. Since the context manager
238      * does not do this automatically, we cache all parent context
239      * identifiers in the modelToContexts map as well
240      */

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 JavaDoc modelIdentifier = element.getAttribute(ATTR_DEBUG_MODEL_ID);
247             String JavaDoc contextId = element.getAttribute(ATTR_CONTEXT_ID);
248             if (modelIdentifier != null && contextId != null) {
249                 List JavaDoc contextIds = (List JavaDoc) modelsToContexts.get(modelIdentifier);
250                 if (contextIds == null) {
251                     contextIds = new ArrayList JavaDoc();
252                     modelsToContexts.put(modelIdentifier, contextIds);
253                 }
254                 contextIds.add(contextId);
255             }
256         }
257     }
258     
259     /**
260      * Loads the extensions which map debug model patterns
261      * to activity ids. This information is used to activate the
262      * appropriate activities when a debug element is selected.
263      */

264     private void loadDebugModelActivityExtensions() {
265         IActivityManager activityManager = PlatformUI.getWorkbench().getActivitySupport().getActivityManager();
266         Set JavaDoc activityIds = activityManager.getDefinedActivityIds();
267         Iterator JavaDoc activityIterator = activityIds.iterator();
268         while (activityIterator.hasNext()) {
269             String JavaDoc activityId= (String JavaDoc) activityIterator.next();
270             IActivity activity = activityManager.getActivity(activityId);
271             if (activity != null) {
272                 Set JavaDoc patternBindings = activity.getActivityPatternBindings();
273                 Iterator JavaDoc patternIterator= patternBindings.iterator();
274                 while (patternIterator.hasNext()) {
275                     IActivityPatternBinding patternBinding= (IActivityPatternBinding) patternIterator.next();
276                     String JavaDoc pattern = patternBinding.getPattern().pattern();
277                     if (pattern.endsWith(DEBUG_MODEL_ACTIVITY_SUFFIX)) {
278                         modelPatternBindings.add(patternBinding);
279                     }
280                 }
281             }
282         }
283     }
284     
285     /**
286      * Lists the contextViews configuration elements for the
287      * given context ID and all its parent context IDs. The
288      * list only contains one configuration element per view
289      * such that if a child context provides a binding for a view
290      * it will override any bindings provided for that same view by
291      * parent contexts.
292      *
293      * @param contextId the context ID
294      * @return the configuration elements for the given context ID and
295      * all parent context IDs.
296      */

297     private List JavaDoc getConfigurationElements(String JavaDoc contextId) {
298         // Collection of view ids for which configuration
299
// elements have been found.
300
List JavaDoc configuredViewIds= new ArrayList JavaDoc();
301         List JavaDoc allConfigurationElements= new ArrayList JavaDoc();
302         IContextManager contextManager = PlatformUI.getWorkbench().getContextSupport().getContextManager();
303         while (contextId != null) {
304             List JavaDoc configurationElements= (List JavaDoc) contextViews.get(contextId);
305             if (configurationElements != null) {
306                 ListIterator JavaDoc iter= configurationElements.listIterator();
307                 while (iter.hasNext()) {
308                     // Only add the element if configuredViewIds do not already
309
// contain viewId.
310
// This allows child contexts to override parent
311
// bindings.
312
IConfigurationElement element= (IConfigurationElement) iter.next();
313                     String JavaDoc 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     /**
335      * Persist the collection of views to not automatically open.
336      */

337     private void saveViewsToNotOpen() {
338         saveViewCollection(LaunchViewContextListener.PREF_VIEWS_TO_NOT_OPEN, viewIdsToNotOpen);
339     }
340     
341     /**
342      * Persist the collection of views which have been automatically
343      * opened.
344      */

345     private void saveOpenedViews() {
346         saveViewMap(LaunchViewContextListener.PREF_OPENED_VIEWS, openedViewIds);
347     }
348
349     /**
350      * Persist the view identifiers that the user has manually
351      * opened/closed so that we continue to not automatically
352      * open/close them.
353      * @param attribute the preference key in which to store the
354      * view id collection
355      * @param collection the view identifier collection
356      */

357     public void saveViewCollection(String JavaDoc attribute, Set JavaDoc collection) {
358         StringBuffer JavaDoc views= new StringBuffer JavaDoc();
359         Iterator JavaDoc iter= collection.iterator();
360         while (iter.hasNext()) {
361             views.append((String JavaDoc) 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     /**
373      * Persist the view identifiers that the user has manually
374      * opened/closed so that we continue to not automatically
375      * open/close them.
376      *
377      * key: perspective id
378      * value: ArrayList of view ids
379      *
380      * The map is saved in the following format:
381      * key1:arrayElm(0),arrayElm(1)/key2:arrayElm(0),arrayElm(1)
382      * Perspective settings are delimited by "/".
383      * view Ids are delimited by ","
384      *
385      * @param attribute attribute the preference key in which to store the
386      * view id map
387      * @param map that maps a persective to a list of view ids
388      */

389     private void saveViewMap(String JavaDoc attribute, Map JavaDoc map) {
390         StringBuffer JavaDoc views= new StringBuffer JavaDoc();
391         Iterator JavaDoc iter= map.keySet().iterator();
392         while (iter.hasNext()) {
393             String JavaDoc perspId = (String JavaDoc) iter.next();
394             Set JavaDoc viewIds = (Set JavaDoc)map.get(perspId);
395             views.append("/"); //$NON-NLS-1$
396
views.append(perspId);
397             if (viewIds != null && !viewIds.isEmpty())
398             {
399                 views.append(":"); //$NON-NLS-1$
400
Iterator JavaDoc viewsIter = viewIds.iterator();
401                 while (viewsIter.hasNext())
402                 {
403                     String JavaDoc viewId = (String JavaDoc)viewsIter.next();
404                     views.append(viewId);
405                     views.append(","); //$NON-NLS-1$
406
}
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     /**
418      * Load the collection of views to not open.
419      */

420     public void loadViewsToNotOpen() {
421         loadViewCollection(LaunchViewContextListener.PREF_VIEWS_TO_NOT_OPEN, viewIdsToNotOpen);
422     }
423     
424     /**
425      * Load the collection of views that have been automatically
426      * opened.
427      */

428     public void loadOpenedViews() {
429         loadViewMap(LaunchViewContextListener.PREF_OPENED_VIEWS, openedViewIds);
430     }
431     
432     /**
433      * Loads a collection of view ids from the preferences keyed to
434      * the given attribute, and stores them in the given collection
435      *
436      * @param attribute the attribute of the view ids
437      * @param collection the collection to store the view ids into.
438      */

439     public void loadViewCollection(String JavaDoc attribute, Set JavaDoc collection) {
440         collection.clear();
441         String JavaDoc 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 JavaDoc 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     /**
458      * Loads a map of view ids from the preferences keyed to
459      * the given attribute, and stores them in the given map
460      *
461      * The map is saved in the following format:
462      * key1:arrayElm(0),arrayElm(1)/key2:arrayElm(0)+arrayElm(1)
463      * Perspective settings are delimited by "/".
464      * Key is the perspective id
465      * ArrayElm's are the view Ids and are delimited by ","
466      *
467      * @param attribute the attribute of the view ids
468      * @param map the map to store the view ids into.
469      */

470     private void loadViewMap(String JavaDoc attribute, Map JavaDoc map) {
471         map.clear();
472         String JavaDoc views = DebugUITools.getPreferenceStore().getString(attribute);
473         
474         if (views.startsWith("/")) //$NON-NLS-1$
475
{
476             // list of views ids in this format:
477
// perspective1Id:viewIdA,viewIdB/persective2Id:viewIda,viewIdB
478
String JavaDoc[] viewsStr = views.split("/"); //$NON-NLS-1$
479

480             for (int i=0; i<viewsStr.length; i++)
481             {
482                 if (viewsStr[i].length() == 0)
483                     continue;
484                 
485                 // split perpectiveId and viewId
486
String JavaDoc[] data = viewsStr[i].split(":"); //$NON-NLS-1$
487

488                 // data[0] = perspective
489
// data[1] = list of view ids delimited by ","
490

491                 if (data.length == 2)
492                 {
493                     String JavaDoc perspId = data[0];
494                     
495                     String JavaDoc[] viewIds = data[1].split(","); //$NON-NLS-1$
496
Set JavaDoc list = new HashSet JavaDoc();
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     /* (non-Javadoc)
509      * @see org.eclipse.ui.contexts.IContextManagerListener#contextManagerChanged(org.eclipse.ui.contexts.ContextManagerEvent)
510      */

511     public void contextManagerChanged(ContextManagerEvent contextManagerEvent) {
512         Set JavaDoc enabled = getNewlyEnabledContexts(contextManagerEvent);
513         Set JavaDoc disabled = getNewlyDisabledContexts(contextManagerEvent);
514         contextEnabled(enabled);
515         contextsDisabled(disabled);
516     }
517     
518     private Set JavaDoc getNewlyEnabledContexts(ContextManagerEvent contextManagerEvent) {
519         Set JavaDoc set = new HashSet JavaDoc(contextManagerEvent.getContextManager().getEnabledContextIds());
520         set.removeAll(contextManagerEvent.getPreviouslyEnabledContextIds());
521         return set;
522     }
523
524     private Set JavaDoc getNewlyDisabledContexts(ContextManagerEvent contextManagerEvent) {
525         Set JavaDoc set = new HashSet JavaDoc(contextManagerEvent.getPreviouslyEnabledContextIds());
526         set.removeAll(contextManagerEvent.getContextManager().getEnabledContextIds());
527         return set;
528     }
529
530     /**
531      * The context with the given ID has been enabled.
532      * Activate the appropriate views.
533      *
534      * @param contextId the ID of the context that has been
535      * enabled
536      */

537     public void contextEnabled(final Set JavaDoc contextIds) {
538         if (!isAutoManageViews()) {
539             return;
540         }
541         
542         final UIJob openViewsJob = new UIJob("Open Context-Enabled Views") { //$NON-NLS-1$
543
public IStatus runInUIThread(IProgressMonitor monitor) {
544                 IWorkbenchPage page = getPage();
545                 if (page == null)
546                     return Status.OK_STATUS;
547                 
548                 // Since we run this job asynchronously on the UI Thread,
549
// #getPerspective() may return null when this job is run
550
// when the workbench is shutting down.
551
if (page.getPerspective() == null)
552                     return Status.OK_STATUS;
553                 
554                 // We ignore the "Debugging" context since we use it
555
// to provide a base set of views for other context
556
// bindings to inherit. If we don't ignore it, we'll
557
// end up opening those views whenever a debug session
558
// starts, which is not the desired behavior.
559
contextIds.remove(DEBUG_CONTEXT);
560                 if (page == null || contextIds.size() == 0) {
561                     return Status.OK_STATUS;
562                 }
563                 Set JavaDoc viewsToShow = new HashSet JavaDoc();
564                 Set JavaDoc viewsToOpen = new HashSet JavaDoc();
565                 computeViewActivation(contextIds, viewsToOpen, viewsToShow);
566
567                 boolean resetTrackingPartChanges = false;
568                 if (fIsTrackingPartChanges) {
569                     // only change the flag if it is currently
570
// tracking part changes.
571
fIsTrackingPartChanges = false;
572                     resetTrackingPartChanges = true;
573                 }
574
575                 Iterator JavaDoc iterator = viewsToOpen.iterator();
576
577                 String JavaDoc id = page.getPerspective().getId();
578                 Set JavaDoc views = (Set JavaDoc) openedViewIds.get(id);
579                 if (views == null) {
580                     views = new HashSet JavaDoc();
581                 }
582
583                 while (iterator.hasNext()) {
584                     String JavaDoc viewId = (String JavaDoc) 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 each applicable view, iterate through the view stack.
609
// If we find that view before any other applicable views,
610
// show it. Otherwise, don't.
611
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                             // If this view is below an appropriate view, don't
617
// show it
618
activate = false;
619                             break;
620                         }
621                     }
622                     if (activate) {
623                         page.bringToTop(view);
624                     }
625                 }
626
627                 // Reset if we have previously changed this setting
628
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     /**
644      * @param contextIds
645      * @return if the specified the context ids are tied
646      * to any context-enabled views.
647      */

648     private boolean isBoundToViews(Set JavaDoc contextIds)
649     {
650         Set JavaDoc possibleViewsToShow = new HashSet JavaDoc();
651         Iterator JavaDoc iter = contextIds.iterator();
652         while (iter.hasNext())
653         {
654             String JavaDoc contextId = (String JavaDoc)iter.next();
655             Set JavaDoc viewIds = getApplicableViewIds(contextId);
656             possibleViewsToShow.addAll(viewIds);
657         }
658         
659         return !possibleViewsToShow.isEmpty();
660     }
661     
662     /**
663      * Compute which views should be automatically opened and which should be
664      * automatically brought to top when the given contexts are enabled.
665      *
666      * @param contextIds the contexts that have been enabled
667      * @param viewIdsToOpen a Set into which this method can store the
668      * collection of view identifiers (String) that should be opened
669      * @param viewIdsShow a Set into which this method can store the
670      * collection of view identifiers (String) that should be brought to top
671      */

672     private void computeViewActivation(Set JavaDoc contextIds, Set JavaDoc viewIdsToOpen, Set JavaDoc viewIdsShow) {
673         IWorkbenchPage page = getPage();
674         if (page == null) {
675             return;
676         }
677         Iterator JavaDoc contexts = contextIds.iterator();
678         while (contexts.hasNext()) {
679             String JavaDoc contextId = (String JavaDoc) contexts.next();
680             Iterator JavaDoc configurationElements= getConfigurationElements(contextId).iterator();
681             while (configurationElements.hasNext()) {
682                 IConfigurationElement element = (IConfigurationElement) configurationElements.next();
683                 String JavaDoc viewId= getViewId(element);
684                 if (viewId == null) {
685                     continue;
686                 }
687                 IViewReference reference = page.findViewReference(viewId);
688                 if (reference != null && reference