Java GWT 自定义事件处理程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/998621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GWT Custom Event Handler
提问by Nick
Can someone give me an example of creating a custom set of an Event and a Handler. Say you have a Person object that you want your widgets to know if it got updated.
有人能给我一个创建自定义事件和处理程序集的例子吗?假设您有一个 Person 对象,您希望您的小部件知道它是否已更新。
You create a HandlerManager and now you have to create an Event and a Handler. How would you define those classes so that you can subscribe and fire events?
您创建了一个 HandlerManager,现在您必须创建一个事件和一个处理程序。您将如何定义这些类以便您可以订阅和触发事件?
Most of the Events are DOM based, while I want to create some custom events and handlers that I can fire outside of any browser-based event.
大多数事件都是基于 DOM 的,而我想创建一些可以在任何基于浏览器的事件之外触发的自定义事件和处理程序。
采纳答案by Zak Linder
Here's a pretty comprehensive example of creating a custom event, taken verbatim from the GwtEventSystem Wiki(when the event system was still in GWT's incubator).
这是创建自定义事件的一个非常全面的示例,逐字取自GwtEventSystem Wiki(当事件系统仍在 GWT 的孵化器中时)。
This is an event that is triggered when the user becomes happy.
这是当用户变得高兴时触发的事件。
Define a new event class. You can add arbitrary metadata in the event class. For simplicity, we will not include any here though.
定义一个新的事件类。您可以在事件类中添加任意元数据。为简单起见,我们不会在这里包含任何内容。
public class HappyEvent extends GwtEvent {
...
}
Define a new handler and marker interface for the event class.
为事件类定义一个新的处理程序和标记接口。
interface HappyHandler extends EventHandler {
public void onHappiness(HappyEvent event);
}
interface HasHappyEvents {
public HandlerRegistration addHappyHandler(HappyHandler handler);
}
Add a unique event type
添加唯一的事件类型
class HappyEvent extends AbstractEvent{
public static AbstractEvent.Key KEY = new AbstractEvent.Key(){...}
public GwtEvent.Key getKey(){
return KEY;
}
...
}
Wire up the handler's fire method
连接处理程序的 fire 方法
class HappyEvent extends GwtEvent {
static Key<HappyEvent,HappyHandler> KEY = new Key<HappyEvent,HappyHandler>(){
protected void fire(HappyHandler handler, HappyEvent event) {
handler.onHappiness(event);
};
...
}
回答by digitaljoel
回答by DLH
You might want to have a look at the ValueChangeHandlerand ValueChangeEventin GWT 1.6. Those might work for what you're trying to do.
您可能想看看GWT 1.6中的ValueChangeHandler和ValueChangeEvent。这些可能适用于您正在尝试做的事情。
回答by Nick
Thanks for all the responses. Zakness came the closest to giving me the answer I needed, however, I came up with a slightly simpler model.
感谢所有的回应。Zakness 最接近于给我我需要的答案,但是,我想出了一个稍微简单的模型。
My main goal was to avoid using a static variable to my main data structure. I also hit the problem of trying to figure out if that main data structure was successfully retrieved from the database at the time of trying to access it and what to do when it's not (i.e. when it's null).
我的主要目标是避免在我的主要数据结构中使用静态变量。我还遇到了试图确定在尝试访问它时是否从数据库中成功检索到该主数据结构以及在它不是时(即当它为空时)时该怎么做的问题。
After watching the Google Web Toolkit Architecture: Best Practices For Architecting Your GWT Appvideo from Google IO, the Event Bus idea seemed perfect.
在观看了来自 Google IO的Google Web Toolkit Architecture: Best Practices For Architecting Your GWT App视频后,Event Bus 的想法似乎很完美。
I'll post my solution here in case it helps anyone else out.
我会在这里发布我的解决方案,以防它对其他人有所帮助。
First, create the Handler class. Note the reference to the Event class already:
首先,创建 Handler 类。请注意对 Event 类的引用:
public interface CategoryChangeHandler extends EventHandler {
void onCategoryChange(CategoryChangeEvent event);
}
Now on to the Event class. This gave me the most trouble:
现在进入事件类。这给我带来了最大的麻烦:
public class CategoryChangeEvent extends GwtEvent<CategoryChangeHandler> {
private final List<Category> category;
public CategoryChangeEvent(List<Category> category) {
super();
this.category = category;
}
public static final Type<CategoryChangeHandler> TYPE = new Type<CategoryChangeHandler>();
@Override
protected void dispatch(CategoryChangeHandler handler) {
handler.onCategoryChange(this);
}
@Override
public com.google.gwt.event.shared.GwtEvent.Type<CategoryChangeHandler> getAssociatedType() {
return TYPE;
}
public List<Category> getCategories(){
return category;
}
}
Now I am able to use these Handler and Event classes like so when this main data structure gets reloaded:
现在,当这个主数据结构被重新加载时,我可以像这样使用这些 Handler 和 Event 类:
This code got the data structure and want to notify everyone who is listening that it got updated:
此代码获得了数据结构,并希望通知所有正在收听的人它已更新:
CategoryChangeEvent event = new CategoryChangeEvent(result);
eventBus.fireEvent(event);
This code is an implementation of the Event
此代码是事件的实现
public class PopulateCategoryHandler implements CategoryChangeHandler {
@Override
public void onCategoryChange(CategoryChangeEvent event) {
tearDownCategories();
List<Category> categories = event.getCategories();
populateCategories(categories);
}
}
回答by tom d
Here is an example of this over on Alex Reid's blog, including a link to an operational code example. The example fills in some of the fuzzy bits and, along with Nick's example here, helps clarify getting started with architecting an event bus in your gwt application.
以下是Alex Reid 博客上的一个示例,包括指向操作代码示例的链接。该示例填补了一些模糊的部分,并与 Nick 在此处的示例一起帮助阐明了在 gwt 应用程序中构建事件总线的入门。
回答by Ning120
Create custom GWT events using the HandlerManger shouldn't be this hard, take a look at the example GwtEventBus @ NingZhang.infoit is real intuitive. The key classes used are:
使用 HandlerManger 创建自定义 GWT 事件应该不难,看看GwtEventBus @ NingZhang.info的例子,它非常直观。使用的关键类是:
- com.google.gwt.event.shared.HandlerManager
- com.google.gwt.event.shared.GwtEvent
- com.google.gwt.event.shared.EventHandler
- com.google.gwt.event.shared.HandlerManager
- com.google.gwt.event.shared.GwtEvent
- com.google.gwt.event.shared.EventHandler
回答by Rubinsh
I think that the most complete and detailed example is in this article
我认为最完整和详细的例子在这篇文章中
It contains also an example project that shows exactly how to properly use define custom events and use GWT's HandlerManager class.
它还包含一个示例项目,它准确地展示了如何正确使用定义自定义事件和使用 GWT 的 HandlerManager 类。
回答by hhh
One additional comment: if you try do to something similar to react in a main application to an event fired from a custom GUI component (like a composite etc.), I think you have to wire the main app explicitly to handle the component's event:
一个额外的评论:如果您尝试在主应用程序中对从自定义 GUI 组件(如复合等)触发的事件做出类似的反应,我认为您必须显式连接主应用程序以处理组件的事件:
yourComponent.addHandler(this, YourEvent.TYPE);
where "this" is the class that implements your custom handler interface.
其中“this”是实现自定义处理程序接口的类。