Package: VdmEvaluationContextManager$1

VdmEvaluationContextManager$1

nameinstructionbranchcomplexitylinemethod
run()
M: 32 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
{...}
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.debug
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.debug.ui;
23:
24: import java.util.ArrayList;
25: import java.util.HashMap;
26: import java.util.List;
27: import java.util.Map;
28:
29: import org.eclipse.core.runtime.IAdaptable;
30: import org.eclipse.debug.ui.DebugUITools;
31: import org.eclipse.debug.ui.contexts.DebugContextEvent;
32: import org.eclipse.debug.ui.contexts.IDebugContextListener;
33: import org.eclipse.jface.viewers.ISelection;
34: import org.eclipse.jface.viewers.IStructuredSelection;
35: import org.eclipse.ui.IWindowListener;
36: import org.eclipse.ui.IWorkbench;
37: import org.eclipse.ui.IWorkbenchPage;
38: import org.eclipse.ui.IWorkbenchPart;
39: import org.eclipse.ui.IWorkbenchWindow;
40: import org.eclipse.ui.PlatformUI;
41: import org.overture.ide.debug.core.VdmDebugPlugin;
42: import org.overture.ide.debug.core.model.IVdmStackFrame;
43:
44: /**
45: * Manages the current evaluation context (stack frame) for evaluation actions. In each page, the selection is tracked
46: * in each debug view (if any). When a stack frame selection exists, the "debuggerActive" System property is set to
47: * true.
48: */
49: public class VdmEvaluationContextManager implements IWindowListener,
50:                 IDebugContextListener
51: {
52:
53:         private static VdmEvaluationContextManager fgManager;
54:         /**
55:          * System property indicating a stack frame is selected in the debug view with an <code>IVdmStackFrame</code>
56:          * adapter.
57:          */
58:         private static final String DEBUGGER_ACTIVE = VdmDebugPlugin.PLUGIN_ID
59:                         + ".debuggerActive"; //$NON-NLS-1$
60:
61:         /**
62:          * System property indicating an element is selected in the debug view that is an instanceof
63:          * <code>IJavaStackFrame</code> or <code>IJavaThread</code>.
64:          */
65:         private static final String INSTANCE_OF_IJAVA_STACK_FRAME = VdmDebugPlugin.PLUGIN_ID
66:                         + ".instanceof.IVdmStackFrame"; //$NON-NLS-1$
67:         /**
68:          * System property indicating the frame in the debug view supports 'force return'
69:          */
70:         private static final String SUPPORTS_FORCE_RETURN = VdmDebugPlugin.PLUGIN_ID
71:                         + ".supportsForceReturn"; //$NON-NLS-1$        
72:         /**
73:          * System property indicating whether the frame in the debug view supports instance and reference retrieval (1.5 VMs
74:          * and later).
75:          */
76:         private static final String SUPPORTS_INSTANCE_RETRIEVAL = VdmDebugPlugin.PLUGIN_ID
77:                         + ".supportsInstanceRetrieval"; //$NON-NLS-1$
78:
79:         private Map<IWorkbenchPage, IVdmStackFrame> fContextsByPage = null;
80:
81:         private IWorkbenchWindow fActiveWindow;
82:
83:         protected VdmEvaluationContextManager()
84:         {
85:                 DebugUITools.getDebugContextManager().addDebugContextListener(this);
86:
87:         }
88:
89:         public static void startup()
90:         {
91:                 Runnable r = new Runnable()
92:                 {
93:                         public void run()
94:                         {
95:•                                if (fgManager == null)
96:                                 {
97:                                         fgManager = new VdmEvaluationContextManager();
98:                                         IWorkbench workbench = PlatformUI.getWorkbench();
99:                                         IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
100:•                                        for (int i = 0; i < windows.length; i++)
101:                                         {
102:                                                 fgManager.windowOpened(windows[i]);
103:                                         }
104:                                         workbench.addWindowListener(fgManager);
105:                                         fgManager.fActiveWindow = workbench.getActiveWorkbenchWindow();
106:                                 }
107:                         }
108:                 };
109:                 VdmDebugPlugin.getStandardDisplay().asyncExec(r);
110:         }
111:
112:         public void windowActivated(IWorkbenchWindow window)
113:         {
114:                 fActiveWindow = window;
115:         }
116:
117:         public void windowClosed(IWorkbenchWindow window)
118:         {
119:         }
120:
121:         public void windowDeactivated(IWorkbenchWindow window)
122:         {
123:         }
124:
125:         public void windowOpened(IWorkbenchWindow window)
126:         {
127:         }
128:
129:         /**
130:          * Sets the evaluation context for the given page, and notes that a valid execution context exists.
131:          *
132:          * @param page
133:          * @param frame
134:          */
135:         private void setContext(IWorkbenchPage page, IVdmStackFrame frame)
136:         {
137:                 // pageToContextMap.put(page, frame);
138:                 //                System.setProperty(DEBUGGER_ACTIVE, "true"); //$NON-NLS-1$
139:
140:                 /*
141:                  * if (frame.canForceReturn()) { System.setProperty(SUPPORTS_FORCE_RETURN, "true"); //$NON-NLS-1$ } else {
142:                  * System.setProperty(SUPPORTS_FORCE_RETURN, "false"); //$NON-NLS-1$ } if
143:                  * (((IVdmStackFrame)frame.getDebugTarget()).supportsInstanceRetrieval()){
144:                  * System.setProperty(SUPPORTS_INSTANCE_RETRIEVAL, "true"); //$NON-NLS-1$ } else {
145:                  * System.setProperty(SUPPORTS_INSTANCE_RETRIEVAL, "false"); //$NON-NLS-1$ } if (instOf) {
146:                  * System.setProperty(INSTANCE_OF_IJAVA_STACK_FRAME, "true"); //$NON-NLS-1$ } else {
147:                  * System.setProperty(INSTANCE_OF_IJAVA_STACK_FRAME, "false"); //$NON-NLS-1$ }
148:                  */
149:
150:                 if (fContextsByPage == null)
151:                 {
152:                         fContextsByPage = new HashMap<IWorkbenchPage, IVdmStackFrame>();
153:                 }
154:                 fContextsByPage.put(page, frame);
155:                 System.setProperty(DEBUGGER_ACTIVE, "true"); //$NON-NLS-1$
156:                 // if (frame.canForceReturn()) {
157:                 //                        System.setProperty(SUPPORTS_FORCE_RETURN, "true"); //$NON-NLS-1$
158:                 // } else {
159:                 //                        System.setProperty(SUPPORTS_FORCE_RETURN, "false"); //$NON-NLS-1$
160:                 // }
161:                 // if (((IJavaDebugTarget)frame.getDebugTarget()).supportsInstanceRetrieval()){
162:                 //                        System.setProperty(SUPPORTS_INSTANCE_RETRIEVAL, "true"); //$NON-NLS-1$
163:                 // } else {
164:                 //                        System.setProperty(SUPPORTS_INSTANCE_RETRIEVAL, "false"); //$NON-NLS-1$
165:                 // }
166:                 // if (instOf) {
167:                 //                        System.setProperty(INSTANCE_OF_IJAVA_STACK_FRAME, "true"); //$NON-NLS-1$
168:                 // } else {
169:                 //                        System.setProperty(INSTANCE_OF_IJAVA_STACK_FRAME, "false"); //$NON-NLS-1$
170:                 // }
171:         }
172:
173:         /**
174:          * Removes an evaluation context for the given page, and determines if any valid execution context remain.
175:          *
176:          * @param page
177:          */
178:         private void removeContext(IWorkbenchPage page)
179:         {
180:                 // pageToContextMap.remove(page);
181:                 // if (pageToContextMap.isEmpty())
182:                 // {
183:                 //                        System.setProperty(DEBUGGER_ACTIVE, "false"); //$NON-NLS-1$
184:                 // // System.setProperty(INSTANCE_OF_IJAVA_STACK_FRAME, "false");
185:                 //                        // //$NON-NLS-1$
186:                 //                        // System.setProperty(SUPPORTS_FORCE_RETURN, "false"); //$NON-NLS-1$
187:                 // // System.setProperty(SUPPORTS_INSTANCE_RETRIEVAL, "false");
188:                 //                        // //$NON-NLS-1$
189:                 // }
190:                 if (fContextsByPage != null)
191:                 {
192:                         fContextsByPage.remove(page);
193:                         if (fContextsByPage.isEmpty())
194:                         {
195:                                 System.setProperty(DEBUGGER_ACTIVE, "false"); //$NON-NLS-1$
196:                                 System.setProperty(INSTANCE_OF_IJAVA_STACK_FRAME, "false"); //$NON-NLS-1$
197:                                 System.setProperty(SUPPORTS_FORCE_RETURN, "false"); //$NON-NLS-1$
198:                                 System.setProperty(SUPPORTS_INSTANCE_RETRIEVAL, "false"); //$NON-NLS-1$
199:                         }
200:                 }
201:         }
202:
203:         private static IVdmStackFrame getContext(IWorkbenchPage page)
204:         {
205:                 // if (manager != null)
206:                 // {
207:                 // if (manager.pageToContextMap != null)
208:                 // {
209:                 // return (IVdmStackFrame) manager.pageToContextMap.get(page);
210:                 // }
211:                 // }
212:                 // return null;
213:                 if (fgManager != null)
214:                 {
215:                         if (fgManager.fContextsByPage != null)
216:                         {
217:                                 return (IVdmStackFrame) fgManager.fContextsByPage.get(page);
218:                         }
219:                 }
220:                 return null;
221:         }
222:
223:         /**
224:          * Returns the evaluation context for the given part, or <code>null</code> if none. The evaluation context
225:          * corresponds to the selected stack frame in the following priority order:
226:          * <ol>
227:          * <li>stack frame in the same page</li>
228:          * <li>stack frame in the same window</li>
229:          * <li>stack frame in active page of other window</li>
230:          * <li>stack frame in page of other windows</li>
231:          * </ol>
232:          *
233:          * @param part
234:          * the part that the evaluation action was invoked from
235:          * @return the stack frame that supplies an evaluation context, or <code>null</code> if none
236:          */
237:         public static IVdmStackFrame getEvaluationContext(IWorkbenchPart part)
238:         {
239:                 IWorkbenchPage page = part.getSite().getPage();
240:                 IVdmStackFrame frame = getContext(page);
241:                 if (frame == null)
242:                 {
243:                         return getEvaluationContext(page.getWorkbenchWindow());
244:                 }
245:                 return frame;
246:         }
247:
248:         /**
249:          * Returns the evaluation context for the given window, or <code>null</code> if none. The evaluation context
250:          * corresponds to the selected stack frame in the following priority order:
251:          * <ol>
252:          * <li>stack frame in active page of the window</li>
253:          * <li>stack frame in another page of the window</li>
254:          * <li>stack frame in active page of another window</li>
255:          * <li>stack frame in a page of another window</li>
256:          * </ol>
257:          *
258:          * @param window
259:          * the window that the evaluation action was invoked from, or <code>null</code> if the current window
260:          * should be consulted
261:          * @return the stack frame that supplies an evaluation context, or <code>null</code> if none
262:          * @return IJavaStackFrame
263:          */
264:         public static IVdmStackFrame getEvaluationContext(IWorkbenchWindow window)
265:         {
266:                 List<IWorkbenchWindow> alreadyVisited = new ArrayList<IWorkbenchWindow>();
267:                 if (window == null)
268:                 {
269:                         window = fgManager.fActiveWindow;
270:                 }
271:                 return getEvaluationContext(window, alreadyVisited);
272:         }
273:
274:         private static IVdmStackFrame getEvaluationContext(IWorkbenchWindow window,
275:                         List<IWorkbenchWindow> alreadyVisited)
276:         {
277:                 IWorkbenchPage activePage = window.getActivePage();
278:                 IVdmStackFrame frame = null;
279:                 if (activePage != null)
280:                 {
281:                         frame = getContext(activePage);
282:                 }
283:                 if (frame == null)
284:                 {
285:                         IWorkbenchPage[] pages = window.getPages();
286:                         for (int i = 0; i < pages.length; i++)
287:                         {
288:                                 if (activePage != pages[i])
289:                                 {
290:                                         frame = getContext(pages[i]);
291:                                         if (frame != null)
292:                                         {
293:                                                 return frame;
294:                                         }
295:                                 }
296:                         }
297:
298:                         alreadyVisited.add(window);
299:
300:                         IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
301:                         for (int i = 0; i < windows.length; i++)
302:                         {
303:                                 if (!alreadyVisited.contains(windows[i]))
304:                                 {
305:                                         frame = getEvaluationContext(windows[i], alreadyVisited);
306:                                         if (frame != null)
307:                                         {
308:                                                 return frame;
309:                                         }
310:                                 }
311:                         }
312:                         return null;
313:                 }
314:                 return frame;
315:         }
316:
317:         public void debugContextChanged(DebugContextEvent event)
318:         {
319:                 // if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0)
320:                 // {
321:                 // IWorkbenchPart part = event.getDebugContextProvider().getPart();
322:                 // if (part != null)
323:                 // {
324:                 // IWorkbenchPage page = part.getSite().getPage();
325:                 // ISelection selection = event.getContext();
326:                 // if (selection instanceof IStructuredSelection)
327:                 // {
328:                 // IStructuredSelection ss = (IStructuredSelection) selection;
329:                 // if (ss.size() == 1)
330:                 // {
331:                 // Object element = ss.getFirstElement();
332:                 // if (element instanceof IAdaptable)
333:                 // {
334:                 // IVdmStackFrame frame = (IVdmStackFrame) ((IAdaptable) element).getAdapter(IVdmStackFrame.class);
335:                 // if (frame != null)
336:                 // {
337:                 // setContext(page, frame);
338:                 // return;
339:                 // }
340:                 // }
341:                 // }
342:                 // }
343:                 //
344:                 // // no context in the given view
345:                 // removeContext(page);
346:                 // }
347:                 // }
348:                 if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0)
349:                 {
350:                         IWorkbenchPart part = event.getDebugContextProvider().getPart();
351:                         if (part != null)
352:                         {
353:                                 IWorkbenchPage page = part.getSite().getPage();
354:                                 ISelection selection = event.getContext();
355:                                 if (selection instanceof IStructuredSelection)
356:                                 {
357:                                         IStructuredSelection ss = (IStructuredSelection) selection;
358:                                         if (ss.size() == 1)
359:                                         {
360:                                                 Object element = ss.getFirstElement();
361:                                                 if (element instanceof IAdaptable)
362:                                                 {
363:                                                         IVdmStackFrame frame = (IVdmStackFrame) ((IAdaptable) element).getAdapter(IVdmStackFrame.class);
364:                                                         // boolean instOf = element instanceof IVdmStackFrame || element instanceof IVdmThread;
365:                                                         if (frame != null)
366:                                                         {
367:                                                                 // do not consider scrapbook frames
368:                                                                 // if (frame.getLaunch().getAttribute(ScrapbookLauncher.SCRAPBOOK_LAUNCH) == null) {
369:                                                                 // setContext(page, frame, instOf);
370:                                                                 // return;
371:                                                                 // }
372:                                                                 setContext(page, frame);
373:                                                                 return;
374:                                                         }
375:                                                 }
376:                                         }
377:                                 }
378:                                 // no context in the given view
379:                                 removeContext(page);
380:                         }
381:                 }
382:         }
383: }