On Nov 26, 11:56 pm, Chris <[email protected]> wrote:
> Hi Guys
>
> I'm trying to build my own custom widget. How can i add event handling
> to the widget?
>
> Here's there idea...
>
> I have a Composite (my View), on which I add a Timeline Widget
> The Timeline widget has many bands TimeBand
> and each TimeBand has many TimeEvents.
>
> I've used the DOM.createDiv(), and then GwtExt to have full control
> over setting styles on elements. I need to do this (I think, but I'd
> love to proven wrong), cos each of my events (individual divs) needs
> to be carefully positioned on a scrollable div, and so on...
>
> Anyway, I've kind of got the layout working to a certain degree. What
> I'm not sure if how to get the Mouse event handlers firing and
> captured so that i can dynamically do stuff to my widget...
>
> so for example:
>
> class Timeline extends Widget {
>         Element t;
>
>         Timeline() {
>            t = DOM.createDiv();
>         }
>
>         public void addTimeBand(TimeBand band) {
>             t.append(band.getDiv());
>         }
>
> }
>
> class TimeBand {
>         ELement band;
>         TimeBand() {
>                 band = DOM.createDiv();
>         }
>
>         public Element getDiv() {
>                 return band;
>         }
>
> }
>
> I've tried a number of different things to get the event handlers
> working but I can't seem to add events to my TimeBand
> All I want to do is track mouse clicks and mouse movements.

In GWT, if you want to have your TimeBands to actually "receive"
events (without hacking things yourself), you have to make them extend
Widget.
In your case, it'd be much easier to take advantage of event bubbling
(a.k.a event delegation) and actually listen to events at the Timeline
level (either you call addDomHandler() with a handler instance, or you
sinkEvents() and override onBrowserEvent()). You'd then use
event.getNativeEvent().getEventTarget() (and cast it to an Element) to
know which DOM element was clicked, then either follow the
getParentElement() path until you find a TimeBand.getDiv(), or loop on
your TimeBand and use getDiv().isOrHasChild(eventTargetElement) to
know if the click occured within a particular TimeBand.

Well, when I say "it'd be much easier ...", that's unless I
misunderstood what you'd like to do. It's easier if you don't need to
attach event handlers on TimeBand instances but rather have Timeline
handle the event and do its things with the target TimeBand (just like
the Tree widget in GWT handles keyboard and click events to mark the
appropriate TreeItem as "selected" and/or expand/collapse it).

(I find com.google.gwt.dom.client.*-based code much easier to read
than com.google.gwt.user.client.DOM.*-based one; e.g. use
myElement.getParentElement().getParentElement() instead of
DOM.getParent(DOM.getParent(myElement)))

--

You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.


Reply via email to