View Full Version : dynamic context menu on tree

25 Jan 2012, 4:38 AM
I am trying to modify the visible context menu items depending on the selected node in a tree.
I tried using


but it is not getting called before the context menu is shown.

Any ideas on how to achieve this? Thank you.

29 Feb 2012, 5:08 AM
+1 (for Beta3)

I need to customize the context menu depending on the clicked node, but the method isn't called.
I explored a bit the sources, but I don't see where any BeforeShowContextMenuEvent is created.
Is it on the TODO list of Sencha?

6 Mar 2012, 2:23 AM
For the record, there is a workaround, using low-level Dom events:

private class NodeCell extends SimpleSafeHtmlCell<String>
// http://stackoverflow.com/questions/5185078/what-is-the-list-of-valid-gwt-dom-consumed-events-for-cells
private static final String NODE_EVENT_CLICK = "click";
private static final String NODE_EVENT_CONTEXTMENU = "contextmenu";

* Constructor, using a default renderer and defining the events we are interested in.
public NodeCell()
super(SimpleSafeHtmlRenderer.getInstance(), NODE_EVENT_CLICK, NODE_EVENT_CONTEXTMENU);

public void onBrowserEvent(
Context context, Element parent, String value, NativeEvent event,
ValueUpdater<String> valueUpdater)
super.onBrowserEvent(context, parent, value, event, valueUpdater);
if (event.getType().equals(NODE_EVENT_CLICK))
handleClick(context, value);
else if (event.getType().equals(NODE_EVENT_CONTEXTMENU))
handleContextMenu(context, value);
It is not perfect as we get a Context, ie. only a string id, not the data associated to a node, but that's better than nothing and it allows to change the context menu state (disable, hide items) before displaying it.
Note also the management of click on a node, as the select event is fired even if we just open a parent node: if we associate a complex display to such parent, we don't necessarily want to display it on each step of navigating the tree.
Think "file explorer" where you might want to go down a hierarchy without displaying the list of files of each level, for example.
If GXT 3 could distinguish the two events (click to fold / unfold a node, click on the node itself) at a higher level, it would be nice.

6 Mar 2012, 5:55 PM
I ran into a similar need to customize the context menu and based on another thread on this forum I was able to accomplish what I needed by selectively showing and hiding (or in my case enabling and disabling) previously created menu items.

final TreePanel<ModelData> tree = new TreePanel<ModelData>(store);

final Menu contextMenu = new Menu();
MenuItem insert = new MenuItem();
insert.setText("Insert New List");
insert.addSelectionListener(new SelectionListener<MenuEvent>() {
public void componentSelected(MenuEvent ce) {
ModelData folder = tree.getSelectionModel().getSelectedItem();
// Do stuff

MenuItem remove = new MenuItem();
remove.setText("Remove Selected List");
remove.addSelectionListener(new SelectionListener<MenuEvent>() {
public void componentSelected(MenuEvent ce) {
List<ModelData> selected = tree.getSelectionModel().getSelectedItems();
for (ModelData sel : selected) {
// Do stuff

// Enable/disable applicable context menu items depending on the type
tree.addListener(Events.ContextMenu, new Listener<TreePanelEvent<ModelData>>() {
public void handleEvent(TreePanelEvent<ModelData> be) {
if (be.getItem() instanceof SubscriberList) {
// If it's a list
} else {
// If it's a client

7 Mar 2012, 1:06 AM
What is this TreePanel? I don't find it in Eclipse.

12 Mar 2012, 8:14 AM