Line | Hits | Source |
---|---|---|
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.VisualizationViewer; | |
31 | import edu.uci.ics.jung.visualization.transform.MutableTransformer; | |
32 | ||
33 | /** | |
34 | * ShearingGraphMousePlugin allows the user to drag with the mouse | |
35 | * to shear the transform either in the horizontal or vertical direction. | |
36 | * By default, the control or meta key must be depressed to activate | |
37 | * shearing. | |
38 | * | |
39 | * | |
40 | * @author Tom Nelson | |
41 | */ | |
42 | public class ShearingGraphMousePlugin extends AbstractGraphMousePlugin | |
43 | implements MouseListener, MouseMotionListener { | |
44 | ||
45 | 0 | private static int mask = MouseEvent.CTRL_MASK; |
46 | ||
47 | static { | |
48 | 0 | if(System.getProperty("os.name").startsWith("Mac")) { |
49 | 0 | mask = MouseEvent.META_MASK; |
50 | } | |
51 | 0 | } |
52 | /** | |
53 | * create an instance with default modifier values | |
54 | */ | |
55 | public ShearingGraphMousePlugin() { | |
56 | 0 | this(MouseEvent.BUTTON1_MASK | mask); |
57 | 0 | } |
58 | ||
59 | /** | |
60 | * create an instance with passed modifier values | |
61 | * @param modifiers the mouse modifiers to use | |
62 | */ | |
63 | public ShearingGraphMousePlugin(int modifiers) { | |
64 | 0 | super(modifiers); |
65 | 0 | Dimension cd = Toolkit.getDefaultToolkit().getBestCursorSize(16,16); |
66 | 0 | BufferedImage cursorImage = |
67 | new BufferedImage(cd.width,cd.height,BufferedImage.TYPE_INT_ARGB); | |
68 | 0 | Graphics g = cursorImage.createGraphics(); |
69 | 0 | Graphics2D g2 = (Graphics2D)g; |
70 | 0 | g2.addRenderingHints(Collections.singletonMap(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)); |
71 | 0 | g.setColor(new Color(0,0,0,0)); |
72 | 0 | g.fillRect(0,0,16,16); |
73 | ||
74 | 0 | int left = 0; |
75 | 0 | int top = 0; |
76 | 0 | int right = 15; |
77 | 0 | int bottom = 15; |
78 | ||
79 | // g.setColor(Color.white); | |
80 | // g2.setStroke(new BasicStroke(3)); | |
81 | // g.drawLine(left+2,top+5,right-2,top+5); | |
82 | // g.drawLine(left+2,bottom-5,right-2,bottom-5); | |
83 | // g.drawLine(left+2,top+5,left+4,top+3); | |
84 | // g.drawLine(left+2,top+5,left+4,top+7); | |
85 | // g.drawLine(right-2,bottom-5,right-4,bottom-3); | |
86 | // g.drawLine(right-2,bottom-5,right-4,bottom-7); | |
87 | ||
88 | 0 | g.setColor(Color.black); |
89 | 0 | g2.setStroke(new BasicStroke(1)); |
90 | 0 | g.drawLine(left+2,top+5,right-2,top+5); |
91 | 0 | g.drawLine(left+2,bottom-5,right-2,bottom-5); |
92 | 0 | g.drawLine(left+2,top+5,left+4,top+3); |
93 | 0 | g.drawLine(left+2,top+5,left+4,top+7); |
94 | 0 | g.drawLine(right-2,bottom-5,right-4,bottom-3); |
95 | 0 | g.drawLine(right-2,bottom-5,right-4,bottom-7); |
96 | 0 | g.dispose(); |
97 | 0 | cursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(), "RotateCursor"); |
98 | ||
99 | 0 | } |
100 | ||
101 | /** | |
102 | * | |
103 | * @param e the event | |
104 | */ | |
105 | public void mousePressed(MouseEvent e) { | |
106 | 0 | VisualizationViewer vv = (VisualizationViewer)e.getSource(); |
107 | 0 | boolean accepted = checkModifiers(e); |
108 | 0 | down = e.getPoint(); |
109 | 0 | if(accepted) { |
110 | 0 | vv.setCursor(cursor); |
111 | } | |
112 | 0 | } |
113 | ||
114 | /** | |
115 | * | |
116 | */ | |
117 | public void mouseReleased(MouseEvent e) { | |
118 | 0 | VisualizationViewer vv = (VisualizationViewer)e.getSource(); |
119 | 0 | down = null; |
120 | 0 | vv.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
121 | 0 | } |
122 | ||
123 | /** | |
124 | * | |
125 | * | |
126 | * | |
127 | * | |
128 | */ | |
129 | public void mouseDragged(MouseEvent e) { | |
130 | 0 | if(down == null) return; |
131 | 0 | VisualizationViewer vv = (VisualizationViewer)e.getSource(); |
132 | 0 | boolean accepted = checkModifiers(e); |
133 | 0 | if(accepted) { |
134 | 0 | MutableTransformer modelTransformer = vv.getLayoutTransformer(); |
135 | 0 | vv.setCursor(cursor); |
136 | 0 | Point2D q = down; |
137 | 0 | Point2D p = e.getPoint(); |
138 | 0 | float dx = (float) (p.getX()-q.getX()); |
139 | 0 | float dy = (float) (p.getY()-q.getY()); |
140 | ||
141 | 0 | Dimension d = vv.getSize(); |
142 | 0 | float shx = 2.f*dx/d.height; |
143 | 0 | float shy = 2.f*dy/d.width; |
144 | 0 | Point2D center = vv.getCenter(); |
145 | 0 | if(p.getX() < center.getX()) { |
146 | 0 | shy = -shy; |
147 | } | |
148 | 0 | if(p.getY() < center.getY()) { |
149 | 0 | shx = -shx; |
150 | } | |
151 | 0 | modelTransformer.shear(shx, shy, center); |
152 | 0 | down.x = e.getX(); |
153 | 0 | down.y = e.getY(); |
154 | ||
155 | 0 | e.consume(); |
156 | } | |
157 | 0 | } |
158 | ||
159 | public void mouseClicked(MouseEvent e) { | |
160 | // TODO Auto-generated method stub | |
161 | ||
162 | 0 | } |
163 | ||
164 | public void mouseEntered(MouseEvent e) { | |
165 | // TODO Auto-generated method stub | |
166 | ||
167 | 0 | } |
168 | ||
169 | public void mouseExited(MouseEvent e) { | |
170 | // TODO Auto-generated method stub | |
171 | ||
172 | 0 | } |
173 | ||
174 | public void mouseMoved(MouseEvent e) { | |
175 | // TODO Auto-generated method stub | |
176 | ||
177 | 0 | } |
178 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |