001: /*
002: * @(#)JGraphGraphvizEncoder.java 1.0 12-MAY-2004
003: *
004: * Copyright (c) 2001-2005, Gaudenz Alder and van woods
005: * All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: package com.jgraph.codecplugin;
022:
023: import java.text.DateFormat;
024: import java.util.Date;
025: import java.util.Hashtable;
026: import java.util.Iterator;
027:
028: import org.jgraph.JGraph;
029: import org.jgraph.graph.GraphModel;
030:
031: /**
032: * Graphviz Codec.
033: */
034: public class JGraphpadGraphvizCodec {
035:
036: static transient Hashtable hash;
037:
038: public static String encode(JGraph graph, Object[] cells) {
039: hash = new Hashtable();
040: DateFormat dateformat = DateFormat.getDateTimeInstance(
041: DateFormat.LONG, DateFormat.LONG);
042: String date = dateformat.format(new Date());
043: StringBuffer gv = new StringBuffer(
044: "/* Graphviz file generated by " + "JGraph - " + date
045: + " */" + "\n\ndigraph G {");
046:
047: // Create external keys for nodes
048: for (int i = 0; i < cells.length; i++)
049: if (JGraphpadCodecPlugin.isVertex(graph, cells[i]))
050: hash.put(cells[i], new Integer(hash.size()));
051:
052: // Process Nodes
053: Iterator it = hash.keySet().iterator();
054: while (it.hasNext()) {
055: Object node = it.next();
056: gv.append(encodeVertex(graph, hash.get(node), node));
057: }
058:
059: // Process Edges
060: int edges = 0;
061: for (int i = 0; i < cells.length; i++)
062: if (graph.getModel().isEdge(cells[i]))
063: gv.append(encodeEdge(graph, new Integer(edges++),
064: cells[i]));
065:
066: // Close main tags
067: gv.append("\n}");
068: return gv.toString();
069: }
070:
071: private static String encodeVertex(JGraph graph, Object id,
072: Object vertex) {
073: if (id == null)
074: return "";
075: String label = graph.convertValueToString(vertex);
076: if (label == null)
077: label = "";
078: return "\n\t" + id.toString() + " [label=\"" + label + "\", "
079: + "shape=\"box\"];";
080: }
081:
082: private static String encodeEdge(JGraph graph, Object id,
083: Object edge) {
084: GraphModel model = graph.getModel();
085: String from = null;
086: Object es = model.getSource(edge);
087: if (es != null) {
088: Object ps = hash.get(model.getParent(es));
089: if (ps != null)
090: from = ps.toString();
091: // debug if (es != null)
092: // from = graph.convertValueToString(model.getParent(es));
093: String to = null;
094: Object et = model.getTarget(edge);
095: if (et != null) {
096: Object tp = hash.get(model.getParent(et));
097: if (tp != null)
098: to = tp.toString();
099: // debug if (et != null)
100: // to = graph.convertValueToString(model.getParent(et));
101: }
102: if (from != null && to != null)
103: return "\n\t" + from + " -> " + to + ";";
104: }
105: return "";
106: }
107:
108: }
|