Overview of Handler in GWT - gwt

Is there an overview about all handlers and their corresponding containers in GWT? Whenever I try to add a handler to a container, I have to check, whether the handler fires an event or not (the JavaDoc does not provide useful information about this). For example a ResizeHandler:
SplitLayoutPanel splitLayoutPanel = new SplitLayoutPanel() {
#Override
public void onResize() {
super.onResize();
System.out.println("onResize");
}
};
splitLayoutPanel.addHandler(new ResizeHandler() {
#Override
public void onResize(ResizeEvent event) {
System.out.println("resize");
}
}, ResizeEvent.getType());
Overwriting the onResize()-method (1. example) gives an information, if the splitter changes, but if I add a ResizeHandler (2. example), I do not get any call of the onResize-method. I don't understand why and don't find the documentation why the handler is not allowed in this container.
I search for an overview of all available handlers, together with their possible containers and event, when they will be fired.

Usually there will be a more specific addHandler method. For example, Button has addClickHandler(ClickHandler). The interface that defines that method is HasClickHandlers, so you can look out for that, for example.
If the event doesn't have it's own addXyzHandler method, it probably won't be supported very well. In that case, it's usually pretty easy to subclass the widget and add support for that handler yourself.

If you add a handler using addHandler() on a Widget, you have to ensure the underlying DOM element could catch matching event. If yes, you also have to tell your Widget to sink this event using
void com.google.gwt.user.client.ui.Widget.sinkEvents(int eventBitsToAdd)
where eventBitsToAdd is a constant from com.google.gwt.user.client.Event.
AFAIK ONRESIZE event is not yet supported natively. So as I said yesterday, you have to implement your mouse handlers and gesture, or override a slider ;-)

Related

GWT's KeyUpHandler is called multiple times instead of one

I've added a KeyUpHandler to a Button object as follows:
button.addKeyUpHandler(new KeyUpHandler() {
#Override
public void onKeyUp(final KeyUpEvent event1) {
if (event.isDownArrow()) {
counter++;
}
}
});
However, when I debug with SuperDev mode step by step, there are two calls to the onKeyUp where the initial calls has always counter's initial value (0 in this case) and second call has the latest -maintained- value of counter. The method where I added the logic of onKeyUp is also marked as synchronized. I also tried to call removeHandler() method of HandlerRegistration right after onKeyUp is called. The result was that the mentioned two calls happened after first key up event, but when I did another key up event only one more call happened and further key up events area didn't happen. I assume this is sth related with superdev mode or there is some internal overhead after the compilation. What is the correct way of adding any event handlers at GWT? Do we need to always take care of each handler by calling removeHandler() method of HandlerRegistration?
Re: your comment about multiple calls for click/mouseup/keyup
The browser "helps" for the click handler by firing it whenever any event would effectively click, either mousedown, then mouseup but no move, or various touch or keyboard events. Consider calling preventDefault() on an event you have already handled and you don't want the browser to further look at - for example, if you call this on mouseup and keyup, then those particular actions should never result in click events going off.

Implementing "Expand/Collapse all" with DisclosurePanels in GWT

I have a lot of DisclosurePanels nested one inside another (or may not be nested either). I want to implement "expand all" and "collapse all" buttons.
Please suggest a clean way to do so.
Note: I tried looking a bit onto Event handling in GWT but got a bit confused with things. I am not sure if I could use Custom Events for my case. It seems that I can define a custom event and its handler and do the addHandler() stuff but in the class that'll implement the handler interface I don't have the instance of DisclosurePanel that'll allow me to use setOpen() property of disclosure panel.
One way of doing this would be to introspect the Widget hierarchy. Starting from the topmost parent disclosure panel, look at all of the child widgets, check if they are DisclosurePanels, and either open/close them. This could be potentially bad for performance if there are many widgets, but you could try anyways.
This code hasn't been tested, but might do what you need:
void openChildren(Widget w, boolean isOpen){
if(w instanceof DisclosurePanel){
((DisclosurePanel)w).setOpen(isOpen);
}
if(w instanceof HasWidgets){
for(Widget ch : ((HasWidgets)w)){
openChildren(ch, isOpen);
}
}
if(w instanceof HasOneWidget){
openChildren(((HasOneWidget)w).getWidget(), isOpen);
}
}

Gtk.Bin derived class does not receive MotionNotifyEvent

i am developing a Gtk# based application which uses a custom widget derived from Gtk.Bin. For some reason not known to me, it does not receive mouse movement events. The code below is never called:
[GLib.ConnectBefore]
protected override bool OnMotionNotifyEvent (Gdk.EventMotion evnt)
{
Console.Out.WriteLine( "Mouse move!" );
return base.OnMotionNotifyEvent (evnt);
}
I have also extended the Event mask of the widget to receive all events (in the constructor):
AddEvents(( int ) Gdk.EventMask.AllEventsMask );
Any ideas?
Gtk.Bin is a windowless container so it does not receive mouse events by default.
You can create a Gdk.Window manually in your subclass in order to receive mouse events, or the easier path is to subclass Gtk.EventBox, which is a Gtk.Bin subclass designed for this purpose by internally creating its own Gdk window.

how to keep view "humble" -using SuggestBox with special Oracle and Suggestion

i learned how to implement my own SuggestionOracle("AuSuggestOracle") and own
Suggestions("AuMultiWordSuggestion"). In my case the suggestion object
is constructed with a DTO. On a selection event i need this dto (or
some fields of it) to react appropriate.
I implemented a widget containing 3 suggest boxes with this special
oracle and some logic between them. Now i want to apply MVP pattern -
split this widget in presenter and view.
At the moment the presenters display interface look like that:
public interface Display {
HasSelectionHandlers<Suggestion> getFedLand();
HasSelectionHandlers<Suggestion> getCounty();
HasSelectionHandlers<Suggestion> getCommunity();
AuSuggestOracle getFedLandOracle();
AuSuggestOracle getCountyOracle();
AuSuggestOracle getCommunityOracle();
void clearCounty();
void clearCommunity();
void activateForm();
Widget asWidget();
}
the problem is the implicit knowledge about my model in methods
returning "AuSuggestOracle". so my question is how to get the view/
interface "humble". in my case the displayed suggestion-strings are
ambiguous and i need at least the "id" of a selected item to know what
DTObject is selected.
The way I got around this is by leaving out the getters for the Oracle since once my presenter sets it my view doesn't need any information about it. So, my interface looked like this:
public interface Display {
...
void setSuggestionOracle(SuggestOracle oracle);
HasSelectionHandlers<SuggestOracle.Suggestion> getSelectionListener();
}
The problem I encountered was being able to add the suggestion to the SuggestBox after it was instantiated. To get around this, I initialized with a blank SuggestBox and then removed it from the view, updated in, and inserted it back into position.
After that, you can write your handler (in the presenter) to check if the suggestion is an instance of your custom suggestion and your presenter can handle the selection and push the relevant information back down to your view.
By doing this, all your view knows is that it will be taking generic suggestions for something, and that at some later time it will be updating with information (which will be as a result of the suggestion, but the view is to 'humble' to know that).

What is the list of valid GWT DOM / consumed events for Cells?

I've run into this multiple times, and can't find anything comprehensive. I want to know the complete list of all valid consumable DOM events for GWT.
The GWT docs for NativeEvent says:
public final java.lang.String getType()
Gets the enumerated type of this event.
Returns:
the event's enumerated type
Where is this enumeration? Does it actually exist? The actual code used (that I've found) that explicitly says these events always uses strings: "click", "contextmenu", "mouseup", "dblclick", etc. (etc covers so many vaguaries ...)
I'm trying to implement both Double Click and Right Click for cells in a CellTable ala this post. I'm passing super("click", "contextmenu", "mouseup", "dblclick"); in the constructor of my extension of AbstractCell. Then I overrode onBrowserEvent:
#Override
public void onBrowserEvent(Context context, Element parent, ImageProperties<T> value,
NativeEvent event, ValueUpdater<ImageProperties<T>> valueUpdater) {
if (event.getButton() == NativeEvent.BUTTON_RIGHT) {
event.stopPropagation();
event.preventDefault();
eventBus.fireEvent(new RightClickEvent<Context>(context, event));
} else {
super.onBrowserEvent(context, parent, value, event, valueUpdater);
}
}
However, I run into two issues. One, the default contextMenu still gets shown (over my custom one) - not to mention it doesn't even use the DOM event type. A different problem, how do I check if its a double click event? I find it hard to believe that it's literally an arbitrary set of strings ...
Thanks in advance!
John
Native JavaScript DOM event types really are arbitrary strings and support for a given event type (and the name thereof) can be browser dependent.
the enumeration you are looking for does exist in the BrowserEvents class. you should use those constants instead of “magic” string literals.