It's all about the answers!

Ask a question

Enabling menu items for specific attributes of the WorkItem


Swathi Rao (5112) | asked Oct 31 '07, 3:47 p.m.
JAZZ DEVELOPER
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



permanent link
Swathi Rao (5112) | answered Nov 19 '07, 1:48 a.m.
JAZZ DEVELOPER
Thanks for the information.

permanent link
Frank Lyner (91) | answered Nov 15 '07, 12:48 p.m.
swathi.rao schrieb:
:
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

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?

I played around a bit more and found out that the state property doesn't

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

permanent link
Swathi Rao (5112) | answered Nov 12 '07, 10:11 a.m.
JAZZ DEVELOPER
:
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


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?

permanent link
Frank Lyner (91) | answered Nov 08 '07, 5:38 a.m.
shufnagl schrieb:
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


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

permanent link
Stefan Hufnagl (29411920) | answered Nov 08 '07, 3:16 a.m.
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

permanent link
Frank Lyner (91) | answered Nov 07 '07, 4:28 a.m.
swathi.rao schrieb:
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


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

permanent link
Swathi Rao (5112) | answered Nov 06 '07, 10:05 a.m.
JAZZ DEVELOPER
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

permanent link
Frank Lyner (91) | answered Nov 06 '07, 5:28 a.m.
Patrick Streule schrieb:
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

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

permanent link
Patrick Streule (4.9k21) | answered Nov 02 '07, 10:37 a.m.
JAZZ DEVELOPER
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

permanent link
Stefan Hufnagl (29411920) | answered Nov 01 '07, 1:31 p.m.
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

Your answer


Register or to post 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.