Coverage details for edu.uci.ics.jung.visualization.control.LensTranslatingGraphMousePlugin

LineHitsSource
1 /*
2  * Copyright (c) 2005, the JUNG Project and the Regents of the University
3  * of California
4  * All rights reserved.
5  *
6  * This software is open-source under the BSD license; see either
7  * "license.txt" or
8  * http://jung.sourceforge.net/license.txt for a description.
9  * Created on Mar 8, 2005
10  *
11  */
12 package edu.uci.ics.jung.visualization.control;
13  
14 import java.awt.Cursor;
15 import java.awt.event.MouseEvent;
16 import java.awt.event.MouseListener;
17 import java.awt.event.MouseMotionListener;
18 import java.awt.geom.Point2D;
19  
20 import edu.uci.ics.jung.visualization.VisualizationViewer;
21 import edu.uci.ics.jung.visualization.transform.LensTransformer;
22 import edu.uci.ics.jung.visualization.transform.MutableTransformer;
23  
24 /**
25  * Extends TranslatingGraphMousePlugin and adds the capability
26  * to drag and resize the viewing
27  * lens in the graph view. Mouse1 in the center moves the lens,
28  * mouse1 on the edge resizes the lens. The default mouse button and
29  * modifiers can be overridden in the constructor.
30  *
31  *
32  * @author Tom Nelson
33  */
34 public class LensTranslatingGraphMousePlugin extends TranslatingGraphMousePlugin
35 implements MouseListener, MouseMotionListener {
36     
37     protected boolean dragOnLens;
38     protected boolean dragOnEdge;
39     protected double edgeOffset;
40     /**
41      * create an instance with default modifiers
42      */
43     public LensTranslatingGraphMousePlugin() {
440        this(MouseEvent.BUTTON1_MASK);
450    }
46     
47     /**
48      * create an instance with passed modifer value
49      * @param modifiers the mouse event modifier to activate this function
50      */
51     public LensTranslatingGraphMousePlugin(int modifiers) {
520        super(modifiers);
530    }
54     
55     /**
56      * Check the event modifiers. Set the 'down' point for later
57      * use. If this event satisfies the modifiers, change the cursor
58      * to the system 'move cursor'
59      * @param e the event
60      */
61     public void mousePressed(MouseEvent e) {
620        VisualizationViewer vv = (VisualizationViewer)e.getSource();
630        boolean accepted = checkModifiers(e);
640        if(accepted) {
650            vv.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
660            testViewCenter(vv.getLayoutTransformer(), e.getPoint());
670            testViewCenter(vv.getViewTransformer(), e.getPoint());
680            vv.repaint();
69         }
700        super.mousePressed(e);
710    }
72     
73     /**
74      * called to change the location of the lens
75      * @param transformer
76      * @param point
77      */
78     private void setViewCenter(MutableTransformer transformer, Point2D point) {
790        if(transformer instanceof LensTransformer) {
800            LensTransformer ht =
81                 (LensTransformer)transformer;
820            ht.setViewCenter(point);
83         }
840    }
85     
86     /**
87      * called to change the radius of the lens
88      * @param transformer
89      * @param point
90      */
91     private void setViewRadius(MutableTransformer transformer, Point2D point) {
920        if(transformer instanceof LensTransformer) {
930            LensTransformer ht =
94                 (LensTransformer)transformer;
950            double distanceFromCenter = ht.getDistanceFromCenter(point);
960            ht.setViewRadius(distanceFromCenter+edgeOffset);
97         }
980    }
99     
100     /**
101      * called to set up translating the lens center or changing the size
102      * @param transformer
103      * @param point
104      */
105     private void testViewCenter(MutableTransformer transformer, Point2D point) {
1060        if(transformer instanceof LensTransformer) {
1070            LensTransformer ht =
108                 (LensTransformer)transformer;
1090            double distanceFromCenter = ht.getDistanceFromCenter(point);
1100            if(distanceFromCenter < 10) {
1110                ht.setViewCenter(point);
1120                dragOnLens = true;
1130            } else if(Math.abs(distanceFromCenter - ht.getViewRadius()) < 10) {
1140                edgeOffset = ht.getViewRadius() - distanceFromCenter;
1150                ht.setViewRadius(distanceFromCenter+edgeOffset);
1160                dragOnEdge = true;
117             }
118         }
1190    }
120     
121     /**
122      * unset the 'down' point and change the cursoe back to the system
123      * default cursor
124      */
125     public void mouseReleased(MouseEvent e) {
1260        super.mouseReleased(e);
1270        dragOnLens = false;
1280        dragOnEdge = false;
1290        edgeOffset = 0;
1300    }
131     
132     /**
133      * check the modifiers. If accepted, move or resize the lens according
134      * to the dragging of the mouse pointer
135      * @param e the event
136      */
137     public void mouseDragged(MouseEvent e) {
1380        VisualizationViewer vv = (VisualizationViewer)e.getSource();
1390        boolean accepted = checkModifiers(e);
1400        if(accepted ) {
1410            MutableTransformer modelTransformer = vv.getLayoutTransformer();
1420            vv.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
143             
1440            if(dragOnLens) {
145                 
1460                setViewCenter(modelTransformer, vv.inverseViewTransform(e.getPoint()));
1470                setViewCenter(vv.getViewTransformer(), e.getPoint());
1480                e.consume();
1490                vv.repaint();
150  
1510            } else if(dragOnEdge) {
152                 
1530                setViewRadius(modelTransformer, e.getPoint());
1540                setViewRadius(vv.getViewTransformer(), e.getPoint());
1550                e.consume();
1560                vv.repaint();
157                 
158             } else {
1590                super.mouseDragged(e);
160             }
161         }
1620    }
163 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.