Vaadin 7消防组件之间的自定义事件 [英] Vaadin 7 fire custom events between components

查看:154
本文介绍了Vaadin 7消防组件之间的自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建自定义事件并在视图的某些部分触发它们,以便视图的其他部分被更新/删除/刷新。

I want to create custom events and fire them in some part of the view such that some other part of the view is updated/removed/refreshed.

我有通过扩展Component.Event和Component.Listener尝试,但它不起作用。我认为事件和听众必须限于相同的组件实例。

I have tried by extending the Component.Event and Component.Listener but it doesn't work. I think that events and listeners mustbe limited to the same component instance.

这可以通过Vaadin 7完成吗?

基本上我想解除我的观点,并提供组件之间的简单通信。我也和Vaadin一起使用Spring。如果你在观察者模式旁边有更好的想法,我也会感谢。

Basically I want to decouple my views and provide easy communication between components. I'm also using Spring with Vaadin. If you have better ideas beside the Observer pattern I would also appreciate it.

谢谢

推荐答案

您需要什么:

1.触发事件的组件

2.自定义事件类

3.一个监听器接口

4.一个监听器实现

What you need:
1. A component that fires the event
2. A custom event class
3. A listener interface
4. And a listener implementation

来自1的组件需要有一个事件侦听器列表,方法: addListener ; 的removeListener ;和 dispatchEvent dispatchEvent -method将遍历事件侦听器列表并执行一些方法(参见4:监听器实现)。该方法具有一个事件对象(参见2:自定义事件类)。

Component from 1 need to have an list of event-listeners, and the methods: addListener; removeListener; and dispatchEvent. The dispatchEvent-method will iterate through the list of event-listeners and execute some method (see 4: the listener implementation). That method has as argument an event-object (see 2: the custom event class).

1的基本代码:

protected List<MyListener> listeners; 

protected synchronized void dispatchEvent(MyEvent event) {
   if (listeners != null) {
      for (MyListener listener : listeners) {
         listener.myMethod(event);
      }
   }
}

public synchronized void addListener(MyListener listener) {
   if (listeners == null) {
      listeners = new ArrayList<MenuListener>();
   }
   listeners.add(listener);
}

public synchronized void removeListener(MyListener listener) {
   if (listeners == null) {
       listeners = new ArrayList<MyListener>();
       return;
   }
   listeners.remove(listener);
}

2的基本代码:

public class MyEvent {
   protected String eventType;

   public MyEvent(String eventType) {
      this.eventType = eventType;
   }

   //getters and setters
}

3的基本部分:

public interface MyListener {
    public void doSomething(MyEvent event);
}

代码4(监听器实现)

public class MyImplementation implements MyListener {

    @Override
    public void doSomething(myEvent event) {
        //do something concretes here
    }
}

代码流如何工作:

调度程序类在进行一些交互之后,会向每个事件监听器发布一些事情。

How the code flow works:
The dispatcher class will, after some interaction, "announce" to every event-listener that something has happened.

dispatchEvent(new MyEvent("ADD_USER"));

在您要接收事件的类中,需要实例化调度器类和监听器实现类,并将侦听器添加到调度程序的列表中:

In a class you want to receive the event you need to instantiate the dispatcher class and listener implementation class, and add the listener to the dispatcher's list:

MyDispatcher disp = new MyDispatcher();
MyImplementation myImpl = new MyImplementation();
// Then add the event-listener to the dispatcher:
disp.addListener(myImpl);

当然,这些类中的一些可以是内部类,代码可以简化或扩展,但是这是一般的想法。这也只是纯Java,没有Vaadin依赖。

Sure, some of these classes can be inner classes, and the code could be simplified or extended, but this is the general idea. This is also just plain Java and has nothing Vaadin-dependent.

这篇关于Vaadin 7消防组件之间的自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆