Initial opendaylight infrastructure commit!!
[controller.git] / third-party / net.sf.jung2 / src / main / java / edu / uci / ics / jung / algorithms / layout / CircleLayout.java
1 /*
2  * Copyright (c) 2003, 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  */
10 /*
11  * Created on Dec 4, 2003
12  */
13 package edu.uci.ics.jung.algorithms.layout;
14
15 import java.awt.Dimension;
16 import java.awt.geom.Point2D;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.Comparator;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.commons.collections15.Factory;
25 import org.apache.commons.collections15.map.LazyMap;
26
27 import edu.uci.ics.jung.graph.Graph;
28
29
30
31 /**
32  * A {@code Layout} implementation that positions vertices equally spaced on a regular circle.
33  *
34  * @author Masanori Harada
35  */
36 public class CircleLayout<V, E> extends AbstractLayout<V,E> {
37
38         private double radius;
39         private List<V> vertex_ordered_list;
40         
41         Map<V, CircleVertexData> circleVertexDataMap =
42                         LazyMap.decorate(new HashMap<V,CircleVertexData>(), 
43                         new Factory<CircleVertexData>() {
44                                 public CircleVertexData create() {
45                                         return new CircleVertexData();
46                                 }});    
47
48         /**
49          * Creates an instance for the specified graph.
50          */
51         public CircleLayout(Graph<V,E> g) {
52                 super(g);
53         }
54
55         /**
56          * Returns the radius of the circle.
57          */
58         public double getRadius() {
59                 return radius;
60         }
61
62         /**
63          * Sets the radius of the circle.  Must be called before
64          * {@code initialize()} is called.
65          */
66         public void setRadius(double radius) {
67                 this.radius = radius;
68         }
69
70         /**
71          * Sets the order of the vertices in the layout according to the ordering
72          * specified by {@code comparator}.
73          */
74         public void setVertexOrder(Comparator<V> comparator)
75         {
76             if (vertex_ordered_list == null)
77                 vertex_ordered_list = new ArrayList<V>(getGraph().getVertices());
78             Collections.sort(vertex_ordered_list, comparator);
79         }
80
81     /**
82      * Sets the order of the vertices in the layout according to the ordering
83      * of {@code vertex_list}.
84      */
85         public void setVertexOrder(List<V> vertex_list)
86         {
87             if (!vertex_list.containsAll(getGraph().getVertices())) 
88                 throw new IllegalArgumentException("Supplied list must include " +
89                                 "all vertices of the graph");
90             this.vertex_ordered_list = vertex_list;
91         }
92         
93         public void reset() {
94                 initialize();
95         }
96
97         public void initialize() 
98         {
99                 Dimension d = getSize();
100                 
101                 if (d != null) 
102                 {
103                     if (vertex_ordered_list == null) 
104                         setVertexOrder(new ArrayList<V>(getGraph().getVertices()));
105
106                         double height = d.getHeight();
107                         double width = d.getWidth();
108
109                         if (radius <= 0) {
110                                 radius = 0.45 * (height < width ? height : width);
111                         }
112
113                         int i = 0;
114                         for (V v : vertex_ordered_list)
115                         {
116                                 Point2D coord = transform(v);
117
118                                 double angle = (2 * Math.PI * i) / vertex_ordered_list.size();
119
120                                 coord.setLocation(Math.cos(angle) * radius + width / 2,
121                                                 Math.sin(angle) * radius + height / 2);
122
123                                 CircleVertexData data = getCircleData(v);
124                                 data.setAngle(angle);
125                                 i++;
126                         }
127                 }
128         }
129
130         protected CircleVertexData getCircleData(V v) {
131                 return circleVertexDataMap.get(v);
132         }
133
134         protected static class CircleVertexData {
135                 private double angle;
136
137                 protected double getAngle() {
138                         return angle;
139                 }
140
141                 protected void setAngle(double angle) {
142                         this.angle = angle;
143                 }
144
145                 @Override
146                 public String toString() {
147                         return "CircleVertexData: angle=" + angle;
148                 }
149         }
150 }