GWT JSNI BOOLEAN - gwt

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!

You have to declare the Boolean as the primitive boolean to set the value from javascript
and you dont need to specifiy L/java/lang/Boolean in the call but Z instead

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.

Related

How to assert property of objects at a list?

I am trying to have proper assertion (with implicit null checks) for a property of a list element.
The first assertion is working as expected, except that it will generate no proper error message if actual is null.
The second is supposed to provide proper null check for actual, but it's not compiling.
Is there an option tweak the second assertion to make it work?
import java.util.List;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class ExampleTest {
private static class Sub {
private String value;
public String getValue() {
return value;
}
}
private static class Example {
private List<Sub> subs;
public List<Sub> getSubs() {
return subs;
}
}
#Test
void test() {
Example actual = null;
assertThat(actual.getSubs())//not null safe
.extracting(Sub::getValue)
.contains("something");
// assertThat(actual)
// .extracting(Example::getSubs)
// .extracting(Sub::getValue)//not compiling
// .contains("something");
}
}
For type-specific assertions, extracting(Function, InstanceOfAssertFactory) should be used:
assertThat(actual)
.extracting(Example::getSubs, as(list(Sub.class)))
.extracting(Sub::getValue) // compiles
.contains("something");
Assertions.as(InstanceOfAssertFactory) is an optional syntax sugar to improve readability
InstanceOfAssertFactories.list(Class) provides the list-specific assertions after the extracting call

interface does not give me initalize method

I am missing method from interface
value of annotiations is null.
Already tried hardcoded, it works, but I need to be customizable.
package com.luv2code.springdemo.mvc.validation;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class CourseCodeConstraintValidator implements ConstraintValidator {
private String coursePrefix;
public void initalize(CourseCode theCourseCode) {
coursePrefix = theCourseCode.value();
}
#Override
public boolean isValid(String theCode, ConstraintValidatorContext theConstraintValidatorContext) {
/*if (coursePrefix==null)
coursePrefix = "LUV";*/
System.out.println(theConstraintValidatorContext.getDefaultConstraintMessageTemplate());
boolean result;
if (theCode!=null) {
System.out.println(coursePrefix);
result = theCode.startsWith(coursePrefix);
} else {
result = true;
}
return result;
}
}
Expecting possible #Override method initialize
initalize => initialize
just one letter missing

propertyChange not called when restoring default values

I am building a preference page extending the FieldEditorPreferencePage class.
This is the code (some obvious code not displayed):
public class PreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
public static final String PREF_KEY_1 = "checkBoxPref";
public static final String PREF_KEY_2 = "filePref";
private FileFieldEditor pathField;
private BooleanFieldEditor yesOrNoField;
private Composite pathFieldParent;
#Override
public void init(IWorkbench workbench) {
setPreferenceStore(new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID));
}
#Override
protected void createFieldEditors() {
this.yesOrNoField = new BooleanFieldEditor(PREF_KEY_1, "Check this box!", getFieldEditorParent());
this.pathFieldParent = getFieldEditorParent();
this.pathField = new FileFieldEditor(PREF_KEY_2, "Path:", this.pathFieldParent);
addField(this.yesOrNoField);
addField(this.pathField);
boolean isChecked = getPreferenceStore().getBoolean(PREF_KEY_1);
updatePathFieldEnablement(! isChecked);
}
/**
* Updates the fields according to entered values
*/
private void updatePathFieldEnablement(boolean enabled) {
this.pathField.setEnabled(enabled, this.pathFieldParent);
}
#SuppressWarnings("boxing")
#Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getProperty().equals(FieldEditor.VALUE) && event.getSource() == this.yesOrNoField) {
updatePathFieldEnablement(! (boolean) event.getNewValue());
}
super.propertyChange(event);
}
}
The propertyChange method is there to enable/disable the FileFieldEditor depending on the BooleanFieldEditor value.
It works OK if I change the BooleanFieldEditor valeu by checking or unchecking it, but the propertyChange is not called when I hit the "Restore default values" button.
Do someone see a reason for that?
OK, I think I've got my response.
I went further in my investigation and I got to this code which seems suspect to me:
In class BooleanFieldEditor :
#Override
protected void doLoadDefault() {
if (checkBox != null) {
boolean value = getPreferenceStore().getDefaultBoolean(getPreferenceName());
checkBox.setSelection(value);
wasSelected = value;
}
}
and in class StringFieldEditor
#Override
protected void doLoadDefault() {
if (textField != null) {
String value = getPreferenceStore().getDefaultString(
getPreferenceName());
textField.setText(value);
}
valueChanged();
}
We can see that the FileFieldEditor (that inherits from StringFieldEditor) launches an PropertyChangeEvent to its listeners (valueChanged();) but not the BooleanFieldEditor. I did not find any code indicating that BooleanFieldEditor are using another mechanism. I think this is a bug in jFace.
To get around this problem, I just had to override the FieldEditorPreferencePage#performDefaults method and the result's fine.

Bukkit Plugin: Listener in the main class

I'm trying to make a plugin where you type the command /settings and it toggles the boolean set. If set is true, I want it so when players join it says 'hi' to them but if it's 'false' it does nothing. (Btw im the only one who can use the command). I tried making two classes, one the main and the second the listener, But I couldn't access the boolean from the listener class so I tried making it all in one class. When using the code I've provided, everything works except for the PlayerJoinEvent. I either need to work out how to access the boolean from another class or how to fix this.
package me.jakegeyer28;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import net.md_5.bungee.api.ChatColor;
public class Main extends JavaPlugin implements Listener{
public boolean set = true;
#Override
public void onEnable() {
getLogger().info("Done");
}
#Override
public void onDisable() {
getLogger().info("Done");
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("settings")) {
Player player = (Player) sender;
if (player.getName().equalsIgnoreCase("jakegeyer27")) {
if(set == true) {
set = false;
player.sendMessage(ChatColor.RED + "Off");
}
else if (set == false) {
set = true;
player.sendMessage(ChatColor.GREEN + "On");
}
}
return true;
}
return false;
}
#EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
Player player = e.getPlayer();
if (set == true) {
player.sendMessage("hi");
}
}
}
Thanks
It seems that you haven't registered your listener. Even though the listener is your main class you still need to register it in your onEnable method with this.getServer().getPluginManager().registerEvents(this, this);.

Use Java constant in GWT Javascript Overlay Type (JSO)?

I would like to define the GWT JSO property name as a constant in the JSO, in order to avoid typos and benefit from Eclipse code completion, like so:
public final class MyJSO extends JavaScriptObject
{
/** here is the constant */
private static final String MY_CONST = "myPropName";
protected MyJSO() {
super();
}
public native void setMyProp(final boolean pFlag)
/*-{
this.#fully.qualified.MyJSO::MY_CONST = pFlag;
}-*/;
public native boolean isMyProp()
/*-{
if (this.hasOwnProperty(#fully.qualified.MyJSO::MY_CONST)) {
return this.#fully.qualified.MyJSO::MY_CONST;
} else {
return false;
}
}-*/;
}
The GWT compiler should be able to replace the String from the constant at compile time, so there is no problem with the object living as Javascript later on.
But this is so totally not working, I'm thinking I may be wrong. :-) Can anyone explain why? Do you have better ideas how to achieve this?
Thanks!
The correct syntax to refer to a static variable is:
#fully.qualified.MyJSO::MY_CONST
No qualifier (this., in your example) is needed since the variable is static.
If you want to set/get a property on the JavaScript object with the constant name do so as follows:
public native void setMyProp(final boolean pFlag) /*-{
this[#fully.qualified.MyJSO::MY_CONST] = pFlag;
}-*/;
public native boolean isMyProp() /*-{
if (this[#fully.qualified.MyJSO::MY_CONST] != null) {
return this[#fully.qualified.MyJSO::MY_CONST];
} else {
return false;
}
}-*/;