本文介绍了Vaadin使用多个上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Vaadin创建一个表,根据您选择的是单行还是多行,您可以在上下文菜单中使用不同的选项。

Im trying to create a Table with Vaadin where you have different options in the context menu depending on if you have selected a single row or multiple rows.

我需要一段时间才能做到这一点,但是现在我有了一个可行的解决方案。问题是感觉它不是很好的编码实践,我很乐意就如何将我的函数划分为较小的类或函数提出建议。也许我可以创建一个独立的Action类?随意发表评论和建议,请注意,我刚开始使用Vaadin =)!

It took me a while to do this but now i have a working solution. The problem is that is feel that its not good coding practice and I would gladly take any advice in how to perhaps split my "function" into smaller classes or functions. Could I perhaps create a standalone Action class?. Feel free to comment and advice and please do note that I just started with 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        });

谢谢!
/ Marthin

Thx for any help!/Marthin

推荐答案

因此,我分解了匿名类,并将其设置为内部类。还有一些新功能。当然,欢迎您提供所有反馈,希望这可以帮助其他人摆脱他们喜欢在Vaadin中使用的匿名类。

So i broke down the anonymouse classes and made them to inner classes. There are also some new functionalities. All feedback is welcome ofcourse, hopfully this might help someone else to break free the anonymouse classes they like to use in 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

Best RegardsMarthin

这篇关于Vaadin使用多个上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:50