DBVIEW
src/org/dbview/utils/dot/SubGraph.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 package org.dbview.utils.dot;
00023 
00024 import java.util.*;
00025 import org.dbview.utils.*;
00026 
00027 /**
00028  * This class represents sub graphs.
00029  * @author Denis Beurive
00030  */
00031 public class SubGraph
00032 {
00033     /**
00034      * Name of the sub graph.
00035      */
00036     private String __name             = null;
00037 
00038     /**
00039      * Style of the sub graph.
00040      */
00041     private String __style            = null;
00042 
00043     /**
00044      * Background color for the sub graph.
00045      */
00046     private String __background_color = null;
00047 
00048     /**
00049      * Sub graph's label.
00050      */
00051     private String __label            = null;
00052 
00053     /**
00054      * Style of all nodes within the sub graph.
00055      */
00056     private String __node_style       = null;
00057 
00058     /**
00059      * Background color of all nodes within the sub graph.
00060      */
00061     private String __node_color       = null;
00062 
00063     /**
00064      * List of nodes within the sub graph.
00065      */
00066     private ArrayList<Node> __nodes   = new ArrayList<Node>();
00067 
00068     /**
00069      * The list of edges.
00070      */
00071     private ArrayList<Edge> __edges   = new ArrayList<Edge>();
00072     
00073     /**
00074      * Add an edge to the subgraph.
00075      * @param in_edge Edge to add.
00076      */
00077     public void addEdge(Edge in_edge) { this.__edges.add(in_edge); }
00078     
00079     /**
00080      * Create a unique, <b>and valid</b> (no spaces...), name for the sub graph.
00081      * @param in_name Original name.
00082      * @return The method returns a unique name.
00083      * @throws Exception
00084      */
00085     public static String generateName(String in_name)
00086     {
00087         return "cluster_" + Strings.SHAsum(in_name);
00088     }
00089     
00090     /**
00091      * Create a sub graph.
00092      * @param in_name Friendly name of the sub graph.
00093      * @note Please note that spaces are replaced by "_".
00094      */
00095     public SubGraph(String in_name)
00096     {
00097         this.__name = SubGraph.generateName(in_name);
00098     }
00099 
00100     /**
00101      * Set the style of the sub graph.
00102      * @param in_style Name of the style to apply.
00103      */
00104     public void setStyle(String in_style) { this.__style = in_style; }
00105 
00106     /**
00107      * Set the background color of the sub graph.
00108      * @param in_color The color that must be applied to the background.
00109      */
00110     public void setBackgroundColor(String in_color)     { this.__background_color = in_color; }
00111 
00112     /**
00113      * Set the label of the sub graph (this is the title).
00114      * @param in_label The label to set.
00115      */
00116     public void setLabel(String in_label) { this.__label = in_label; }
00117 
00118     /**
00119      * Set the style of all nodes within the sub graph.
00120      * @param in_style Name of the style to apply to all nodes.
00121      */
00122     public void setNodeStyle(String in_style) { this.__node_style = in_style; }
00123 
00124     /**
00125      * Set the background color of all nodes within the sub graph.
00126      * @param in_color Background color of all nodes within the sub graph.
00127      */
00128     public void setNodeColor(String in_color) { this.__node_color = in_color; }
00129 
00130     /**
00131      * Add a node to the sub graph.
00132      * @param in_node Node to add to the sub graph.
00133      */
00134     public void addNode(Node in_node) { this.__nodes.add(in_node); }
00135 
00136     /**
00137      * The method returns the DOT's name of a sub graph, given its friendly name.
00138      * @param in_name Friendly name of the sub graph.
00139      * @return The method returns the DOT's name of the sub graph.
00140      */
00141     // public static String getName(String in_name) { return "cluster_" + in_name; }
00142 
00143     /**
00144      * Return a string that represents this graph.
00145      * @return The method returns a string that represents this graph.
00146      */
00147     public String toString()
00148     {
00149         ArrayList<String> lines = new ArrayList<String>();
00150         lines.add("subgraph " + this.__name);
00151         lines.add("{");
00152 
00153         // Global configuration for the sub graph.
00154         if (null != this.__style)                        { lines.add("\t" + "style=" + this.__style + ";"); }
00155         if (null != this.__background_color) { lines.add("\t" + "color=" + this.__background_color + ";"); }
00156 
00157         // Set the title.
00158         if (null != this.__label) { lines.add("\t" + "label=\"" + this.__label + "\";"); }
00159 
00160         // Add the configuration for the nodes.
00161         if ((null != this.__node_style) || (null != this.__node_color))
00162         {
00163                 ArrayList<String> params = new ArrayList<String>();
00164                 if (null != this.__node_style) { params.add("style=" + this.__node_style); }
00165                 if (null != this.__node_color) { params.add("color=" + this.__node_color); }
00166                 lines.add("\t" + "node [" + org.dbview.utils.Strings.join(params, ", ") + "];");
00167         }
00168 
00169         // Add the nodes.
00170         for (Node node: this.__nodes) { lines.add(org.dbview.utils.Strings.indent("\t", node.toString())); }
00171 
00172         // Add all edges.
00173         for (Edge edge: this.__edges) { lines.add(org.dbview.utils.Strings.indent("\t", edge.toString())); }
00174         
00175         lines.add("}");
00176         return org.dbview.utils.Strings.joinWithNewLines(lines);
00177     }
00178 }