1 11 package org.eclipse.debug.ui.actions; 12 13 14 import org.eclipse.debug.core.DebugPlugin; 15 import org.eclipse.debug.core.ILaunchConfiguration; 16 import org.eclipse.debug.core.ILaunchConfigurationType; 17 import org.eclipse.debug.core.ILaunchManager; 18 import org.eclipse.debug.internal.ui.DebugUIPlugin; 19 import org.eclipse.debug.internal.ui.ILaunchHistoryChangedListener; 20 import org.eclipse.debug.internal.ui.ILaunchLabelChangedListener; 21 import org.eclipse.debug.internal.ui.actions.ActionMessages; 22 import org.eclipse.debug.internal.ui.contextlaunching.LaunchingResourceManager; 23 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager; 24 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchHistory; 25 import org.eclipse.debug.ui.ILaunchGroup; 26 import org.eclipse.jface.action.ActionContributionItem; 27 import org.eclipse.jface.action.IAction; 28 import org.eclipse.jface.viewers.ISelection; 29 import org.eclipse.swt.SWT; 30 import org.eclipse.swt.events.MenuAdapter; 31 import org.eclipse.swt.events.MenuEvent; 32 import org.eclipse.swt.widgets.Control; 33 import org.eclipse.swt.widgets.Menu; 34 import org.eclipse.swt.widgets.MenuItem; 35 import org.eclipse.ui.IWorkbenchWindow; 36 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2; 37 38 import com.ibm.icu.text.MessageFormat; 39 40 52 public abstract class AbstractLaunchHistoryAction implements IWorkbenchWindowPulldownDelegate2, ILaunchHistoryChangedListener { 53 54 57 private Menu fMenu; 58 59 62 private IAction fAction; 63 64 68 private ILaunchGroup fLaunchGroup = null; 69 70 74 protected boolean fRecreateMenu = false; 75 76 82 public AbstractLaunchHistoryAction(String launchGroupIdentifier) { 83 fLaunchGroup = getLaunchConfigurationManager().getLaunchGroup(launchGroupIdentifier); 84 } 85 86 90 private ILaunchLabelChangedListener fLabelListener = new ILaunchLabelChangedListener() { 91 public ILaunchGroup getLaunchGroup() { 92 return fLaunchGroup; 93 } 94 public void labelChanged() { 95 updateTooltip(); 96 } 97 }; 98 99 104 private void setAction(IAction action) { 105 fAction = action; 106 } 107 108 113 protected IAction getAction() { 114 return fAction; 115 } 116 117 125 protected void addToMenu(Menu menu, IAction action, int accelerator) { 126 StringBuffer label= new StringBuffer (); 127 if (accelerator >= 0 && accelerator < 10) { 128 label.append('&'); 130 label.append(accelerator); 131 label.append(' '); 132 } 133 label.append(action.getText()); 134 action.setText(label.toString()); 135 ActionContributionItem item= new ActionContributionItem(action); 136 item.fill(menu, -1); 137 } 138 139 144 private void initialize(IAction action) { 145 getLaunchConfigurationManager().addLaunchHistoryListener(this); 146 setAction(action); 147 updateTooltip(); 148 action.setEnabled(existsConfigTypesForMode()); 149 } 150 151 158 private boolean existsConfigTypesForMode() { 159 ILaunchConfigurationType[] configTypes = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationTypes(); 160 for (int i = 0; i < configTypes.length; i++) { 161 if (configTypes[i].supportsMode(getMode())) { 162 return true; 163 } 164 } 165 return false; 166 } 167 168 176 protected void updateTooltip() { 177 getAction().setToolTipText(getToolTip()); 178 } 179 180 186 protected String getToolTip(ILaunchConfiguration configuration) { 187 String launchName= configuration.getName(); 188 String mode= getMode(); 189 String label; 190 if (mode.equals(ILaunchManager.RUN_MODE)) { 191 label= ActionMessages.AbstractLaunchHistoryAction_1; 192 } else if (mode.equals(ILaunchManager.DEBUG_MODE)){ 193 label= ActionMessages.AbstractLaunchHistoryAction_2; 194 } else if (mode.equals(ILaunchManager.PROFILE_MODE)){ 195 label= ActionMessages.AbstractLaunchHistoryAction_3; 196 } else { 197 label= ActionMessages.AbstractLaunchHistoryAction_4; 198 } 199 return MessageFormat.format(ActionMessages.AbstractLaunchHistoryAction_0, new String [] {label, launchName}); 200 } 201 202 208 private String getToolTip() { 209 String launchName = getLaunchingResourceManager().getLaunchLabel(fLaunchGroup); 210 if(launchName == null) { 211 return DebugUIPlugin.removeAccelerators(internalGetHistory().getLaunchGroup().getLabel()); 212 } 213 String label = null; 214 String mode = getMode(); 215 if (mode.equals(ILaunchManager.RUN_MODE)) { 216 label = ActionMessages.AbstractLaunchHistoryAction_1; 217 } else if (mode.equals(ILaunchManager.DEBUG_MODE)){ 218 label = ActionMessages.AbstractLaunchHistoryAction_2; 219 } else if (mode.equals(ILaunchManager.PROFILE_MODE)){ 220 label = ActionMessages.AbstractLaunchHistoryAction_3; 221 } else { 222 label = ActionMessages.AbstractLaunchHistoryAction_4; 223 } 224 if("".equals(launchName)) { return MessageFormat.format(ActionMessages.AbstractLaunchHistoryAction_5, new String [] {label}); 226 } 227 else { 228 return MessageFormat.format(ActionMessages.AbstractLaunchHistoryAction_0, new String [] {label, launchName}); 229 } 230 } 231 232 235 public void launchHistoryChanged() { 236 fRecreateMenu = true; 237 } 238 239 242 public void dispose() { 243 setMenu(null); 244 getLaunchConfigurationManager().removeLaunchHistoryListener(this); 245 getLaunchingResourceManager().removeLaunchLabelChangedListener(fLabelListener); 246 } 247 248 254 protected ILaunchConfiguration getLastLaunch() { 255 return getLaunchConfigurationManager().getFilteredLastLaunch(getLaunchGroupIdentifier()); 256 } 257 258 261 public Menu getMenu(Control parent) { 262 setMenu(new Menu(parent)); 263 fillMenu(fMenu); 264 initMenu(); 265 return fMenu; 266 } 267 268 271 public Menu getMenu(Menu parent) { 272 setMenu(new Menu(parent)); 273 fillMenu(fMenu); 274 initMenu(); 275 return fMenu; 276 } 277 278 281 private void initMenu() { 282 fMenu.addMenuListener(new MenuAdapter() { 285 public void menuShown(MenuEvent e) { 286 if (fRecreateMenu) { 287 Menu m = (Menu)e.widget; 288 MenuItem[] items = m.getItems(); 289 for (int i=0; i < items.length; i++) { 290 items[i].dispose(); 291 } 292 fillMenu(m); 293 fRecreateMenu= false; 294 } 295 } 296 }); 297 } 298 299 304 private void setMenu(Menu menu) { 305 if (fMenu != null) { 306 fMenu.dispose(); 307 } 308 fMenu = menu; 309 } 310 311 316 protected void fillMenu(Menu menu) { 317 ILaunchConfiguration[] historyList= getHistory(); 318 ILaunchConfiguration[] favoriteList = getFavorites(); 319 320 int accelerator = 1; 322 for (int i = 0; i < favoriteList.length; i++) { 323 ILaunchConfiguration launch= favoriteList[i]; 324 LaunchAction action= new LaunchAction(launch, getMode()); 325 addToMenu(menu, action, accelerator); 326 accelerator++; 327 } 328 329 if (favoriteList.length > 0 && historyList.length > 0) { 331 addSeparator(menu); 332 } 333 334 for (int i = 0; i < historyList.length; i++) { 336 ILaunchConfiguration launch= historyList[i]; 337 LaunchAction action= new LaunchAction(launch, getMode()); 338 addToMenu(menu, action, accelerator); 339 accelerator++; 340 } 341 } 342 343 348 protected void addSeparator(Menu menu) { 349 new MenuItem(menu, SWT.SEPARATOR); 350 } 351 352 355 public void run(IAction action) { 356 } 358 359 362 public void selectionChanged(IAction action, ISelection selection){ 363 if (fAction == null) { 364 initialize(action); 365 } 366 } 367 368 371 public void init(IWorkbenchWindow window) { 372 if (this instanceof AbstractLaunchToolbarAction) { 373 getLaunchingResourceManager().addLaunchLabelUpdateListener(fLabelListener); 374 } 375 } 376 377 385 protected LaunchHistory getLaunchHistory() { 386 return getLaunchConfigurationManager().getLaunchHistory(getLaunchGroupIdentifier()); 387 } 388 389 395 private LaunchHistory internalGetHistory() { 396 return getLaunchConfigurationManager().getLaunchHistory(getLaunchGroupIdentifier()); 397 } 398 399 409 protected ILaunchConfiguration[] getHistory() { 410 return LaunchConfigurationManager.filterConfigs(internalGetHistory().getHistory()); 411 } 412 413 423 protected ILaunchConfiguration[] getFavorites() { 424 return LaunchConfigurationManager.filterConfigs(internalGetHistory().getFavorites()); 425 } 426 427 432 protected String getMode() { 433 return internalGetHistory().getLaunchGroup().getMode(); 434 } 435 436 441 private LaunchConfigurationManager getLaunchConfigurationManager() { 442 return DebugUIPlugin.getDefault().getLaunchConfigurationManager(); 443 } 444 445 450 private LaunchingResourceManager getLaunchingResourceManager() { 451 return DebugUIPlugin.getDefault().getLaunchingResourceManager(); 452 } 453 454 461 protected String getLaunchGroupIdentifier() { 462 return fLaunchGroup.getIdentifier(); 463 } 464 } 465 | Popular Tags |