【问题标题】:Vaadin using multiple context menusVaadin 使用多个上下文菜单
【发布时间】:2011-04-01 13:42:13
【问题描述】:

我正在尝试使用 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        });

感谢您的帮助! /马丁

【问题讨论】:

    标签: java contextmenu action vaadin


    【解决方案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) 以确保显示正确的上下文菜单。

    【讨论】:

    • 感谢您的帮助,但是,这不是我在自己的回答中所做的吗?还是我错过了什么?
    • 只有当你在Table.ValueChangeListener中的表上调用requestRepaint()时才有效,否则不会再次调用action handler的getActions()方法。
    【解决方案2】:

    所以我分解了匿名类并将它们变成内部类。还有一些新功能。当然欢迎所有反馈,希望这可能有助于其他人摆脱他们喜欢在 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;
    
        }
    
    }
    

    最好的问候 马丁

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-15
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多