KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > ext > VisioExporter


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  * VisioExporter.java
27  * ------------------
28  * (C) Copyright 2003-2006, by Avner Linder and Contributors.
29  *
30  * Original Author: Avner Linder
31  * Contributor(s): Barak Naveh
32  *
33  * $Id: VisioExporter.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  * 27-May-2004 : Initial Version (AL);
38  *
39  */

40 package org.jgrapht.ext;
41
42 import java.io.*;
43
44 import java.util.*;
45
46 import org.jgrapht.*;
47
48
49 /**
50  * Exports a graph to a csv format that can be imported into MS Visio.
51  *
52  * <p><b>Tip:</b> By default, the exported graph doesn't show link directions.
53  * To show link directions:<br>
54  *
55  * <ol>
56  * <li>Select All (Ctrl-A)</li>
57  * <li>Right Click the selected items</li>
58  * <li>Format/Line...</li>
59  * <li>Line ends: End: (choose an arrow)</li>
60  * </ol>
61  * </p>
62  *
63  * @author Avner Linder
64  */

65 public class VisioExporter<V, E>
66 {
67
68     //~ Instance fields -------------------------------------------------------
69

70     private VertexNameProvider<V> vertexNameProvider;
71
72     //~ Constructors ----------------------------------------------------------
73

74     /**
75      * Creates a new VisioExporter object with the specified naming policy.
76      *
77      * @param vertexNameProvider the vertex name provider to be used for naming
78      * the Visio shapes.
79      */

80     public VisioExporter(VertexNameProvider<V> vertexNameProvider)
81     {
82         this.vertexNameProvider = vertexNameProvider;
83     }
84
85     /**
86      * Creates a new VisioExporter object.
87      */

88     public VisioExporter()
89     {
90         this(new StringNameProvider<V>());
91     }
92
93     //~ Methods ---------------------------------------------------------------
94

95     /**
96      * Exports the specified graph into a Visio csv file format.
97      *
98      * @param output the print stream to which the graph to be exported.
99      * @param g the graph to be exported.
100      */

101     public void export(OutputStream output, Graph<V, E> g)
102     {
103         PrintStream out = new PrintStream(output);
104
105         for (Iterator<V> i = g.vertexSet().iterator(); i.hasNext();) {
106             exportVertex(out, i.next());
107         }
108
109         for (Iterator<E> i = g.edgeSet().iterator(); i.hasNext();) {
110             exportEdge(out, i.next(), g);
111         }
112
113         out.flush();
114     }
115
116     private void exportEdge(PrintStream out, E edge, Graph<V, E> g)
117     {
118         String JavaDoc sourceName =
119             vertexNameProvider.getVertexName(g.getEdgeSource(edge));
120         String JavaDoc targetName =
121             vertexNameProvider.getVertexName(g.getEdgeTarget(edge));
122
123         out.print("Link,");
124
125         // create unique ShapeId for link
126
out.print(sourceName);
127         out.print("-->");
128         out.print(targetName);
129
130         // MasterName and Text fields left blank
131
out.print(",,,");
132         out.print(sourceName);
133         out.print(",");
134         out.print(targetName);
135         out.print("\n");
136     }
137
138     private void exportVertex(PrintStream out, V vertex)
139     {
140         String JavaDoc name = vertexNameProvider.getVertexName(vertex);
141
142         out.print("Shape,");
143         out.print(name);
144         out.print(",,"); // MasterName field left empty
145
out.print(name);
146         out.print("\n");
147     }
148 }
149
Popular Tags