]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz/src/org/simantics/graphviz/Graph.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / Graph.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.graphviz;
13
14 import java.io.ByteArrayOutputStream;
15 import java.io.PrintStream;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19
20 /**
21  * Root graph that contains nodes, edges and subgraphs.
22  * 
23  * @author Hannu Niemistö
24  */
25 public class Graph extends AbstractAttributeContainer implements IGraph {
26     Collection<IGraphPart> parts = new ArrayList<IGraphPart>();
27     int curId = 0;
28     
29     public Graph() {
30         super();
31     }
32
33     public Graph(String label) {
34         super();
35         setLabel(label);
36     }
37
38     @Override
39     public void addPart(IGraphPart part) {
40         parts.add(part);        
41     }
42
43     @Override
44     public String newId() {
45         return "n" + (++curId);
46     }
47
48     @Override
49     public Collection<IGraphPart> getParts() {
50         return Collections.unmodifiableCollection(parts);
51     }
52
53     public void write(PrintStream s) {
54         s.print("digraph {");
55         s.print("graph");
56         writeAttributes(s);
57         for(IGraphPart part : parts)
58             part.write(s);
59         s.println("};");        
60     }
61     
62     @Override
63     public String toString() {
64         ByteArrayOutputStream ba = new ByteArrayOutputStream();
65         write(new PrintStream(ba));
66         return ba.toString();
67     }
68     
69     public void setLabel(String label) {
70         set("label", label);
71     }
72     
73     /**
74      * Color used for text.
75      */
76     public void setFontColor(String color) {
77         set("fontcolor", color);
78     }
79     
80     /**
81      * <p>Sets direction of graph layout. For example, if rankdir="LR", and barring cycles, an edge T -> H; will go from left to right. By default, graphs are laid out from top to bottom.</p>
82      * <p>Alternatives: <tt>TB LR BT RL</tt></p>
83      */
84     public void setRankdir(String rankdir) {
85         set("rankdir", rankdir);
86     }
87     
88     /**
89      * <p>In dot, this gives the desired rank separation, in inches. This is the minimum vertical distance between the bottom of the nodes in one rank and the tops of nodes in the next. If the value contains "equally", the centers of all ranks are spaced equally apart. Note that both settings are possible, e.g., ranksep = "1.2 equally". In twopi, specifies radial separation of concentric circles.</p>
90      */
91     public void setRanksep(double ranksep, boolean equally) {
92         set("ranksep", Double.toString(ranksep) + (equally ? " equally" : ""));
93     }
94
95 }