private void selectPerspective() { // let the handler perform the work to consolidate all the code ParameterizedCommand command = commandService.createCommand( IWorkbenchCommandConstants.PERSPECTIVES_SHOW_PERSPECTIVE, Collections.EMPTY_MAP); handlerService.executeHandler(command); }
@Inject public void setSelection(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) Contact contact) { if (contact != null) { if (dirtyable.isDirty()) { MessageDialog dialog = new MessageDialog( detailComposite.getShell(), "Save vCard", null, "The current vCard has been modified. Save changes?", MessageDialog.CONFIRM, new String[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL}, 0); dialog.create(); ThemeUtil.applyDialogStyles(engine, dialog.getShell()); if (dialog.open() == Window.OK) { ParameterizedCommand saveCommand = commandService.createCommand("contacts.save", Collections.EMPTY_MAP); handlerService.executeHandler(saveCommand); } } updatePartTitle(contact); } else { uiItem.setLabel("Details"); } dirtyable.setDirty(false); if (!detailComposite.isDisposed()) { detailComposite.update(contact); } }
private void setEnabled(MHandledMenuItem itemModel, MenuItem newItem) { ParameterizedCommand cmd = itemModel.getWbCommand(); if (cmd == null) { return; } final IEclipseContext lclContext = getContext(itemModel); EHandlerService service = lclContext.get(EHandlerService.class); final IEclipseContext staticContext = EclipseContextFactory.create(HMI_STATIC_CONTEXT); ContributionsAnalyzer.populateModelInterfaces( itemModel, staticContext, itemModel.getClass().getInterfaces()); try { itemModel.setEnabled(service.canExecute(cmd, staticContext)); } finally { staticContext.dispose(); } newItem.setEnabled(itemModel.isEnabled()); }
private void simulateMenuSelection(MMenuItem item) { // FIXME: pity this code isn't available through the MMenuItem instance // somehow IEclipseContext lclContext = getContext(item); if (item instanceof MDirectMenuItem) { MDirectMenuItem dmi = (MDirectMenuItem) item; if (dmi.getObject() == null) { IContributionFactory cf = (IContributionFactory) lclContext.get(IContributionFactory.class.getName()); dmi.setObject(cf.create(dmi.getContributionURI(), lclContext)); } lclContext.set(MItem.class.getName(), item); ContextInjectionFactory.invoke(dmi.getObject(), Execute.class, lclContext); lclContext.remove(MItem.class.getName()); } else if (item instanceof MHandledMenuItem) { MHandledMenuItem hmi = (MHandledMenuItem) item; EHandlerService service = (EHandlerService) lclContext.get(EHandlerService.class.getName()); ParameterizedCommand cmd = hmi.getWbCommand(); if (cmd == null) { cmd = HandledMenuItemRenderer.generateParameterizedCommand(hmi, lclContext); } lclContext.set(MItem.class.getName(), item); service.executeHandler(cmd); lclContext.remove(MItem.class.getName()); } else { statusReporter .get() .report( new Status( IStatus.WARNING, CocoaUIProcessor.FRAGMENT_ID, "Unhandled menu type: " + item.getClass() + ": " + item), //$NON-NLS-1$ //$NON-NLS-2$ $NON-NLS-2$ StatusReporter.LOG); } }
/** * Delegate to the handler for the provided command id. * * @param commandId * @return true if the command was found, false otherwise */ private boolean runCommand(String commandId) { if (commandService == null || handlerService == null) { return false; } ParameterizedCommand cmd = commandService.createCommand(commandId, Collections.emptyMap()); if (cmd == null) { return false; } // Unfortunately there's no way to check if a handler was available... // EHandlerService#executeHandler() returns null if a handler cannot be // found, but the handler itself could also return null too. handlerService.executeHandler(cmd); return true; }