View Javadoc
1   /*
2    * Copyright (c) 2005, The JUNG Authors 
3    *
4    * All rights reserved.
5    *
6    * This software is open-source under the BSD license; see either
7    * "license.txt" or
8    * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
9    * Created on Mar 8, 2005
10   *
11   */
12  package edu.uci.ics.jung.visualization.control;
13  
14  import java.awt.BasicStroke;
15  import java.awt.Color;
16  import java.awt.Cursor;
17  import java.awt.Dimension;
18  import java.awt.Graphics;
19  import java.awt.Graphics2D;
20  import java.awt.Point;
21  import java.awt.RenderingHints;
22  import java.awt.Toolkit;
23  import java.awt.event.MouseEvent;
24  import java.awt.event.MouseListener;
25  import java.awt.event.MouseMotionListener;
26  import java.awt.geom.Point2D;
27  import java.awt.image.BufferedImage;
28  import java.util.Collections;
29  
30  import edu.uci.ics.jung.visualization.Layer;
31  import edu.uci.ics.jung.visualization.VisualizationViewer;
32  import edu.uci.ics.jung.visualization.transform.MutableTransformer;
33  
34  /** 
35   * ShearingGraphMousePlugin allows the user to drag with the mouse
36   * to shear the transform either in the horizontal or vertical direction.
37   * By default, the control or meta key must be depressed to activate
38   * shearing. 
39   * 
40   * 
41   * @author Tom Nelson
42   */
43  public class ShearingGraphMousePlugin extends AbstractGraphMousePlugin
44      implements MouseListener, MouseMotionListener {
45  
46      private static int mask = MouseEvent.CTRL_MASK;
47      
48      static {
49          if(System.getProperty("os.name").startsWith("Mac")) {
50              mask = MouseEvent.META_MASK;
51          }
52      }
53  	/**
54  	 * create an instance with default modifier values
55  	 */
56  	public ShearingGraphMousePlugin() {
57  	    this(MouseEvent.BUTTON1_MASK | mask);
58  	}
59  
60  	/**
61  	 * create an instance with passed modifier values
62  	 * @param modifiers the mouse modifiers to use
63  	 */
64  	public ShearingGraphMousePlugin(int modifiers) {
65  	    super(modifiers);
66  	    Dimension cd = Toolkit.getDefaultToolkit().getBestCursorSize(16,16);
67          BufferedImage cursorImage = 
68          		new BufferedImage(cd.width,cd.height,BufferedImage.TYPE_INT_ARGB);
69          Graphics g = cursorImage.createGraphics();
70          Graphics2D g2 = (Graphics2D)g;
71          g2.addRenderingHints(Collections.singletonMap(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
72          g.setColor(new Color(0,0,0,0));
73          g.fillRect(0,0,16,16);
74          
75          int left = 0;
76          int top = 0;
77          int right = 15;
78          int bottom = 15;
79          
80          g.setColor(Color.white);
81          g2.setStroke(new BasicStroke(3));
82          g.drawLine(left+2,top+5,right-2,top+5);
83          g.drawLine(left+2,bottom-5,right-2,bottom-5);
84          g.drawLine(left+2,top+5,left+4,top+3);
85          g.drawLine(left+2,top+5,left+4,top+7);
86          g.drawLine(right-2,bottom-5,right-4,bottom-3);
87          g.drawLine(right-2,bottom-5,right-4,bottom-7);
88  
89          g.setColor(Color.black);
90          g2.setStroke(new BasicStroke(1));
91          g.drawLine(left+2,top+5,right-2,top+5);
92          g.drawLine(left+2,bottom-5,right-2,bottom-5);
93          g.drawLine(left+2,top+5,left+4,top+3);
94          g.drawLine(left+2,top+5,left+4,top+7);
95          g.drawLine(right-2,bottom-5,right-4,bottom-3);
96          g.drawLine(right-2,bottom-5,right-4,bottom-7);
97          g.dispose();
98          cursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(), "RotateCursor");
99  	}
100 
101 	public void mousePressed(MouseEvent e) {
102 	    VisualizationViewer<?, ?> vv = (VisualizationViewer<?, ?>)e.getSource();
103 	    boolean accepted = checkModifiers(e);
104 	    down = e.getPoint();
105 	    if(accepted) {
106 	        vv.setCursor(cursor);
107 	    }
108 	}
109     
110     public void mouseReleased(MouseEvent e) {
111         VisualizationViewer<?, ?> vv = (VisualizationViewer<?, ?>)e.getSource();
112         down = null;
113         vv.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
114     }
115     
116     public void mouseDragged(MouseEvent e) {
117         if(down == null) return;
118         VisualizationViewer<?, ?> vv = (VisualizationViewer<?, ?>)e.getSource();
119         boolean accepted = checkModifiers(e);
120         if(accepted) {
121             MutableTransformer modelTransformer = 
122             	vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
123             vv.setCursor(cursor);
124             Point2D q = down;
125             Point2D p = e.getPoint();
126             float dx = (float) (p.getX()-q.getX());
127             float dy = (float) (p.getY()-q.getY());
128 
129             Dimension d = vv.getSize();
130             float shx = 2.f*dx/d.height;
131             float shy = 2.f*dy/d.width;
132             Point2D center = vv.getCenter();
133             if(p.getX() < center.getX()) {
134                 shy = -shy;
135             }
136             if(p.getY() < center.getY()) {
137                 shx = -shx;
138             }
139             modelTransformer.shear(shx, shy, center);
140             down.x = e.getX();
141             down.y = e.getY();
142         
143             e.consume();
144         }
145     }
146 
147     public void mouseClicked(MouseEvent e) {
148         // TODO Auto-generated method stub
149         
150     }
151 
152     public void mouseEntered(MouseEvent e) {
153         // TODO Auto-generated method stub
154         
155     }
156 
157     public void mouseExited(MouseEvent e) {
158         // TODO Auto-generated method stub
159         
160     }
161 
162     public void mouseMoved(MouseEvent e) {
163         // TODO Auto-generated method stub
164         
165     }
166 }