Enabling menu items for specific attributes of the WorkItem
If I want to add a context menu item on a Work Item object, I can do so by extending the "org.eclipse.ui.popupMenus" extension point. However how can I add a filter to enable the menu item for specific attributes of the work item. For instance one might want to enable a menu item only for "Defect" type of Work Items or only for "High Priorty" Work Items etc.
The Eclipse help on Extension Point reference for PopupMenus specify that the selected object (which is the Work Item in the above example) must implement, or adapt to, org.eclipse.ui.IActionFilter interface and the action filter for the object class will then be exposed using the IAdaptable mechanism. However when I looked at the Jazz source code, it seemed that the WorkItem object did not implement the IActionFilter interface. Am I missing something here? Or is there a workaround to enable context menus for specific attributes of the WorkItem? |
10 answers
Hi Rao,
WorkItem Object: * Also I used "org.eclipse.ui.popupMenus" and the objectclass "com.ibm.team.workitem.common.model.IWorkItemHandle" to include my own action to WorkItems. Filter: * The action "Start Working" and "Stop Working" depends on "Status". If we understand this behavior it should be possible to implement menu items depending on the type "Defect". IActionFilter: * Also interesting for me... Stefan |
In general, you may want to take a look at PropertyTester to base the
action enablement on an item's attributes. However: The work item handle does not contain any attributes and if you contribute your action to IWorkItem, there is no guarantee that the property you need is actually set on the item, and accessing a missing attribute without a preceding check will cause an assertion failure. Regards, Patrick |
Patrick Streule schrieb:
In general, you may want to take a look at PropertyTester to base the Here is the pattern I would recommend to solve this problem: 1. Contribute your action which implements IObjectActionDelegate to the handle (in this case IWorkItemHandle) 2. Check in your implementation of the IObjectActionDelegate.selectionChanged() if the item is cached using: IWorkItem workitem= (IWorkItem) ITeamRepository.itemManager().getSharedItemIfKnown(IItemHandle itemHandle) If workitem is null or doesn't match your enablement condition use action.setEnabled(false) to disable the action. Enable if the contition is met. Please note that the action will show up in the menu when you disable it but this is correct w.r.t. the Eclipse UI Guidelines. HTH Regards, Frank |
I tried it and it works fine. The only thing is that for the first time if I right-click on a WorkItem which does not meet the condition, it shows up in the context menu as "enabled" and on clicking it, it throws a message stating that the chosen operation is not enabled. However I guess this is acceptable since it is a one time thing.
Thanks for the info Frank. It certainly helped :D |
swathi.rao schrieb:
I tried it and it works fine. The only thing is that for the first Glad I could help. To get rid of your last problem, make sure that your action contribution in the plugin.xml has the state property set to false. This will disable the action per default. Regards, Frank |
Hi,
also from me thanks for your hints. Question: * The only obstacle I have is the "IWorkItem workitem= (IWorkItem) ITeamRepository.itemManager().getSharedItemIfKnown(IItemHandle itemHandle)" part * Do you have a code snippet? Stefan |
shufnagl schrieb:
Hi, Here is what you selectionChanged() method would look like (assuming that you have chosen that the action only enablesFor 1, see plugin contribution): public void selectionChanged(IAction action, ISelection selection) { // store selection fSelection= selection; if (selection instanceof IStructuredSelection) { IStructuredSelection structuredSelection= (IStructuredSelection) selection; for (Iterator iter= structuredSelection.iterator(); iter.hasNext(); ) { IWorkItemHandle handle= (IWorkItemHandle) iter.next(); ITeamRepository repository= (ITeamRepository) handle.getOrigin(); if (repository != null) { IWorkItem workItem= (IWorkItem) repository.itemManager().getSharedItemIfKnown(handle); if (workItem != null) { if (checkCondition(workItem)) { action.setEnabled(true); return; } } } } } action.setEnabled(false); } checkCondition() would then check if the workitem matches the enablement condition. Make sure to check if the workitem really matches your required profile by calling: workItem.isPropertySet(propertyName) for every property you need. Accessing an unresolved property throws an exception. Of course you can replace IWorkItem and IWorkItemHandle with every Jazz item type (subclassing IItemHandle). Regards, Frank |
: I set the State Property to "false" to see if the action would be disabled by default. However I noted that the action was still enabled for the first time when I right-clicked on a WorkItem which did not meet the specified condition. Is this a defect or am I missing something? |
swathi.rao schrieb:
: have an effect if you set the action enablement in the selectionChanged() method. Here's an example where it works fine: com.ibm.team.workitem.rcp.ui.internal.actions.MarkReadAction |
|
Your answer
Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.