Trouble getting custom widget to move

608 views
Skip to first unread message

rBehrens

unread,
Mar 6, 2007, 8:52:46 PM3/6/07
to gwt-dnd
I've created my own widget...an absolute panel that contains an image
and a label placed over the image. The thought is that the user can
define the label and place as many of these widgets on the screen as
he desires according to a grid layout.

My widget extends Composite and I have implemented
SourcesMouseEvents. In the class where I define the panel that will
contain the widgets, I've added the DragController and a
GridConstrainedDropController. From what I can see based on examples,
I've done everything correctly.

However, when I add a widget, I cannot move it. I'm not sure what I'm
doing wrong. I did test it with a simple Label and it works so I
think there may be an issue with the fact that I'm trying to move a
custom widget.

public void addRuleGraphics(GraphicImage image)
{
dragController.makeDraggable(image);
dropController.drop(image, 0, 0);
}

public class GraphicImage extends Composite implements
SourcesMouseEvents
{
private Image image;
private Label label;
private AbsolutePanel imagePanel = new AbsolutePanel();
private MouseListenerCollection mouseListenerCollection;

public GraphicImage(String url, String text)
{
image = new Image(url);

label = new Label(text);
label.setStyleName("gui-GraphicLabel");

label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
label.setPixelSize(70, 70);

imagePanel.setPixelSize(100, 100);
imagePanel.add(image);
imagePanel.add(label, 0, 0);

initWidget(imagePanel);
}

public void addMouseListener(MouseListener listener)
{
if (mouseListenerCollection == null)
{
mouseListenerCollection = new MouseListenerCollection();
}

mouseListenerCollection.add(listener);
}

public void removeMouseListener(MouseListener listener)
{
if (mouseListenerCollection != null)
{
mouseListenerCollection.remove(listener);
}
}
}

Fred Sauer

unread,
Mar 6, 2007, 10:24:20 PM3/6/07
to gwt...@googlegroups.com
rBehrens,

You probably just need to implement onBrowserEvent() to get things working. I think the following covers the pieces you need.

Let me know if you still have trouble.

Fred



public class MyClass implements SourcesMouseEvents {

  private MouseListenerCollection mouseListeners;

  public MyClass() {
    sinkEvents( Event.MOUSEEVENTS);
  }

  public void addMouseListener(MouseListener listener) {
    if (mouseListeners == null) {
      mouseListeners = new MouseListenerCollection();
    }
    mouseListeners.add(listener);
  }

  public void onBrowserEvent(Event event) {
    switch (DOM.eventGetType (event)) {
      case Event.ONMOUSEDOWN:
      case Event.ONMOUSEUP:
      case Event.ONMOUSEMOVE:
      case Event.ONMOUSEOVER:
      case Event.ONMOUSEOUT:
        if (mouseListeners != null) {
          mouseListeners.fireMouseEvent(this, event);
        }
        break;
    }
  }

  public void removeMouseListener(MouseListener listener) {
    if (mouseListeners != null) {
      mouseListeners.remove(listener);
--
Fred Sauer
fr...@allen-sauer.com

rBehrens

unread,
Mar 7, 2007, 7:29:07 PM3/7/07
to gwt-dnd
Apparently, after investigating Composite in the GWT group, it looks
like other people had issues with getting it to pick up events. So I
changed my class to extend AbsolutePanel instead and modified it
accordingly. Combined with your suggestion of implementing
onBrowserEvent(), it works now. :) Thank you!

> f...@allen-sauer.com

Fred Sauer

unread,
Mar 9, 2007, 5:35:41 PM3/9/07
to gwt...@googlegroups.com
rBehrens,

Using Composite is indeed very tricky. You'll using other panels easier. If you must use Composite in its current incarnation, you might glance at this thread:

http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/11642d517d15a07b/4fd5063d8fdb13b9

Fred

>           mouseListeners.fireMouseEvent (this, event);
> >       label.setStyleName ("gui-GraphicLabel");
>
> > label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
> >       label.setPixelSize(70, 70);
>
> >       imagePanel.setPixelSize(100, 100);
> >       imagePanel.add (image);
> >       imagePanel.add(label, 0, 0);
>
> >       initWidget(imagePanel);
> >    }
>
> >    public void addMouseListener(MouseListener listener)
> >    {
> >       if (mouseListenerCollection == null)
> >       {
> >          mouseListenerCollection = new MouseListenerCollection();
> >       }
>
> >       mouseListenerCollection.add (listener);
> >    }
>
> >    public void removeMouseListener(MouseListener listener)
> >    {
> >       if (mouseListenerCollection != null)
> >       {
> >           mouseListenerCollection.remove(listener);
> >       }
> >    }
> > }
>
> --
> Fred Sauer
> f...@allen-sauer.com


boscomonkey

unread,
Mar 9, 2007, 6:57:38 PM3/9/07
to gwt-dnd
Nub Games' tutorial on GWT drag & drop goes into a bit of depth about
source mouse clicks...

http://blogs.nubgames.com/code/?p=9

And then near the end (in the "Wait! It gets even easier" section),
says to wrap your widget up in a FocusPanel and you're done.

-- bosco


On Mar 9, 2:35 pm, "Fred Sauer" <f...@allen-sauer.com> wrote:
> rBehrens,
>

> Using Composite is indeed very tricky. You'll using other panels easier. If
> you must use Composite in its current incarnation, you might glance at this
> thread:
>

> http://groups.google.com/group/Google-Web-Toolkit/browse_thread/threa...
>
> Fred

> > > > label.setStyleName("gui-GraphicLabel");


>
> > > > label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
> > > > label.setPixelSize(70, 70);
>
> > > > imagePanel.setPixelSize(100, 100);
> > > > imagePanel.add (image);
> > > > imagePanel.add(label, 0, 0);
>
> > > > initWidget(imagePanel);
> > > > }
>
> > > > public void addMouseListener(MouseListener listener)
> > > > {
> > > > if (mouseListenerCollection == null)
> > > > {
> > > > mouseListenerCollection = new MouseListenerCollection();
> > > > }
>
> > > > mouseListenerCollection.add (listener);
> > > > }
>
> > > > public void removeMouseListener(MouseListener listener)
> > > > {
> > > > if (mouseListenerCollection != null)
> > > > {
> > > > mouseListenerCollection.remove(listener);
> > > > }
> > > > }
> > > > }
>
> > > --
> > > Fred Sauer
> > > f...@allen-sauer.com
>

> --
> Fred Sauer
> f...@allen-sauer.com- Hide quoted text -
>
> - Show quoted text -

Reply all
Reply to author
Forward
0 new messages