768
|
1 |
/*
|
|
2 |
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*
|
|
23 |
*/
|
|
24 |
package com.sun.hotspot.igv.controlflow;
|
|
25 |
|
|
26 |
import com.sun.hotspot.igv.data.InputBlock;
|
|
27 |
import com.sun.hotspot.igv.data.InputBlockEdge;
|
|
28 |
import com.sun.hotspot.igv.data.InputGraph;
|
|
29 |
import com.sun.hotspot.igv.data.services.InputGraphProvider;
|
|
30 |
import com.sun.hotspot.igv.data.InputNode;
|
|
31 |
import java.awt.Color;
|
|
32 |
import java.awt.Point;
|
|
33 |
import java.awt.Rectangle;
|
|
34 |
import java.util.ArrayList;
|
|
35 |
import java.util.HashSet;
|
|
36 |
import java.util.Hashtable;
|
|
37 |
import java.util.Set;
|
|
38 |
import javax.swing.BorderFactory;
|
|
39 |
import org.netbeans.api.visual.action.ActionFactory;
|
|
40 |
import org.netbeans.api.visual.action.MoveProvider;
|
|
41 |
import org.netbeans.api.visual.action.RectangularSelectDecorator;
|
|
42 |
import org.netbeans.api.visual.action.RectangularSelectProvider;
|
|
43 |
import org.netbeans.api.visual.action.SelectProvider;
|
|
44 |
import org.netbeans.api.visual.action.WidgetAction;
|
|
45 |
import org.netbeans.api.visual.anchor.AnchorFactory;
|
|
46 |
import org.netbeans.api.visual.anchor.AnchorShape;
|
|
47 |
import com.sun.hotspot.igv.controlflow.HierarchicalGraphLayout;
|
|
48 |
import org.netbeans.api.visual.layout.LayoutFactory;
|
|
49 |
import org.netbeans.api.visual.router.RouterFactory;
|
|
50 |
import org.netbeans.api.visual.widget.LayerWidget;
|
|
51 |
import org.netbeans.api.visual.widget.Widget;
|
|
52 |
import org.netbeans.api.visual.graph.GraphScene;
|
|
53 |
import org.netbeans.api.visual.graph.layout.GraphLayout;
|
|
54 |
import org.netbeans.api.visual.layout.SceneLayout;
|
|
55 |
import org.netbeans.api.visual.widget.ConnectionWidget;
|
|
56 |
import org.openide.util.Lookup;
|
|
57 |
|
|
58 |
/**
|
|
59 |
*
|
|
60 |
* @author Thomas Wuerthinger
|
|
61 |
*/
|
|
62 |
public class ControlFlowScene extends GraphScene<InputBlock, InputBlockEdge> implements SelectProvider, MoveProvider, RectangularSelectDecorator, RectangularSelectProvider {
|
|
63 |
|
|
64 |
private Set<BlockWidget> selection;
|
|
65 |
private Hashtable<InputBlock, BlockWidget> blockMap;
|
|
66 |
private InputGraph oldGraph;
|
|
67 |
private LayerWidget edgeLayer;
|
|
68 |
private LayerWidget mainLayer;
|
|
69 |
private LayerWidget selectLayer;
|
|
70 |
private WidgetAction hoverAction = this.createWidgetHoverAction();
|
|
71 |
private WidgetAction selectAction = ActionFactory.createSelectAction(this);
|
|
72 |
private WidgetAction moveAction = ActionFactory.createMoveAction(null, this);
|
|
73 |
|
|
74 |
public ControlFlowScene() {
|
|
75 |
selection = new HashSet<BlockWidget>();
|
|
76 |
|
|
77 |
this.getInputBindings().setZoomActionModifiers(0);
|
|
78 |
this.setLayout(LayoutFactory.createAbsoluteLayout());
|
|
79 |
|
|
80 |
mainLayer = new LayerWidget(this);
|
|
81 |
this.addChild(mainLayer);
|
|
82 |
|
|
83 |
edgeLayer = new LayerWidget(this);
|
|
84 |
this.addChild(edgeLayer);
|
|
85 |
|
|
86 |
selectLayer = new LayerWidget(this);
|
|
87 |
this.addChild(selectLayer);
|
|
88 |
|
|
89 |
this.getActions().addAction(hoverAction);
|
|
90 |
this.getActions().addAction(selectAction);
|
|
91 |
this.getActions().addAction(ActionFactory.createRectangularSelectAction(this, selectLayer, this));
|
|
92 |
this.getActions().addAction(ActionFactory.createMouseCenteredZoomAction(1.1));
|
|
93 |
}
|
|
94 |
|
|
95 |
public void setGraph(InputGraph g) {
|
|
96 |
if (g == oldGraph) {
|
|
97 |
return;
|
|
98 |
}
|
|
99 |
oldGraph = g;
|
|
100 |
|
|
101 |
ArrayList<InputBlock> blocks = new ArrayList<InputBlock>(this.getNodes());
|
|
102 |
for (InputBlock b : blocks) {
|
|
103 |
removeNode(b);
|
|
104 |
}
|
|
105 |
|
|
106 |
ArrayList<InputBlockEdge> edges = new ArrayList<InputBlockEdge>(this.getEdges());
|
|
107 |
for (InputBlockEdge e : edges) {
|
|
108 |
removeEdge(e);
|
|
109 |
}
|
|
110 |
|
|
111 |
for (InputBlock b : g.getBlocks()) {
|
|
112 |
addNode(b);
|
|
113 |
}
|
|
114 |
|
|
115 |
for (InputBlock b : g.getBlocks()) {
|
|
116 |
for (InputBlockEdge e : b.getOutputs()) {
|
|
117 |
addEdge(e);
|
|
118 |
assert g.getBlocks().contains(e.getFrom());
|
|
119 |
assert g.getBlocks().contains(e.getTo());
|
|
120 |
this.setEdgeSource(e, e.getFrom());
|
|
121 |
this.setEdgeTarget(e, e.getTo());
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
GraphLayout layout = new HierarchicalGraphLayout();//GridGraphLayout();
|
|
126 |
SceneLayout sceneLayout = LayoutFactory.createSceneGraphLayout(this, layout);
|
|
127 |
sceneLayout.invokeLayout();
|
|
128 |
|
|
129 |
this.validate();
|
|
130 |
}
|
|
131 |
|
|
132 |
public BlockWidget getBlockWidget(InputBlock b) {
|
|
133 |
return blockMap.get(b);
|
|
134 |
}
|
|
135 |
|
|
136 |
public void clearSelection() {
|
|
137 |
for (BlockWidget w : selection) {
|
|
138 |
w.setState(w.getState().deriveSelected(false));
|
|
139 |
}
|
|
140 |
selection.clear();
|
|
141 |
selectionChanged();
|
|
142 |
}
|
|
143 |
|
|
144 |
public void selectionChanged() {
|
|
145 |
InputGraphProvider p = Lookup.getDefault().lookup(InputGraphProvider.class);
|
|
146 |
if (p != null) {
|
|
147 |
Set<InputNode> inputNodes = new HashSet<InputNode>();
|
|
148 |
for (BlockWidget w : selection) {
|
|
149 |
inputNodes.addAll(w.getBlock().getNodes());
|
|
150 |
}
|
|
151 |
p.setSelectedNodes(inputNodes);
|
|
152 |
}
|
|
153 |
}
|
|
154 |
|
|
155 |
public void addToSelection(BlockWidget widget) {
|
|
156 |
widget.setState(widget.getState().deriveSelected(true));
|
|
157 |
selection.add(widget);
|
|
158 |
selectionChanged();
|
|
159 |
}
|
|
160 |
|
|
161 |
public void removeFromSelection(BlockWidget widget) {
|
|
162 |
widget.setState(widget.getState().deriveSelected(false));
|
|
163 |
selection.remove(widget);
|
|
164 |
selectionChanged();
|
|
165 |
}
|
|
166 |
|
|
167 |
public boolean isAimingAllowed(Widget widget, Point point, boolean b) {
|
|
168 |
return false;
|
|
169 |
}
|
|
170 |
|
|
171 |
public boolean isSelectionAllowed(Widget widget, Point point, boolean b) {
|
|
172 |
return true;
|
|
173 |
}
|
|
174 |
|
|
175 |
public void select(Widget widget, Point point, boolean change) {
|
|
176 |
if (widget == this) {
|
|
177 |
clearSelection();
|
|
178 |
} else {
|
|
179 |
|
|
180 |
assert widget instanceof BlockWidget;
|
|
181 |
BlockWidget bw = (BlockWidget) widget;
|
|
182 |
if (change) {
|
|
183 |
if (selection.contains(bw)) {
|
|
184 |
removeFromSelection(bw);
|
|
185 |
} else {
|
|
186 |
addToSelection(bw);
|
|
187 |
}
|
|
188 |
} else {
|
|
189 |
if (!selection.contains(bw)) {
|
|
190 |
clearSelection();
|
|
191 |
addToSelection(bw);
|
|
192 |
}
|
|
193 |
}
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
public void movementStarted(Widget widget) {
|
|
198 |
}
|
|
199 |
|
|
200 |
public void movementFinished(Widget widget) {
|
|
201 |
}
|
|
202 |
|
|
203 |
public Point getOriginalLocation(Widget widget) {
|
|
204 |
return widget.getPreferredLocation();
|
|
205 |
}
|
|
206 |
|
|
207 |
public void setNewLocation(Widget widget, Point location) {
|
|
208 |
Point originalLocation = getOriginalLocation(widget);
|
|
209 |
int xOffset = location.x - originalLocation.x;
|
|
210 |
int yOffset = location.y - originalLocation.y;
|
|
211 |
for (Widget w : this.selection) {
|
|
212 |
Point p = new Point(w.getPreferredLocation());
|
|
213 |
p.translate(xOffset, yOffset);
|
|
214 |
w.setPreferredLocation(p);
|
|
215 |
}
|
|
216 |
|
|
217 |
}
|
|
218 |
|
|
219 |
public Widget createSelectionWidget() {
|
|
220 |
Widget widget = new Widget(this);
|
|
221 |
widget.setOpaque(false);
|
|
222 |
widget.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
|
223 |
widget.setForeground(Color.red);
|
|
224 |
return widget;
|
|
225 |
}
|
|
226 |
|
|
227 |
public void performSelection(Rectangle rectangle) {
|
|
228 |
|
|
229 |
if (rectangle.width < 0) {
|
|
230 |
rectangle.x += rectangle.width;
|
|
231 |
rectangle.width *= -1;
|
|
232 |
}
|
|
233 |
|
|
234 |
if (rectangle.height < 0) {
|
|
235 |
rectangle.y += rectangle.height;
|
|
236 |
rectangle.height *= -1;
|
|
237 |
}
|
|
238 |
|
|
239 |
boolean changed = false;
|
|
240 |
for (InputBlock b : this.getNodes()) {
|
|
241 |
BlockWidget w = (BlockWidget) findWidget(b);
|
|
242 |
Rectangle r = new Rectangle(w.getBounds());
|
|
243 |
r.setLocation(w.getLocation());
|
|
244 |
if (r.intersects(rectangle)) {
|
|
245 |
if (!selection.contains(w)) {
|
|
246 |
changed = true;
|
|
247 |
selection.add(w);
|
|
248 |
w.setState(w.getState().deriveSelected(true));
|
|
249 |
}
|
|
250 |
} else {
|
|
251 |
if (selection.contains(w)) {
|
|
252 |
changed = true;
|
|
253 |
selection.remove(w);
|
|
254 |
w.setState(w.getState().deriveSelected(false));
|
|
255 |
}
|
|
256 |
}
|
|
257 |
}
|
|
258 |
|
|
259 |
if (changed) {
|
|
260 |
selectionChanged();
|
|
261 |
}
|
|
262 |
|
|
263 |
}
|
|
264 |
|
|
265 |
protected Widget attachNodeWidget(InputBlock node) {
|
|
266 |
BlockWidget w = new BlockWidget(this, node);
|
|
267 |
mainLayer.addChild(w);
|
|
268 |
w.getActions().addAction(hoverAction);
|
|
269 |
w.getActions().addAction(selectAction);
|
|
270 |
w.getActions().addAction(moveAction);
|
|
271 |
return w;
|
|
272 |
}
|
|
273 |
|
|
274 |
protected Widget attachEdgeWidget(InputBlockEdge edge) {
|
|
275 |
ConnectionWidget w = new BlockConnectionWidget(this, edge);
|
|
276 |
w.setRouter(RouterFactory.createDirectRouter());
|
|
277 |
w.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
|
|
278 |
edgeLayer.addChild(w);
|
|
279 |
return w;
|
|
280 |
}
|
|
281 |
|
|
282 |
protected void attachEdgeSourceAnchor(InputBlockEdge edge, InputBlock oldSourceNode, InputBlock sourceNode) {
|
|
283 |
Widget w = this.findWidget(edge);
|
|
284 |
assert w instanceof ConnectionWidget;
|
|
285 |
ConnectionWidget cw = (ConnectionWidget) w;
|
|
286 |
cw.setSourceAnchor(AnchorFactory.createRectangularAnchor(findWidget(sourceNode)));
|
|
287 |
|
|
288 |
}
|
|
289 |
|
|
290 |
protected void attachEdgeTargetAnchor(InputBlockEdge edge, InputBlock oldTargetNode, InputBlock targetNode) {
|
|
291 |
Widget w = this.findWidget(edge);
|
|
292 |
assert w instanceof ConnectionWidget;
|
|
293 |
ConnectionWidget cw = (ConnectionWidget) w;
|
|
294 |
cw.setTargetAnchor(AnchorFactory.createRectangularAnchor(findWidget(targetNode)));
|
|
295 |
}
|
|
296 |
}
|