1 11 package org.eclipse.debug.ui.actions; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Set ; 19 20 import org.eclipse.core.expressions.EvaluationContext; 21 import org.eclipse.core.expressions.Expression; 22 import org.eclipse.core.expressions.IEvaluationContext; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.debug.core.DebugPlugin; 25 import org.eclipse.debug.core.ILaunchConfiguration; 26 import org.eclipse.debug.core.ILaunchManager; 27 import org.eclipse.debug.internal.ui.DebugUIPlugin; 28 import org.eclipse.debug.internal.ui.actions.LaunchConfigurationAction; 29 import org.eclipse.debug.internal.ui.actions.LaunchShortcutAction; 30 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager; 31 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension; 32 import org.eclipse.debug.internal.ui.stringsubstitution.SelectedResourceManager; 33 import org.eclipse.debug.ui.DebugUITools; 34 import org.eclipse.debug.ui.ILaunchGroup; 35 import org.eclipse.jface.action.ActionContributionItem; 36 import org.eclipse.jface.action.IAction; 37 import org.eclipse.jface.action.IMenuCreator; 38 import org.eclipse.jface.viewers.ISelection; 39 import org.eclipse.jface.viewers.IStructuredSelection; 40 import org.eclipse.swt.SWT; 41 import org.eclipse.swt.events.MenuAdapter; 42 import org.eclipse.swt.events.MenuEvent; 43 import org.eclipse.swt.widgets.Control; 44 import org.eclipse.swt.widgets.Menu; 45 import org.eclipse.swt.widgets.MenuItem; 46 import org.eclipse.ui.IObjectActionDelegate; 47 import org.eclipse.ui.IWorkbenchPart; 48 import org.eclipse.ui.PlatformUI; 49 import org.eclipse.ui.activities.WorkbenchActivityHelper; 50 51 65 public abstract class ContextualLaunchAction implements IObjectActionDelegate, IMenuCreator { 66 67 private IAction fDelegateAction; 68 private String fMode; 69 private ILaunchGroup fGroup = null; 71 private Map fGroupsByCategory = null; 73 private boolean fFillMenu = true; 75 76 81 public ContextualLaunchAction(String mode) { 82 fMode = mode; 83 ILaunchGroup[] groups = DebugUITools.getLaunchGroups(); 84 fGroupsByCategory = new HashMap (3); 85 for (int i = 0; i < groups.length; i++) { 86 ILaunchGroup group = groups[i]; 87 if (group.getMode().equals(mode)) { 88 if (group.getCategory() == null) { 89 fGroup = group; 90 } else { 91 fGroupsByCategory.put(group.getCategory(), group); 92 } 93 } 94 } 95 } 96 97 100 public void setActivePart(IAction action, IWorkbenchPart targetPart) { 101 } 103 106 public void dispose() { 107 } 109 112 public Menu getMenu(Control parent) { 113 return null; 115 } 116 119 public Menu getMenu(Menu parent) { 120 Menu menu = new Menu(parent); 122 127 menu.addMenuListener(new MenuAdapter() { 128 public void menuShown(MenuEvent e) { 129 if (fFillMenu) { 130 Menu m = (Menu)e.widget; 131 MenuItem[] items = m.getItems(); 132 for (int i=0; i < items.length; i++) { 133 items[i].dispose(); 134 } 135 fillMenu(m); 136 fFillMenu = false; 137 } 138 } 139 }); 140 return menu; 141 } 142 143 146 public void run(IAction action) { 147 } 149 150 153 public void selectionChanged(IAction action, ISelection selection) { 154 if (selection instanceof IStructuredSelection) { 156 fFillMenu = true; 157 if (fDelegateAction != action) { 158 fDelegateAction = action; 159 fDelegateAction.setMenuCreator(this); 160 } 161 action.setEnabled(true); 163 return; 164 } 165 action.setEnabled(false); 166 } 167 168 173 protected ILaunchManager getLaunchManager() { 174 return DebugPlugin.getDefault().getLaunchManager(); 175 } 176 177 181 protected void fillMenu(Menu menu) { 182 IStructuredSelection ss = SelectedResourceManager.getDefault().getCurrentSelection(); 183 int accelerator = 1; 184 if(!ss.isEmpty()) { 185 try { 186 ILaunchConfiguration config = getLaunchConfigurationManager().isSharedConfig(ss.getFirstElement()); 188 if(config != null && config.exists() && config.supportsMode(fMode)) { 189 IAction action = new LaunchConfigurationAction(config, fMode, config.getName(), DebugUITools.getDefaultImageDescriptor(config), accelerator++); 190 ActionContributionItem item = new ActionContributionItem(action); 191 item.fill(menu, -1); 192 } 193 } 194 catch (CoreException ce) {} 195 } 196 197 List allShortCuts = getLaunchConfigurationManager().getLaunchShortcuts(); 198 Iterator iter = allShortCuts.iterator(); 199 List filteredShortCuts = new ArrayList (10); 200 201 List selection = ss.toList(); 203 IEvaluationContext context = new EvaluationContext(null, selection); 204 context.setAllowPluginActivation(true); 205 context.addVariable("selection", selection); while (iter.hasNext()) { 207 LaunchShortcutExtension ext = (LaunchShortcutExtension) iter.next(); 208 try { 209 if (!WorkbenchActivityHelper.filterItem(ext) && isApplicable(ext, context)) { 210 filteredShortCuts.add(ext); 211 } 212 } 213 catch (CoreException e) {} 214 } 215 iter = filteredShortCuts.iterator(); 216 217 if(menu.getItemCount() > 0 && filteredShortCuts.size() > 0) { 219 new MenuItem(menu, SWT.SEPARATOR); 220 } 221 List categories = new ArrayList (); 222 while (iter.hasNext()) { 223 LaunchShortcutExtension ext = (LaunchShortcutExtension) iter.next(); 224 Set modes = ext.getModes(); Iterator modeIter = modes.iterator(); 226 while (modeIter.hasNext()) { 227 String mode = (String ) modeIter.next(); 228 if (mode.equals(fMode)) { 229 String category = ext.getCategory(); 230 if (categories!= null && !categories.contains(category)) { 232 categories.add(category); 233 } 234 populateMenuItem(mode, ext, menu, accelerator++); 235 } 236 } 237 } 238 239 if (categories.isEmpty()) { 241 if (accelerator > 1) { 242 new MenuItem(menu, SWT.SEPARATOR); 243 } 244 IAction action = new OpenLaunchDialogAction(fGroup.getIdentifier()); 245 ActionContributionItem item = new ActionContributionItem(action); 246 item.fill(menu, -1); 247 } else { 248 boolean addedSep = false; 249 iter = categories.iterator(); 250 while (iter.hasNext()) { 251 String category = (String ) iter.next(); 253 ILaunchGroup group = fGroup; 254 if (category != null) { 255 group = (ILaunchGroup) fGroupsByCategory.get(category); 256 } 257 if (group != null) { 258 if (accelerator > 1 && !addedSep) { 259 new MenuItem(menu, SWT.SEPARATOR); 260 addedSep = true; 261 } 262 IAction action = new OpenLaunchDialogAction(group.getIdentifier()); 263 ActionContributionItem item= new ActionContributionItem(action); 264 item.fill(menu, -1); 265 } 266 } 267 } 268 269 } 270 271 277 private boolean isApplicable(LaunchShortcutExtension ext, IEvaluationContext context) throws CoreException { 278 Expression expr = ext.getContextualLaunchEnablementExpression(); 279 return ext.evalEnablementExpression(context, expr); 280 } 281 282 285 private void populateMenuItem(String mode, LaunchShortcutExtension ext, Menu menu, int accelerator) { 286 LaunchShortcutAction action = new LaunchShortcutAction(mode, ext); 287 action.setActionDefinitionId(ext.getId() + "." + mode); String helpContextId = ext.getHelpContextId(); 289 if (helpContextId != null) { 290 PlatformUI.getWorkbench().getHelpSystem().setHelp(action, helpContextId); 291 } 292 StringBuffer label= new StringBuffer (); 293 if (accelerator >= 0 && accelerator < 10) { 294 label.append('&'); 296 label.append(accelerator); 297 label.append(' '); 298 } 299 String contextLabel= ext.getContextLabel(mode); 300 label.append((contextLabel != null) ? contextLabel : action.getText()); 302 action.setText(label.toString()); 303 ActionContributionItem item= new ActionContributionItem(action); 304 item.fill(menu, -1); 305 } 306 307 312 private LaunchConfigurationManager getLaunchConfigurationManager() { 313 return DebugUIPlugin.getDefault().getLaunchConfigurationManager(); 314 } 315 316 } 317 | Popular Tags |