PDA

View Full Version : dynamic context menu on tree



nakhli
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

Tree.addBeforeShowContextMenuHandler(BeforeShowContextMenuEvent.BeforeShowContextMenuHandler)

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

Any ideas on how to achieve this? Thank you.

PhiLho
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?

PhiLho
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);
}

@Override
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.

mspronk
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.setIcon(Resources.ICONS.addIcon());
insert.addSelectionListener(new SelectionListener<MenuEvent>() {
public void componentSelected(MenuEvent ce) {
ModelData folder = tree.getSelectionModel().getSelectedItem();
// Do stuff
}
});
contextMenu.add(insert);

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

// 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
contextMenu.getItems().get(0).disable();
contextMenu.getItems().get(1).enable();
} else {
// If it's a client
contextMenu.getItems().get(0).enable();
contextMenu.getItems().get(1).disable();
}
}
});

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

sven
12 Mar 2012, 8:14 AM
http://www.sencha.com/forum/showthread.php?186193-Beta4-BeforeShowContextMenuHandler-onBeforeShowContextMenu-never-gets-called