View Javadoc

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.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 
102 	/**
103 	 * 
104 	 * @param e the event
105 	 */
106 	public void mousePressed(MouseEvent e) {
107 	    VisualizationViewer vv = (VisualizationViewer)e.getSource();
108 	    boolean accepted = checkModifiers(e);
109 	    down = e.getPoint();
110 	    if(accepted) {
111 	        vv.setCursor(cursor);
112 	    }
113 	}
114     
115 	/**
116 	 * 
117 	 */
118     public void mouseReleased(MouseEvent e) {
119         VisualizationViewer vv = (VisualizationViewer)e.getSource();
120         down = null;
121         vv.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
122     }
123     
124     /**
125 	 * 
126 	 * 
127 	 * 
128 	 * 
129 	 */
130     public void mouseDragged(MouseEvent e) {
131         if(down == null) return;
132         VisualizationViewer vv = (VisualizationViewer)e.getSource();
133         boolean accepted = checkModifiers(e);
134         if(accepted) {
135             MutableTransformer modelTransformer = 
136             	vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
137             vv.setCursor(cursor);
138             Point2D q = down;
139             Point2D p = e.getPoint();
140             float dx = (float) (p.getX()-q.getX());
141             float dy = (float) (p.getY()-q.getY());
142 
143             Dimension d = vv.getSize();
144             float shx = 2.f*dx/d.height;
145             float shy = 2.f*dy/d.width;
146             Point2D center = vv.getCenter();
147             if(p.getX() < center.getX()) {
148                 shy = -shy;
149             }
150             if(p.getY() < center.getY()) {
151                 shx = -shx;
152             }
153             modelTransformer.shear(shx, shy, center);
154             down.x = e.getX();
155             down.y = e.getY();
156         
157             e.consume();
158         }
159     }
160 
161     public void mouseClicked(MouseEvent e) {
162         // TODO Auto-generated method stub
163         
164     }
165 
166     public void mouseEntered(MouseEvent e) {
167         // TODO Auto-generated method stub
168         
169     }
170 
171     public void mouseExited(MouseEvent e) {
172         // TODO Auto-generated method stub
173         
174     }
175 
176     public void mouseMoved(MouseEvent e) {
177         // TODO Auto-generated method stub
178         
179     }
180 }