GWT #UiHandler dont work - java

I'm using GWT UiBinder... but I cant use #UiHandler because it not work.
xml
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui">
<g:Button ui:field="btn" text="btn"></g:Button>
</ui:UiBinder>
Java
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
public class HowToHelp extends Composite {
interface HowToHelpUiBinder extends UiBinder<Widget, HowToHelp> {
}
private static HowToHelpUiBinder uiBinder = GWT
.create(HowToHelpUiBinder.class);
#UiField
Button btn;
public HowToHelp() {
initWidget(uiBinder.createAndBindUi(this));
}
#UiHandler("btn")
void handleClick(ClickEvent e) {
Window.alert("Hello, AJAX");
}
}
the Window.alert("Hello, AJAX"); never is called. I did exactly what was passed on the official GWT: http://www.gwtproject.org/doc/latest/DevGuideUiBinder.html
There is a complect project with some error zip

From the linked zip, code missing from the question:
package source.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
public class UiHandlerTest implements EntryPoint {
#Override
public void onModuleLoad() {
RootPanel.getBodyElement().appendChild(new TheHandlerTest("My Button").getElement());
}
}
The problem is that you are adding a widget to another widget without actually doing the add part of it ;). Instead, you are appending the contents of the uibinder-widget to the contents of the RootPanel widget.
Instead, do this (it is shorter/simpler, and won't have this bug):
RootPanel.get().add(new TheHandlerTest("My Button"));

Related

Initialize Hats methods using GenericPortlet class

I am replacing PortletAdapter with GenericPortlet class in code. I followed this link - https://help.hcltechsw.com/digital-experience/9.5/dev-portlet/jsrmig.html. I am trying to initialize HATS-Host Access Transformation Services methods (see initializeHats() method) and it needs ServletConfig as a parameter. But I am not able to access the getServletConfig() method in the GenericPortlet class. I passed getPortletConfig() but got a null pointer exception. Below are my old code and new code. What is the replacement for the getServletConfig()?
Old code:
package abcpostavailablefreight;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.jetspeed.portlet.*;
import org.apache.jetspeed.portlet.event.*;
import com.ibm.hats.runtime.connmgr.Runtime;
import com.ibm.hats.util.LicenseManager;
public class AbcPostAvailableFreightPortlet extends PortletAdapter implements ActionListener {
public void init(PortletConfig portletConfig) throws UnavailableException {
super.init(portletConfig);
}
public void initializeHats() {
initHATS = true;
//Initialize and activate the HATS runtime RAS functions,
// including tracing, logging, PII retrieval, locale.
com.ibm.hats.util.Ras.initializeRas(getServletConfig());
//Create the license manager
LicenseManager.getInstance();
//Initialize Host Publisher/connection management runtime
Runtime.initRuntime(getServletConfig());
}
}
New Code:
package abcpostavailablefreight;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.portlet.*;
import javax.portlet.UnavailableException;
import javax.servlet.*;
import javax.servlet.http.HttpSession;
public class AbcPostAvailableFreightPortlet extends GenericPortlet{
public void init(PortletConfig portletConfig) throws UnavailableException, PortletException {
super.init(portletConfig);
}
public void initializeHats(ActionRequest request) {
initHATS = true;
//Initialize and activate the HATS runtime RAS functions,
// including tracing, logging, PII retrieval, locale.
com.ibm.hats.util.Ras.initializeRas(getPortletConfig());
//Create the license manager
LicenseManager.getInstance();
//Initialize Host Publisher/connection management runtime
Runtime.initRuntime(getPortletConfig());
}
}

Multiple functions for the same path in jax.ws.rs?

Lets say I wanted in implement readonly behaviour on my application (not allowing posts/puts). Could I do this by disabling these types/setting a #POST/#PUT that catches requests on any endpoint? (As opposed to putting a boolean flag on every single post/put in my application
You could add a filter, disallowing all the methods you don't want to support:
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.ext.Provider;
#Provider
public class AuthorizationRequestFilter implements ContainerRequestFilter {
private final List<String> disallowed=Arrays.asList("POST","PUT","DELETE","PATCH");
#Override
public void filter(ContainerRequestContext requestContext)
throws IOException {
if (disallowed.contains(requestContext.getMethod())){
requestContext.abortWith(Response
.status(Response.Status.FORBIDDEN)
.entity("User cannot modify the resource.")
.build());
}
}
}

Cannot make a static reference to the non-static method --- from the type --- Java Bukkit Minecraft

I know there have been plenty of other posts like this from people trying to find their problem in their static class and I have read them but to no avail. I am trying to make a minecraft bukkit plugin for bedwars and when trying to use world.--- I constantly get these errors(Cannot make a static reference to the non-static method getPlayers from the type World) and cannot find where the static class is originating from. Here is my code:
package me.fitch.bedwars.timers;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.World;
import org.bukkit.entity.Player;
import me.fitch.bedwars.main;
public class starttimer implements Listener {
private main plugin;
public starttimer(main plugin) {
this.plugin = plugin;
Bukkit.getPluginManager().registerEvents(this, plugin);
}
#EventHandler
public void startjoin(PlayerJoinEvent e)
{
Object [] players = World.getPlayers().toArray(); //error here
if(players.length == 8)
{
//start countdown
}
}
}
Hope you can help and that I'm not just blind
Thanks :)
Edit: after instantiating World server = new World(); I am now getting "Cannot instantiate the type World" errors, thanks for the help so far guys, hope you can help with this :)
Edit 2: so e.getPlayer().getWorld() works now so thanks but I'm now having issues in my main class where e is not a thing
package me.fitch.bedwars;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.plugin.java.JavaPlugin;
import me.fitch.bedwars.listeners.beddestroy;
public class main extends JavaPlugin {
private main plugin;
public main(main plugin) {
this.plugin = plugin;
}
#Override
public void onEnable()
{
World.setSpawnLocation(Integer.parseInt( plugin.getConfig().getString("respawn_pointx")), Integer.parseInt( plugin.getConfig().getString("respawn_pointx")) + 1,Integer.parseInt( plugin.getConfig().getString("respawn_pointz")));
new beddestroy(this);
}
}
You will want your listener in its own class myListener.java.
From within the listener events in that class you should be able to do everything you need to do.
I like to create a public static Plugin object in case you ever need to reference the specific instance of the Plugin object from any other class in your plugin.
You will register the listener like this in Main.onEnable()
package myPlugin;
import myPlugin.myListenerClass;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
public static Plugin instance;
#Override
public void onEnable() {
instance = this;
PluginManager my_pm = getServer().getPluginManager();
my_pm.registerEvents(new myListenerClass(), this);
}
}
Your myListenerClass.java will be setup something like the following:
package myPlugin;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
public class myListenerClass implements Listener {
#EventHandler
public void onJoin(PlayerJoinEvent event) {
System.out.println("PlayerJoinEvent was triggered!");
int playerCount = Bukkit.getServer().getOnlinePlayers().size();
if (playerCount == 8) {
// start countdown
}
}
}

How to style a vaadin component using css?

I get a vaadin project looking like this:
package com.example.myapplication;
import com.vaadin.annotations.StyleSheet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.shared.ui.label.LabelState;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Label;
#StyleSheet ("stylesheet.css")
#Theme("mytheme")
public class MyUI extends UI {
private static final long serialVersionUID = 1L;
#Override
protected void init(VaadinRequest vaadinRequest) {
Label lb = new Label("VAADIN PROJECT");
lb.setStyleName("mystyle");
layout.addComponent(lb);
setContent(layout);
}
#WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
#VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}
And a .css file that is situated in the same directory as the main file:
.mystyle {
color: red;
font-size: 180px;
pading-left: 30px;
}
When i'am trying to run the project,it's print only the text with no more changes. How can i fix it ?
MY RESULT
You can have a look at the basic starter of Vaadin. It imports a CSS file for the overall view and a CSS file for the specific view.
You can see that HelloWorldView has the annotation #CssImport("./styles/views/helloworld/hello-world-view.css"). This results in importing a CSS file only for this view.
The class MainView forms the parent/border for the other views. It also imports a CSS file with specific attributes for the MainView with the annotation #CssImport("./styles/views/main/main-view.css").
The respective files can be found in the frontend folder of the application.

Minecraft Bukkit Events

I'm trying to get into bukkit programming for minecraft, but for some reason I'm stuck with events. Here's my code:
Main class file:
package com.plugin1;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginDescriptionFile;
//import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import net.md_5.bungee.api.ChatColor;
public class Plugin extends JavaPlugin {
public int songStage;
public static Plugin plugin;
public void OnEnable () {
PluginDescriptionFile pluginDesc = getDescription();
Logger logger = getLogger();
plugin = this;
registerEvents(this, new BlockBreak());
logger.info(pluginDesc.getName() + " is enabled! (V. " + pluginDesc.getVersion() + ")");
}
public void OnDisable () {
PluginDescriptionFile pluginDesc = getDescription();
Logger logger = Logger.getLogger("Plugin");
plugin = null;
logger.info(pluginDesc.getName() + " is disabled! (V. " + pluginDesc.getVersion() + ")");
}
public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
for (Listener listener : listeners) {
Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
}
}
public static Plugin getPlugin() {
return plugin;
}
}
Event class file:
package com.plugin1;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
public class BlockBreak implements Listener {
#EventHandler(priority = EventPriority.HIGH)
public void OnBlockBreak (BlockBreakEvent e) {
Player p = e.getPlayer();
p.sendMessage("Block broken.");
}
}
Basically, this returns no errors. I've gone through console and there's nothing. When I break a block, literally nothing happens!
I've tried a few of things: I've gone through it, tried multiple video tutorials and tried a text tutorial on the minecraft forums but still nothing. I also contacted a server owner who codes bukkit plugins, but he couldn't fix this...
If there's anyone who can help me with this, PLEASE LET ME KNOW!!!!
Thanks in advance!
Here is a code example to start the server in a process:
package me.Nightfighter001.GlobalSystem.Listener;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import me.Nightfighter001.GlobalSystem.Main.main;
public class Join implements Listener {
public Join(main main) {
plugin = main;
plugin.getServer().getPluginManager().registerEvents(this, main);
}
#EventHandler
public void onPlayerJoin(PlayerJoinEvent ev) {
ev.setJoinMessage("");
}
main plugin = main.getPlugin();
}
I think you aren't registering the Listeners in the right way...
Try this code and tell me if it works... I'm really wanting to help you
First of all don't use "Plugin" as the name for your Main Class... Use "Main" instead.
Enable:
public class Main extends JavaPlugin {
public void onEnable() {
Bukkit.getPluginManger().registerEvents(new Join(this),this);
}
}
Listener:
public class Join implements Listener {
private Main plugin;
public Join(Main plugin) {
this.plugin = plugin;
}
#EventHandler
public void onPlayerJoin(PlayerJoinEvent ev) {
ev.setJoinMessage("Just another test");
}
}
Hope it works...
I've tested your code and it really doesn't work. I think your Eventregistration isn't working. For my plugins I use this in the mainClass:
package me.Nightfighter001.GlobalSystem.Main;
import org.bukkit.Bukkit;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import me.Nightfighter001.GlobalSystem.Listener.Join;
public class main extends JavaPlugin {
public static main getPlugin() {
return plugin;
}
private static main plugin;
#Override
public void onEnable() {
plugin = this;
new Join(this);
ConsoleCommandSender console = Bukkit.getConsoleSender();
console.sendMessage(new StringBuilder("\247c[\2476GlobalSystem\247c] \247bVersion \247c")
.append(getDescription().getVersion()).append(" \247bdes Plugins wurde aktiviert!").toString());
console.sendMessage(
"\247c[\2476GlobalSystem\247c] \247bDieses Plugin darf nur benutzt werden, wenn der Entwickler \247cNightfighter001 \247bes erlaubt!");
return;
}
#Override
public void onDisable() {
ConsoleCommandSender console = Bukkit.getConsoleSender();
console.sendMessage(new StringBuilder("\247c[\2476GlobalSystem\247c] \247bVersion \2474")
.append(getDescription().getVersion()).append(" \247bdes Plugins wurde deaktiviert!").toString());
}
}
And in the EventClass:
package me.Nightfighter001.GlobalSystem.Listener;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import me.Nightfighter001.GlobalSystem.Main.main;
public class Join implements Listener {
public Join(main main) {
plugin = main;
plugin.getServer().getPluginManager().registerEvents(this, main);
}
#EventHandler
public void onPlayerJoin(PlayerJoinEvent ev) {
ev.setJoinMessage("");
}
main plugin = main.getPlugin();
}
As you can see in my example I use the PlayerJoinEvent, but it also works with the BlockBreakEvent. I hope this helps :) And sorry for my bad English ;D
Your code will work if you don't capitalize the names of the onEnable (and onDisable) methods. onEnable and OnEnable are two different methods since java is case sensitive, and since you're trying to override specific methods in the JavaPlugin super class, you'll need to spell them the exact same way.
Common convention is, as far as I know, that you start your methods with lowercase letters anyway though. The #Override annotation is very useful in catching these kinds of bugs, because it lets the compiler know that you mean to override an existing method, and if that method doesn't exist (for example if you misspelled the name or added different parameters), it will alert you (it also lets anyone reading the code immediately know you're overriding an existing method or implementing an interface).

Categories