|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2003, 2004 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Common Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/cpl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.debug.internal.ui.actions; |
| 12 |
|
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.Iterator; |
| 15 |
import java.util.List; |
| 16 |
import java.util.Set; |
| 17 |
|
| 18 |
import org.eclipse.core.runtime.IExtension; |
| 19 |
import org.eclipse.core.runtime.IPluginDescriptor; |
| 20 |
import org.eclipse.debug.internal.ui.DebugUIPlugin; |
| 21 |
import org.eclipse.debug.internal.ui.Pair; |
| 22 |
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager; |
| 23 |
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchGroupExtension; |
| 24 |
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension; |
| 25 |
import org.eclipse.jface.action.Action; |
| 26 |
import org.eclipse.jface.action.ActionContributionItem; |
| 27 |
import org.eclipse.jface.action.IAction; |
| 28 |
import org.eclipse.jface.action.IMenuCreator; |
| 29 |
import org.eclipse.jface.viewers.ISelection; |
| 30 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 31 |
import org.eclipse.swt.events.MenuAdapter; |
| 32 |
import org.eclipse.swt.events.MenuEvent; |
| 33 |
import org.eclipse.swt.widgets.Control; |
| 34 |
import org.eclipse.swt.widgets.Menu; |
| 35 |
import org.eclipse.swt.widgets.MenuItem; |
| 36 |
import org.eclipse.ui.IActionFilter; |
| 37 |
import org.eclipse.ui.IObjectActionDelegate; |
| 38 |
import org.eclipse.ui.IPerspectiveDescriptor; |
| 39 |
import org.eclipse.ui.IWorkbenchPage; |
| 40 |
import org.eclipse.ui.IWorkbenchPart; |
| 41 |
import org.eclipse.ui.IWorkbenchWindow; |
| 42 |
import org.eclipse.ui.help.WorkbenchHelp; |
| 43 |
|
| 44 |
/** |
| 45 |
* An action delegate that builds a context menu with applicable launch shortcuts. |
| 46 |
* <p> |
| 47 |
* This class can be contributed as pop-up menu extension action. When envolked, |
| 48 |
* it becomes a sub-menu that dynamically builds a list of applicable shortcuts |
| 49 |
* for the current selection (ISelection in the workspace). The LaunchShortCut |
| 50 |
* extension is consulted to obtain the list of registered short cuts. Each short |
| 51 |
* cut may have optional information to support a context menu action. The extra |
| 52 |
* information includes a "filterClass", a list of "contextLabel"s, and a list of |
| 53 |
* "filter" elements. ContextLabels allow custom labels to appear for any mode |
| 54 |
* (run, debug, profile, etc.) in the contextual launch sub-menu. The filterClass |
| 55 |
* is loaded and run over the list of "filter" elements to determine if the |
| 56 |
* shortcut extension item is appropriate for the selected resource. |
| 57 |
* <p> |
| 58 |
* An example is the JDT Java Applet extension, which is only applicable on files |
| 59 |
* of extension "*.java" and being a sub-class of type Applet. Note that it is up |
| 60 |
* to the filterClass to provide attributes and methods to implement the test. In |
| 61 |
* this example, we have extended the AppletShortcut to implement the IActionFilter |
| 62 |
* interface so that it can function as the filterClass, adding only a testAttribute() |
| 63 |
* method. |
| 64 |
* <p> |
| 65 |
* <pre> |
| 66 |
* <shortcut |
| 67 |
* label="%AppletShortcut.label" |
| 68 |
* icon="icons/full/ctool16/java_applet.gif" |
| 69 |
* helpContextId="org.eclipse.jdt.debug.ui.shortcut_java_applet" |
| 70 |
* modes="run, debug" |
| 71 |
* filterClass="org.eclipse.jdt.internal.debug.ui.launcher.JavaAppletLaunchShortcut" |
| 72 |
* class="org.eclipse.jdt.internal.debug.ui.launcher.JavaAppletLaunchShortcut" |
| 73 |
* id="org.eclipse.jdt.debug.ui.javaAppletShortcut"> |
| 74 |
* <filter |
| 75 |
* name="NameMatches" |
| 76 |
* value="*.java"/> |
| 77 |
* <filter |
| 78 |
* name="ContextualLaunchActionFilter" |
| 79 |
* value="supportsContextualLaunch"/> |
| 80 |
* <contextLabel |
| 81 |
* mode="run" |
| 82 |
* label="%RunJavaApplet.label"/> |
| 83 |
* <contextLabel |
| 84 |
* mode="debug" |
| 85 |
* label="%DebugJavaApplet.label"/> |
| 86 |
* ... |
| 87 |
* <shortcut> |
| 88 |
* </pre> |
| 89 |
*/ |
| 90 |
public class ContextualLaunchObjectActionDelegate |
| 91 |
implements |
| 92 |
IObjectActionDelegate, |
| 93 |
IMenuCreator { |
| 94 |
|
| 95 |
private ISelection fSelection; |
| 96 |
|
| 97 |
/* |
| 98 |
* @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart) |
| 99 |
*/ |
| 100 |
public void setActivePart(IAction action, IWorkbenchPart targetPart) { |
| 101 |
// We don't have a need for the active part. |
| 102 |
} |
| 103 |
/* (non-Javadoc) |
| 104 |
* @see org.eclipse.jface.action.IMenuCreator#dispose() |
| 105 |
*/ |
| 106 |
public void dispose() { |
| 107 |
// nothing to do |
| 108 |
} |
| 109 |
/* (non-Javadoc) |
| 110 |
* @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control) |
| 111 |
*/ |
| 112 |
public Menu getMenu(Control parent) { |
| 113 |
// never called |
| 114 |
return null; |
| 115 |
} |
| 116 |
/* (non-Javadoc) |
| 117 |
* @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu) |
| 118 |
*/ |
| 119 |
public Menu getMenu(Menu parent) { |
| 120 |
//Create the new menu. The menu will get filled when it is about to be shown. see fillMenu(Menu). |
| 121 |
Menu menu = new Menu(parent); |
| 122 |
/** |
| 123 |
* Add listener to repopulate the menu each time |
| 124 |
* it is shown because MenuManager.update(boolean, boolean) |
| 125 |
* doesn't dispose pulldown ActionContribution items for each popup menu. |
| 126 |
*/ |
| 127 |
menu.addMenuListener(new MenuAdapter() { |
| 128 |
public void menuShown(MenuEvent e) { |
| 129 |
Menu m = (Menu)e.widget; |
| 130 |
MenuItem[] items = m.getItems(); |
| 131 |
for (int i=0; i < items.length; i++) { |
| 132 |
items[i].dispose(); |
| 133 |
} |
| 134 |
fillMenu(m); |
| 135 |
} |
| 136 |
}); |
| 137 |
return menu; |
| 138 |
} |
| 139 |
|
| 140 |
/* |
| 141 |
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) |
| 142 |
*/ |
| 143 |
public void run(IAction action) { |
| 144 |
// Never called because we become a menu. |
| 145 |
} |
| 146 |
|
| 147 |
IAction delegateAction; |
| 148 |
/* |
| 149 |
* @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection) |
| 150 |
*/ |
| 151 |
public void selectionChanged(IAction action, ISelection selection) { |
| 152 |
if (((IStructuredSelection) selection).size() != 1) |
| 153 |
action.setEnabled(false); // Can only handle one resource at a time |
| 154 |
else { |
| 155 |
if (action instanceof Action) { |
| 156 |
if (delegateAction != action) { |
| 157 |
delegateAction = (Action) action; |
| 158 |
delegateAction.setMenuCreator(this); |
| 159 |
} |
| 160 |
action.setEnabled(true); |
| 161 |
fSelection = selection; |
| 162 |
} else { |
| 163 |
action.setEnabled(false); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
private int fCount = 0; |
| 169 |
/** |
| 170 |
* Fill pull down menu with the pages of the JTabbedPane |
| 171 |
*/ |
| 172 |
protected void fillMenu(Menu menu) { |
| 173 |
// lookup appropriate launch config types and build launch actions for them. |
| 174 |
// Retrieve the current perspective and the registered shortcuts |
| 175 |
String activePerspID = getActivePerspectiveID(); |
| 176 |
if (activePerspID == null || fSelection == null) { |
| 177 |
return; |
| 178 |
} |
| 179 |
// gather all shortcuts and run their filters so that we only run the |
| 180 |
// filters one time for each shortcut. Running filters can be expensive. |
| 181 |
// Also, only *LOADED* plugins get their filters run. |
| 182 |
List /* <LaunchShortcutExtension> */ allShortCuts = getLaunchConfigurationManager().getLaunchShortcuts(); |
| 183 |
Iterator iter = allShortCuts.iterator(); |
| 184 |
List filteredShortCuts = new ArrayList(10); |
| 185 |
while (iter.hasNext()) { |
| 186 |
LaunchShortcutExtension ext = (LaunchShortcutExtension) iter.next(); |
| 187 |
if (isApplicable(ext)) { |
| 188 |
filteredShortCuts.add(ext); |
| 189 |
} |
| 190 |
} |
| 191 |
iter = filteredShortCuts.iterator(); |
| 192 |
if (iter.hasNext()) { |
| 193 |
while (iter.hasNext()) { |
| 194 |
LaunchShortcutExtension ext = (LaunchShortcutExtension) iter.next(); |
| 195 |
Set modes = ext.getModes(); // supported launch modes |
| 196 |
Iterator modeIter = modes.iterator(); |
| 197 |
while (modeIter.hasNext()) { |
| 198 |
String mode = (String) modeIter.next(); |
| 199 |
populateMenu(mode, ext, menu); |
| 200 |
} |
| 201 |
} |
| 202 |
} else { |
| 203 |
// put in a fake action to show there are none |
| 204 |
// IAction action = new FakeAction("{ }"); |
| 205 |
// ActionContributionItem item= new ActionContributionItem(action); |
| 206 |
// item.fill(menu, -1); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
private IActionFilter getFilterClassIfLoaded(LaunchShortcutExtension ext) { |
| 211 |
IExtension extensionPoint = ext.getConfigurationElement().getDeclaringExtension(); |
| 212 |
IPluginDescriptor pluginDescriptor = extensionPoint.getDeclaringPluginDescriptor(); |
| 213 |
if (pluginDescriptor.isPluginActivated()) { |
| 214 |
IActionFilter filter = ext.getFilterClass(); |
| 215 |
return filter; |
| 216 |
} else { |
| 217 |
return null; |
| 218 |
} |
| 219 |
} |
| 220 |
/* (non-javadoc) |
| 221 |
* Apply contextFilters for this extension to decide visibility. |
| 222 |
* |
| 223 |
* @return true if this shortcut should appear in the contextual launch menu |
| 224 |
*/ |
| 225 |
private boolean isApplicable(LaunchShortcutExtension ext) { |
| 226 |
// boolean hasMode = ext.getModes().contains(getMode(launchGroupIdentifier)); |
| 227 |
// return false if there isn't a filter class or there are no filters specified by the shortcut |
| 228 |
// Only loaded plugins will be used, so the actionFilter is null if the filterClass is not loaded |
| 229 |
IActionFilter actionFilter = getFilterClassIfLoaded(ext); |
| 230 |
if (actionFilter == null) { |
| 231 |
return false; |
| 232 |
} |
| 233 |
List filters = ext.getFilters(); |
| 234 |
if (filters.isEmpty()) { |
| 235 |
return false; |
| 236 |
} |
| 237 |
Iterator iter = filters.listIterator(); |
| 238 |
while (iter.hasNext()) { |
| 239 |
Pair pair = (Pair) iter.next(); |
| 240 |
String name = pair.firstAsString(); |
| 241 |
String value= pair.secondAsString(); |
| 242 |
Object target = (Object) fSelection; |
| 243 |
// any filter that returns false makes the shortcut non-visible |
| 244 |
if (!actionFilter.testAttribute(target,name,value)) { |
| 245 |
return false; |
| 246 |
} |
| 247 |
} |
| 248 |
return true; |
| 249 |
} |
| 250 |
/* Add the shortcut to the context menu's launch submenu. |
| 251 |
* |
| 252 |
*/ |
| 253 |
private void populateMenu(String mode, LaunchShortcutExtension ext, Menu menu) { |
| 254 |
LaunchShortcutAction action = new LaunchShortcutAction(mode, ext); |
| 255 |
action.setActionDefinitionId(ext.getId()); |
| 256 |
String helpContextId = ext.getHelpContextId(); |
| 257 |
if (helpContextId != null) { |
| 258 |
WorkbenchHelp.setHelp(action, helpContextId); |
| 259 |
} |
| 260 |
// replace default action label with context label if specified. |
| 261 |
String label = ext.getContextLabel(mode); |
| 262 |
label = (label != null) ? label : action.getText(); |
| 263 |
action.setText(label); |
| 264 |
ActionContributionItem item= new ActionContributionItem(action); |
| 265 |
item.fill(menu, -1); |
| 266 |
} |
| 267 |
|
| 268 |
private class FakeAction extends Action { |
| 269 |
public FakeAction(String name) { |
| 270 |
super(name); |
| 271 |
} |
| 272 |
public void run() { |
| 273 |
} |
| 274 |
} |
| 275 |
/** |
| 276 |
* Return the ID of the currently active perspective. |
| 277 |
* |
| 278 |
* @return the active perspective ID or <code>null</code> if there is none. |
| 279 |
*/ |
| 280 |
private String getActivePerspectiveID() { |
| 281 |
IWorkbenchWindow window = DebugUIPlugin.getActiveWorkbenchWindow(); |
| 282 |
if (window != null) { |
| 283 |
IWorkbenchPage page = window.getActivePage(); |
| 284 |
if (page != null) { |
| 285 |
IPerspectiveDescriptor persp = page.getPerspective(); |
| 286 |
if (persp != null) { |
| 287 |
return persp.getId(); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
return null; |
| 292 |
} |
| 293 |
/** |
| 294 |
* Returns the launch configuration manager. |
| 295 |
* |
| 296 |
* @return launch configuration manager |
| 297 |
*/ |
| 298 |
private LaunchConfigurationManager getLaunchConfigurationManager() { |
| 299 |
return DebugUIPlugin.getDefault().getLaunchConfigurationManager(); |
| 300 |
} |
| 301 |
/** |
| 302 |
* Returns the launch group associatd with this action. |
| 303 |
* |
| 304 |
* @return the launch group associatd with this action |
| 305 |
*/ |
| 306 |
private LaunchGroupExtension getLaunchGroup(String fLaunchGroupIdentifier) { |
| 307 |
return getLaunchConfigurationManager().getLaunchGroup(fLaunchGroupIdentifier); |
| 308 |
} |
| 309 |
/** |
| 310 |
* Returns the mode of this action - run or debug |
| 311 |
* |
| 312 |
* @return the mode of this action - run or debug |
| 313 |
*/ |
| 314 |
private String getMode(String fLaunchGroupIdentifier) { |
| 315 |
return getLaunchGroup(fLaunchGroupIdentifier).getMode(); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Returns the category of this action - possibly <code>null</code> |
| 320 |
* |
| 321 |
* @return the category of this action - possibly <code>null</code> |
| 322 |
*/ |
| 323 |
private String getCategory(String fLaunchGroupIdentifier) { |
| 324 |
return getLaunchGroup(fLaunchGroupIdentifier).getCategory(); |
| 325 |
} |
| 326 |
|
| 327 |
} |