Custom ValueChangeHandler GWT - java

I need to write a custom ValueChangeHandler and call out onValueChange(ValueChangeEvent). However I don't understand how to write a ValueChangeEvent.
Maybe I understand the entire GWT event system wrong. Can anyone help?
Edit: I am asking how to create my own class that dispatches the ValueChangeEvent. I understand how to listen to it.
The constructor of ValueChangeEvent is not visible and I can't create it.

If you want to fire a ValueChangeEvent you must implement the interface HasValueChangeHandlers by or your class or somewhere in the class.
A simple implementation would be to use the EventBus:
EventBus bus = new SimpleEventBus();
#Override
public void fireEvent(GwtEvent<?> event) {
bus.fireEvent(event);
}
#Override
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<T> handler) {
return bus.addHandler(ValueChangeEvent.getType(), handler);
}
Note you need to substitute T with the type you want to fire.
Because you can't create a ValueChangeEvent directly dispatching an event is done via the fire method:
ValueChangeEvent.fire(this, value);
Where this refers to the class/field implementing the HasValueChangeHandlers and value refers to the value that has been changed and you want to dispatch the event.

Actually, instead of creating a new EventBus or HandlerManager, as your widget will be a subclass of Wiget, it the standard way would be to use the Widget.addHandler(handler, eventType) method. Here's a minimal example:
public class MyWidget<T> extends Composite implements HasValueChangeHandlers<T>, HasValue<T> {
private T value;
public MyWidget() {
// Initialize stuff
}
#Override
public HandlerRegistration addValueChangeHandler(final ValueChangeHandler<T> handler) {
return addHandler(handler, ValueChangeEvent.getType());
}
#Override
public T getValue() {
return value;
}
#Override
public void setValue(T value) {
setValue(value, false);
}
#Override
public void setValue(T value, boolean fireEvents) {
this.value = value;
if (fireEvents) {
ValueChangeEvent.fire(this, getValue());
}
}
}

Really late to answer, but I've just solved this problem like this:
ValueChangeEvent.fire(hasValueChangeHandlerInstance, getValue());

The ValueChangeEvent is generated for you by GWT. You can add one (or more) ValueChangeHandler to any class that implements the interface HasValueChangeHandlers. One of these classes is TextArea, so let's look at a little example:
TextArea textArea = new TextArea();
textArea.addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
// do something
}
});
When the value of the text area changes, GWT will automatically generate a ValueChangeEvent, which you can use in the part I marked with "do something".

Related

How to catch/forward MouseWheel event to several objects?

I am using yFiles library and I am handling a Graph2DView object named view. To activate mouseWheel scroll for this object, I have to add a listener in registerViewListeners function. However, I also want to be notified also in myClass mouseWheelMoved function when mouseweel
public class MyClass extends MyBaseClass implements MouseWheelListener {
Graph2DView view;
// .....
#Override
protected void registerViewListeners()
{
Graph2DViewMouseWheelScrollListener wheelListener = new Graph2DViewMouseWheelScrollListener();
wheelListener.addToCanvas(view);
// The two precedent instruction is equivalent to
// view.getCanvasComponent().addMouseWheelListener(this);
}
#Override
public void mouseWheelMoved(MouseWheelEvent e)
{
// some work ...
}
}
Problem:
If i register my view object through registerViewListeners
#Override
protected void registerViewListeners()
{
Graph2DViewMouseWheelScrollListener wheelListener = new Graph2DViewMouseWheelScrollListener();
wheelListener.addToCanvas(view);
}
My mouseWheelMoved function is no longer notified:
#Override
public void mouseWheelMoved(MouseWheelEvent e)
{
// not called
}
Your description sounds as if you have simply removed the registration for your event listener.
In the place where you instantiate MyClass, please also add:
view.getCanvasComponent().addMouseWheelListener(myClassInstance); // register listener
If you don't register your listener, it will not be called, of course. Only instantiating it will not suffice.

How to notify the obsever when list changes in Rxjava

I have class three classes. Pref, ClassA, and ClassB.
public class Pref{
public static ArrayList<Pref> prefList;
public static Observable<ArrayList<Pref>> observable;
public static void loadData(){
prefList = getFromDb();
observable = Observable.just(prefList);
}
}
Application runs the ClassA First.
public ClassA{
public ClassA(){
initObserver();
setObserver();
}
public void initObserver(){
Pref.loadData();
}
public void setObserver(){
Observer<ArrayList<Pref>> obs = new Observer() {
#Override
public void onSubscribe(Disposable dspsbl) {
System.out.println("Subscribed");
}
#Override
public void onNext(ArrayList<Pref>> t) {
System.out.println("Loading Preference.");
//Need to do some other works here.
}
#Override
public void onError(Throwable thrwbl) {
}
#Override
public void onComplete() {
}
};
Pref.observable.subscribe(obs);
}
}
Now I want to change the list from ClassB.
public class ClassB{
private void changeList(){
Pref.prefList = loadDataFromSomeSource();
}
}
When I run ClassA, the System.out works fine. But when I change the list from ClassB nothing happens. My question is, is the right way to work with Rxjava. Is it for Rxjava built? If I am wrong how can I achieve this functionality? How can I write several ClassA like classes so that When the ClassB::changeList() runs, I can listen it in ClassA?
By setting Pref.prefList = loadDataFromSomeSource();, you assign a new list instance to Pref.prefList. This will not update Pref.observable in any way, because this still refers to the old Pref.prefList instance.
I also think that you can not use an Observable to publish events through it. As far as I understand your situation, you need an ObservableSource (see http://reactivex.io/RxJava/javadoc/io/reactivex/ObservableSource.html). For example, it is implemented by PublishSubject. You could use it like this:
PublishSubject<String> source = PublishSubject.create();
source.subscribe(System.out::println);
source.onNext("test 1");
source.onNext("test 2");
source.onNext("test 3");
Or, in your case: in class Pref, you can use public static PublishSubject<ArrayList<Pref>> source = PublishSubject.create();. When loading the data, you can publish the new data using onNext, like this in ClassB: Pref.source.onNext(loadDataFromSomeSource())

Java Observers/Object Listeners (Game Engine)

I'm working on a game engine, and the last question I had regarding this was what good way I can use to make "observers" or listeners. A user suggested that I should use Java's EventObject class to inherit from and make a Listener interface. However, this didn't provide me with good flexibility.
Here is the Handler annotation to state that a method is an event handler in a listener:
#Retention(RetentionPolicy.CLASS)
#Target(ElementType.METHOD)
public #interface Handler {}
Here is the base class for Event, which is basically the same as EventObject (but I'll add abstract methods sooner or later):
public abstract class Event {
private Object source;
public Event(Object source) {
this.source = source;
}
public Object getSource() {
return source;
}
}
Here is the Listener class, which is empty:
public interface Listener {}
Here is the ListenerHandler class, used to handle all listeners. You register and unregister them here. I'll edit the register/unregister methods later for a better use:
public class ListenerHandler {
private ArrayList<Listener> listeners;
public ListenerHandler() {
this.listeners = new ArrayList<Listener>();
}
public void registerListener(Listener l) {
listeners.add(l);
}
public void unregisterListener(Listener l) {
listeners.remove(l);
}
public void onEvent(Event event) {
for(Listener l : listeners) {
Class<?> c = l.getClass();
Method[] methods = c.getDeclaredMethods();
for(Method m : methods) {
if(m.isAccessible()) {
if(m.isAnnotationPresent(Handler.class)) {
Class<?>[] params = m.getParameterTypes();
if(params.length > 1) {
continue;
}
Class<?> par = params[0];
if(par.getSuperclass().equals(Event.class)) {
try {
m.invoke(this, event);
}catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
From what I heard, it's a use of a lot of memory in order to get all methods of a class. I'm not going to assume this is the case, but I'm sure there is a better way as this will be a game engine with many components and such.
I'd like to know the best way to implement this, or if I'm doing it right. I'd also like to know if anyone can help me improve this in any way without hogging memory usage by the game (as of now it's not a big deal -- the "game engine" is not even close to rendering anything yet)
I tried to keep it a very simple example and will comment with different ideas to it:
First meet the Achievement class:
import java.util.Observable;
public class Achievement extends Observable {
public static class AchievementDetails {}
public Achievement() {
addObserver(EventsListener.getInstance());
}
public void achievementReached() {
AchievementDetails achievemetDetails = null;
setChanged();
notifyObservers(achievemetDetails);
}
}
And then the events listener class:
import com.test.Achievement.AchievementDetails;
public class EventsListener implements Observer {
private static EventsListener instance = new EventsListener();
public static EventsListener getInstance() {
return instance;
}
#Override
public void update(Observable o, Object arg) {
if(o instanceof Achievement) {
AchievementDetails achievemetDetails = (AchievementDetails) arg;
//do some logic here
}
}
}
The only one thing that is missing is to create an instance of your achievement (which register the EventsListener to itself) and handle the life cycle of it.

Java Event Listener to detect a variable change

I cannot seem to find an answer anywhere to my question. Is there any event listener which can detect the changing of a boolean or other variable and then act on it. Or is it possible to create a custom event listener to detect this?
Please I cannot seem to find a solution to this anywhere and I found this website explaining how to create custom events
Use PropertyChangeSupport. You wont have to implement as much and it is thread safe.
public class MyClassWithText {
protected PropertyChangeSupport propertyChangeSupport;
private String text;
public MyClassWithText () {
propertyChangeSupport = new PropertyChangeSupport(this);
}
public void setText(String text) {
String oldText = this.text;
this.text = text;
propertyChangeSupport.firePropertyChange("MyTextProperty",oldText, text);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
}
public class MyTextListener implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getPropertyName().equals("MyTextProperty")) {
System.out.println(event.getNewValue().toString());
}
}
}
public class MyTextTest {
public static void main(String[] args) {
MyClassWithText interestingText = new MyClassWithText();
MyTextListener listener = new MyTextListener();
interestingText.addPropertyChangeListener(listener);
interestingText.setText("FRIST!");
interestingText.setText("it's more like when you take a car, and you...");
}
}
Just like you need to create an event listener, you will also need to create the event firer -- since there is nothing automatic that will do this for you. I've provided sample code that shows you how to implement such a firer.
This test implementation isn't perfect. It only includes a way to add listeners. You may wish to include a way to remove listeners who are no longer interested in receiving events. Also note that this class is not thread-safe.
import java.util.List;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.EventObject;
import java.awt.EventQueue;
/**
* This class uses the EventQueue to process its events, but you should only
* really do this if the changes you make have an impact on part of a GUI
* eg. adding a button to a JFrame.
*
* Otherwise, you should create your own event dispatch thread that can handle
* change events
*/
public class BooleanChangeTest implements BooleanChangeDispatcher {
public static void main(String[] args) {
BooleanChangeListener listener = new BooleanChangeListener() {
#Override
public void stateChanged(BooleanChangeEvent event) {
System.out.println("Detected change to: "
+ event.getDispatcher().getFlag()
+ " -- event: " + event);
}
};
BooleanChangeTest test = new BooleanChangeTest(false);
test.addBooleanChangeListener(listener);
test.setFlag(false); // no change, no event dispatch
test.setFlag(true); // changed to true -- event dispatched
}
private boolean flag;
private List<BooleanChangeListener> listeners;
public BooleanChangeTest(boolean initialFlagState) {
flag = initialFlagState;
listeners = new ArrayList<BooleanChangeListener>();
}
#Override
public void addBooleanChangeListener(BooleanChangeListener listener) {
listeners.add(listener);
}
#Override
public void setFlag(boolean flag) {
if (this.flag != flag) {
this.flag = flag;
dispatchEvent();
}
}
#Override
public boolean getFlag() {
return flag;
}
private void dispatchEvent() {
final BooleanChangeEvent event = new BooleanChangeEvent(this);
for (BooleanChangeListener l : listeners) {
dispatchRunnableOnEventQueue(l, event);
}
}
private void dispatchRunnableOnEventQueue(
final BooleanChangeListener listener,
final BooleanChangeEvent event) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
listener.stateChanged(event);
}
});
}
}
interface BooleanChangeDispatcher {
public void addBooleanChangeListener(BooleanChangeListener listener);
public boolean getFlag();
public void setFlag(boolean flag);
}
/**
* Listener interface for classes interested in knowing about a boolean
* flag change.
*/
interface BooleanChangeListener extends EventListener {
public void stateChanged(BooleanChangeEvent event);
}
/**
* This class lets the listener know when the change occured and what
* object was changed.
*/
class BooleanChangeEvent extends EventObject {
private final BooleanChangeDispatcher dispatcher;
public BooleanChangeEvent(BooleanChangeDispatcher dispatcher) {
super(dispatcher);
this.dispatcher = dispatcher;
}
// type safe way to get source (as opposed to getSource of EventObject
public BooleanChangeDispatcher getDispatcher() {
return dispatcher;
}
}
you can also try to implement an Observer.
First create the observable object:
import java.util.Observable;
public class StringObservable extends Observable {
private String name;
public StringObservable(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
setChanged();
notifyObservers(name);
}
}
Then the observer:
import java.util.Observable;
import java.util.Observer;
public class NameObserver implements Observer {
private String name;
public NameObserver() {
name = null;
}
public void update(Observable obj, Object arg) {
if (arg instanceof String) {
name = (String) arg;
System.out.println("NameObserver: Name changed to " + name);
} else {
System.out.println("NameObserver: Some other change to subject!");
}
}
}
And in your main (or wherever else):
public class TestObservers {
public static void main(String args[]) {
// Create the Subject and Observers.
StringObservable s = new StringObservable("Test");
NameObserver nameObs = new NameObserver();
// Add the Observer
s.addObserver(nameObs);
// Make changes to the Subject.
s.setName("Test1");
s.setName("Test2");
}
}
Mostly found here
Very late to answer, but this is a problem that can be solved with Observer/Observable. Example
The boolean you are setting should be allowed to do only through a setter method like:
public void setFlag(boolean flag){
//Method code goes here
}
Now in now set method, you can decide based on what value comes in, what event needs to be fired. I am explaining in simple terms without introducing complex terms so you can understand better, so code snippet would look like:
public void setFlag(boolean flag){
//if flag is TRUE do something
//If flag is FALSE then do something
//And finally do what you needed to do with flag
}
Ask questions if you need more info
you create a listener when you want to listen for I/O changes. mostly on graphics.
the answer to your question is to keep state of the running program, then check if variables change from the state inside the infinite loop of your program.
You can use AOP for that, perhaps AspectJ? Check a few examples here (if you use Eclipse, then using AspectJ is really simple with their plugin).
For you, you would have a pointcut similar to the one used in the SampleAspect, but one that will only be used when someone makes a new SET to a boolean variable (this doesn't mean that the value has changed, just that someone loaded a value to the variable).

How to make addValueChangeHandler act just like addChangeHandler

I made a wrap widget that implements interface HasChangeHandlers
But i just can't fit events to each other.
public HandlerRegistration addChangeHandler( final ChangeHandler handler ) {
HandlerRegistration registration1 = dateFrom.addValueChangeHandler( handler );// Compile error
HandlerRegistration registration2 = dateTo.addValueChangeHandler( new ValueChangeHandler<Date>() {
#Override
public void onValueChange( ValueChangeEvent<Date> dateValueChangeEvent ) {
//have to fire handler ??
}
} );
return null; // what i should return here?
}
Thanks in advance !
You return the handler of the member object that you want the event to be associated to. For example I have a textbox+label widget and I couldnt create #UiHandler event on it from somewhere because it is not standard so what i did was:
public class TextBoxAndLabel implements HasKeyUpHandlers {
private TextBox myTextBox;
private Label myLabel;
#Override
public HandlerRegistration addKeyUpHandler(KeyUpHandler keyUpHandler) {
return myTextBox.addKeyUpHandler(keyUpHandler);
}
}
and now I can implement
#UiHandler("myClassObject")
A ChangeHandler is not a ValueChangeHandler. You have to make another wrapper class which implements ValueChangeHandler and takes a ChangeHandler as an instance variable. You can then write...
HandlerRegistration registration1 = dateFrom.addValueChangeHandler(new ChangeHandlerWrapper(handler));
Where ChangeHandlerWrapper is a class that implements ValueChangeHandler. For example,
class ChangeHandlerWrapper<T> implements ValueChangeHandler<Date>
{
private ChangeHandler handler;
public void onValueChange( ValueChangeEvent<T> changeEvent) {
handler.onChange(null);
}
}
Of course, this assumes that you don't need the actual event in your handler. If you do then things will get more complicated.

Categories