本文介绍了GWT 自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我在理解自定义 GWT 事件处理程序的工作方式时遇到了问题.我已经阅读了很多关于该主题的内容,但它仍然有些模糊.我在 Stackoverflow 上读过类似这样的线程 GWT 自定义事件处理程序.有人可以用以下应用程序来解释它吗.

Hey I have a problem getting my head around how custom GWT event Handlers work. I have read quite a bit about the topic and it still is some what foggy. I have read threads here on Stackoverflow like this one GWT Custom Event Handler. Could someone explain it in an applied mannar such as the following.

我有 2 个类,一个是块,一个是 man 类.当人与块碰撞时,人会触发一个事件( onCollision() ),然后块类会侦听该事件.

I have 2 classes a block and a man class. When the man collides with the block the man fires an event ( onCollision() ) and then the block class listens for that event.

谢谢

推荐答案

一般事件:

事件总是被发送以通知某些事情(例如状态的变化).让我们以一个人和一堵墙为例.在这里我们可以想象有一个游戏,用户可以像人一样在迷宫中行走.每次用户撞到墙壁时,都应该将碰撞通知给它,以便它可以对其做出反应(例如,墙壁可以将自己呈现为被毁坏的墙壁).这可以通过每次检测到与墙壁的碰撞时发送碰撞事件来实现.这个事件是由一个人发送的,系统中对这个事件感兴趣的每个对象都会收到它,并可以对它做出相应的反应.想要接收事件的对象必须将自己注册为对事件感兴趣.

Events in general:

Events are always sent to inform about something (e.g. a change of state). Let's take your example with a man and a wall. Here we can imagine that there is a game where a user can walk as a man in a labyrinth. Every time a user hits the wall it should be informed about the collision so that it can react to it (e.g. a wall can render itself as a destroyed wall). This can be achieved by sending a collision event every time the collision with a wall is detected. This event is sent by a man and every object in the system interested in the event receives it and can react to it accordingly. Objects which want to receive events must register themselves as interested with event.

这就是事件在每个系统或框架中的一般工作方式(不仅在 GWT 中).为了在此类系统中发送和接收事件,您必须定义:

This is how events work in general in every system or framework (not only in GWT). In order to send and receive events in such systems you have to define:

  1. 发送了什么(事件是什么样的)
  2. 谁接收事件(事件接收者)
  3. 谁发送事件(事件发送者)

然后你可以:

  1. 注册想要接收事件的事件接收器
  2. 发送事件

GWT 中的事件:

这里我将展示一个在 GWT 中使用自定义事件的示例.我将使用一个系统示例,该系统负责检查邮箱并通知用户是否有新邮件.让我们假设系统中至少有 2 个组件:

Events in GWT:

Here I will show an example of using custom events in GWT. I will use an example of a system which is responsible for checking a mailbox and inform a user if there are new mails. Let's assume that in the system there are at least 2 components:

  • 消息检查器负责检查邮箱和
  • 负责显示新邮件的消息显示器

消息检查器在收到新邮件时发送事件,消息显示器接收到这些事件.

Message checker sends events when a new mail is received and message displayer receives these events.

有关新邮件的信息将作为 MessageReceivedEvent 类的实例发送.该类包含一个新邮件(为简单起见,我们假设它只是一个 String).

Information about a new mail will be sent as an instance of MessageReceivedEvent class. The class contains a new mail (for the simplicity let's assume it is just a String).

该类的完整源代码如下(注释在源代码下方).

Full source code of this class is presented below (the comment for it is below the source code).

public class MessageReceivedEvent extends GwtEvent<MessageReceivedEventHandler> {

    public static Type<MessageReceivedEventHandler> TYPE = new Type<MessageReceivedEventHandler>();

    private final String message;

    public MessageReceivedEvent(String message) {
        this.message = message;
    }

    @Override
    public Type<MessageReceivedEventHandler> getAssociatedType() {
        return TYPE;
    }

    @Override
    protected void dispatch(MessageReceivedEventHandler handler) {
        handler.onMessageReceived(this);
    }

    public String getMessage() {
        return message;
    }
}

MessageReceivedEventHandler 是一个表示事件接收器的接口.暂时先别管它,后面再讨论.

MessageReceivedEventHandler is an interface that represents event receivers. Don't bother with it at the moment, this will be discussed later.

表示 GWT 事件的每个类都必须扩展 GwtEvent 类.该类包含两个必须实现的抽象方法:getAssociatedTypedispatch.然而,在每个事件类中,它们通常以非常相似的方式实现.

Every class representing a GWT event has to extend GwtEvent class. This class contains two abstract methods which must be implemented: getAssociatedType and dispatch. However in every event class they are usually implemented in a very similar way.

该类存储有关接收到的消息的信息(请参阅构造函数).每个事件接收器都可以使用 getMessage 方法获取它.

The class stores information about a received message (see constructor). Every event receiver can get it using getMessage method.

GWT 中的每个事件类型都与一个接口相关联,该接口代表该事件类型的接收者.在 GWT 中,接收器称为处理程序.在示例中,MessageReceivedEvent 的事件接收器接口将命名为 MessageReceivedEventHandler.源代码如下:

Each event type in GWT is associated to an interface representing receivers of this event type. In GWT receivers are called handlers. In the example an event receiver interface for MessageReceivedEvent will be named MessageReceivedEventHandler. The source code is below:

public interface MessageReceivedEventHandler extends EventHandler {
    void onMessageReceived(MessageReceivedEvent event);
}

每个处理程序都必须扩展 EventHandler 接口.它还应该定义一个在事件发生时将被调用的方法(它应该至少有一个参数 - 一个事件).这里的方法名为onMessageReceived.每个接收器都可以通过实现此方法对事件做出反应.

Each handler has to extend EventHandler interface. It should also define a method which will be invoked when an event occurs (it should take at least one parameter - an event). Here the method is named onMessageReceived. Each receiver can react on an event by implementing this method.

示例中唯一的事件接收器是 MessageDisplayer 组件:

The only event receiver in the example is MessageDisplayer component:

public class MessageDisplayer implements MessageReceivedEventHandler {

    @Override
    public void onMessageReceived(MessageReceivedEvent event) {
        String newMessage = event.getMessage();
        // display a new message
        // ...
    }

}

第 3 步:定义事件发送者

在示例中,唯一的事件发送器是负责检查邮件的组件 - EventChecker:

public class MessageChecker implements HasHandlers {

    private HandlerManager handlerManager;

    public MessageChecker() {
        handlerManager = new HandlerManager(this);
    }

    @Override
    public void fireEvent(GwtEvent<?> event) {
        handlerManager.fireEvent(event);
    }

    public HandlerRegistration addMessageReceivedEventHandler(
            MessageReceivedEventHandler handler) {
        return handlerManager.addHandler(MessageReceivedEvent.TYPE, handler);
    }

}

每个事件发送者都必须实现HasHandlers接口.

Every event sender has to implement HasHandlers interface.

这里最重要的元素是 HandlerManager 字段.在 GWT 中,HandlerManager 顾名思义管理事件处理程序(事件接收器).正如开头所说,每个想要接收事件的事件接收器都必须将自己注册为感兴趣的.这就是处理程序管理器的用途.它们使注册事件处理程序成为可能,并且可以向每个注册的事件处理程序发送特定事件.

The most important element here is a HandlerManager field. In GWT HandlerManager as the name suggest manages event handlers (event receivers). As it was said at the beginning every event receiver that wants to receive events must register itself as interested. This is what handler managers are for. They make it possible to register event handlers an they can send a particular event to every registered event handler.

当一个 HanlderManager 被创建时,它在其构造函数中接受一个参数.每个事件都有一个来源,并且此参数将用作此处理程序管理器发送的所有事件的来源.在示例中,它是 this,因为事件源是 MessageChecker.

When a HanlderManager is created it takes one argument in its constructor. Every event has a source of origin and this parameter will be used as a source for all events send by this handler manager. In the example it is this as the source of events is MessageChecker.

fireEvent 方法定义在HasHandlers 接口中,负责发送事件.如您所见,它仅使用处理程序管理器来发送(触发)和事件.

The method fireEvent is defined in HasHandlers interface and is responsible for sending events. As you can see it just uses a handler manager to send (fire) and event.

addMessageReceivedEventHandler 被事件接收者用来将自己注册为对接收事件感兴趣.再次使用处理程序管理器.

addMessageReceivedEventHandler is used by event receivers to register themselves as interested in receiving events. Again handler manager is used for this.

当一切都被定义时,事件接收者必须在事件发送者中注册自己.这通常在创建对象期间完成:

When everything is defined event receivers must register themselves in event senders. This is usually done during creation of objects:

MessageChecker checker = new MessageChecker();
MessageDisplayer displayer = new MessageDisplayer();
checker.addMessageReceivedEventHandler(displayer);

现在checker发送的所有事件都会被displayer接收.

Now all events sent by checker will be received by displayer.

要发送事件,MessageChecker 必须创建一个事件实例并使用 fireEvent 方法发送它.这个甘蔗在 newMailReceived 方法中完成:

To send an event, MessageChecker must create an event instance and send it using fireEvent method. This cane be done in newMailReceived method:

public class MessageChecker implements HasHandlers {

    // ... not important stuff omitted

    public void newMailReceived() {
        String mail = ""; // get a new mail from mailbox
        MessageReceivedEvent event = new MessageReceivedEvent(mail);
        fireEvent(event);
    }

}

我希望它很清楚并且会有所帮助:)

I hope it is clear and will help :)

这篇关于GWT 自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:59