1 /*------------------------------------------------------------------------------
  2 Name:      Interactor.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   The class which takes care of the user events (mouse events)
  6 Version:   $Id: Interactor.java 16476 2007-09-06 22:36:52Z laghi $
  7 ------------------------------------------------------------------------------*/
  8 package javaclients.svg.batik;
  9 
 10 import java.util.logging.Logger;
 11 import java.util.logging.Level;
 12 import org.xmlBlaster.util.Global;
 13 import org.xmlBlaster.util.XmlBlasterException;
 14 
 15 import org.apache.batik.swing.JSVGCanvas;
 16 import org.apache.batik.gvt.GraphicsNode;
 17 import org.apache.batik.bridge.BridgeContext;
 18 import org.apache.batik.swing.gvt.InteractorAdapter;
 19 
 20 import java.awt.event.MouseEvent;
 21 import java.awt.event.InputEvent;
 22 import java.awt.geom.Point2D;
 23 import java.awt.geom.Rectangle2D;
 24 import java.awt.Point;
 25 
 26 import java.util.List;
 27 
 28 
 29 /**
 30  * We need to create an extention of JSVGCanvas in order to be able to
 31  * retrieve the protected member bridgeContext. Lets call this class
 32  * com.eclettic.svg.JSVGCanvasExtended
 33  * @author $Author: laghi $ (michele@laghi.eu)
 34  */
 35 public class Interactor extends InteractorAdapter
 36 {
 37    /**
 38     * The JSVGCanvas on which to work on.
 39     */
 40    private final String ME                   = "Interactor";
 41    private static Logger log = Logger.getLogger(Interactor.class.getName());
 42    private JSVGCanvas   canvas               = null;
 43    private GraphicsNode graphicsNode         = null;
 44    // the mouse position where the last mousePressed event occured
 45    private Point        lastMousePosition    = null;
 46    // the graphicsNode on which the mouse hit when mousePressed occured
 47    private GraphicsNode selectedGraphicsNode = null;
 48    private BridgeContext bridgeContext       = null;
 49    private Transceiver   transceiver         = null;
 50 
 51    public Interactor ()
 52    {
 53       super();
 54 
 55       log.fine("constructor called");
 56    }
 57 
 58 
 59    public void setTransceiver (Transceiver transceiver)
 60    {
 61       this.transceiver = transceiver;
 62    }
 63 
 64 
 65    public void setBridgeContext (BridgeContext bridgeContext)
 66    {
 67       this.bridgeContext = bridgeContext;
 68    }
 69 
 70 
 71    public BridgeContext getBridgeContext ()
 72    {
 73       return this.bridgeContext;
 74    }
 75 
 76 
 77    /**
 78     * setter method for the canvas. At the same time it adds itself to the
 79     * interactor list of the related canvas object (if it has not been set
 80     * previously). When doing this, it also creates (and retrieves) a new
 81     * BridgeContext for that canvas. This means that you should call this
 82     * method BEFORE you load any document
 83     */
 84    public void setCanvas (JSVGCanvas canvas)
 85    {
 86       log.fine(".setCanvas start");
 87       boolean setInteractorList = false;
 88       System.out.println(ME + ".setCanvas " + canvas + " start");
 89       // if the same canvas is set for the second time, the interactor list
 90       // is unchanged.
 91       if ((canvas != this.canvas) && (canvas != null)) {
 92          log.info(".setCanvas interactor list set");
 93          List interactorList = canvas.getInteractors();
 94          interactorList.add(this);
 95       }
 96       this.canvas   = canvas;
 97       log.fine(".setCanvas end");
 98    }
 99 
100 
101    /**
102     * The reason why this method is not called together when setting the canvas
103     * is that the graphics node is different for each new DOM loaded, while the
104     * canvas is always the same even if a second DOM has been loaded. If you
105     * invoke this method before a DOM has been completely loaded, it is set to
106     * null.
107     */
108    public void setGraphicsNode () throws XmlBlasterException
109    {
110       log.fine(".setGraphicsNode start");
111       if (this.canvas == null) {
112          log.severe(".setGraphicsNode canvas is null");
113          throw new XmlBlasterException(ME, ".setGraphicsNode canvas is null");
114       }
115       this.graphicsNode = this.canvas.getGraphicsNode();
116       if (this.graphicsNode == null)
117          log.warning(".setGraphicsNode: the graphics node is null");
118       log.fine(".setGraphicsNode end");
119    }
120 
121 
122    public boolean startInteraction (InputEvent ie)
123    {
124       log.fine(".startInteraction called");
125       // don't really know what to return here ...
126       return true;
127    }
128 
129 
130    public JSVGCanvas getCanvas ()
131    {
132       return canvas;
133    }
134 
135 
136    public void mousePressed (MouseEvent evt)
137    {
138       log.fine(".mousePressed");
139       // this.graphicsNode.getGlobalTransform().deltaTransform(p0, p1);
140       this.lastMousePosition = evt.getPoint();
141       Rectangle2D rect = graphicsNode.getBounds();
142 //      double width  = rect.getX() * point.getX() / canvas.getWidth();
143 //      double height = rect.getY() * point.getY() / canvas.getHeight();
144       Point2D point2D = new Point2D.Double();
145 //      point2D.setLocation(width, height);
146       point2D.setLocation(this.lastMousePosition.getX(),
147                           this.lastMousePosition.getY());
148       this.selectedGraphicsNode = this.graphicsNode.nodeHitAt(point2D);
149       if (this.selectedGraphicsNode == null) {
150          log.warning(".mousePressed hit a null object");
151       }
152       else {
153          log.info(".mousePressed hit " + this.selectedGraphicsNode.toString());
154       }
155    }
156 
157 
158    public void mouseDragged (MouseEvent evt)
159    {
160 //      System.out.println(ME + ".mouseDragged");
161    }
162 
163 
164    public void mouseEntered (MouseEvent evt)
165    {
166 //      System.out.println(ME + ".mouseEntered");
167    }
168 
169 
170    public void mouseExited (MouseEvent evt)
171    {
172 //      System.out.println(ME + ".mouseExited");
173    }
174 
175 
176    public void mouseMoved (MouseEvent evt)
177    {
178 //      System.out.println(ME + ".mouseMoved");
179    }
180 
181 
182    public void mouseClicked (MouseEvent evt)
183    {
184 //      System.out.println(ME + ".mouseClicked");
185    }
186 
187 
188    public void mouseReleased (MouseEvent evt)
189    {
190       log.info(".mouseReleased");
191 
192       // here you can perform the tasks which are specific to this implementation
193       /* In this case translate the current object */
194 
195       // calculate the translation coordinates ..
196       if (this.lastMousePosition == null) {
197          log.severe("the last mouse position was null");
198       }
199       else {
200          if (this.selectedGraphicsNode != null) {
201             Point p = new Point();
202             p.x = evt.getX() - this.lastMousePosition.x;
203             p.y = evt.getY() - this.lastMousePosition.y;
204             this.transceiver.move(this.selectedGraphicsNode, p);
205          }
206          else log.warning(".mouseReleased: selectedGraphicsNode was null");
207       }
208       // reset the members
209       this.selectedGraphicsNode = null;
210       this.lastMousePosition    = null;
211    }
212 
213 }


syntax highlighted by Code2HTML, v. 0.9.1