]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz/src/org/simantics/graphviz/Edge.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / Edge.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.PrintStream;
15 import java.util.HashSet;
16 import java.util.Set;
17
18 /**
19  * An edge of a graph.
20  * 
21  * @author Hannu Niemistö
22  */
23 public class Edge extends AbstractGraphPart {
24
25         Identifiable tail;
26         Identifiable head;
27         
28         public Edge(IGraph graph, Identifiable tail, Identifiable head) {
29         super(graph);
30         this.tail = tail;
31         this.head = head;
32     }
33         
34         private static IGraph chooseParentGraph(IGraph a, IGraph b) {
35             if(a == b)
36                 return a;
37             Set<IGraph> graphs = new HashSet<IGraph>();
38             while(true) {
39                 graphs.add(a);
40                 if(a instanceof Identifiable)
41                     a = ((Identifiable)a).getParent();
42                 else
43                     break;
44             }
45             while(true) {
46                 if(graphs.contains(b))
47                     return b;
48                 if(b instanceof Identifiable)
49                 b = ((Identifiable)b).getParent();
50             else
51                 break;
52             }
53             throw new IllegalArgumentException("Tried to connect nodes that do not belong to the same graph.");
54         }
55         
56         public Edge(Identifiable tail, Identifiable head) {
57         this(chooseParentGraph(tail.getParent(), head.getParent()), tail, head);
58     }
59         
60     /**
61      * Text label attached to objects. 
62      */
63         public void setLabel(String label) {
64                 set("label", label);
65         }
66         
67         /**
68          * Text label to be placed near head of edge.
69          */
70         public void setHeadLabel(String label) {
71             set("headlabel", label);
72         }
73         
74         
75         /**
76          * Text label to be placed near tail of edge.
77          */
78         public void setTailLabel(String label) {
79             set("taillabel", label);
80     }
81         
82         /**
83          * <p>Set edge type for drawing arrowheads. This indicates which ends of the edge should be decorated with an arrowhead. The actual style of the arrowhead can be specified using the arrowhead and arrowtail attributes.</p>
84          * <p>Alternatives: <tt>forward back both none</tt></p> 
85          */
86     public void setDir(String label) {
87         set("dir", label);
88     }
89     
90     /**
91      * Set style for node or edge. For cluster subgraph, if "filled", the cluster box's background is filled.
92      */
93     public void setStyle(String style) {
94         set("style", style);
95     }
96     
97     /**
98      * If false, the edge is not used in ranking the nodes.
99      */
100     public void setConstraint(boolean constraint) {
101         set("constraint", Boolean.toString(constraint));
102     }
103     
104     /**
105      * <p>
106      * Basic drawing color for graphics, not text. For the latter, use the
107      * fontcolor attribute. For edges, the value can either be a single color or
108      * a colorList. In the latter case, the edge is drawn using parallel splines
109      * or lines, one for each color in the list, in the order given. The head
110      * arrow, if any, is drawn using the first color in the list, and the tail
111      * arrow, if any, the second color. This supports the common case of drawing
112      * opposing edges, but using parallel splines instead of separately routed
113      * multiedges.
114      * </p>
115      * <p>Supports arbitrary RGB(A) colors in format #xxxxxx, where
116      * each x is a hex digit. Supports also color names in x11 color chart.</p>
117      */
118     public void setColor(String color) {
119         set("color", color);
120     }
121     
122     /**
123      * Color used for text.
124      */
125     public void setFontColor(String color) {
126         set("fontcolor", color);
127     }
128         
129         /**
130          * <p>Sets the shape of the arrow head</p>
131          * 
132          * <p>Arrow shapes can be specified and named using the following simple grammar. Literal characters are given in single quotes. Square brackets [ and ] enclose optional items. Vertical bars | separate alternatives.
133          * </p>
134          * <pre>
135          * arrowname   :   aname [ aname [ aname [ aname ] ] ]
136          * aname   :   [ modifiers ] shape
137          * modifiers   :   [ 'o' ] [ side ]
138          * side    :   'l'
139          *         |   'r'
140          * shape   :   box
141          *         |   crow
142          *         |   diamond
143          *         |   dot
144          *         |   inv
145          *         |   none
146          *         |   normal
147          *         |   tee
148          *         |   vee
149          * </pre>
150          * As for the modifiers:
151          * <ul>
152          * <li> 'l' Clip the shape, leaving only the part to the left of the edge.</li>
153          * <li> 'r' Clip the shape, leaving only the part to the right of the edge.</li>
154          * <li> 'o' Use an open (non-filled) version of the shape.</li>
155          * </ul>
156          * <p>Left and right are defined as those directions determined by looking from the edge towards the point where the arrow "touches" the node.
157          * </p>
158          * <p>Note that the first arrow shape specified occurs closest to the node. Subsequent arrow shapes, if specified, occur further from the node.
159          * </p>
160          */
161         public void setArrowhead(String arrowName) {
162             set("arrowhead", arrowName);
163         }
164         
165         /**
166          * <p>Sets the shape of the arrow tail</p>
167          * 
168          * @see Edge#setArrowhead
169          */
170         public void setArrowtail(String arrowName) {
171         set("arrowtail", arrowName);
172     }
173         
174         public void setArrowsize(double arrowSize) {
175         set("arrowsize", Double.toString(arrowSize));
176     }
177         
178         public Identifiable getTail() {
179                 return tail;
180         }
181
182         public Identifiable getHead() {
183                 return head;
184         }
185
186         @Override
187         public void write(PrintStream s) {
188                 s.print(tail.getId());
189                 s.print(" -> ");
190                 s.print(head.getId());
191                 writeAttributes(s);
192         }
193
194 }