|
Added
Link Here
|
| 1 |
/* |
| 2 |
* Created on Jan 24, 2004 |
| 3 |
* |
| 4 |
* To change the template for this generated file go to |
| 5 |
* Window - Preferences - Java - Code Generation - Code and Comments |
| 6 |
*/ |
| 7 |
package org.eclipse.debug.internal.ui.actions; |
| 8 |
|
| 9 |
import java.util.Iterator; |
| 10 |
import java.util.List; |
| 11 |
|
| 12 |
import org.eclipse.debug.internal.ui.DebugUIPlugin; |
| 13 |
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager; |
| 14 |
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchGroupExtension; |
| 15 |
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension; |
| 16 |
import org.eclipse.debug.ui.Pair; |
| 17 |
import org.eclipse.jface.action.Action; |
| 18 |
import org.eclipse.jface.action.ActionContributionItem; |
| 19 |
import org.eclipse.jface.action.IAction; |
| 20 |
import org.eclipse.jface.action.IMenuCreator; |
| 21 |
import org.eclipse.jface.viewers.ISelection; |
| 22 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 23 |
import org.eclipse.swt.SWT; |
| 24 |
import org.eclipse.swt.events.MenuAdapter; |
| 25 |
import org.eclipse.swt.events.MenuEvent; |
| 26 |
import org.eclipse.swt.widgets.Control; |
| 27 |
import org.eclipse.swt.widgets.Menu; |
| 28 |
import org.eclipse.swt.widgets.MenuItem; |
| 29 |
import org.eclipse.ui.IActionFilter; |
| 30 |
import org.eclipse.ui.IObjectActionDelegate; |
| 31 |
import org.eclipse.ui.IPerspectiveDescriptor; |
| 32 |
import org.eclipse.ui.IWorkbenchPage; |
| 33 |
import org.eclipse.ui.IWorkbenchPart; |
| 34 |
import org.eclipse.ui.IWorkbenchWindow; |
| 35 |
import org.eclipse.ui.help.WorkbenchHelp; |
| 36 |
|
| 37 |
/** |
| 38 |
* @author Administrator |
| 39 |
* |
| 40 |
* To change the template for this generated type comment go to |
| 41 |
* Window - Preferences - Java - Code Generation - Code and Comments |
| 42 |
*/ |
| 43 |
public class ContextualLaunchObjectActionDelegate |
| 44 |
implements |
| 45 |
IObjectActionDelegate, |
| 46 |
IMenuCreator { |
| 47 |
|
| 48 |
/* |
| 49 |
* The launch groups for which we should check for applicable configurations. |
| 50 |
* Should we get this programmatically from the plugin.xml file? |
| 51 |
*/ |
| 52 |
private static String[] fLaunchGroups = {"org.eclipse.debug.ui.launchGroup.run", |
| 53 |
"org.eclipse.debug.ui.launchGroup.debug", |
| 54 |
"org.eclipse.debug.ui.launchGroup.profile"}; |
| 55 |
|
| 56 |
private ISelection fSelection; |
| 57 |
|
| 58 |
/* (non-Javadoc) |
| 59 |
* @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart) |
| 60 |
*/ |
| 61 |
public void setActivePart(IAction action, IWorkbenchPart targetPart) { |
| 62 |
// We don't have a need for the active part. |
| 63 |
} |
| 64 |
/* (non-Javadoc) |
| 65 |
* @see org.eclipse.jface.action.IMenuCreator#dispose() |
| 66 |
*/ |
| 67 |
public void dispose() { |
| 68 |
// nothing to do |
| 69 |
} |
| 70 |
/* (non-Javadoc) |
| 71 |
* @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control) |
| 72 |
*/ |
| 73 |
public Menu getMenu(Control parent) { |
| 74 |
// never called |
| 75 |
return null; |
| 76 |
} |
| 77 |
/* (non-Javadoc) |
| 78 |
* @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu) |
| 79 |
*/ |
| 80 |
public Menu getMenu(Menu parent) { |
| 81 |
//Create the new menu. The menu will get filled when it is about to be shown. see fillMenu(Menu). |
| 82 |
Menu menu = new Menu(parent); |
| 83 |
/** |
| 84 |
* Add listener to repopulate the menu each time |
| 85 |
* it is shown because MenuManager.update(boolean, boolean) |
| 86 |
* doesn't dispose pulldown ActionContribution items for each popup menu. |
| 87 |
*/ |
| 88 |
menu.addMenuListener(new MenuAdapter() { |
| 89 |
public void menuShown(MenuEvent e) { |
| 90 |
Menu m = (Menu)e.widget; |
| 91 |
MenuItem[] items = m.getItems(); |
| 92 |
for (int i=0; i < items.length; i++) { |
| 93 |
items[i].dispose(); |
| 94 |
} |
| 95 |
fillMenu(m); |
| 96 |
} |
| 97 |
}); |
| 98 |
return menu; |
| 99 |
} |
| 100 |
|
| 101 |
private int fCount = 0; |
| 102 |
/** |
| 103 |
* Fill pull down menu with the pages of the JTabbedPane |
| 104 |
*/ |
| 105 |
protected void fillMenu(Menu menu) { |
| 106 |
// lookup appropriate launch config types and build launch actions for them. |
| 107 |
// Retrieve the current perspective and the registered shortcuts |
| 108 |
String activePerspID = getActivePerspectiveID(); |
| 109 |
if (activePerspID == null) { |
| 110 |
return; |
| 111 |
} |
| 112 |
for (int i=0; i<fLaunchGroups.length; i++) { |
| 113 |
String launchGroupIdentifier = fLaunchGroups[i]; |
| 114 |
boolean needsGroupSeparator = true; |
| 115 |
List shortcuts = getLaunchConfigurationManager().getLaunchShortcuts(activePerspID, getCategory(launchGroupIdentifier)); |
| 116 |
Iterator iter = shortcuts.iterator(); |
| 117 |
while (iter.hasNext()) { |
| 118 |
LaunchShortcutExtension ext = (LaunchShortcutExtension) iter.next(); |
| 119 |
if (isApplicable(ext, launchGroupIdentifier)) { |
| 120 |
if (i != 0 && needsGroupSeparator) { |
| 121 |
// except for the first group, add separator before non-empty menu groups |
| 122 |
{new MenuItem(menu, SWT.SEPARATOR);} |
| 123 |
needsGroupSeparator = false; |
| 124 |
} |
| 125 |
populateMenu(launchGroupIdentifier, ext, menu); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
/* |
| 131 |
* return true if this shortcut should appear in the contextual launch menu |
| 132 |
*/ |
| 133 |
private boolean isApplicable(LaunchShortcutExtension ext, String launchGroupIdentifier) { |
| 134 |
boolean hasMode = ext.getModes().contains(getMode(launchGroupIdentifier)); |
| 135 |
// check that shortcut has a mode for the launch group and there is a resource selected |
| 136 |
if (!hasMode || fSelection == null) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
// return false if there isn't a filter class or there are no filters specified by the shortcut |
| 140 |
IActionFilter actionFilter = ext.getFilterClass(); |
| 141 |
List filters = ext.getFilters(); |
| 142 |
if (actionFilter == null || filters.isEmpty()) { |
| 143 |
return false; |
| 144 |
} |
| 145 |
boolean accumulator = false; |
| 146 |
Iterator iter = filters.listIterator(); |
| 147 |
while (iter.hasNext()) { |
| 148 |
Pair pair = (Pair) iter.next(); |
| 149 |
String name = (String)pair.fFirst; |
| 150 |
String value= (String)pair.fSecond; |
| 151 |
Object target = (Object) fSelection; |
| 152 |
// any filter that returns false makes the shortcut non-visible |
| 153 |
if (!actionFilter.testAttribute(target,name,value)) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
} |
| 157 |
return true; |
| 158 |
} |
| 159 |
/** |
| 160 |
* Add the shortcut to the menu. |
| 161 |
*/ |
| 162 |
private void populateMenu(String launchGroupIdentifier, LaunchShortcutExtension ext, Menu menu) { |
| 163 |
String mode = getMode(launchGroupIdentifier); |
| 164 |
LaunchShortcutAction action = new LaunchShortcutAction(mode, ext); |
| 165 |
action.setActionDefinitionId(ext.getId()); |
| 166 |
String helpContextId = ext.getHelpContextId(); |
| 167 |
if (helpContextId != null) { |
| 168 |
WorkbenchHelp.setHelp(action, helpContextId); |
| 169 |
} |
| 170 |
createMenuAction(menu, action, mode, ext.getContextLabel()); |
| 171 |
} |
| 172 |
|
| 173 |
private void createMenuAction(Menu parent, IAction action, String mode, String contextLabel) { |
| 174 |
// replace default action label with context label if specified. |
| 175 |
String label = (contextLabel != null) ? contextLabel : action.getText(); |
| 176 |
// if it's not simple "run" mode, append mode to label |
| 177 |
label += ("run".equals(mode)) ? "" : " ("+mode+")"; |
| 178 |
action.setText(label); |
| 179 |
ActionContributionItem item= new ActionContributionItem(action); |
| 180 |
item.fill(parent, -1); |
| 181 |
} |
| 182 |
|
| 183 |
/* (non-Javadoc) |
| 184 |
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) |
| 185 |
*/ |
| 186 |
public void run(IAction action) { |
| 187 |
// Never called because we become a menu. |
| 188 |
} |
| 189 |
|
| 190 |
IAction delegateAction; |
| 191 |
/* (non-Javadoc) |
| 192 |
* @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection) |
| 193 |
*/ |
| 194 |
public void selectionChanged(IAction action, ISelection selection) { |
| 195 |
if (((IStructuredSelection) selection).size() != 1) |
| 196 |
action.setEnabled(false); // Can only handle one resource at a time |
| 197 |
else { |
| 198 |
if (action instanceof Action) { |
| 199 |
if (delegateAction != action) { |
| 200 |
delegateAction = (Action) action; |
| 201 |
delegateAction.setMenuCreator(this); |
| 202 |
} |
| 203 |
action.setEnabled(true); |
| 204 |
fSelection = selection; |
| 205 |
} else { |
| 206 |
action.setEnabled(false); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Return the ID of the currently active perspective, or <code>null</code> |
| 213 |
* if there is none. |
| 214 |
*/ |
| 215 |
private String getActivePerspectiveID() { |
| 216 |
IWorkbenchWindow window = DebugUIPlugin.getActiveWorkbenchWindow(); |
| 217 |
if (window != null) { |
| 218 |
IWorkbenchPage page = window.getActivePage(); |
| 219 |
if (page != null) { |
| 220 |
IPerspectiveDescriptor persp = page.getPerspective(); |
| 221 |
if (persp != null) { |
| 222 |
return persp.getId(); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
return null; |
| 227 |
} /** |
| 228 |
* Returns the launch configuration manager. |
| 229 |
* |
| 230 |
* @return launch configuration manager |
| 231 |
*/ |
| 232 |
private LaunchConfigurationManager getLaunchConfigurationManager() { |
| 233 |
return DebugUIPlugin.getDefault().getLaunchConfigurationManager(); |
| 234 |
} |
| 235 |
/** |
| 236 |
* Returns the launch group associatd with this action. |
| 237 |
* |
| 238 |
* @return the launch group associatd with this action |
| 239 |
*/ |
| 240 |
private LaunchGroupExtension getLaunchGroup(String fLaunchGroupIdentifier) { |
| 241 |
return getLaunchConfigurationManager().getLaunchGroup(fLaunchGroupIdentifier); |
| 242 |
} |
| 243 |
/** |
| 244 |
* Returns the mode of this action - run or debug |
| 245 |
* |
| 246 |
* @return the mode of this action - run or debug |
| 247 |
*/ |
| 248 |
private String getMode(String fLaunchGroupIdentifier) { |
| 249 |
return getLaunchGroup(fLaunchGroupIdentifier).getMode(); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Returns the category of this action - possibly <code>null</code> |
| 254 |
* |
| 255 |
* @return the category of this action - possibly <code>null</code> |
| 256 |
*/ |
| 257 |
private String getCategory(String fLaunchGroupIdentifier) { |
| 258 |
return getLaunchGroup(fLaunchGroupIdentifier).getCategory(); |
| 259 |
} |
| 260 |
|
| 261 |
} |