HierarchicalGraphLayout.java revision 1472:c18cbe5936b8
1/*
2 * Copyright (c) 2008, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24package com.sun.hotspot.igv.controlflow;
25
26import com.sun.hotspot.igv.hierarchicallayout.HierarchicalLayoutManager;
27import com.sun.hotspot.igv.layout.Cluster;
28import com.sun.hotspot.igv.layout.LayoutGraph;
29import com.sun.hotspot.igv.layout.Link;
30import com.sun.hotspot.igv.layout.Port;
31import com.sun.hotspot.igv.layout.Vertex;
32import java.awt.Dimension;
33import java.awt.Point;
34import java.util.ArrayList;
35import java.util.Collection;
36import java.util.HashMap;
37import java.util.HashSet;
38import java.util.List;
39import java.util.Map;
40import java.util.Set;
41import org.netbeans.api.visual.graph.layout.GraphLayout;
42import org.netbeans.api.visual.graph.layout.UniversalGraph;
43import org.netbeans.api.visual.widget.Widget;
44
45/**
46 *
47 * @author Thomas Wuerthinger
48 */
49public class HierarchicalGraphLayout<N, E> extends GraphLayout<N, E> {
50
51    public HierarchicalGraphLayout() {
52    }
53
54    private class LinkWrapper implements Link {
55
56        private VertexWrapper from;
57        private VertexWrapper to;
58
59        public LinkWrapper(VertexWrapper from, VertexWrapper to) {
60            this.from = from;
61            this.to = to;
62        }
63
64        public Port getFrom() {
65            return from.getSlot();
66        }
67
68        public Port getTo() {
69            return to.getSlot();
70        }
71
72        public List<Point> getControlPoints() {
73            return new ArrayList<Point>();
74        }
75
76        public void setControlPoints(List<Point> list) {
77        // Do nothing for now
78        }
79    }
80
81    private class VertexWrapper implements Vertex {
82
83        private N node;
84        private UniversalGraph<N, E> graph;
85        private Port slot;
86        private Point position;
87
88        public VertexWrapper(N node, UniversalGraph<N, E> graph) {
89            this.node = node;
90            this.graph = graph;
91            final VertexWrapper vertex = this;
92            this.slot = new Port() {
93
94                public Vertex getVertex() {
95                    return vertex;
96                }
97
98                public Point getRelativePosition() {
99                    return new Point((int) (vertex.getSize().getWidth() / 2), (int) (vertex.getSize().getHeight() / 2));
100                }
101            };
102
103            Widget w = graph.getScene().findWidget(node);
104            this.position = w.getPreferredLocation();
105        }
106
107        public Cluster getCluster() {
108            return null;
109        }
110
111        public Dimension getSize() {
112            Widget w = graph.getScene().findWidget(node);
113            return w.getBounds().getSize();
114        }
115
116        public Point getPosition() {
117            return position;
118        }
119
120        public void setPosition(Point p) {
121            HierarchicalGraphLayout.this.setResolvedNodeLocation(graph, node, p);
122            position = p;
123        }
124
125        public boolean isRoot() {
126            return false;
127        }
128
129        public int compareTo(Vertex o) {
130            VertexWrapper vw = (VertexWrapper) o;
131            return node.toString().compareTo(vw.node.toString());
132        }
133
134        public Port getSlot() {
135            return slot;
136        }
137    }
138
139    protected void performGraphLayout(UniversalGraph<N, E> graph) {
140
141        Set<LinkWrapper> links = new HashSet<LinkWrapper>();
142        Set<VertexWrapper> vertices = new HashSet<VertexWrapper>();
143        Map<N, VertexWrapper> vertexMap = new HashMap<N, VertexWrapper>();
144
145        for (N node : graph.getNodes()) {
146            VertexWrapper v = new VertexWrapper(node, graph);
147            vertexMap.put(node, v);
148            vertices.add(v);
149        }
150
151        for (E edge : graph.getEdges()) {
152            N source = graph.getEdgeSource(edge);
153            N target = graph.getEdgeTarget(edge);
154            LinkWrapper l = new LinkWrapper(vertexMap.get(source), vertexMap.get(target));
155            links.add(l);
156        }
157
158        HierarchicalLayoutManager m = new HierarchicalLayoutManager(HierarchicalLayoutManager.Combine.NONE);
159
160        LayoutGraph layoutGraph = new LayoutGraph(links, vertices);
161        m.doLayout(layoutGraph);
162    }
163
164    protected void performNodesLayout(UniversalGraph<N, E> graph, Collection<N> nodes) {
165        throw new UnsupportedOperationException();
166    }
167}
168