DBVIEW
src/org/dbview/utils/dot/DiGraph.java
00001 /*
00002         DbView - Graph Visualization
00003     Copyright (C) 2012  Denis BEURIVE
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 /**
00020  * @author Denis Beurive
00021  */
00022 
00023 package org.dbview.utils.dot;
00024 
00025 import java.lang.Float;
00026 import java.util.*;
00027 
00028 /**
00029  * This class represents a directed graph.
00030  * @author Denis Beurive
00031  */
00032 public class DiGraph
00033 {
00034     /**
00035      * <p>The initial orientation of a record node depends on the rankdir attribute. If this attribute is TB (the default) or TB, corresponding to vertical layouts, the top-level fields in a record are displayed horizontally. If, however, this attribute is LR or RL, corresponding to horizontal layouts, the top-level fields are displayed vertically.</p>
00036      * <p>See GRAPHVIZ' documentation for "rankdir".</p>
00037      * @see http://www.graphviz.org/doc/info/shapes.html
00038      */
00039     private String              __rankdir = "LR";
00040 
00041     /**
00042      * See GRAPHVIZ' documentation for "nodesep".
00043      * @see http://www.graphviz.org/doc/info/attrs.html
00044      */
00045         private Float                __nodesep     = null;
00046 
00047     /**
00048      * See GRAPHVIZ' documentation for "ranksep".
00049      * @see http://www.graphviz.org/doc/info/attrs.html
00050      */
00051         private Float                __ranksep     = null;
00052 
00053         /**
00054      * See GRAPHVIZ' documentation for "compound".
00055      * @see http://www.graphviz.org/doc/info/attrs.html
00056          */
00057         private Boolean                          __compound    = Boolean.FALSE;
00058 
00059         /**
00060          * The list of sub graphs.
00061          */
00062         private ArrayList<SubGraph>  __subgraphs   = new ArrayList<SubGraph>();
00063 
00064         /**
00065          * The list of nodes.
00066          */
00067         private ArrayList<Node>      __nodes       = new ArrayList<Node>();
00068 
00069         /**
00070          * The list of edges.
00071          */
00072         private ArrayList<Edge>      __edges       = new ArrayList<Edge>();
00073 
00074         /**
00075          * Set the initial orientation of a record node.
00076          * @param in_direction This string defines the direction. Values can be:
00077          *        <ul>
00078          *             <li>TB (Top to bottom)</li>
00079          *             <li>LR (Left to right)</li>
00080          *             <li>RL 'Right to left)</li>
00081          *        </ul>
00082          */
00083         public void setRankdir(String in_direction) { this.__rankdir = in_direction; }
00084 
00085         /**
00086          * This method returns the graph's layout.
00087          * @return The method returns the graph's layout. Values can be:
00088      *         <ul>
00089      *             <li>TB (Top to bottom)</li>
00090      *             <li>LR (Left to right)</li>
00091      *             <li>RL 'Right to left)</li>
00092      *         </ul>
00093          */
00094         public String getRankdir() { return this.__rankdir; }
00095 
00096         /**
00097          * This method sets the minimum distance between two nodes.
00098          * @param in_value Minimum distance to set.
00099          * @see GraphViz documentation.
00100          */
00101         public void setNodesep(Float in_value) { this.__nodesep = in_value; }
00102 
00103         /**
00104          * This method sets the minimum distance between two graph's ranks.
00105          * @param in_value Minimum distance to set.
00106          * @see GraphViz documentation.
00107          */
00108         public void setRankset(Float in_value) { this.__ranksep = in_value; }
00109 
00110         /**
00111          * This method set the compound's flag.
00112          * @param in_value TRUE or FALSE.
00113          * @see GraphViz documentation.
00114          */
00115         public void setCompound(Boolean in_value) { this.__compound = in_value; }
00116 
00117         /**
00118          * Add a sub graph to the graph.
00119          * @param in_subgraph Sub graph to add.
00120          */
00121         public void addSubGraph(SubGraph in_subgraph) { this.__subgraphs.add(in_subgraph); }
00122 
00123         /**
00124          * Add an edge to the graph.
00125          * @param in_edge Edge to add.
00126          */
00127         public void addEdge(Edge in_edge) { this.__edges.add(in_edge); }
00128 
00129         /**
00130          * Add a node to the graph.
00131          * @param in_node Node to add.
00132          */
00133         public void addNode(Node in_node) { this.__nodes.add(in_node); }
00134 
00135         /**
00136          * This method returns a string that represents the directed graph.
00137          * @return The method returns a string that represents the directed graph.
00138          */
00139         public String toString()
00140         {
00141                 ArrayList<String> lines = new ArrayList<String>();
00142 
00143                 lines.add("digraph G");
00144                 lines.add("{");
00145 
00146                 // Add the global configuration for the graph.
00147                 lines.add("\trankdir=" + this.__rankdir + ";");
00148 
00149                 if (this.__compound)        { lines.add("\t" + "compound=true;"); }
00150                 if (null != this.__nodesep) { lines.add("\t" + "nodesep=" + this.__nodesep + ";"); }
00151                 if (null != this.__ranksep) { lines.add("\t" + "ranksep=" + this.__ranksep + ";"); }
00152                 lines.add("");
00153 
00154                 // Add all sub graphs.
00155                 for (SubGraph subgraph: this.__subgraphs) { lines.add(org.dbview.utils.Strings.indent("\t", subgraph.toString())); lines.add(""); }
00156 
00157                 // Add all nodes.
00158                 for (Node node: this.__nodes) { lines.add(org.dbview.utils.Strings.indent("\t", node.toString())); lines.add(""); }
00159 
00160                 // Add all edges.
00161                 for (Edge edge: this.__edges) { lines.add(org.dbview.utils.Strings.indent("\t", edge.toString())); lines.add(""); }
00162 
00163                 lines.add("}");
00164                 return org.dbview.utils.Strings.joinWithNewLines(lines);
00165         }
00166 }