/* * Xcm2Gxl.java * * Copyright (C) 2003 Miika Nurminen * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ /* This library includes modified code from JGraphPad 3.0. */ import java.awt.Font; import java.awt.Color; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.*; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Map; import java.util.List; import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; import java.util.StringTokenizer; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.jgraph.*; import org.jgraph.graph.*; import org.jgraph.pad.GPUserObject; import org.jgraph.pad.GPConverter; import org.jgraph.pad.GPGraph; // to do: versio ilman ikkonointia? (mahdollisesti toinen luokka ikkunointia varten) /** * Adapted from org.jgraph.pad.GPConverter */ class MyGPConverter { // Create a GXL-representation for the specified cells. static transient Hashtable hash; /** * Modification (25.7.2003): doctype. / Miika Nurminen */ public static String toGXL(GPGraph graph, Object[] cells) { hash = new Hashtable(); String gxl = "\n"+ // "\n"+ "\n"; // graphwiz requires graph attributes. current id is not very useful, though... /mn, 6.1.2003 // Create external keys for nodes for (int i = 0; i < cells.length; i++) if (graph.isVertex(cells[i])) { GPUserObject target = (GPUserObject)(((DefaultGraphCell)(cells[i])).getUserObject()); Object id = target.getProperty("id"); hash.put(cells[i], id/*new Integer(hash.size())*/); } // Convert Nodes Iterator it = hash.keySet().iterator(); while (it.hasNext()) { Object node = it.next(); gxl += vertexGXL(graph, hash.get(node), node); } // Convert Edges int edges = 0; for (int i = 0; i < cells.length; i++) if (graph.isEdge(cells[i])) { GPUserObject target = (GPUserObject)(((DefaultEdge)(cells[i])).getUserObject()); gxl += edgeGXL(graph, target.getProperty("id").toString(), ((DefaultEdge)(cells[i])).getUserObject().toString(), cells[i]); } // Close main tags gxl += "\n"; return gxl; } private static void addAttribute(StringBuffer str,String key,String value) { str.append("\n\t\t\n\t\t\t"); str.append(value); str.append("\n\t\t"); } /** * Modification (25.7.2003): added layout information. / Miika Nurminen */ public static String vertexGXL(GPGraph graph, Object id, Object vertex) { Rectangle bounds = graph.getCellBounds(vertex); String stylestr = "style=\"left="+bounds.x+"px;top="+bounds.y+"px;width="+bounds.width+"px;height="+bounds.height+"px;\""; StringBuffer nodeStr = new StringBuffer(100); nodeStr.append("\n\t"); GPUserObject target = (GPUserObject)(((DefaultGraphCell)(vertex)).getUserObject()); Map m = target.getProperties(); Iterator i = m.keySet().iterator(); while (i.hasNext()) { String key = i.next().toString(); if (!key.equals("id")) { String value = m.get(key).toString(); if (key.equals("value")) key = "label"; addAttribute(nodeStr,key, value); } } nodeStr.append("\n\t"); return nodeStr.toString(); } public static String edgeGXL(GPGraph graph, String id,String label, Object edge) { GraphModel model = graph.getModel(); String from = ""; if (model.getSource(edge) != null) { Object source = hash.get(model.getParent(model.getSource(edge))); if (source != null) from = /*"node" +*/ source.toString(); } String to = ""; if (model.getTarget(edge) != null) { Object target = hash.get(model.getParent(model.getTarget(edge))); if (target != null) to = /*"node" +*/ target.toString(); } if (from != null && to != null) { StringBuffer result = new StringBuffer(100); result.append("\n\t"); addAttribute(result, "label", label); result.append(""); return result.toString(); } else return ""; } } class MyGPUserObject extends org.jgraph.pad.GPUserObject { public MyGPUserObject(String label) { super(label); } // added method 3.9.2003 / mn public void showPublicPropertyDialog(final GPGraph graph, final Object cell) { showPropertyDialog(graph,cell); } } /** * Test class based on JGraph tutorial. * * Converted to standard filter 19.9.2003 * * @version 0.1 / 24.7.2003 * @author minurmin */ public class Xcm2Gxl { class MyTestGraph extends GPGraph { // Construct the Graph using the Model as its Data Source public MyTestGraph(GraphModel model) { super(model); // Use a Custom Marquee Handler setMarqueeHandler(new MyMarqueeHandler()); } /* public Object[] getAll() { return getDescendants(getRoots()); }*/ } class MyMarqueeHandler extends BasicMarqueeHandler { // Display PopupMenu or Remember Start Location and First Port // Override to Gain Control (for PopupMenu and ConnectMode) public boolean isForceMarqueeEvent(MouseEvent e) { // If Right Mouse Button we want to Display the PopupMenu if (SwingUtilities.isRightMouseButton(e)) // Return Immediately return true; // Else Call Superclass return super.isForceMarqueeEvent(e); } public void mousePressed(final MouseEvent e) { // If Right Mouse Button if (SwingUtilities.isRightMouseButton(e)) { Point loc = graph.fromScreen(e.getPoint()); // Find Cell in Model Coordinates Object cell = graph.getFirstCellForLocation(loc.x, loc.y); if (cell!=null) { final Object userObject = ((DefaultGraphCell) cell).getUserObject(); if (userObject instanceof MyGPUserObject) { ((MyGPUserObject) userObject).showPublicPropertyDialog(graph, cell); } } // Else if in ConnectMode and Remembered Port is Valid } else // Call Superclass super.mousePressed(e); } } Document doc = null; Map ids = null; // Hashtable for the ID lookup (ID to StorableObject, ie. all objects in IHMC model) GPGraph graph; /** * Parses filename out of IHMC resourcelink / resourceref attribute. */ private String parseResourceRef(String input) { StringTokenizer st = new StringTokenizer(input,";"); while (st.hasMoreTokens()) { String current = st.nextToken(); if (current.indexOf("relativeURL")>=0) { return current; } } return ""; } private String getChildAttribute(Node n, String attrname) { NodeList nl = n.getChildNodes(); for (int i=0; i output.xml"); System.out.println(" java -cp jgraph.jar:jgraphpad.jar Xcm2Gxl -nowindow -i input.xml -o output.xml\n"); return; } m.openStreams(); Xcm2Gxl application = new Xcm2Gxl(); application.run(m.getInput(),m.getOutput(),keepWindow); } catch (IOException e) { ErrorLib.errorReport(e); } try { m.getInput().close(); m.getOutput().close(); } catch (IOException e) { ErrorLib.errorReport(e); } } }