Costum Event Handling - event-handling

I issue I am trying to get rid off is the following:
I intend to setup a costum event handling chain as a workaround for JavaFX's lack of actioncommands.
The issue in particular is, that a menuitem upon clicking it, still fires an ActionEvent instead of the self-written MilvaLabActionEvent.
The code:
Event class
package jpt.gui.items;
import javafx.event.ActionEvent;
public class MilvaLabActionEvent extends ActionEvent {
private static final long serialVersionUID = 6757067652205246280L;
private String actionCommand ="";
public MilvaLabActionEvent(String actionCommand2) {
setActionCommand(actionCommand2);
}
public MilvaLabActionEvent() {}
public String getActionCommand() {
return actionCommand;
}
public void setActionCommand(String actioncommand) {
this.actionCommand = actioncommand;
}
}
My EventHandler:
package jpt.gui.items;
import javafx.event.EventHandler;
import jpt.MilvaLabGlobal;
import jpt.MilvaLabKonst;
import jpt.handle.MilvaLabDateiHandle;
import jpt.handle.MilvaLabEinHandle;
import jpt.handle.MilvaLabHilfeHandle;
import jpt.handle.MilvaLabMilvaHandle;
import jpt.handle.MilvaLabRvAnwendungHandle;
import jpt.handle.MilvaLabrvTextHandle;
import jpt.log4j.MilvaLabLogger;
public class MilvaLabEventHandler implements EventHandler<MilvaLabActionEvent>{
#Override
public void handle(MilvaLabActionEvent event) {
// the command string of the menu item
final String sCmd = event.getActionCommand();
if (sCmd.charAt(0) == 'M')
{//doing something here
}
}
The costum MenuItem-Class I figured out I gotta write.
package jpt.gui.items;
import javafx.event.Event;
import javafx.scene.control.MenuItem;
public class MilvaLabMenuItem extends MenuItem {
private String actionCommand;
public MilvaLabMenuItem(String sText) {
this.setText(sText);
}
#Override
public void fire() {
Event.fireEvent(this, new MilvaLabActionEvent(getActionCommand()));
}
public String getActionCommand() {
return actionCommand;
}
public void setActionCommand(String actionCommand) {
this.actionCommand = actionCommand;
}
}
And the initialization of the costum MenuItem:
final MilvaLabMenuItem jmi = new MilvaLabMenuItem("I am a menuItem");
jmi.addEventHandler(evtype, new MilvaLabEventHandler());
jmi.setOnAction((event) -> {
System.out.print("I have fired an ActionEvent!");
});
Well, as of now, I got "I have fired an ActionEvent" when I click on the MilvaLabMenuItem, nothing else happens. (Looked into that thing already using the debugger).
What I want to happen is that, obviously, the MilvaLabEventHandler is called.

I figured it out again.
I declared two EventTypes, though, only one was necessary.
This helped me finding the solution, though, they use Nodes instead of MenuItems.
How to emit and handle custom events?

Related

Javafx: Can't import custom control into Scene Builder

I tried to import my gui control (jar file) into the scene builder but without success. The file was created using eclipse export -> jar. When it was selected in scene builder "import JAR/FXML File", there was nothing inside the Import Dialog.
My control is a TextField with TextFormatter:
import java.text.NumberFormat;
import java.text.ParseException;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.util.StringConverter;
public class CustomTextField extends TextField {
public CustomTextField() {
setText("Custom");
NumberTextFormatter formatter = new NumberTextFormatter(new StringConverter<Number>() {
#Override
public String toString(Number object) {
return object.toString();
}
#Override
public Number fromString(String string) {
try {
return NumberFormat.getInstance().parse(string);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
});
setTextFormatter(formatter);
}
public static class NumberTextFormatter extends TextFormatter<Number> {
public NumberTextFormatter(StringConverter<Number> valueConverter) {
super(valueConverter);
}
}
}
I am sure that the culprit is the usage of TextFormatter. Because the control will works (appear in Import Dialog) if I removed the TextFormatter from the codes.
Whats wrong?
Update: Even if I simplified my constructor to exclude setTextFormatter(), as long as it still have a line of TextFormatter declaration the problem still persist. Such as:
public CustomTextField() {
setText("Custom");
TextFormatter<Integer> formatter = new TextFormatter<Integer>(new IntegerStringConverter());
}
Found the solution. The oracle scene builder 2.0 is too old/ too dumb to work with TextFormatter. Need to upgrade to Gluon scene builder 8.5.0.

Custom Button in MessageDialog

I want to add my custom button with AjaxEventBehavior to MessageDialog , i can add simple custom button without AjaxEventBehavior by extending DialogButton class, but this will be useless button with out listener, anyone no how to do it?
here is my code:
import com.googlecode.wicket.jquery.ui.widget.dialog.MessageDialog;
import org.apache.wicket.ajax.AjaxRequestTarget;
import
org.apache.wicket.core.request.handler.IPartialPageRequestHandler;
import org.apache.wicket.model.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
public class TipOfTheDayDialog extends MessageDialog {
private static final Logger log =
LoggerFactory.getLogger(TipOfTheDayDialog.class);
Model<String> model = null;
List<DialogButton> dialogButtons =null;
public TipOfTheDayDialog(String id, Model<String> model,List<DialogButton> dialogButtons) {
super(id, Model.of("Совет дня"), model, dialogButtons);
this.model = model;
}
#Override
public void onClose(IPartialPageRequestHandler handler, DialogButton button) {}
#Override
public void onClick(AjaxRequestTarget target, DialogButton button)
{
if(!button.getName().equals("next")){
super.close(target,button);
this.close(target, button);
}else {
model.setObject("another message");
target.add(this);
}
}
}
I have decided to override method onclick instead of adding button with its own listener, but now i have another problem, i can't change message of dialog without it's closing , when i change message by this line of the code: model.setObject("another message"); and then add it to the target by this code : target.add(this); dialog window closes, how to fix it?

Eclipse Plug-in Development: DefaultInformationControl Does'nt work as expected

I am writing eclipse plugin that using the texthover and override it for my own text.
I need to add an action to the hover window so I used the following function:
public IInformationControlCreator getHoverControlCreator()
This is my code:
#Override
public IInformationControlCreator getHoverControlCreator() {
return new IInformationControlCreator() {
public IInformationControl createInformationControl(Shell parent) {
Shell s=new Shell(parent,SWT.V_SCROLL | SWT.H_SCROLL);
ToolBarManager tbm = new ToolBarManager(SWT.FLAT);
NoFocusDic dic = new NoFocusDic(parent, tbm);
dic.setBackgroundColor(new Color(null,123,234,12));
ButtonAction ba=new ButtonAction();
tbm.add(ba);
tbm.update(true);
dic.setSize(300,100);
dic.setLocation(new Point(500,500));
// dic.setInformation("abc");
dic.setVisible(true);
return dic;
}
};
}
When I run this code with the setInformation line I get this window:
And when I run it without the setInformation line I get this window:
So The window I want to always get is the second one, but with text inside.
I read in eclipse site that the setVisible won't work if I try to catch the focus, but I don't think that setInformation is catching focus.
Any Ideas what to do?
package TextHoverPackage;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.IInformationControl;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.IInformationControlExtension2;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Shell;
public class NoFocusDic extends DefaultInformationControl implements IInformationControlExtension2 {
public NoFocusDic(Shell parent, ToolBarManager toolBarManager) {
super(parent, toolBarManager);
}
public NoFocusDic(Shell parent, ToolBarManager toolBarManager, IInformationPresenter a){
super(parent, toolBarManager,a);
}
#Override
public void setInput(Object arg0) {
super.setInformation(arg0.toString());
}
}
DefaultInformationControl.setInformation adjusts the size of the window to fix the text that you give it. However it only does this if an IInformationPresenter has been defined.
So one option is to specify null for the IInformationPresenter in your constructor:
public NoFocusDic(Shell parent, ToolBarManager toolBarManager) {
super(parent, toolBarManager, null);
}

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);.

Getting user data in NewProjectCreationPage in Eclipse Plugin

I have been successful in making a plugin. However now i need that on project creation page i add some more textboxes to get the user information. Also i need to use this information to add into the auto generated .php files made in project directory.
I want to know how can i override the WizardNewProjectCreationPage to add some more textboxes to the already given layout. I am pretty new to plugin development. Here is the code for my custom wizard.
import java.net.URI;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
import rudraxplugin.pages.MyPageOne;
import rudraxplugin.projects.RudraxSupport;
public class CustomProjectNewWizard extends Wizard implements INewWizard, IExecutableExtension {
private WizardNewProjectCreationPage _pageOne;
protected MyPageOne one;
private IConfigurationElement _configurationElement;
public CustomProjectNewWizard() {
// TODO Auto-generated constructor stub
setWindowTitle("RudraX");
}
#Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
// TODO Auto-generated method stub
}
#Override
public void addPages() {
super.addPages();
_pageOne = new WizardNewProjectCreationPage("From Scratch Project Wizard");
_pageOne.setTitle("From Scratch Project");
_pageOne.setDescription("Create something from scratch.");
addPage(one);
addPage(_pageOne);
}
#Override
public boolean performFinish() {
String name = _pageOne.getProjectName();
URI location = null;
if (!_pageOne.useDefaults()) {
location = _pageOne.getLocationURI();
System.err.println("location: " + location.toString()); //$NON-NLS-1$
} // else location == null
RudraxSupport.createProject(name, location);
// Add this
BasicNewProjectResourceWizard.updatePerspective(_configurationElement);
return true;
}
#Override
public void setInitializationData(IConfigurationElement config,
String propertyName, Object data) throws CoreException {
_configurationElement = config;
// TODO Auto-generated method stub
}
}
Ask for any other code required. Any help is appreciated. Thank You.
Instead of using WizardNewProjectCreationPage directly create a new class extending WizardNewProjectCreationPage and override the createControl method to create new controls:
class MyNewProjectCreationPage extends WizardNewProjectCreationPage
{
#Override
public void createControl(Composite parent)
{
super.createControl(parent);
Composite body = (Composite)getControl();
... create new controls here
}
}