Santy, Michael wrote:
> The Gadget approach is *exactly* what I'm needing.
> 
> I was able to implement a subclass of TextLabelVP, return it from a
> implementation of GadgetFactory, and setup a CSS rule to include it in
> the editor.  It behaved visually like I would expect, however I could
> not get it to respond to any events.  I implemented the grabsMouseButton
> and handleMouseEvent as the documentation specified, but neither of
> these methods were ever being called.  I even overrode the focusGained
> and focusLost method to see if this gadget was responding to any editor
> events, and neither of these methods are ever being called as well.
> 
> Below is a listing of the relevant code.  The print statement in the
> MyGadget constructor is being called and the gadget is included in the
> document.  When I double-click on the "My Gadget" label in the document,
> nothing happens.
> 
> Can anyone please shed some light on what I might be doing wrong?
> 
> 
> public class MyGadgetFactory implements GadgetFactory {
> 
>   public Gadget createGadget(StyledElementView view, int role, Style style,
>     StyleValue[] parameters, StyledViewFactory viewFactory) {
> 
>     return new MyGadget(style, "My Gadget", view, role);
> 
>   }
> }
> 
> public class MyGadget extends TextLabelVP {
> 
>   public MyGadget(Style style, String text,
>     StyledElementView view, int role) {
>     super(style, text, view, role);
> 
>     Node node = view.getModel();
> 
>     System.err.println("MyGadget::MyGadget() node: "+node);
>   }
> 
>   public boolean grabsMouseButton(MouseEvent e) {
>     System.err.println("My::grabsMouseButton()");
>     boolean rv = false;
>     if(MouseEvent.BUTTON1==e.getButton() && 2==e.getClickCount()) {
>       rv = true; //double left-click
>     }
>     return rv;
>   }
> 
>   public void handleMouseEvent(MouseEvent e) {
>     System.err.println("MyGadget::handleMouseEvent()");
>   }
> }
> 

This should work:

---
  public boolean grabsMouseButton(MouseEvent e) {
    System.err.println("My::grabsMouseButton()" + e);
    return (MouseEvent.BUTTON1==e.getButton());
  }

  public void handleMouseEvent(MouseEvent e {
   DocumentView docView = (DocumentView) getRoot();
   switch (e.getClickCount())
   case 1:
       // Not strictly needed but consistent with the other
       // StyledElementViewParts.
       docView.selectNode(node);
       break;
   case 2:
      System.err.println("MyGadget::handleMouseEvent()" + e);
      break;
   }
  }
---

Explanations:

When grabsMouseButton returns true for a mouse event about button #1,
this means: direct *all* subsequent mouse events (press, release, drag,
click) about button #1 to my handleMouseEvent methods.

When grabsMouseButton returns false for a mouse event about button #1,
then, may be the parent of the Gadget, will process all mouse events
about button #1, and if not, the grand-parent, and so on, until you
reach the root Gadget.



(The root Gadget is a com.xmlmind.xmledit.view.DocumentView (or a
com.xmlmind.xmledit.styledview.StyledDocumentView).

A com.xmlmind.xmledit.view.DocumentView manages mouse event by using
much more sophisticated means than grabsMouseButton/handleMouseEvent. It
uses com.xmlmind.xmledit.gadget.Bindings:
http://www.xmlmind.com/xmleditor/_distrib/doc/api/com/xmlmind/xmledit/gadget/Bindings.html

But this is unrelated to your problem.)



Reply via email to