1
/*******************************************************************************
2
* Copyright (c) 2008, 2011 Wind River Systems 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
9
* Wind River Systems - initial API and implementation
10
* Winnie Lai (Texas Instruments) - Individual Element Number Format (Bug 202556)
11
*******************************************************************************/
12
package org.eclipse.cdt.dsf.debug.ui.viewmodel.actions;
14
import org.eclipse.cdt.dsf.ui.viewmodel.IVMAdapter;
15
import org.eclipse.cdt.dsf.ui.viewmodel.IVMContext;
16
import org.eclipse.cdt.dsf.ui.viewmodel.IVMProvider;
17
import org.eclipse.core.commands.ExecutionEvent;
18
import org.eclipse.core.runtime.IAdaptable;
19
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
20
import org.eclipse.debug.internal.ui.viewers.model.provisional.TreeModelViewer;
21
import org.eclipse.debug.ui.DebugUITools;
22
import org.eclipse.debug.ui.IDebugView;
23
import org.eclipse.debug.ui.contexts.IDebugContextService;
24
import org.eclipse.jface.viewers.ISelection;
25
import org.eclipse.jface.viewers.IStructuredSelection;
26
import org.eclipse.jface.viewers.Viewer;
27
import org.eclipse.ui.IPartService;
28
import org.eclipse.ui.ISelectionService;
29
import org.eclipse.ui.IViewPart;
30
import org.eclipse.ui.IViewSite;
31
import org.eclipse.ui.IWorkbenchPart;
32
import org.eclipse.ui.handlers.HandlerUtil;
33
import org.eclipse.ui.services.IServiceLocator;
36
* Static utility methods for use with View Model related
37
* commands and handlers.
41
public class VMHandlerUtils {
44
* Retrieves the active VM provider based on the currently active
46
* @param serviceLocator Service locator for access to active selection
49
* @return The active VM provder.
51
static public IVMProvider getActiveVMProvider(IServiceLocator serviceLocator) {
52
ISelection selection = null;
54
ISelectionService selectionService =
55
(ISelectionService)serviceLocator.getService(ISelectionService.class);
56
if (selectionService != null) {
57
selection = selectionService.getSelection();
60
if (selection != null && !selection.isEmpty()) {
61
return getVMProviderForSelection(selection);
64
IWorkbenchPart part = null;
65
IPartService partService = (IPartService)serviceLocator.getService(IPartService.class);
66
if (partService != null) {
67
part = partService.getActivePart();
69
return getVMProviderForPart(part);
74
* Retrieves the active VM provider based on the given execution event.
75
* @param event The execution event which is usually given as an argument
76
* to the command handler execution call.
78
* @return The active VM provder.
80
static public IVMProvider getActiveVMProvider(ExecutionEvent event) {
81
ISelection selection = HandlerUtil.getCurrentSelection(event);
82
if (selection != null && !selection.isEmpty()) {
83
return getVMProviderForSelection(selection);
86
IWorkbenchPart part = HandlerUtil.getActivePart(event);
87
return getVMProviderForPart(part);
92
* Retrieves the selection from a given service locator's selection service.
93
* @param serviceLocator Service locator for access to active selection
96
* @return The selection if available; return null otherwise.
99
static public ISelection getSelection(IServiceLocator serviceLocator) {
100
ISelectionService selectionService =
101
(ISelectionService)serviceLocator.getService(ISelectionService.class);
102
if (selectionService != null) {
103
return selectionService.getSelection();
108
public static IVMProvider getVMProviderForPart(IWorkbenchPart part) {
109
IDebugContextService contextService =
110
DebugUITools.getDebugContextManager().getContextService(part.getSite().getWorkbenchWindow());
112
ISelection debugContext = contextService.getActiveContext(getPartId(part));
113
if (debugContext == null) {
114
debugContext = contextService.getActiveContext();
118
if (debugContext instanceof IStructuredSelection) {
119
input = ((IStructuredSelection)debugContext).getFirstElement();
122
if (part instanceof IDebugView) {
123
Viewer viewer = ((IDebugView)part).getViewer();
124
if (input instanceof IAdaptable && viewer instanceof TreeModelViewer) {
125
IPresentationContext presContext = ((TreeModelViewer)viewer).getPresentationContext();
126
IVMAdapter vmAdapter = (IVMAdapter)((IAdaptable)input).getAdapter(IVMAdapter.class);
127
if (vmAdapter != null) {
128
return vmAdapter.getVMProvider(presContext);
135
private static String getPartId(IWorkbenchPart part) {
136
if (part instanceof IViewPart) {
137
IViewSite site = (IViewSite)part.getSite();
138
return site.getId() + (site.getSecondaryId() != null ? (":" + site.getSecondaryId()) : ""); //$NON-NLS-1$ //$NON-NLS-2$
140
return part.getSite().getId();
144
public static IVMProvider getVMProviderForSelection(ISelection selection) {
145
if (selection instanceof IStructuredSelection) {
146
Object element = ((IStructuredSelection)selection).getFirstElement();
147
if (element instanceof IVMContext) {
148
return ((IVMContext)element).getVMNode().getVMProvider();