2011-04-01 63 views
1

我试图用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

回答

0

所以我打破了anonymouse类,并将它们提供给内部类。还有一些新的功能。所有的反馈都是可以接受的,可以帮助其他人摆脱他们喜欢在Vaadin使用的anonymouse类。

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

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

+0

感谢您的帮助,但是,这不是我在自己的答案做什么?或者我错过了什么? – Marthin 2011-04-11 10:51:11

+1

这只适用于在Table.ValueChangeListener中的表上调用requestRepaint()方法,否则不会再调用操作处理函数的getActions()方法。 – 2011-10-04 14:09:26