Vaadin使用多个上下文菜单

时间:2011-04-01 13:42:13

标签: java contextmenu action vaadin

我尝试使用Vaadin创建一个表,在上下文菜单中有不同的选项,具体取决于您是选择了一行还是多行。

我花了一段时间才做到这一点,但现在我有一个有效的解决方案。问题是感觉它不是很好的编码实践,我很乐意接受任何建议,如何将我的“功能”分成更小的类或功能。我可以创建一个独立的Action类吗?请随意评论和建议,请注意我刚开始使用Vaadin =)!

          Table contactList = new Table("Test table");
 3        contactList.addListener(new Property.ValueChangeListener(){
 4            public void valueChange(ValueChangeEvent event){            
 5                Set<?> value = (Set<?>) event.getProperty().getValue();
 6                if(value == null || value.size() == 0){
 7                    getMainWindow().showNotification("NULL or 0");
 8                }else if(value.size() == 1){
 9                    contactList.removeAllActionHandlers();
10                    contactList.addActionHandler(new Action.Handler(){
11                        public Action[] getActions(Object target, Object sender){                           
12                            return ACTIONS_EDIT;                        
13                        }                        
14                        public void handleAction(Action action, Object sender, Object target){                               
15                            getMainWindow().showNotification("ACTION_EDIT");                               
16                        }
17                    });
18                }else{
19                    contactList.removeAllActionHandlers();
20                    contactList.addActionHandler(new Action.Handler(){
21                        public Action[] getActions(Object target, Object sender){                           
22                            return ACTIONS_EDIT_ALL;                        
23                        }                        
24                        public void handleAction(Action action, Object sender, Object target){                               
25                            getMainWindow().showNotification("ACTION_EDIT_ALL");                               
26                        }
27                    });       
28                }
29            }
30        });

Thx任何帮助! / Marthin

2 个答案:

答案 0 :(得分:1)

对我而言,这看起来过于复杂。

您可以只检查处理程序中的选择数,而不是更改操作处理程序。类似的东西:

 contactList.addActionHandler(new Action.Handler(){
      public Action[] getActions(Object target, Object sender){                           
           Set<?> value = (Set<?>) contactList.getValue();
           return (value == null || value.size() == 0) = ACTIONS_EDIT : ACTIONS_EDIT_ALL;                       
      }                    
      public void handleAction(Action action, Object sender, Object target){                               
           getMainWindow().showNotification("Action: " + action);                               
      }
 });

没有测试代码以查看它是否有效。我怀疑你必须设置contactList.setImmediate(true)以确保显示正确的上下文菜单。

答案 1 :(得分:0)

所以我打破了anonymouse类并将它们变成了内部类。还有一些新的功能。所有反馈都是受欢迎的,这可能会帮助其他人摆脱他们喜欢在Vaadin中使用的匿名课程。

class ExtendedTableFieldFactory implements TableFieldFactory{
    private static final long serialVersionUID = 1L;

    public Field createField(Container container, Object itemId, Object propertyId, Component uiContext){
        if(selectedRows != null){
            if(selectedRows.contains(itemId)){              
                return new com.vaadin.ui.TextField();
            }
        }
        return null;
    }
}


/**
 * Event handling for the table.
 * If one or more rows has been selected we set the corresponding action.
 * The action repaints the context menu.
 */ 
class ExtendedValueChangeListener implements Property.ValueChangeListener
{
    private static final long serialVersionUID = 1L;

    @Override
    public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
        setEditable(false);
        selectedRows = (Set<T>) event.getProperty().getValue();


        if(selectedRows == null || selectedRows.size() == 0){
            extActionHandler.setCurrentAction(ACTIONS_EDIT);        
        }else if(selectedRows.size() == 1){
            extActionHandler.setCurrentAction(ACTIONS_EDIT);        
            requestRepaint();
        }else{
            if(extActionHandler.getCurrentAction() != ACTIONS_EDIT_ALL)
            {                   
                extActionHandler.setCurrentAction(ACTIONS_EDIT_ALL);
                requestRepaint();
            }
        }
    }       
}


/**
 * The action handler is the context menu in the table.
 * We have a handler that takes the appropriate action on events.
 */
class ExtendedActionHandler implements Action.Handler{
    private static final long serialVersionUID = 1L;
    private Action[] currentAction = null;

    @Override
    public Action[] getActions(Object target, Object sender) {          
        System.out.println("calling GETACTIONS!");
        return currentAction;
    }

    @Override
    public void handleAction(Action action, Object sender, Object target) {
        System.out.println("calling handleActions aciton: "+action);
        if(action == ACTION_EDIT_ALL_MODAL){
            if (subwindow != null && subwindow.getParent() != null) {
                subwindow.focus();                  
            } else {          
                subwindow = new Window("Edit contacts");
                subwindow.setModal(true);

                VerticalLayout layout = (VerticalLayout) subwindow.getContent();
                layout.setMargin(true);
                layout.setSpacing(true);

                T data = selectedRows.iterator().next();
                try {
                    T dataEmpty = (T) data.getClass().newInstance();                        
                    modalForm.setItemDataSource(new BeanItem<T>(dataEmpty));

                } catch (InstantiationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                subwindow.addComponent(modalForm);
                subwindow.setWidth("720px");
                subwindow.center();                 
                getParent().getWindow().addWindow(subwindow);

                //getWindow().addWindow(subwindow);

            }
        }else{
            setEditable(true);
        }       

    }       
    public Action[] getCurrentAction() {
        return currentAction;
    }

    public void setCurrentAction(Action[] currentAction) {
        System.out.println("setting current action to: " + currentAction);
        this.currentAction = currentAction;

    }

}

最诚挚的问候 Marthin