Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9c32509f47a84b964a663ad415a00d4bb9698c71 (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
/*******************************************************************************
 * Copyright (c) 2014 Wind River Systems, Inc. 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
 *******************************************************************************/
package org.eclipse.tcf.internal.cdt.ui.breakpoints;

import java.text.MessageFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.actions.IToggleBreakpointsTarget;
import org.eclipse.debug.ui.actions.IToggleBreakpointsTargetFactory;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.tcf.internal.cdt.ui.Activator;
import org.eclipse.tcf.internal.cdt.ui.preferences.PreferenceConstants;
import org.eclipse.tcf.internal.debug.model.ITCFConstants;
import org.eclipse.tcf.internal.debug.model.TCFLaunch;
import org.eclipse.tcf.internal.debug.ui.model.TCFNode;
import org.eclipse.ui.IWorkbenchPart;

/**
 * Toggle breakpoints target factory.
 * We use a separate factory so that we can control it through an action set.
 *
 */

public class TCFToggleBreakpointsTargetFactory implements IToggleBreakpointsTargetFactory {

    public static final String ID_TCF_BREAKPOINT_TOGGLE_TARGET = ITCFConstants.ID_TCF_DEBUG_MODEL + ".toggleTCFBreakpoint";

    private static final Set<String> BP_TOGGLE_TYPE_SET = new HashSet<String>(2);

    static {
        BP_TOGGLE_TYPE_SET.add(ID_TCF_BREAKPOINT_TOGGLE_TARGET);
    }

    private Map<String, TCFToggleBreakpointAdapter> fAdapterMap = new HashMap<String, TCFToggleBreakpointAdapter>();

    public IToggleBreakpointsTarget createToggleTarget(String targetID) {
        TCFToggleBreakpointAdapter adapter = null;
        if ( ID_TCF_BREAKPOINT_TOGGLE_TARGET.equals(targetID)) {
            if (fAdapterMap != null && !fAdapterMap.isEmpty()) {
                adapter = fAdapterMap.get(ID_TCF_BREAKPOINT_TOGGLE_TARGET);
            }
            if (adapter == null) {
                adapter = new TCFToggleBreakpointAdapter(ID_TCF_BREAKPOINT_TOGGLE_TARGET);
                fAdapterMap.put(ID_TCF_BREAKPOINT_TOGGLE_TARGET, adapter);
            }
        }
        return adapter;
    }

    public String getToggleTargetDescription(String targetID) {
        if (ID_TCF_BREAKPOINT_TOGGLE_TARGET.equals(targetID)) {
            String scope = getDefaultBPContextQuery();
            if (scope == null) scope = "None";
            return MessageFormat.format(Messages.TCFBreakpointToggle, scope);
        }
        return null;
    }

    public String getToggleTargetName(String targetID) {
        if (ID_TCF_BREAKPOINT_TOGGLE_TARGET.equals(targetID)) {
            String scope = getDefaultBPContextQuery();
            if (scope == null ) scope = "None";
            return MessageFormat.format(Messages.TCFBreakpointToggle, scope);
        }
        return null;
    }

    public String getDefaultToggleTarget(IWorkbenchPart part, ISelection selection) {
        if (isTCFBreakpointActive(part, selection)) {
            return ID_TCF_BREAKPOINT_TOGGLE_TARGET;
        }
        return null;
    }

    public Set<String> getToggleTargets(IWorkbenchPart part, ISelection selection) {
        if (isTCFBreakpointActive(part, selection)) {
            return BP_TOGGLE_TYPE_SET;
        }
        return Collections.emptySet();
    }

    private static IStructuredSelection getDebugContext(IWorkbenchPart part) {
        ISelection selection = DebugUITools.getDebugContextManager().
            getContextService(part.getSite().getWorkbenchWindow()).getActiveContext();
        if (selection instanceof IStructuredSelection) {
            return (IStructuredSelection)selection;
        }
        return StructuredSelection.EMPTY;
    }

    private static boolean isTCFBreakpointActive(IWorkbenchPart part, ISelection selection) {
        if (selection != null && !selection.isEmpty()) {
            // If the selection has the context data we want, use it.
            if (selection instanceof IStructuredSelection) {
                Object obj = ((IStructuredSelection)selection).getFirstElement();
                if (obj instanceof TCFNode || obj instanceof TCFLaunch) {
                    return true;
                }
            }
        }
        if (part != null) {
            // Get the debug context from the WorkbenchPart.
            Object obj = getDebugContext(part).getFirstElement();
            if (obj instanceof TCFNode || obj instanceof TCFLaunch) {
                return true;
            }
        }
        return false;
    }

    private static boolean isDefaultBPContextQueryEnabled() {
        return Platform.getPreferencesService().getBoolean(
                Activator.PLUGIN_ID,
                PreferenceConstants.PREF_DEFAULT_TRIGGER_SCOPE_ENABLED,
                false,
                null);
    }

    private static String getDefaultBPContextQuery() {
        String result = null;
        if (isDefaultBPContextQueryEnabled()) {
            result = Platform.getPreferencesService().getString(
                    Activator.PLUGIN_ID,
                    PreferenceConstants.PREF_DEFAULT_TRIGGER_SCOPE,
                    null,
                    null);
        }
        return result;
    }
}

Back to the top