GWT 中的自定义插件事件

发布于 2024-12-11 04:49:29 字数 1933 浏览 0 评论 0原文

我们编写了一个浏览器插件。它会触发“mycustomevent”等自定义事件。我需要将这些自定义事件合并(即为其分配侦听器)到 GWT 中。这是我在 GWT 中的插件类。

class PluginObject extends Composite implements HasMyCustomEventHandler{
    private Element plugin;
    private HTML html;
    private HandlerManager handlerManager;

    public PluginObject(){
        handlerManager = new HandlerManager(this);
        html = new HTML("<object id='plugin0' type='application/x-ourplugin' width='300' height='300'>"
                       +"<param name='onload' value='pluginLoaded' />"
                       +"</object>");
        initWidget(html);
        plugin = html.getElement().getFirstChildElement();
    }
}

以下是我将其添加到视图中的方法(我正在使用 gwtp)。它正在添加并且在页面中正常运行,我可以使用 JSNI 从 gwt 调用它的 js 函数。

@Inject
public MyPageView(){

    ...
    plugin = new PluginObject();
    panel.add(plugin);
}

我需要能够为从插件触发的事件分配处理程序。我希望能够在我的演示者中执行以下操作,但到目前为止我还无法将其连接起来。

@Override
protected void onBind(){
    super.onBind();
    registerHandler(getView().getPluginObject().addMyCustomEventHandler(
        new MyCustomEventHandler() {
            @Override
            public void onMyCustomEvent(MyCustomEvent event) {
                // call some other presenter stuff here...
            }
        }));
}

上面提到了一些额外的类。我定义了一个 MyCustomEvent 类、一个扩展定义的 EventHandler 的 MyCustomEventHandler 接口以及一个扩展定义的 HasHandlers 的 HasMyCustomEventHandler 接口,但将它们全部连接起来后我什么也没得到。为了简洁起见,我省略了它们,我很乐意添加任何可能有帮助的内容。

我可以在裸js中添加事件监听器。

obj.addEventListener("mycustomevent", function(){ foo(); }, false);

并每次捕获事件(foo() 被调用),因此我确信事件正在被触发。

我尝试将以下内容添加到我的 PluginObject 类中,但这只是我从搜索到的内容中猜测的。但它没有起作用。

public HandlerRegistration addMyCustomEventHandler(MyCustomEventhandler handler){
    return handlerManager.addHandler(MyCustomEvent.TYPE, handler);
}

这里正确的方法是什么?

We have coded a browser plugin. It fires custom events like 'mycustomevent'. I need to incorporate (i.e. assign listeners for) these custom events into GWT. Here is my plugin class in GWT.

class PluginObject extends Composite implements HasMyCustomEventHandler{
    private Element plugin;
    private HTML html;
    private HandlerManager handlerManager;

    public PluginObject(){
        handlerManager = new HandlerManager(this);
        html = new HTML("<object id='plugin0' type='application/x-ourplugin' width='300' height='300'>"
                       +"<param name='onload' value='pluginLoaded' />"
                       +"</object>");
        initWidget(html);
        plugin = html.getElement().getFirstChildElement();
    }
}

Here is how I am adding it in my View (I am using gwtp). It is being added and is functioning correctly in the page and I can call its js functions from gwt with JSNI.

@Inject
public MyPageView(){

    ...
    plugin = new PluginObject();
    panel.add(plugin);
}

I need to be able to assign handlers for the events fired from the plugin. I'd like to be able to do the following in my presenter but thus far I have been un able to get it wired up.

@Override
protected void onBind(){
    super.onBind();
    registerHandler(getView().getPluginObject().addMyCustomEventHandler(
        new MyCustomEventHandler() {
            @Override
            public void onMyCustomEvent(MyCustomEvent event) {
                // call some other presenter stuff here...
            }
        }));
}

The above mentions a few additional classes. I have a MyCustomEvent class defined, a MyCustomEventHandler interface that extends EventHandler defined, and a HasMyCustomEventHandler interface that extends HasHandlers defined but after wiring them all up I get nothing. I've omitted them for brevity, I'd be glad to add anything that might help.

I can add event listeners in bare js.

obj.addEventListener("mycustomevent", function(){ foo(); }, false);

and catch the event (foo() gets called) every time so I'm confident the events are being fired.

I've tried adding the following to my PluginObject class, but this is just me guessing from things I've found searching. It has not worked.

public HandlerRegistration addMyCustomEventHandler(MyCustomEventhandler handler){
    return handlerManager.addHandler(MyCustomEvent.TYPE, handler);
}

What is the proper approach here?

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

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

发布评论

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

评论(1

北方的巷 2024-12-18 04:49:29

这是我最终在 PluginObject 中所做的事情。我很想听听改进它的方法。我对 subscribeToBrowserEvents() 和对“this”的引用挂断了一段时间。最初,我调用 addEvent 方法并引用

this.@....

Which 在运行时引用了错误的位置。有关此问题的更多信息,请参阅这段 JSNI 文档。享受。

class PluginObject extends Composite implements HasMyCustomEventHandler{

public PluginObject(){
    handlerManager = new HandlerManager(this);

    html = new HTML("<object id='plugin0' type='application/x-myplugin' width='300' height='300'>"
                         +"<param name='onload' value='pluginLoaded' />"
                         +"</object>");
    initWidget(html);
    plugin = html.getElement().getFirstChildElement();      
}

public void bindBrowserEvents(){
    subscribeToBrowserEvents(plugin);
}

public void eventHandlerMethod(String message){
    MyCustomEvent event = new MyCustomEvent(message);       
    fireEvent(event);
}
@Override
public void fireEvent(GwtEvent<?> event){
    handlerManager.fireEvent(event);
}

public native void subscribeToBrowserEvents(Element eventObject) /*-{

    var pluginReference = this;

    // bind the listener
    function addEvent(obj, name, func)
    {
        if (window.addEventListener) {
            obj.addEventListener(name, func, false); 
        } else {
            obj.attachEvent("on"+name, func);
        }
    }

    addEvent(eventObject.br,'mycustomevent', 
        function(message){
            [email protected]::eventHandlerMethod(Ljava/lang/String;)(message);
        });
}-*/;

@Override
protected void onAttach(){
    super.onAttach();
    // bind in the onAttach, it was not working earlier. I assume the plugin needs to exist before the bindings will work  
    bindBrowserEvents();
}

@Override
public HandlerRegistration addMyCustomEventHandler(MyCustomEventHandler handler)
{ 
    return handlerManager.addHandler(MyCustomEvent.TYPE, handler);
}
}

Here is what I ended up doing in the PluginObject. I'd love to hear ways to improve it. I got hung up a while on subscribeToBrowserEvents() and the reference to 'this'. Initially I was calling the addEvent method and referring to

this.@....

Which was referencing the wrong place at runtime. More about the issue here in this bit of the JSNI docs. Enjoy.

class PluginObject extends Composite implements HasMyCustomEventHandler{

public PluginObject(){
    handlerManager = new HandlerManager(this);

    html = new HTML("<object id='plugin0' type='application/x-myplugin' width='300' height='300'>"
                         +"<param name='onload' value='pluginLoaded' />"
                         +"</object>");
    initWidget(html);
    plugin = html.getElement().getFirstChildElement();      
}

public void bindBrowserEvents(){
    subscribeToBrowserEvents(plugin);
}

public void eventHandlerMethod(String message){
    MyCustomEvent event = new MyCustomEvent(message);       
    fireEvent(event);
}
@Override
public void fireEvent(GwtEvent<?> event){
    handlerManager.fireEvent(event);
}

public native void subscribeToBrowserEvents(Element eventObject) /*-{

    var pluginReference = this;

    // bind the listener
    function addEvent(obj, name, func)
    {
        if (window.addEventListener) {
            obj.addEventListener(name, func, false); 
        } else {
            obj.attachEvent("on"+name, func);
        }
    }

    addEvent(eventObject.br,'mycustomevent', 
        function(message){
            [email protected]::eventHandlerMethod(Ljava/lang/String;)(message);
        });
}-*/;

@Override
protected void onAttach(){
    super.onAttach();
    // bind in the onAttach, it was not working earlier. I assume the plugin needs to exist before the bindings will work  
    bindBrowserEvents();
}

@Override
public HandlerRegistration addMyCustomEventHandler(MyCustomEventHandler handler)
{ 
    return handlerManager.addHandler(MyCustomEvent.TYPE, handler);
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文