Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8c2340e5e961552ee773aaaad4e3e060b2c6564d (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
/*******************************************************************************
 * Copyright (c) 2012, 2013 Wind River Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *     IBM Corporation - bug fixing
 *******************************************************************************/
package org.eclipse.debug.ui.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.eclipse.debug.internal.ui.actions.ToggleBreakpointsTargetManager;
import org.eclipse.debug.internal.ui.actions.breakpoints.Messages;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.actions.CompoundContributionItem;
import org.eclipse.ui.menus.IWorkbenchContribution;
import org.eclipse.ui.services.IServiceLocator;

/**
 * Breakpoint ruler pop-up action that creates a sub-menu to select the
 * currently active breakpoint type. This menu contribution can be added to an
 * editor with the <code>org.eclipse.ui.menus</code> extension point. The
 * breakpoint types are calculated based on the toggle breakpoint target
 * factories contributed through the
 * <code>toggleBreakpointsTargetFactories</code> extension point.
 * <p>
 * Following is example plug-in XML used to contribute this action to an
 * editor's vertical ruler context menu.
 * </p>
 * 
 * <pre>
 * &lt;extension point="org.eclipse.ui.menus"&gt;
 *   &lt;menuContribution
 *     locationURI="popup:#CEditorRulerContext?after=additions"
 *     id="example.RulerPopupActions"&gt;
 *       &lt;menu\
 *         id="breakpointTypes"
 *         label="Toggle Breakpoint"&gt;
 *         &lt;dynamic\
 *           id="example.rulerContextMenu.breakpointTypesAction"&gt;
 *           class="org.eclipse.debug.ui.actions.BreakpointTypesContribution"
 *         menubarPath="additions"&gt;/
 *       &lt;/menu&gt;
 *   &lt;/menuContribution&gt;
 * </pre>
 *
 * <p>
 * Clients may refer to this class in plug-in XML. This class is not intended to
 * be sub-classed.
 * </p>
 *
 * @since 3.8
 * @noextend This class is not intended to be sub-classed by clients.
 */
public class BreakpointTypesContribution extends CompoundContributionItem implements IWorkbenchContribution {

    private class SelectTargetAction extends Action {
		private final Set<String> fPossibleIDs;
        private final String fID;

		SelectTargetAction(String name, Set<String> possibleIDs, String ID) {
            super(name, AS_RADIO_BUTTON);
            fID = ID;
            fPossibleIDs = possibleIDs;
        }

        @Override
		public void run() {
            if (isChecked()) {
                // Note: setPreferredTarget is not declared on the
                // IToggleBreakpontsTargetManager interface.
                ToggleBreakpointsTargetManager.getDefault().setPreferredTarget(fPossibleIDs, fID);
            }
        }
    }

    private IServiceLocator fServiceLocator;

    private static IContributionItem[] NO_BREAKPOINT_TYPES_CONTRIBUTION_ITEMS = new IContributionItem[] {
    	new ContributionItem() {
			@Override
			public void fill(Menu menu, int index) {
				MenuItem item = new MenuItem(menu, SWT.NONE);
				item.setEnabled(false);
				item.setText(Messages.BreakpointTypesContribution_0);
			}

			@Override
			public boolean isEnabled() {
				return false;
			}
    	}
    };

    /* (non-Javadoc)
     * @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
     */
    @Override
	protected IContributionItem[] getContributionItems() {
        IWorkbenchPart part = null;
        ISelection selection = null;

        ISelectionService selectionService =
            fServiceLocator.getService(ISelectionService.class);
        if (selectionService != null) {
            selection = selectionService.getSelection();
        }
        IPartService partService = fServiceLocator.getService(IPartService.class);
        if (partService != null) {
            part = partService.getActivePart();
        }

        // If no part or selection, disable all.
        if (part == null || selection == null) {
            return NO_BREAKPOINT_TYPES_CONTRIBUTION_ITEMS;
        }

        // Get breakpoint toggle target IDs.
        IToggleBreakpointsTargetManager manager = DebugUITools.getToggleBreakpointsTargetManager();
		Set<String> enabledIDs = manager.getEnabledToggleBreakpointsTargetIDs(part, selection);
        String preferredId = manager.getPreferredToggleBreakpointsTargetID(part, selection);

		List<Action> actions = new ArrayList<>(enabledIDs.size());
		for (String id : enabledIDs) {
            Action action = new SelectTargetAction(manager.getToggleBreakpointsTargetName(id), enabledIDs, id);
            if (id.equals(preferredId)) {
                action.setChecked(true);
            }
            actions.add(action);
        }

        if ( enabledIDs.isEmpty() ) {
            return NO_BREAKPOINT_TYPES_CONTRIBUTION_ITEMS;
        }

        IContributionItem[] items = new IContributionItem[enabledIDs.size()];
        for (int i = 0; i < actions.size(); i++) {
            items[i] = new ActionContributionItem(actions.get(i));
        }
        return items;
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.menus.IWorkbenchContribution#initialize(org.eclipse.ui.services.IServiceLocator)
     */
    @Override
	public void initialize(IServiceLocator serviceLocator) {
        fServiceLocator = serviceLocator;
    }
}

Back to the top