KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > breakpointGroups > GroupBreakpointsByAction


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.actions.breakpointGroups;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.debug.internal.ui.DebugPluginImages;
18 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
19 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointOrganizerManager;
20 import org.eclipse.debug.internal.ui.views.breakpoints.IBreakpointOrganizer;
21 import org.eclipse.debug.ui.IDebugUIConstants;
22 import org.eclipse.jface.action.ActionContributionItem;
23 import org.eclipse.jface.action.IAction;
24 import org.eclipse.jface.action.IMenuCreator;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.swt.events.MenuAdapter;
27 import org.eclipse.swt.events.MenuEvent;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Menu;
30 import org.eclipse.swt.widgets.MenuItem;
31
32 /**
33  *
34  */

35 public class GroupBreakpointsByAction extends AbstractBreakpointsViewAction implements IMenuCreator {
36
37     private IAction fAction= null;
38     
39     public GroupBreakpointsByAction() {
40     }
41     
42     /* (non-Javadoc)
43      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
44      */

45     public void run(IAction action) {
46     }
47
48     /* (non-Javadoc)
49      * @see org.eclipse.jface.action.IMenuCreator#dispose()
50      */

51     public void dispose() {
52     }
53
54     /* (non-Javadoc)
55      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
56      */

57     public Menu getMenu(Control parent) {
58         // Never called
59
return null;
60     }
61
62     /* (non-Javadoc)
63      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
64      */

65     public Menu getMenu(Menu parent) {
66         Menu menu = new Menu(parent);
67         menu.addMenuListener(new MenuAdapter() {
68             public void menuShown(MenuEvent e) {
69                 Menu m = (Menu)e.widget;
70                 MenuItem[] items = m.getItems();
71                 for (int i=0; i < items.length; i++) {
72                     items[i].dispose();
73                 }
74                 fillMenu(m);
75             }
76         });
77         return menu;
78     }
79     
80     /**
81      * Fill pull down menu with the "group by" options
82      */

83     private void fillMenu(Menu menu) {
84         // determine which item should be checked
85
IBreakpointOrganizer[] organizers = fView.getBreakpointOrganizers();
86         boolean none = false;
87         boolean advanced = false;
88         IBreakpointOrganizer organizer = null;
89         if (organizers == null || organizers.length == 0) {
90             none = true;
91         } else if (organizers.length > 1) {
92             advanced = true;
93         } else {
94             organizer = organizers[0];
95         }
96         
97         int accel = 1;
98         // Add hard-coded action for flat breakpoints list
99
IAction action = new GroupBreakpointsAction(null, fView);
100         addAccel(accel, action, BreakpointGroupMessages.GroupBreakpointsByAction_0);
101         accel++;
102         action.setImageDescriptor(DebugPluginImages.getImageDescriptor(IDebugUIConstants.IMG_VIEW_BREAKPOINTS));
103         action.setChecked(none);
104         ActionContributionItem item= new ActionContributionItem(action);
105         item.fill(menu, -1);
106
107         // Add actions for each contributed orgranizer
108
List JavaDoc actions = getActions(accel);
109         accel = accel + actions.size();
110         Iterator JavaDoc actionIter = actions.iterator();
111         while (actionIter.hasNext()) {
112             GroupBreakpointsAction bpAction = (GroupBreakpointsAction) actionIter.next();
113             bpAction.setChecked(bpAction.getOrganizer().equals(organizer));
114             item= new ActionContributionItem(bpAction);
115             item.fill(menu, -1);
116         }
117                         
118         // advanced action
119
AdvancedGroupBreakpointsByAction advancedAction = new AdvancedGroupBreakpointsByAction(fView);
120         addAccel(accel, advancedAction,BreakpointGroupMessages.GroupBreakpointsByAction_1);
121         advancedAction.setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_HIERARCHICAL));
122         advancedAction.setChecked(advanced);
123         item= new ActionContributionItem(advancedAction);
124         item.fill(menu, -1);
125     }
126     
127     public List JavaDoc getActions(int accel) {
128         List JavaDoc actions= new ArrayList JavaDoc();
129         IBreakpointOrganizer[] organizers = BreakpointOrganizerManager.getDefault().getOrganizers();
130         for (int i = 0; i < organizers.length; i++) {
131             IBreakpointOrganizer organizer = organizers[i];
132             IAction action = new GroupBreakpointsAction(organizer, fView);
133             addAccel(accel, action, organizer.getLabel());
134             accel++;
135             action.setImageDescriptor(organizer.getImageDescriptor());
136             actions.add(action);
137         }
138         return actions;
139     }
140     
141     private void addAccel(int accel, IAction action, String JavaDoc label) {
142         StringBuffer JavaDoc actionLabel= new StringBuffer JavaDoc();
143         if (accel != 10) {
144             if (accel < 10) {
145                 // add the numerical accelerators 1 through 9
146
actionLabel.append('&');
147             }
148             actionLabel.append(accel);
149         } else {
150             actionLabel.append("1&0"); //$NON-NLS-1$
151
}
152         accel++;
153         actionLabel.append(' ');
154         actionLabel.append(label);
155         action.setText(actionLabel.toString());
156     }
157     
158     /* (non-Javadoc)
159      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
160      */

161     public void selectionChanged(IAction action, ISelection selection) {
162         if (action != fAction) {
163             action.setMenuCreator(this);
164             fAction= action;
165         }
166     }
167 }
168
Popular Tags