Skip to main content
summaryrefslogtreecommitdiffstats
blob: bc179355e35bbe819fe1f77b2c9ed36f19ab6d22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*******************************************************************************
 * Copyright (c) 2007, 2010 Wind River Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *     Ericsson           - Added tracepoint support (284286)
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.ui.breakpoints;

import java.util.Map;

import org.eclipse.cdt.debug.core.CDIDebugModel;
import org.eclipse.cdt.debug.core.model.ICAddressBreakpoint;
import org.eclipse.cdt.debug.core.model.ICBreakpoint;
import org.eclipse.cdt.debug.core.model.ICEventBreakpoint;
import org.eclipse.cdt.debug.core.model.ICFunctionBreakpoint;
import org.eclipse.cdt.debug.core.model.ICLineBreakpoint;
import org.eclipse.cdt.debug.core.model.ICTracepoint;
import org.eclipse.cdt.debug.core.model.ICWatchpoint;
import org.eclipse.cdt.debug.ui.breakpoints.ICBreakpointContext;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IDebugModelProvider;
import org.eclipse.debug.ui.contexts.IDebugContextListener;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionFilter;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
 * Input for breakpoint properties dialog.  It captures both the 
 * selected breakpoint object as well as the selected debug context.
 * This combined context can then be used by breakpoint property
 * pages to access model and target specific breakpoint settings.  
 */
public class CBreakpointContext extends PlatformObject implements ICBreakpointContext {

    // Register an adapter factory for the class when it is first loaded.
    static {
        Platform.getAdapterManager().registerAdapters(new CBreakpointContextAdapterFactory(), CBreakpointContext.class);
    }
    
    /**
     * Breakpoint object held by this context.
     */
    private final ICBreakpoint fBreakpoint;
    
    /**
     * The resource that the breakpoint is to be created for.
     */
    private final IResource fResource;
    
    /**
     * The active debug context held by this context.
     */
    private final ISelection fDebugContext;
    
    /**
     * Associated preference store.
     */
    private final CBreakpointPreferenceStore fPreferenceStore;
    
    /**
     * Creates a new breakpoint context with given breakpoint and debug 
     * context selection.
     */
    public CBreakpointContext(ICBreakpoint breakpoint, ISelection debugContext) {
        this (breakpoint, debugContext, null, null);
    }

    public CBreakpointContext(ICBreakpoint breakpoint, ISelection debugContext, IResource resource, Map<String, Object> attributes) {
        fBreakpoint = breakpoint;
        fResource = resource;
        fDebugContext = debugContext;
        fPreferenceStore = new CBreakpointPreferenceStore(this, attributes);
    }

    @Override
    public ICBreakpoint getBreakpoint() { return fBreakpoint; }

    @Override
    public IResource getResource() { return fResource; }
    
    @Override
    public IPreferenceStore getPreferenceStore() { return fPreferenceStore; }
    
    /**
     * Returns the debug context.
     */
    public ISelection getDebugContext() { return fDebugContext; }

    /**
     * (non-Javadoc)
     * @see org.eclipse.debug.ui.contexts.IDebugContextProvider implementation
     */
	public IWorkbenchPart getPart() { return null; }
	public void addDebugContextListener(IDebugContextListener listener) {}
	public void removeDebugContextListener(IDebugContextListener listener) {}

	public ISelection getActiveContext() {
		return fDebugContext;
	}
}

/**
 * Action filter for the breakpoint context, which allows property
 * pages to filter their activation based on the debug model ID of 
 * the active debug context.
 */
class CBreakpointContextActionFilter implements IActionFilter {

    private static String[] EMPTY_IDENTIFIERS_ARRAY = new String[0];
    
    @Override
	public boolean testAttribute(Object target, String name, String value) {
        if (target instanceof CBreakpointContext) {
            if ("debugModelId".equals(name)) { //$NON-NLS-1$
                String[] targetModelIds = getDebugModelIds( (CBreakpointContext)target );
                for (int i = 0; i < targetModelIds.length; i++) {
                    if (targetModelIds[i].equals(value)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    
    private String[] getDebugModelIds(CBreakpointContext bpContext) {
        ISelection debugContext = bpContext.getDebugContext();
        if (debugContext instanceof IStructuredSelection) {
            Object debugElement = ((IStructuredSelection)debugContext).getFirstElement();
            if (debugElement instanceof IAdaptable) {
                IDebugModelProvider debugModelProvider = 
                    (IDebugModelProvider)((IAdaptable)debugElement).getAdapter(IDebugModelProvider.class);
                if (debugModelProvider != null) {
                    return debugModelProvider.getModelIdentifiers();
                } else if (debugElement instanceof IDebugElement) {
                    return new String[] { ((IDebugElement)debugElement).getModelIdentifier() };
                }
            }
        }
        return EMPTY_IDENTIFIERS_ARRAY; 
    }
}

class CBreakpointContextWorkbenchAdapter implements IWorkbenchAdapter {
    @Override
    public String getLabel(Object o) {
        if (o instanceof ICBreakpointContext) {
            ICBreakpoint bp = ((ICBreakpointContext)o).getBreakpoint();
            return getBreakpointMainLabel(bp);
        }
        return ""; //$NON-NLS-1$
    }
    
    @Override
    public Object[] getChildren(Object o) { return null; }
    
    @Override
    public ImageDescriptor getImageDescriptor(Object object) { return null; }
    
    @Override
    public Object getParent(Object o) { return null; }
    
    private String getBreakpointMainLabel(ICBreakpoint breakpoint) {
        if (breakpoint instanceof ICFunctionBreakpoint) {
            return BreakpointsMessages.getString("CBreakpointPropertyPage.breakpointType_function_label"); //$NON-NLS-1$
        } else if (breakpoint instanceof ICAddressBreakpoint) {
            return BreakpointsMessages.getString("CBreakpointPropertyPage.breakpointType_address_label"); //$NON-NLS-1$
        } else if (breakpoint instanceof ICLineBreakpoint) {
            return BreakpointsMessages.getString("CBreakpointPropertyPage.breakpointType_line_label"); //$NON-NLS-1$
        } else if (breakpoint instanceof ICEventBreakpoint) {
            return BreakpointsMessages.getString("CBreakpointPropertyPage.breakpointType_event_label"); //$NON-NLS-1$
        } else if (breakpoint instanceof ICWatchpoint) {
            return BreakpointsMessages.getString("CBreakpointPropertyPage.breakpointType_watchpoint_label"); //$NON-NLS-1$
        }
        // default main label is the label of marker type for the breakpoint
        return CDIDebugModel.calculateMarkerType(breakpoint);
    }

}

/**
 * Adapter factory which returns the breakpoint object and the action
 * filter for the CBreakpointContext type.
 */
class CBreakpointContextAdapterFactory implements IAdapterFactory {
    
    private static final Class<?>[] fgAdapterList = new Class[] {
        IBreakpoint.class, ICBreakpoint.class, ICTracepoint.class, IActionFilter.class, IPreferenceStore.class,
        IWorkbenchAdapter.class,
    };

    private static final IActionFilter fgActionFilter = new CBreakpointContextActionFilter();
    private static final IWorkbenchAdapter fgWorkbenchAdapter = new CBreakpointContextWorkbenchAdapter();
    
    @Override
	public Object getAdapter(Object obj, @SuppressWarnings("rawtypes") Class adapterType) {
        // Note: only return the breakpoint object as an adapter if it has 
        // an associated marker.  Otherwise the property pages will throw multiple 
        // exceptions.
        if (adapterType.isInstance( ((CBreakpointContext)obj).getBreakpoint() ) &&
            ((CBreakpointContext)obj).getBreakpoint().getMarker() != null) 
        {
            return ((CBreakpointContext)obj).getBreakpoint();
        }
        
        if ( IPreferenceStore.class.equals(adapterType) ) {
        	return ((CBreakpointContext)obj).getPreferenceStore();
        }
        
        if (IActionFilter.class.equals(adapterType)) {
            return fgActionFilter;
        }
        
        if (IWorkbenchAdapter.class.equals(adapterType)) {
            return fgWorkbenchAdapter;
        }

        return null;
    }
    
    @SuppressWarnings("rawtypes")
    @Override
	public Class[] getAdapterList() {
        return fgAdapterList;
    }
}

Back to the top