1 11 package org.eclipse.debug.ui.actions; 12 13 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.debug.core.DebugPlugin; 18 import org.eclipse.debug.core.ILaunchMode; 19 import org.eclipse.debug.internal.ui.DebugUIPlugin; 20 import org.eclipse.debug.internal.ui.actions.LaunchShortcutAction; 21 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager; 22 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchGroupExtension; 23 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension; 24 import org.eclipse.jface.action.Action; 25 import org.eclipse.jface.action.ActionContributionItem; 26 import org.eclipse.jface.action.IAction; 27 import org.eclipse.jface.action.IMenuCreator; 28 import org.eclipse.jface.viewers.ISelection; 29 import org.eclipse.swt.events.MenuAdapter; 30 import org.eclipse.swt.events.MenuEvent; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Menu; 33 import org.eclipse.swt.widgets.MenuItem; 34 import org.eclipse.ui.IPerspectiveDescriptor; 35 import org.eclipse.ui.IWorkbenchPage; 36 import org.eclipse.ui.IWorkbenchWindow; 37 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2; 38 import org.eclipse.ui.PlatformUI; 39 import org.eclipse.ui.activities.WorkbenchActivityHelper; 40 41 52 public class LaunchAsAction extends Action implements IMenuCreator, IWorkbenchWindowPulldownDelegate2 { 53 54 57 private Menu fCreatedMenu; 58 59 62 private String fLaunchGroupIdentifier; 63 64 67 private IAction fAction; 68 69 75 public LaunchAsAction(String launchGroupIdentifier) { 76 super(); 77 fLaunchGroupIdentifier = launchGroupIdentifier; 78 ILaunchMode launchMode = DebugPlugin.getDefault().getLaunchManager().getLaunchMode(getMode()); 79 setText(launchMode.getLaunchAsLabel()); 80 setMenuCreator(this); 81 } 82 83 88 private LaunchGroupExtension getLaunchGroup() { 89 return getLaunchConfigurationManager().getLaunchGroup(fLaunchGroupIdentifier); 90 } 91 92 95 public void run() { 96 } 98 99 private void createAction(Menu parent, IAction action, int count) { 100 StringBuffer label= new StringBuffer (); 101 if (count < 10) { 103 label.append('&'); 104 label.append(count); 105 label.append(' '); 106 } 107 label.append(action.getText()); 108 action.setText(label.toString()); 109 ActionContributionItem item= new ActionContributionItem(action); 110 item.fill(parent, -1); 111 } 112 113 116 public void dispose() { 117 if (getCreatedMenu() != null) { 118 getCreatedMenu().dispose(); 119 } 120 } 121 122 125 public Menu getMenu(Control parent) { 126 return null; 127 } 128 129 132 public Menu getMenu(Menu parent) { 133 if (getCreatedMenu() != null) { 134 getCreatedMenu().dispose(); 135 } 136 setCreatedMenu(new Menu(parent)); 137 fillMenu(); 138 initMenu(); 139 return getCreatedMenu(); 140 } 141 142 private void fillMenu() { 143 List shortcuts = null; 145 String activePerspID = getActivePerspectiveID(); 146 if (activePerspID != null) { 147 shortcuts = getLaunchConfigurationManager().getLaunchShortcuts(activePerspID, getCategory()); 148 } 149 150 if (shortcuts == null || shortcuts.isEmpty()) { 153 shortcuts = getLaunchConfigurationManager().getLaunchShortcuts(getCategory()); 154 } 155 156 int menuCount = 1; 157 158 Iterator iter = shortcuts.iterator(); 159 while (iter.hasNext()) { 160 LaunchShortcutExtension ext = (LaunchShortcutExtension) iter.next(); 161 if (ext.getModes().contains(getMode()) && !WorkbenchActivityHelper.filterItem(ext)) { 162 populateMenu(ext, getCreatedMenu(), menuCount); 163 menuCount++; 164 } 165 } 166 } 167 168 171 private void initMenu() { 172 fCreatedMenu.addMenuListener(new MenuAdapter() { 175 public void menuShown(MenuEvent e) { 176 Menu m = (Menu)e.widget; 177 MenuItem[] items = m.getItems(); 178 for (int i=0; i < items.length; i++) { 179 items[i].dispose(); 180 } 181 fillMenu(); 182 } 183 }); 184 } 185 186 189 private void populateMenu(LaunchShortcutExtension ext, Menu menu, int menuCount) { 190 LaunchShortcutAction action = new LaunchShortcutAction(getMode(), ext); 191 action.setActionDefinitionId(ext.getId() + "." + getMode()); String helpContextId = ext.getHelpContextId(); 193 if (helpContextId != null) { 194 PlatformUI.getWorkbench().getHelpSystem().setHelp(action, helpContextId); 195 } 196 199 createAction(menu, action, menuCount); 200 } 201 202 206 private String getActivePerspectiveID() { 207 IWorkbenchWindow window = DebugUIPlugin.getActiveWorkbenchWindow(); 208 if (window != null) { 209 IWorkbenchPage page = window.getActivePage(); 210 if (page != null) { 211 IPerspectiveDescriptor persp = page.getPerspective(); 212 if (persp != null) { 213 return persp.getId(); 214 } 215 } 216 } 217 return null; 218 } 219 220 225 private String getMode() { 226 return getLaunchGroup().getMode(); 227 } 228 229 234 private String getCategory() { 235 return getLaunchGroup().getCategory(); 236 } 237 238 private Menu getCreatedMenu() { 239 return fCreatedMenu; 240 } 241 242 private void setCreatedMenu(Menu createdMenu) { 243 fCreatedMenu = createdMenu; 244 } 245 246 251 private LaunchConfigurationManager getLaunchConfigurationManager() { 252 return DebugUIPlugin.getDefault().getLaunchConfigurationManager(); 253 } 254 255 258 public void init(IWorkbenchWindow window) { 259 } 263 264 267 public void run(IAction action) { 268 } 270 271 274 public void selectionChanged(IAction action, ISelection selection) { 275 if (fAction == null) { 276 initialize(action); 277 } 278 } 279 280 284 private void initialize(IAction action) { 285 fAction = action; 286 action.setEnabled(existsShortcutsForMode()); 287 } 288 289 296 private boolean existsShortcutsForMode() { 297 List shortcuts = getLaunchConfigurationManager().getLaunchShortcuts(getCategory()); 298 Iterator iter = shortcuts.iterator(); 299 while (iter.hasNext()) { 300 LaunchShortcutExtension ext = (LaunchShortcutExtension) iter.next(); 301 if (ext.getModes().contains(getMode())) { 302 return true; 303 } 304 } 305 return false; 306 } 307 } 308 309 | Popular Tags |