Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 36b30036d2b4e11e97de6284baa637803bf9cfa6 (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
/*******************************************************************************
 * Copyright (c) 2004, 2009 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 implementation
 *     Patrick Chuong (Texas Instruments) - Improve usability of the breakpoint view (Bug 238956)
 *******************************************************************************/
package org.eclipse.debug.internal.ui.actions.breakpointGroups;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.debug.internal.ui.DebugPluginImages;
import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
import org.eclipse.debug.internal.ui.breakpoints.provisional.IBreakpointOrganizer;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointOrganizerManager;
import org.eclipse.debug.internal.ui.views.breakpoints.IBreakpointOrganizerInputProvider;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.viewers.ISelection;
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;

/**
 * 
 */
public class GroupBreakpointsByAction extends AbstractBreakpointsViewAction implements IMenuCreator {

	private IAction fAction= null;
	
	public GroupBreakpointsByAction() {
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	public void run(IAction action) {
	}

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IMenuCreator#dispose()
     */
    public void dispose() {
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
     */
    public Menu getMenu(Control parent) {
        // Never called
        return null;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
     */
    public Menu getMenu(Menu parent) {
		Menu menu = new Menu(parent);
		menu.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);
			}
		});		
		return menu;
    }

    /**
     * Get the breakpoint organizer input provider from the view input.
     * 
     * @param input the view input.
     * @return the breakpoint organizer input provider, can be <code>null</code>.
     */
    private IBreakpointOrganizerInputProvider getOrganizerProvider(Object input) {
    	if (input instanceof IAdaptable) {
    		return (IBreakpointOrganizerInputProvider) ((IAdaptable) input).getAdapter(IBreakpointOrganizerInputProvider.class);
    	}
    	return null;
    }
	/**
	 * Fill pull down menu with the "group by" options
	 */
	private void fillMenu(Menu menu) {
		// allow the organizer provider to populate the menu first
		Object input = fView.getTreeModelViewer().getInput();
		IBreakpointOrganizerInputProvider provider = getOrganizerProvider(input);
		if (provider != null) {				
			IPresentationContext context = fView.getTreeModelViewer().getPresentationContext();
			provider.fillMenu(input, context, menu);
			fAction.setEnabled(menu.getItemCount() > 0);
			return;
		}
				
		// determine which item should be checked
		IBreakpointOrganizer[] organizers = fView.getBreakpointOrganizers();					
		
		boolean none = false;
		boolean advanced = false;
		IBreakpointOrganizer organizer = null;
		if (organizers == null || organizers.length == 0) {
			none = true;
		} else if (organizers.length > 1) {
			advanced = true;
		} else {
			organizer = organizers[0];
		}
		
        int accel = 1;
        // Add hard-coded action for flat breakpoints list
        IAction action = new GroupBreakpointsAction(null, fView);
        addAccel(accel, action, BreakpointGroupMessages.GroupBreakpointsByAction_0); 
        accel++;
        action.setImageDescriptor(DebugPluginImages.getImageDescriptor(IDebugUIConstants.IMG_VIEW_BREAKPOINTS));
        action.setChecked(none);
        ActionContributionItem item= new ActionContributionItem(action);
        item.fill(menu, -1);

		// Add actions for each contributed organizer
	    List actions = getActions(accel);
        accel = accel + actions.size();
        Iterator actionIter = actions.iterator();
	    while (actionIter.hasNext()) {
			GroupBreakpointsAction bpAction = (GroupBreakpointsAction) actionIter.next();
			bpAction.setChecked(bpAction.getOrganizer().equals(organizer));
			item= new ActionContributionItem(bpAction);
			item.fill(menu, -1);
	    }
	                    
        // advanced action
        AdvancedGroupBreakpointsByAction advancedAction = new AdvancedGroupBreakpointsByAction(fView);
        addAccel(accel, advancedAction,BreakpointGroupMessages.GroupBreakpointsByAction_1); 
        advancedAction.setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_HIERARCHICAL));
        advancedAction.setChecked(advanced);
		item= new ActionContributionItem(advancedAction);
		item.fill(menu, -1);
	}
    
    public List getActions(int accel) {
        List actions= new ArrayList();
        IBreakpointOrganizer[] organizers = BreakpointOrganizerManager.getDefault().getOrganizers();
        for (int i = 0; i < organizers.length; i++) {
        	IBreakpointOrganizer organizer = organizers[i];
            IAction action = new GroupBreakpointsAction(organizer, fView);
            addAccel(accel, action, organizer.getLabel());
            accel++;
            action.setImageDescriptor(organizer.getImageDescriptor());
            actions.add(action);
        }        
        return actions;
    }
    
    private void addAccel(int accel, IAction action, String label) {
        StringBuffer actionLabel= new StringBuffer();
        if (accel != 10) {
            if (accel < 10) {
                // add the numerical accelerators 1 through 9
                actionLabel.append('&');
            }
            actionLabel.append(accel);
        } else {
            actionLabel.append("1&0"); //$NON-NLS-1$
        }
        accel++;
        actionLabel.append(' ');
        actionLabel.append(label);
        action.setText(actionLabel.toString());        
    }
    
    /* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	    if (action != fAction) {
	        action.setMenuCreator(this);
	        fAction= action;
	    }
	}
}

Back to the top