如何使用 GWT 的 SimpleEventBus 或 EventBus?

发布于 2024-10-04 04:49:50 字数 465 浏览 6 评论 0原文

我正在开发一些简单的绘图软件,需要在 swing 和 gwt 中实现。在 gwt 方面,我将使用 gwt-g2d 作为画布。我希望在这两种实现中使用事件总线来简化一些软件。

我的理解是,它应该是这样的:

  1. 实例化EventBus
  2. 实例化Parent Widget,接收您想要的EventBus事件(例如鼠标悬停或rpc回调)
  3. 设置EventBus以侦听来自Parent Widget的事件。
  4. 实例化子部件,谁应该向 EventBus 注册它拥有的侦听器(它们是否需要注册可能触发的事件?)
  5. 当 EventBus 接收到事件时,它应该对事件做出决定,然后根据该决定采取行动,无论是意味着忽略它,更改事件类型,或者只是将其转发到所有适用的子窗口小部件。

这通常是它应该如何工作的吗? SimpleEventBus 仍然是一种新事物,我在网上找不到太多关于如何真正使用它的信息。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

将军与妓 2024-10-11 04:49:50

SimpleEventBus 提供了存储处理程序集合以及在适当时使用所有相关处理程序调用 event.dispatch() 的非常基本的功能。我不会说您“设置 EventBus 以侦听来自父窗口小部件的事件”。 SimpleEventBus 没有“父级”的概念。相反,您传递对 SimpleEventBus 的引用。然后,任何对象都可以使用 SimpleEventBus.fireEvent 方法“触发事件”。没有等级制度。

同样,任何对象都可以使用 SimpleEventBus.addHandler() 监听这些事件。这最接近您的 #4 - addHandler 接受类型的事件和处理程序,因此每当该类型的事件被传递给 fireEvent (通过应用程序中的某些内容)时,该处理程序将被传递给事件。请注意,Event 是调用处理程序的内容,而不是 EventBus! EventBus只调用event.dispatch(theHandler),事件的程序员负责调用theHandler中正确的函数。你的 #5 是不准确的,至少对于 SimpleEventBus 来说 - SimpleEventBus 根本不检查事件,除了查看要调用哪些处理程序。它不知道任何子小部件或任何应用程序逻辑,并且从不更改事件的类型。

关于查看哪个区域被选中的逻辑在 EventBus 中不合适 - 相反,您将创建一个处理程序来执行逻辑并侦听 ClickEvents。然后,该处理程序可以告诉所选区域它已被直接选择,或者您可以创建自己的 RegionSelectionEvent,您可以沿着 EventBus 触发该事件,然后可以通知所有区域已发生 RegionSelection,并且您的记录器可以获得通知,或者您的服务器监视器可以收到通知并通知老板有人选择了区域或其他什么。如果您只需要取消选择一个区域并选择另一个区域,则事件总线就有点过分了。

另一方面,像“调整大小”事件这样的事情很有意义,因为所有的小部件可能都需要知道它。

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.

初见你 2024-10-11 04:49:50

据我了解 GWT EventBus,它适用于应用程序范围的事件(认为“系统范围对象 x 更改属性 y”;任何监听该特定事件的东西都可以对其采取行动)。这可以帮助您分离应用程序逻辑。

您可能不想向其中触发任何 UI 事件:您的父窗口小部件可以实现处理程序,或者您可以只使用匿名实例。

As far as I understand the GWT EventBus, it's meant for events that are application wide (think "systemwide object x chanded property y"; anything that listens to that particular event can act upon it). This helps you to separate your application logic.

You probably do not want to fire any UI events into it: your parent widgets can implement the handlers or you can just use anonymous instances.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文