is it the right choice ?

Hay,

I begin in Vaadin,

I try to understand Event working in java, and in Vaadin.

To illustrate my missunderstanding, i try to make a component called ManagementButtons composed by 3 buttons : add, edit, and delete.

I want to add 3 listener to my component corresponding to the 3 actions.

I tried to make the same behaviour than Button class, for it only Event : Click;

Here you can see my class :


public class ManagementButtons extends HorizontalLayout 
{
    private Button addButton;
    private Button editButton;
    private Button deleteButton;
    
    public ManagementButtons()
    {
        super();
        
        addButton = new Button("add");
        editButton = new Button("edit");
        deleteButton = new Button("delete");
        
        addComponent(addButton);
        addComponent(editButton);
        addComponent(deleteButton);
        
        addButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                fireAddEvent();
            }
        });
        
        editButton.addListener(new Button.ClickListener() {
            public void buttonClick(Button.ClickEvent event) {
                fireEditEvent();
            }
        });
        
        deleteButton.addListener(new Button.ClickListener() {
            public void buttonClick(Button.ClickEvent event) {
                fireDeleteEvent();
            }
        });
    }
    
    public void fireAddEvent()
    {
        fireEvent(new AddClickEvent(this));
    }
    
    public void fireEditEvent()
    {
        fireEvent(new EditClickEvent(this));
    }
    
    public void fireDeleteEvent()
    {
        fireEvent(new DeleteClickEvent(this));
    }
    
    private static final Method ADD_BUTTON_CLICK_METHOD;
    private static final Method EDIT_BUTTON_CLICK_METHOD;
    private static final Method DELETE_BUTTON_CLICK_METHOD;
    static {
        try {
            ADD_BUTTON_CLICK_METHOD = AddClickListener.class.getDeclaredMethod(
                    "addButtonClick", new Class[] { Button.ClickEvent.class });
            EDIT_BUTTON_CLICK_METHOD = EditClickListener.class.getDeclaredMethod(
                    "editButtonClick", new Class[] { Button.ClickEvent.class });
            DELETE_BUTTON_CLICK_METHOD = DeleteClickListener.class.getDeclaredMethod(
                    "deleteButtonClick", new Class[] { Button.ClickEvent.class });
        } catch (final java.lang.NoSuchMethodException e) {
            // This should never happen
            throw new java.lang.RuntimeException(
                    "Internal error finding methods in Button");
        }
    }
    
    public interface AddClickListener extends Serializable 
    {
        public void addButtonClick(Button.ClickEvent event);        
    }
    
    public interface EditClickListener extends Serializable 
    {
        public void editButtonClick(Button.ClickEvent event);        
    }
    
    public interface DeleteClickListener extends Serializable 
    {
        public void deleteButtonClick(Button.ClickEvent event);        
    }
    
    public void addListener(AddClickListener listener) {
        addListener(AddClickEvent.class, listener, ADD_BUTTON_CLICK_METHOD);
    }
    
    public void addListener(EditClickListener listener) {
        addListener(EditClickEvent.class, listener, EDIT_BUTTON_CLICK_METHOD);
    }
    
    public void addListener(DeleteClickListener listener) {
        addListener(DeleteClickEvent.class, listener, DELETE_BUTTON_CLICK_METHOD);
    }
    
    public class AddClickEvent extends Button.Event {
        public AddClickEvent(Component source) 
        {
            super(source);
        }
    }
    
    public class EditClickEvent extends Button.Event {
        public EditClickEvent(Component source) 
        {
            super(source);
        }
    }
    
    public class DeleteClickEvent extends Button.Event {
        public DeleteClickEvent(Component source) 
        {
            super(source);
        }
    }  
}

That is the right way to do what I want to do ?

More over, that don’t work because an IllegalArguementException is thrown when I try to access to addListener method : addListener(AddClickListener listener)

java.lang.IllegalArgumentException: Method requires unknown parameters

Can you help me, or give me any url wich explain how to do what i want to do : i don’t find !

and sorry for my poor english,

Thanks.

OK ! with little modifications, my class work.

If anyone want to look the correcttions,

It look like that :


public class ManagementButtons extends HorizontalLayout 
{
    private Button addButton;
    private Button editButton;
    private Button deleteButton;
    
    public ManagementButtons()
    {
        super();
        
        addButton = new Button("add");
        editButton = new Button("edit");
        deleteButton = new Button("delete");
        
        addComponent(addButton);
        addComponent(editButton);
        addComponent(deleteButton);
        
        addButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                fireAddEvent();
            }
        });
        
        editButton.addListener(new Button.ClickListener() {
            public void buttonClick(Button.ClickEvent event) {
                fireEditEvent();
            }
        });
        
        deleteButton.addListener(new Button.ClickListener() {
            public void buttonClick(Button.ClickEvent event) {
                fireDeleteEvent();
            }
        });
    }
    
    public void fireAddEvent()
    {
        fireEvent(new AddClickEvent(this));
    }
    
    public void fireEditEvent()
    {
        fireEvent(new EditClickEvent(this));
    }
    
    public void fireDeleteEvent()
    {
        fireEvent(new DeleteClickEvent(this));
    }
    
    private static final Method ADD_BUTTON_CLICK_METHOD;
    private static final Method EDIT_BUTTON_CLICK_METHOD;
    private static final Method DELETE_BUTTON_CLICK_METHOD;
    static {
        try {
            ADD_BUTTON_CLICK_METHOD = AddClickListener.class.getDeclaredMethod(
                    "addButtonClick", new Class[] { AddClickEvent.class });
            EDIT_BUTTON_CLICK_METHOD = EditClickListener.class.getDeclaredMethod(
                    "editButtonClick", new Class[] { EditClickEvent.class });
            DELETE_BUTTON_CLICK_METHOD = DeleteClickListener.class.getDeclaredMethod(
                    "deleteButtonClick", new Class[] { DeleteClickEvent.class });
        } catch (final java.lang.NoSuchMethodException e) {
            // This should never happen
            throw new java.lang.RuntimeException(
                    "Internal error finding methods in Button");
        }
    }
    
    public interface AddClickListener extends Serializable 
    {
        public void addButtonClick(AddClickEvent event);        
    }
    
    public interface EditClickListener extends Serializable 
    {
        public void editButtonClick(EditClickEvent event);        
    }
    
    public interface DeleteClickListener extends Serializable 
    {
        public void deleteButtonClick(DeleteClickEvent event);        
    }
    
    public void addListener(AddClickListener listener) {
        addListener(AddClickEvent.class, listener, ADD_BUTTON_CLICK_METHOD);
    }
    
    public void addListener(EditClickListener listener) {
        addListener(EditClickEvent.class, listener, EDIT_BUTTON_CLICK_METHOD);
    }
    
    public void addListener(DeleteClickListener listener) {
        addListener(DeleteClickEvent.class, listener, DELETE_BUTTON_CLICK_METHOD);
    }
    
    public class AddClickEvent extends Button.Event {
        public AddClickEvent(Component source) 
        {
            super(source);
        }
    }
    
    public class EditClickEvent extends Button.Event {
        public EditClickEvent(Component source) 
        {
            super(source);
        }
    }
    
    public class DeleteClickEvent extends Button.Event {
        public DeleteClickEvent(Component source) 
        {
            super(source);
        }
    }  
}

Can you say me if it’s an elegant solution ?

i dont know what your code is exctly doing. the code up to line 53 is ok and a common writing. the whole thing after that seems to be redundant.