PDA

View Full Version : ContextMenu + TreeItem + different menus



mmiotk
24 Apr 2008, 1:22 AM
I want to create a Tree which different context menu for each TreeItem.
What is best solution to do it?

cdasher
24 Apr 2008, 3:35 AM
This is how I am doing it (if there is a better way please let me know) I have to destroy the menu everytime if not the previous items are still there (even if I call removeAll() which is supposed to remove all child MenuItems :)

tree.addListener(Events.ContextMenu, new Listener() {
public void handleEvent(BaseEvent be) {

if (be instanceof ComponentEvent) {
ComponentEvent ce = (ComponentEvent) be;

if (ce.component instanceof Tree) {
Tree t = (Tree) ce.component;

if (t.getSelectedItem() instanceof TreeItem) {
TreeItem ti = (TreeItem) t.getSelectedItem();

if (ti.getData() instanceof MyObject) {
final MyObject myObject = (MyObject) ti.getData();

if (myObject.getSomePropertyToCheck().equals("SOME STRING PROPERTY PERHAPS")){
Menu menu = t.getContextMenu();
menu.destroy();
menu = new Menu();
menu.setWidth(130);
TextMenuItem dynamic = new TextMenuItem("Add Text Menu Item 1");
dynamic.addSelectionListener(new SelectionListener(){

public void componentSelected(
ComponentEvent ce) {

//Do something when the context menu is selected



}});
menu.add(dynamic);
t.setContextMenu(menu);
} else if (myObject.getSomePropertyToCheck().equals("SOME STRING PROPERTY PERHAPS")){
Menu menu = t.getContextMenu();
menu.destroy();
menu = new Menu();
menu.setWidth(130);
TextMenuItem dynamic = new TextMenuItem("Add Text Menu Item 2");
dynamic.addSelectionListener(new SelectionListener(){

public void componentSelected(
ComponentEvent ce) {

//Do something when the context menu is selected



}});
menu.add(dynamic);
t.setContextMenu(menu);
} else {
Menu menu = t.getContextMenu();
menu.destroy();

}

}

}

}

}


}
});

mmiotk
24 Apr 2008, 4:19 AM
I saw this method at the old forum (myGWT), but i thought that it's any better, recommended method now (Ext GWT).

I have one more question.

If I create menu by this method, my menu can't auto resize width to its content. If I create one menu with long text, this text escapes the border of menu. With myGWT it worked correctly.

jayj
24 Apr 2008, 5:00 AM
I am having a similar problem in firefox where the menus don't size to fit the text in them. Seems to work fine in IE.

Cypher
24 Jun 2008, 9:57 AM
Hi guys...I'd like to also create a tree with different context menus on tree items...any better solution than the one mentioned above? Thanks.

zaccret
24 Jun 2008, 11:01 PM
Instead of destroying the menu each time, I add all items for all menus in the context menu. When a tree item is selected, I show()/hide() some context menu items.

assassin
25 Jun 2008, 12:14 AM
Since RC 1.0b the removeAll and Width bugs are fixed. So with this version you can create a dynamic menu like this:



final Menu dynamicMenu = new Menu();
dynamicMenu.setAutoWidth(true);
dynamicMenu.setAutoHeight(true);
Tree.setContentMenu(dynamicMenu)

dynamicMenu.addListener(Events.BeforeShow, new Listener<MenuEvent>(){
public void handleEvent(MenuEvent e) {
dynamicMenu.removeAll();
List<String> items = testData.getMenuItems();
for(String item : items){
dynamicMenu.add(new MenuItem(item));
}
}
}