Hi all,
as in another post with the same title (https://vaadin.com/forum/-/message_boards/view_message/245995) i’m tryng to get different context menu on single item and multiple items selection.
I found also this: http://stackoverflow.com/questions/5514223/vaadin-using-multiple-context-menus
firstly i’ve tried this:
static final Action ACTION_NEW = new Action("Nuovo");
static final Action ACTION_EDIT = new Action("Modifica");
static final Action ACTION_DELETE = new Action("Elimina");
static final Action[] ITEM_ACTIONS = new Action[]
{ ACTION_EDIT, ACTION_NEW, ACTION_DELETE };
static final Action[] MULTI_ITEM_ACTIONS = new Action[]
{ ACTION_DELETE, ACTION_NEW };
static final Action[] NO_ITEM_ACTIONS = new Action[]
{ ACTION_NEW };
Action[] action = NO_ITEM_ACTIONS;
public TestApplication() {
final Table table = new Table();
table.setSelectable(true);
table.setMultiSelect(true);
table.setImmediate(true);
table.addListener(new Property.ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
Collection value = (Collection) event.getProperty().getValue();
if(value.size() == 0){
action = NO_ITEM_ACTIONS;
}else if(value.size() == 1){
action = ITEM_ACTIONS;
}else{
action = MULTI_ITEM_ACTIONS;
}
table.requestRepaint();
}
});
table.addActionHandler(new Handler() {
@Override
public void handleAction(Action action, Object sender, Object target) {
System.out.println("action" + action.getCaption());
}
@Override
public Action[] getActions(Object target, Object sender) {
if(action == NO_ITEM_ACTIONS){
System.out.println("NO_ITEM_ACTIONS");
}else if(action == ITEM_ACTIONS){
System.out.println("ITEM_ACTIONS");
}else{
System.out.println("MULTI_ITEM_ACTIONS");
}
return action;
}
});
the method getActions is called at each right click and it return the rught Action but the UI didn’t seem to care and still use the NO_ITEM_ACTIONS (the first setted)
then i have tryed this:
table.addListener(new Property.ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
Collection value = (Collection) event.getProperty().getValue();
if(value.size() == 0){
action = NO_ITEM_ACTIONS;
}else if(value.size() == 1){
action = ITEM_ACTIONS;
}else{
action = MULTI_ITEM_ACTIONS;
}
table.removeAllActionHandlers();
table.addActionHandler(new Handler() {
@Override
public void handleAction(Action action, Object sender, Object target) {
System.out.println("action" + action.getCaption());
}
@Override
public Action[] getActions(Object target, Object sender) {
if(action == NO_ITEM_ACTIONS){
System.out.println("NO_ITEM_ACTIONS");
}else if(action == ITEM_ACTIONS){
System.out.println("ITEM_ACTIONS");
}else{
System.out.println("MULTI_ITEM_ACTIONS");
}
return action;
}
});
table.requestRepaint();
}
});
it work, but i lost the no-selection contextual menu and to get the menu i must select one or more items,
so, i add this:
table.addActionHandler(new Handler() {
@Override
public void handleAction(Action action, Object sender, Object target) {
System.out.println("action" + action.getCaption());
}
@Override
public Action[] getActions(Object target, Object sender) {
if(action == NO_ITEM_ACTIONS){
System.out.println("NO_ITEM_ACTIONS");
}else if(action == ITEM_ACTIONS){
System.out.println("ITEM_ACTIONS");
}else{
System.out.println("MULTI_ITEM_ACTIONS");
}
return action;
}
});
now i get the no-item contextual menu, but also a very strange UI behaviour: the UI menu keep the initial menu lenght, so: if the NO_ITEM_ACTIONS has 1 Action i only see the first action on the other menues, or if the first menu has 3 items and the others only 2, i get the correct 2 items but the third of the previous menu.
(this seem to me a graphic bug, since clicking the third item give no callback)
How can i solve this ? Any workaround ?