tags:

views:

49

answers:

2

Let's assume that I have a GWT application with many instances of Label (let's say hundreds of them), and a Button. When the user click on the Button, my application does the following:

  • highlights every Label by making its background color green;
  • when the user moves the mouse on an highlighted Label, its background becomes dark green, and the font turns white;
  • when the user moves the mouse out of an an highlighted Label, its background reverts green, and the font reverts to black;
  • when the user clicks on an highlighted Label, every Label is reverted to the original appearance (i.e. before the user clicked the Button).

In order to achieve the above behaviour I need to register MouseOverHandler, MouseOutHandler, and ClickHandler for every Label.

Let's say that I have a class MyHandler implementing all 3 handlers. From performance perspective, what is better?

  • have one single instance of MyHandler and register it as MouseOverHandler, MouseOutHandler, and ClickHandler for every Label
  • register a new instance of MyHandler as MouseOverHandler, MouseOutHandler, and ClickHandler for every Label

Moreover, after the user has clicked an highlighted Label, I'm no longer interested in MouseOverEvent, MouseOutEvent, and ClickEvent from the labels. In this situation, is it better to unregister the instance(s) of MyHandler, or to keep them registered for the next time the user will click the Button?

+2  A: 

There is no reason to use different handlers, they all act the same. Just use the passed-in event to identify which label is being interacted with and modify the object passed into that event.

Your handler doesn't even have to know that multiple labels exist.

When they click on it you can either remove it as you suggest or identify the "clicked" state at the beginning of your handler and just return (Perhaps by looking at the color).

Don't worry about the performance, the difference will be absolutely impossible to detect, do whatever seems more readable/maintainable to you.

Personally I think I'd leave them registered in case I want to add more behavior later.

Bill K
That is exactly how I implemented it at the moment, and I see from your comment that is not worth unregistering the handler. One more question: would it make sense to make `MyHandler` a singleton? I need only one instance of it in my application, and if it's a singleton I save the time of creating new object when registering it has handler for a new Label. What do you think?
MarcoS
I'm answering my comment above: it's not really necessary to make `MyHandler` a singleton. I can have a single instance of `MyHandler` and reuse it for every registration.
MarcoS
Sorry I didn't get back to you on that, but yes you are correct--the advantage of using the passed in event is that you only need a single instance. Also remember that MyHandler is a real class, so IF NECESSARY it can be subclassed for some very flexible handling (of course then you may need more than one instance). At times I've even passed in an initializing variable to each instance (For instance, if you had a cash register with +1c, +5c, +10c, +25c, ... buttons, you could use a single new MoneyHandler(int cents) object with different instances for each button.
Bill K
+3  A: 

You can make do with just 1 handler per event and share them across all your widgets. Since the event source is passed into the event handlers, you can identify the actual widget that fired the event.

That said, what you are trying to do is easily achievable though CSS HOVER state, with no javascript and the associated memory leak risks, if any.

You just need to define 3 label CSS classes:

.label {
# define your general label style
}

.label-highlight {
    background-color: #< rgb for light green >;
    color: #< rgb for font color >;
}

.label-highlight:HOVER {
    background-color: #< rgb for dark green >;
    color: #< rgb for white font color >;
}

Setup the labels with style class "label".

On button click make a call to addStyleDependentName( "highlight" ) on the Labels which you wish to highlight, and let the CSS manage the hover states for you.

Ashwin Prabhu
Thank you for your comment. I'm already using CSS and `addStyleDependentName(...)` in this way in my code.
MarcoS
I mentioned CSS Hover states in my response, just in case your were using MouseIn/Out Handlers for highlighting. Use of Mouse handlers can be completely avoided by use of CSS Hover state, unless you have some additional business logic to perform on mouse in/out
Ashwin Prabhu