View Javadoc

1   /*
2    * TouchGraph LLC. Apache-Style Software License
3    *
4    *
5    * Copyright (c) 2001-2002 Alexander Shapiro. All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 1. Redistributions of source code must retain the above copyright
12   *    notice, this list of conditions and the following disclaimer. 
13   *
14   * 2. Redistributions in binary form must reproduce the above copyright
15   *    notice, this list of conditions and the following disclaimer in
16   *    the documentation and/or other materials provided with the
17   *    distribution.
18   *
19   * 3. The end-user documentation included with the redistribution,
20   *    if any, must include the following acknowledgment:  
21   *       "This product includes software developed by 
22   *        TouchGraph LLC (http://www.touchgraph.com/)."
23   *    Alternately, this acknowledgment may appear in the software itself,
24   *    if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The names "TouchGraph" or "TouchGraph LLC" must not be used to endorse 
27   *    or promote products derived from this software without prior written 
28   *    permission.  For written permission, please contact 
29   *    alex@touchgraph.com
30   *
31   * 5. Products derived from this software may not be called "TouchGraph",
32   *    nor may "TouchGraph" appear in their name, without prior written
33   *    permission of alex@touchgraph.com.
34   *
35   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38   * DISCLAIMED.  IN NO EVENT SHALL TOUCHGRAPH OR ITS CONTRIBUTORS BE 
39   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
40   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
41   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
42   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
43   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
44   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
45   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46   * ====================================================================
47   *
48   */
49  
50  package com.touchgraph.graphlayout.interaction;
51  
52  import java.awt.Point;
53  import java.awt.event.MouseEvent;
54  
55  import com.touchgraph.graphlayout.*;
56  
57  /*** DragNodeUI contains code for dragging nodes.
58    *
59    * <p><b>
60    * Parts of this code build upon Sun's Graph Layout example.
61    * http://java.sun.com/applets/jdk/1.1/demo/GraphLayout/Graph.java
62    * </b></p>
63    *
64    * @author   Alexander Shapiro
65    * @version  1.21  $Id: DragNodeUI.java,v 1.1.1.1 2004/02/06 08:44:07 keesj Exp $
66    */
67  public class DragNodeUI extends TGAbstractDragUI{
68  
69     /*** Stores the distance between the cursor and the center of the node
70       * when dragging occurs so that the cursor remains at the same position
71       * on the node otherwise, the cursor jumps to the center of the node.
72       */
73      public Point dragOffs;
74  
75    // ............
76  
77      /*** Constructor with TGPanel <tt>tgp</tt>.
78        */
79      public DragNodeUI( TGPanel tgp ) {
80          super(tgp);
81      }
82  
83      public void preActivate() {
84          if (dragOffs ==null) dragOffs=new Point(0,0);
85      }
86  
87      public void preDeactivate() {};
88  
89      public void mousePressed( MouseEvent e ) {
90          Node mouseOverN = tgPanel.getMouseOverN();
91          Point mousePos;
92  
93          if (e!=null) mousePos = e.getPoint(); //e can be null if the wrong activate() method was used
94          else mousePos= new Point((int) mouseOverN.drawx,(int) mouseOverN.drawy);
95  
96          if ( mouseOverN != null) { //Should never be null if TGUIManager works properly
97              tgPanel.setDragNode(mouseOverN);
98  
99              dragOffs.setLocation((int) (mouseOverN.drawx-mousePos.x), //For when you click to the side of
100                     (int)(mouseOverN.drawy-mousePos.y));//the node, but you still want to drag it
101         }
102     }
103 
104     public void mouseReleased( MouseEvent e ) {
105         tgPanel.setDragNode(null);
106         tgPanel.repaintAfterMove();
107         tgPanel.startDamper();
108     }
109 
110 
111     public void mouseDragged( MouseEvent e ) {
112         Node dragNode = tgPanel.getDragNode();
113         dragNode.drawx = e.getX()+dragOffs.x;
114         dragNode.drawy = e.getY()+dragOffs.y;
115         tgPanel.updatePosFromDraw(dragNode);
116         tgPanel.repaintAfterMove();
117         tgPanel.stopDamper(); //Keep the graph alive while dragging.
118     }
119 
120 } // end com.touchgraph.graphlayout.interaction.DragNodeUI