KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > experimental > touchgraph > TouchgraphConverter


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* -------------------
26  * TouchgraphConverter.java
27  * -------------------
28  * (C) Copyright 2006-2006, by Carl Anderson and Contributors.
29  *
30  * Original Author: Carl Anderson
31  * Contributor(s): -
32  *
33  * $Id: TouchgraphConverter.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  * 8-May-2006 : Initial revision (CA);
38  *
39  */

40 package org.jgrapht.experimental.touchgraph;
41
42 import com.touchgraph.graphlayout.*;
43
44 import java.util.*;
45
46 import org.jgrapht.*;
47
48
49 /**
50  * A Converter class that converts a JGraphT graph to that used in the
51  * TouchGraph library.
52  *
53  * @author canderson
54  */

55 public class TouchgraphConverter<V, E>
56 {
57
58     //~ Methods ---------------------------------------------------------------
59

60     /**
61      * Convert a JGraphT graph to the representation used in the TouchGraph
62      * library. http://sourceforge.net/projects/touchgraph TouchGraph doesn't
63      * have a sensible, extensible graph object class and so one has to add them
64      * to a TGPanel which will store the graph components (the set of nodes and
65      * edges) in its own way. The closest Touchgraph has to a graph object is a
66      * GraphEltSet but Touchgraph does not provide the visibility to use it
67      * easily and one can use a JGraphT graph. While JGraphT nodes can be any
68      * type of objects, TouchGraph uses a set of com.touchgraph.graphlayout.Node
69      * and com.touchgraph.graphlayout.Edge only. Moreover, TouchGraph edges are
70      * always directed. Having said that, if you want a nice way to visualize
71      * and explore a graph, especially large complex graphs, TouchGraph is very
72      * nice
73      *
74      * @param graph: the JGraphT graph
75      * @param tgPanel: the TouchGraph TGPanel
76      * @param selfReferencesAllowed: do you want to include self-referenctial
77      * edges, ie an edge from a node to itself?
78      * Self-referential loops do not show up in
79      * the TG visualization but you may want to
80      * subclass TG's Node class to show them
81      *
82      * @return first node of the TouchGraph graph
83      *
84      * @throws TGException
85      */

86     @SuppressWarnings JavaDoc("unchecked")
87     public Node convertToTouchGraph(
88         Graph<V, E> graph,
89         TGPanel tgPanel,
90         boolean selfReferencesAllowed)
91         throws TGException
92     {
93         List<V> jgtNodes = new ArrayList<V>(graph.vertexSet());
94         Node [] tgNodes = new Node [jgtNodes.size()];
95
96         // add all the nodes...
97
for (int i = 0; i < jgtNodes.size(); i++) {
98             Node n;
99             if (jgtNodes.get(i) instanceof Node) {
100                 // if our JGraphT object was a touchGraph node, add it unaltered
101
n = (Node) jgtNodes.get(i);
102             } else {
103                 // create a TG Node with a "label" and "id" equals to the
104
// objects toString() value
105
n = new Node(jgtNodes.get(i).toString());
106             }
107
108             // store this for edge-related creation below
109
tgNodes[i] = n;
110
111             // add the node to the TG panel
112
tgPanel.addNode(n);
113         }
114
115         // add the edges...
116
for (int i = 0; i < tgNodes.length; i++) {
117             for (int j = 0; j < tgNodes.length; j++) {
118                 // self-referential loops do not show up in the TG
119
// visualization but you may want to
120
// subclass TG's Node class to show them
121
if ((i != j) || selfReferencesAllowed) {
122                     if (
123                         graph.getEdge(jgtNodes.get(i), jgtNodes.get(j))
124                         != null) {
125                         // add TG directed edge from i to j
126
tgPanel.addEdge(new Edge(tgNodes[i], tgNodes[j]));
127                     }
128                 }
129             }
130         }
131
132         // return the first node as a focal point for the TG panel
133
return tgNodes[0];
134     }
135 }
136
137 // End TouchgraphConverter.java
138
Popular Tags