KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > EvaluationContextManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.debug.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.debug.ui.DebugUITools;
20 import org.eclipse.debug.ui.contexts.DebugContextEvent;
21 import org.eclipse.debug.ui.contexts.IDebugContextListener;
22 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
23 import org.eclipse.jdt.debug.core.IJavaStackFrame;
24 import org.eclipse.jdt.debug.core.IJavaThread;
25 import org.eclipse.jdt.internal.debug.ui.snippeteditor.ScrapbookLauncher;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.ui.IWindowListener;
29 import org.eclipse.ui.IWorkbench;
30 import org.eclipse.ui.IWorkbenchPage;
31 import org.eclipse.ui.IWorkbenchPart;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.PlatformUI;
34
35
36 /**
37  * Manages the current evaluation context (stack frame) for evaluation actions.
38  * In each page, the selection is tracked in each debug view (if any). When a stack
39  * frame selection exists, the "debuggerActive" System property is set to true.
40  */

41 public class EvaluationContextManager implements IWindowListener, IDebugContextListener {
42
43     private static EvaluationContextManager fgManager;
44     
45     /**
46      * System property indicating a stack frame is selected in the debug view with an
47      * <code>IJavaStackFrame</code> adapter.
48      */

49     private static final String JavaDoc DEBUGGER_ACTIVE = JDIDebugUIPlugin.getUniqueIdentifier() + ".debuggerActive"; //$NON-NLS-1$
50
/**
51      * System property indicating an element is selected in the debug view that is
52      * an instanceof <code>IJavaStackFrame</code> or <code>IJavaThread</code>.
53      */

54     private static final String JavaDoc INSTANCE_OF_IJAVA_STACK_FRAME = JDIDebugUIPlugin.getUniqueIdentifier() + ".instanceof.IJavaStackFrame"; //$NON-NLS-1$
55
/**
56      * System property indicating the frame in the debug view supports 'force return'
57      */

58     private static final String JavaDoc SUPPORTS_FORCE_RETURN = JDIDebugUIPlugin.getUniqueIdentifier() + ".supportsForceReturn"; //$NON-NLS-1$
59
/**
60      * System property indicating whether the frame in the debug view supports instance and reference retrieval (1.5 VMs and later).
61      */

62     private static final String JavaDoc SUPPORTS_INSTANCE_RETRIEVAL = JDIDebugUIPlugin.getUniqueIdentifier() + ".supportsInstanceRetrieval"; //$NON-NLS-1$
63

64     private Map JavaDoc fContextsByPage = null;
65     
66     private IWorkbenchWindow fActiveWindow;
67     
68     private EvaluationContextManager() {
69         DebugUITools.getDebugContextManager().addDebugContextListener(this);
70     }
71     
72     public static void startup() {
73         Runnable JavaDoc r = new Runnable JavaDoc() {
74             public void run() {
75                 if (fgManager == null) {
76                     fgManager = new EvaluationContextManager();
77                     IWorkbench workbench = PlatformUI.getWorkbench();
78                     IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
79                     for (int i = 0; i < windows.length; i++) {
80                         fgManager.windowOpened(windows[i]);
81                     }
82                     workbench.addWindowListener(fgManager);
83                     fgManager.fActiveWindow = workbench.getActiveWorkbenchWindow();
84                 }
85             }
86         };
87         JDIDebugUIPlugin.getStandardDisplay().asyncExec(r);
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.ui.IWindowListener#windowActivated(org.eclipse.ui.IWorkbenchWindow)
92      */

93     public void windowActivated(IWorkbenchWindow window) {
94         fActiveWindow = window;
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.ui.IWindowListener#windowClosed(org.eclipse.ui.IWorkbenchWindow)
99      */

100     public void windowClosed(IWorkbenchWindow window) {
101     }
102
103     /* (non-Javadoc)
104      * @see org.eclipse.ui.IWindowListener#windowDeactivated(org.eclipse.ui.IWorkbenchWindow)
105      */

106     public void windowDeactivated(IWorkbenchWindow window) {
107     }
108
109     /* (non-Javadoc)
110      * @see org.eclipse.ui.IWindowListener#windowOpened(org.eclipse.ui.IWorkbenchWindow)
111      */

112     public void windowOpened(IWorkbenchWindow window) {
113     }
114
115     /**
116      * Sets the evaluation context for the given page, and notes that
117      * a valid execution context exists.
118      *
119      * @param page
120      * @param frame
121      */

122     private void setContext(IWorkbenchPage page, IJavaStackFrame frame, boolean instOf) {
123         if (fContextsByPage == null) {
124             fContextsByPage = new HashMap JavaDoc();
125         }
126         fContextsByPage.put(page, frame);
127         System.setProperty(DEBUGGER_ACTIVE, "true"); //$NON-NLS-1$
128
if (frame.canForceReturn()) {
129             System.setProperty(SUPPORTS_FORCE_RETURN, "true"); //$NON-NLS-1$
130
} else {
131             System.setProperty(SUPPORTS_FORCE_RETURN, "false"); //$NON-NLS-1$
132
}
133         if (((IJavaDebugTarget)frame.getDebugTarget()).supportsInstanceRetrieval()){
134             System.setProperty(SUPPORTS_INSTANCE_RETRIEVAL, "true"); //$NON-NLS-1$
135
} else {
136             System.setProperty(SUPPORTS_INSTANCE_RETRIEVAL, "false"); //$NON-NLS-1$
137
}
138         if (instOf) {
139             System.setProperty(INSTANCE_OF_IJAVA_STACK_FRAME, "true"); //$NON-NLS-1$
140
} else {
141             System.setProperty(INSTANCE_OF_IJAVA_STACK_FRAME, "false"); //$NON-NLS-1$
142
}
143     }
144
145     /**
146      * Removes an evaluation context for the given page, and determines if
147      * any valid execution context remain.
148      *
149      * @param page
150      */

151     private void removeContext(IWorkbenchPage page) {
152         if (fContextsByPage != null) {
153             fContextsByPage.remove(page);
154             if (fContextsByPage.isEmpty()) {
155                 System.setProperty(DEBUGGER_ACTIVE, "false"); //$NON-NLS-1$
156
System.setProperty(INSTANCE_OF_IJAVA_STACK_FRAME, "false"); //$NON-NLS-1$
157
System.setProperty(SUPPORTS_FORCE_RETURN, "false"); //$NON-NLS-1$
158
System.setProperty(SUPPORTS_INSTANCE_RETRIEVAL, "false"); //$NON-NLS-1$
159
}
160         }
161     }
162     
163     private static IJavaStackFrame getContext(IWorkbenchPage page) {
164         if (fgManager != null) {
165             if (fgManager.fContextsByPage != null) {
166                 return (IJavaStackFrame)fgManager.fContextsByPage.get(page);
167             }
168         }
169         return null;
170     }
171     
172     /**
173      * Returns the evaluation context for the given part, or <code>null</code> if none.
174      * The evaluation context corresponds to the selected stack frame in the following
175      * priority order:<ol>
176      * <li>stack frame in the same page</li>
177      * <li>stack frame in the same window</li>
178      * <li>stack frame in active page of other window</li>
179      * <li>stack frame in page of other windows</li>
180      * </ol>
181      *
182      * @param part the part that the evaluation action was invoked from
183      * @return the stack frame that supplies an evaluation context, or <code>null</code>
184      * if none
185      */

186     public static IJavaStackFrame getEvaluationContext(IWorkbenchPart part) {
187         IWorkbenchPage page = part.getSite().getPage();
188         IJavaStackFrame frame = getContext(page);
189         if (frame == null) {
190             return getEvaluationContext(page.getWorkbenchWindow());
191         }
192         return frame;
193     }
194
195     /**
196      * Returns the evaluation context for the given window, or <code>null</code> if none.
197      * The evaluation context corresponds to the selected stack frame in the following
198      * priority order:<ol>
199      * <li>stack frame in active page of the window</li>
200      * <li>stack frame in another page of the window</li>
201      * <li>stack frame in active page of another window</li>
202      * <li>stack frame in a page of another window</li>
203      * </ol>
204      *
205      * @param window the window that the evaluation action was invoked from, or
206      * <code>null</code> if the current window should be consulted
207      * @return the stack frame that supplies an evaluation context, or <code>null</code>
208      * if none
209      * @return IJavaStackFrame
210      */

211     public static IJavaStackFrame getEvaluationContext(IWorkbenchWindow window) {
212         List JavaDoc alreadyVisited= new ArrayList JavaDoc();
213         if (window == null) {
214             window = fgManager.fActiveWindow;
215         }
216         return getEvaluationContext(window, alreadyVisited);
217     }
218     
219     private static IJavaStackFrame getEvaluationContext(IWorkbenchWindow window, List JavaDoc alreadyVisited) {
220         IWorkbenchPage activePage = window.getActivePage();
221         IJavaStackFrame frame = null;
222         if (activePage != null) {
223             frame = getContext(activePage);
224         }
225         if (frame == null) {
226             IWorkbenchPage[] pages = window.getPages();
227             for (int i = 0; i < pages.length; i++) {
228                 if (activePage != pages[i]) {
229                     frame = getContext(pages[i]);
230                     if (frame != null) {
231                         return frame;
232                     }
233                 }
234             }
235             
236             alreadyVisited.add(window);
237             
238             IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
239             for (int i = 0; i < windows.length; i++) {
240                 if (!alreadyVisited.contains(windows[i])) {
241                     frame = getEvaluationContext(windows[i], alreadyVisited);
242                     if (frame != null) {
243                         return frame;
244                     }
245                 }
246             }
247             return null;
248         }
249         return frame;
250     }
251     
252     public void debugContextChanged(DebugContextEvent event) {
253         if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) {
254             IWorkbenchPart part = event.getDebugContextProvider().getPart();
255             if (part != null) {
256                 IWorkbenchPage page = part.getSite().getPage();
257                 ISelection selection = event.getContext();
258                 if (selection instanceof IStructuredSelection) {
259                     IStructuredSelection ss = (IStructuredSelection)selection;
260                     if (ss.size() == 1) {
261                         Object JavaDoc element = ss.getFirstElement();
262                         if (element instanceof IAdaptable) {
263                             IJavaStackFrame frame = (IJavaStackFrame)((IAdaptable)element).getAdapter(IJavaStackFrame.class);
264                             boolean instOf = element instanceof IJavaStackFrame || element instanceof IJavaThread;
265                             if (frame != null) {
266                                 // do not consider scrapbook frames
267
if (frame.getLaunch().getAttribute(ScrapbookLauncher.SCRAPBOOK_LAUNCH) == null) {
268                                     setContext(page, frame, instOf);
269                                     return;
270                                 }
271                             }
272                         }
273                     }
274                 }
275                 // no context in the given view
276
removeContext(page);
277             }
278         }
279     }
280
281 }
282
Popular Tags