]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz/src/org/simantics/graphviz/Record.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / Record.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.util.ArrayList;
15
16 /**
17  * A helper class for creating record shaped nodes.
18  * 
19  * @author Hannu Niemistö
20  */
21 public class Record {
22  
23     static class IdentifiableField {
24         String id;
25         String label;
26         
27         public IdentifiableField(String id, String label) {
28             super();
29             this.id = id;
30             this.label = label;
31         }            
32     }
33     
34     static class Field {
35         String label;
36
37         public Field(String label) {
38             super();
39             this.label = label;
40         }        
41     }
42     
43     ArrayList<Object> fields = new ArrayList<Object>();
44     boolean rotated = false;
45     
46     public Record() {        
47     }
48     
49     /**
50      * Adds a simple nonreferable field to a record.
51      */
52     public void add(String label) {
53         fields.add(new Field(label));
54     }
55
56     /**
57      * Adds a field to a record that can be referred in 
58      * connections. Example
59      * <blockquote><pre>
60      *   Record r = new Record();
61      *   r.add("A field");
62      *   r.add("id1", "An another field");
63      *   Node n1 = r.toField(g);
64      *   Node n2 = new Node(g2);
65      *   new Edge(n1.getPort("id1"), n2);
66      * </pre></blockquote>
67      */
68     public void add(String id, String label) {
69         fields.add(new IdentifiableField(id, label));
70     }
71     
72     /**
73      * Puts a record inside this record. This is most useful
74      * if the record added is rotated. In this way, one can
75      * build records that both horizontally and vertically
76      * divided areas.
77      */
78     public void add(Record record) {
79         fields.add(record);
80     }
81     
82     /**
83      * Creates a node whose shape is build according to the
84      * record definition.
85      */
86     public Node toNode(Graph g) {
87         Node node = new Node(g);
88         node.setShape("record");
89         StringBuilder b = new StringBuilder();
90         toString(b);
91         node.setLabel(b.toString());
92         return node;
93     }
94     
95     private void toString(StringBuilder b) {
96         if(rotated)
97             b.append('{');
98         for(int i=0;i<fields.size();++i) {
99             Object f = fields.get(i);
100             if(i > 0)
101                 b.append('|');
102             if(f instanceof Field) {
103                 Field ff = (Field)f;
104                 b.append(ff.label);
105             }
106             else if(f instanceof IdentifiableField) {
107                 IdentifiableField ff = (IdentifiableField)f;
108                 b.append('<');
109                 b.append(ff.id);
110                 b.append('>');
111                 b.append(ff.label);
112             }
113             else if(f instanceof Record) {
114                 ((Record)f).toString(b);
115             }
116         }
117         if(rotated)
118             b.append('}');
119     }
120     
121     /**
122      * Tells if the record has opposite orientation from
123      * its surroundings. If the record is converted into
124      * node, this means the rankdir of the graph. If
125      * the record is put inside other record, then rotation
126      * is relative to the orientation of the parent record.
127      */
128     public void setRotated(boolean rotated) {
129         this.rotated = rotated;
130     }
131 }