Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7acd41ae97c498d83625ca41df2a16f8328ab890 (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
243
/*******************************************************************************
 * Copyright (c) 2005, 2008 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *     Wind River Systems - adapted action to use for breakpoint types 
******************************************************************************/
package org.eclipse.debug.ui.actions;

import java.util.Iterator;
import java.util.Set;

import org.eclipse.debug.internal.ui.actions.ToggleBreakpointsTargetManager;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.text.source.IVerticalRulerInfo;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.ITextEditorExtension;

/**
 * Breakpoint ruler pop-up action that creates a sub-menu to select the currently 
 * active breakpoint type.   This action delegate can be contributed to an editor 
 * with the <code>editorActions</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.  It uses the <code>popupMenus</code> extension 
 * point, by referencing the ruler's context menu identifier in the 
 * <code>targetID</code> attribute.
 * <pre>
 * &lt;extension point="org.eclipse.ui.popupMenus"&gt;
 *   &lt;viewerContribution
 *     targetID="example.rulerContextMenuId"
 *     id="example.RulerPopupActions"&gt;
 *       &lt;action
 *         label="Toggle Breakpoint"
 *         class="org.eclipse.debug.ui.actions.RulerBreakpointTypesActionDelegate"
 *         menubarPath="additions"
 *         id="example.rulerContextMenu.breakpointTypesAction"&gt;
 *       &lt;/action&gt;
 *   &lt;/viewerContribution&gt;
 * </pre>
 * </p>
 * <p>
 * Clients may refer to this class as an action delegate in plug-in XML. This class
 * is not intended to be subclassed.
 * </p>
 * @see ToggleBreakpointsTargetManager
 * @see IToggleBreakpointsTargetFactory
 * @noextend This class is not intended to be subclassed by clients.
 * @since 3.5
 *  
 */
public class RulerBreakpointTypesActionDelegate implements IEditorActionDelegate, IMenuListener, IMenuCreator {
    private ITextEditor fEditor = null;
    private IAction fCallerAction = null;
    private IVerticalRulerInfo fRulerInfo;
    private ISelection fSelection;
    
    /**
     * The menu created by this action
     */
    private Menu fMenu;
    
    private class SelectTargetAction extends Action {
        private final Set fPossibleIDs;
        private final String fID;
        SelectTargetAction(String name, Set possibleIDs, String ID) {
            super(name, AS_RADIO_BUTTON);
            fID = ID;
            fPossibleIDs = possibleIDs;
        }

        public void run() {
            if (!isChecked()) {
                ToggleBreakpointsTargetManager.getDefault().setPreferredTarget(fPossibleIDs, fID);
            }
        }
    }

    
    public void selectionChanged(IAction action, ISelection selection) {
        // In the editor we're not using the selection.
    }
    
    public void run(IAction action) {
        // Do nothing, this is a pull-down menu.
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction, org.eclipse.ui.IEditorPart)
     */
    public void setActiveEditor(IAction callerAction, IEditorPart targetEditor) {
        // Clean up old editor data. 
        if (fCallerAction != null) {
            fCallerAction.setMenuCreator(null);
        }
        if (fEditor instanceof ITextEditorExtension) {
            ((ITextEditorExtension) fEditor).removeRulerContextMenuListener(this);
        }
        fRulerInfo = null;
        
        // Set up new editor data.
        fCallerAction = callerAction;
        fCallerAction.setMenuCreator(this);
        
        fEditor= (ITextEditor)(targetEditor == null ? null : targetEditor.getAdapter(ITextEditor.class));
        
        if (fEditor != null) {
            if (fEditor instanceof ITextEditorExtension) {
                ((ITextEditorExtension) fEditor).addRulerContextMenuListener(this);
            }

            fRulerInfo= (IVerticalRulerInfo) fEditor.getAdapter(IVerticalRulerInfo.class);
        }

    }

    public void dispose() {
        if (fCallerAction != null) {
            fCallerAction.setMenuCreator(null);
        }
        if (fEditor instanceof ITextEditorExtension) {
            ((ITextEditorExtension) fEditor).removeRulerContextMenuListener(this);
        }
        fRulerInfo = null;
    }

    public void menuAboutToShow(IMenuManager manager) {
        fSelection = StructuredSelection.EMPTY;
        if (fEditor != null && fRulerInfo != null) {
            
            IDocumentProvider provider = fEditor.getDocumentProvider();
            if (provider != null) {
                IDocument document =  provider.getDocument(fEditor.getEditorInput());
                int line = fRulerInfo.getLineOfLastMouseButtonActivity();
                if (line > -1) {
                    try {
                        IRegion region = document.getLineInformation(line);
                        fSelection = new TextSelection(document, region.getOffset(), 0);
                    } catch (BadLocationException e) {}
                }
            }
            ToggleBreakpointsTargetManager toggleTargetManager = ToggleBreakpointsTargetManager.getDefault(); 
            Set enabledIDs = toggleTargetManager.getEnabledToggleBreakpointsTargetIDs(fEditor, fSelection);
            fCallerAction.setEnabled(enabledIDs.size() > 0);
        } else {
            fCallerAction.setEnabled(false);
        }
        
    }
    
    /**
     * Sets this action's drop-down menu, disposing the previous menu.
     * 
     * @param menu the new menu
     */
    private void setMenu(Menu menu) {
        if (fMenu != null) {
            fMenu.dispose();
        }
        fMenu = menu;
    }

    public Menu getMenu(Menu parent) {
        setMenu(new Menu(parent));
        fillMenu(fMenu);
        initMenu();
        return fMenu;
    }

    public Menu getMenu(Control parent) {
        setMenu(new Menu(parent));
        fillMenu(fMenu);
        initMenu();
        return fMenu;
    }
    
    /**
     * Fills the drop-down menu with enabled toggle breakpoint targets
     * 
     * @param menu the menu to fill
     */
    private void fillMenu(Menu menu) {
        ToggleBreakpointsTargetManager manager = ToggleBreakpointsTargetManager.getDefault(); 
        Set enabledIDs = manager.getEnabledToggleBreakpointsTargetIDs(fEditor, fSelection);
        String preferredId = manager.getPreferredToggleBreakpointsTargetID(fEditor, fSelection);
        
        for (Iterator itr = enabledIDs.iterator(); itr.hasNext();) {
            String id = (String)itr.next();
            SelectTargetAction action= new SelectTargetAction(manager.getToggleBreakpointsTargetName(id), enabledIDs, id);
            
            if (id.equals(preferredId)){
                action.setChecked(true);
            }

            ActionContributionItem item= new ActionContributionItem(action);
            item.fill(menu, -1);
        } 
    }

    /**
     * Creates the menu for the action
     */
    private void initMenu() {
        // Add listener to re-populate the menu each time
        // it is shown because of dynamic history list
        fMenu.addMenuListener(new MenuAdapter() {
            public void menuShown(MenuEvent e) {
                Menu m = (Menu)e.widget;
                MenuItem[] items = m.getItems();
                for (int i=0; i < items.length; i++) {
                    items[i].dispose();
                }
                fillMenu(m);
            }
        });
    }

}

Back to the top