你如何使用GWT的SimpleEventBus或EventBus? [英] How do you use GWT's SimpleEventBus or EventBus?

查看:182
本文介绍了你如何使用GWT的SimpleEventBus或EventBus?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一些简单的图形软件,需要在swing和gwt中实现。在gwt方面,我将使用gwt-g2d作为画布。我希望能够在两个实现中使用事件总线来简化一些软件。我的理解是它应该是这样的:$ b $
$ b

b


  1. 实例化EventBus

  2. 实例化父窗口小部件,吸收EventBus所需的事件(例如mouseover或rpc回调)

  3. 将EventBus设置为侦听来自Parent Widget的事件。

  4. 实例化子部件,谁应该向EventBus注册它拥有的侦听器(他们需要注册它们可能触发的事件?)

  5. 当EventBus接收到一个事件时,它应该对该事件作出决定,然后根据该决定采取行动,无论这意味着忽略它,更改事件类型,或只是将其中继到所有适用的子窗口小部件。

这一般是如何工作的? SimpleEventBus仍然有些新颖,但我无法在网上找到有关如何真正使用它的信息。

解决方案

SimpleEventBus提供了存储处理程序集合的非常基本的功能,并在适当时调用了所有相关处理程序的event.dispatch()。我不会说你从父窗口小部件设置EventBus来监听事件。 SimpleEventBus没有父的概念。相反,你传递一个对你的SimpleEventBus的引用。然后,任何对象都可以使用SimpleEventBus.fireEvent方法触发事件。没有层次结构。

同样,任何对象都可以使用SimpleEventBus.addHandler()来监听这些事件。这最接近你的#4 - addHandler接收事件和处理程序的类型,所以无论何时将该类型的事件传递给 fireEvent 某些东西在你的应用中),该处理程序将被传递给该事件。请注意, Event 是调用处理程序的事件,而不是EventBus! EventBus只调用event.dispatch(theHandler),事件的程序员负责调用Handler中的函数。您的#5是不准确的,至少对于SimpleEventBus来说 - SimpleEventBus根本不检查这些事件,除了查看哪些处理程序要调用。它不知道任何子窗口小部件或任何应用程序逻辑,也不会更改事件的类型。



您关于查看哪个区域被选中的逻辑将不合适在EventBus中 - 相反,您将创建一个执行逻辑并侦听ClickEvents的处理程序。然后,该处理程序可以告诉选定的区域它直接被选中,或者你可以创建你自己的RegionSelectionEvent,你可以沿着一个EventBus开火,然后所有的区域可以被通知RegionSelection已经发生,并且你的记录器可以得到一个通知,或者你的服务器监视器可以得到通知,并通知老板某人选择了一个区域,或者其他什么。如果您只需取消选择一个区域并选择另一个区域,事件总线就会过度消耗。另一方面,类似调整大小事件的情况非常有用,因为所有的小部件可能都需要知道它。


I'm working on developing some simple graphing software which needs to be implemented in both swing and gwt. On the gwt side, I'm going to use gwt-g2d for the canvas. I was hoping to use an eventbus across both implementations to simplify some of the software.

My understanding is that it should be something like this:

  1. Instantiate EventBus
  2. Instantiate Parent Widget, sink the events you want for the EventBus (mouseover or rpc callback, for example)
  3. Set EventBus to listen for events from the Parent Widget.
  4. Instantiate child widgets, who should register with the EventBus the listeners it has (Do they need to register events they might fire?)
  5. When the EventBus receives an event, it should make a decision about the event and then act on that decision, whether that means ignoring it, changing the event type, or just relaying it to all applicable child widgets.

Is this generally how it should work? SimpleEventBus is still sort of new, and I can't find much on web about how to really use it.

解决方案

A SimpleEventBus provides the very basic functionality of storing a collection of handlers, and calling event.dispatch() with all of the relevant handlers when appropriate. I wouldn't say that you "set EventBus to listen for events" from the parent widget. The SimpleEventBus doesn't have a concept of a "parent." Instead, you pass around a reference to your SimpleEventBus. Then, any object can "fire an event" with the SimpleEventBus.fireEvent method. There's no hierarchy.

Similarly, any object can listen for those events with SimpleEventBus.addHandler(). This is closest to your #4 - addHandler takes a type of event and a handler, so whenever that type of event is passed to fireEvent (by something in your app), that handler will be passed to the event. Note that the Event is what calls the handler, NOT the EventBus! The EventBus just calls event.dispatch(theHandler), and the programmer of the event is responsible for calling the proper function in theHandler. Your #5 is inaccurate, at least for a SimpleEventBus - SimpleEventBus does not inspect the events at all, except to see which handlers to call. It doesn't know about any child widgets or any app logic, and never changes the type of an Event.

Your logic about seeing which region was picked would not be appropriate in an EventBus - instead, you would make a handler that does the logic and listens for ClickEvents. Then, that handler could tell the selected region that it was selected directly, or you could create your own RegionSelectionEvent that you could fire along an EventBus, and then all of the regions could be informed that a RegionSelection has occurred, and your logger could get a notification, or your server monitor could get the notification and notify the boss that someone selected a region, or whatever. If you just need to deselect one region and select another, an eventbus is overkill.

On the other hand, something like a "resize" event makes a lot of sense, since all of your widgets might need to know about it.

这篇关于你如何使用GWT的SimpleEventBus或EventBus?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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