1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.graphviz;
14 import java.util.ArrayList;
17 * A helper class for creating record shaped nodes.
19 * @author Hannu Niemistö
23 static class IdentifiableField {
27 public IdentifiableField(String id, String label) {
37 public Field(String label) {
43 ArrayList<Object> fields = new ArrayList<Object>();
44 boolean rotated = false;
50 * Adds a simple nonreferable field to a record.
52 public void add(String label) {
53 fields.add(new Field(label));
57 * Adds a field to a record that can be referred in
58 * connections. Example
60 * Record r = new Record();
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);
68 public void add(String id, String label) {
69 fields.add(new IdentifiableField(id, label));
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
78 public void add(Record record) {
83 * Creates a node whose shape is build according to the
86 public Node toNode(Graph g) {
87 Node node = new Node(g);
88 node.setShape("record");
89 StringBuilder b = new StringBuilder();
91 node.setLabel(b.toString());
95 private void toString(StringBuilder b) {
98 for(int i=0;i<fields.size();++i) {
99 Object f = fields.get(i);
102 if(f instanceof Field) {
106 else if(f instanceof IdentifiableField) {
107 IdentifiableField ff = (IdentifiableField)f;
113 else if(f instanceof Record) {
114 ((Record)f).toString(b);
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.
128 public void setRotated(boolean rotated) {
129 this.rotated = rotated;