Search code examples
gwtjsni

GWT JSNI BOOLEAN


Here's my code:

package com.eggproject_hu.WPECommerceAdminSales.client;

import java.lang.Boolean;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;

public class AblakVillogo
{
    public static Boolean focusedWindow = true;
    private static Boolean init = false;

    public static void setFocused(Boolean focus)
    {
        focusedWindow = focus;
    }

    public static Boolean getFocused()
    {
        return focusedWindow;
    }

    public static void focusVizsgalat()
    {
        if(focusedWindow == true)
        {
            GWT.log("igen");
        }
        else
        {
            GWT.log("nem");
        }
    }

    public static void init()
    {
        if(init == false)
        {
            _init();
        }
    }

    private native static void _init() /*-{
        $wnd.jQuery(document).ready(function()
        {
            $wnd.jQuery($wnd).focus(function()
            {
@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::focusVizsgalat()();               @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(true);
                $wnd.console.log("focus");
            }).blur(function()
            {
                var ret = false;
                @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::focusVizsgalat()();
                                @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(false);
                $wnd.console.log("blur");
            });
        });
    }-*/;
}

I see this in the browser console:

uncaught exception: java.lang.IllegalArgumentException: invoke arguments: JS value of type boolean, expected java.lang.Boolean

I've tested in Chrome and Firefox.

What is the problem?

Thanks for the help!


Solution

  • Either follow Daniel's advice, but then you have to change your method to take a boolean argument (i.e. use boolean all the way through), or you can explicitly cast/box your boolean in a java.lang.Boolean in your JSNI method:

    @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(@java.lang.Boolean::valueOf(Z)(true));
    

    …even though in your case, because the value is a constant, I'd rather directly use the Boolean constants TRUE and FALSE:

    @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(@java.lang.Boolean::TRUE);
    

    That being said, I do believe Daniel's advice is your best fit.