]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/GraphDrawable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / drawable / GraphDrawable.java
diff --git a/bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/GraphDrawable.java b/bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/GraphDrawable.java
new file mode 100644 (file)
index 0000000..ef8a014
--- /dev/null
@@ -0,0 +1,126 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.graphviz.drawable;\r
+\r
+import java.awt.Graphics2D;\r
+import java.awt.geom.Rectangle2D;\r
+import java.util.concurrent.Semaphore;\r
+import java.util.concurrent.TimeUnit;\r
+\r
+import org.simantics.graphviz.Graph;\r
+import org.simantics.graphviz.Graphs;\r
+import org.simantics.graphviz.continuation.Computation;\r
+import org.simantics.graphviz.continuation.Continuation;\r
+import org.simantics.graphviz.internal.xdot.DrawCommand;\r
+import org.simantics.graphviz.internal.xdot.DrawCommandParser;\r
+\r
+/**\r
+ * A drawable that draws a given graph.\r
+ * \r
+ * @author Hannu Niemist�\r
+ */\r
+public class GraphDrawable implements Drawable {\r
+\r
+       private static String DEFAULT_ALGORITHM = "dot";\r
+       \r
+       DrawCommand[] commands;\r
+       Rectangle2D bounds;\r
+       \r
+       \r
+       private Graph graph;\r
+       private String algorithm;\r
+       \r
+       public GraphDrawable(Graph graph, String algorithm) {\r
+               setGraph(graph, algorithm);\r
+       }\r
+       \r
+       public GraphDrawable(Graph graph) {\r
+               setGraph(graph);\r
+       }\r
+       \r
+       public GraphDrawable() {\r
+               commands = new DrawCommand[0];\r
+               bounds = new Rectangle2D.Double(0, 0, 100, 100);\r
+       }\r
+       \r
+       \r
+       public Graph getGraph() {\r
+               return graph;\r
+       }\r
+       \r
+       public String getAlgorithm() {\r
+               return algorithm;\r
+       }\r
+       /**\r
+        * Sets a new graph to be drawn. This operation may take a while. It can\r
+        * be called from any thread.\r
+        * @param graph\r
+        */\r
+       public void setGraph(Graph graph) {\r
+               setGraph(graph, DEFAULT_ALGORITHM);\r
+       }\r
+       \r
+       /**\r
+        * Sets a new graph to be drawn. This operation may take a while. It can\r
+        * be called from any thread.\r
+        * @param graph\r
+        */\r
+       public Computation<Graph> setGraph(Graph graph, String algorithm) {\r
+           this.graph = graph;\r
+        this.algorithm = algorithm;\r
+           Computation<Graph> computation = Graphs.createXDot(graph, algorithm);\r
+           final Semaphore semaphore = new Semaphore(0);\r
+           computation.addContinuation(new Continuation<Graph>() {\r
+            @Override\r
+            public void succeeded(Graph xgraph) {\r
+                commands = DrawCommandParser.parse(xgraph);\r
+                readBoundingBox(xgraph);\r
+                semaphore.release();\r
+            }\r
+\r
+            @Override\r
+            public void failed(Exception exception) {\r
+                exception.printStackTrace();\r
+                semaphore.release();\r
+            }\r
+           });\r
+           try {\r
+                       semaphore.tryAcquire(5L, TimeUnit.SECONDS);\r
+               } catch (InterruptedException e) {\r
+                       e.printStackTrace();\r
+               }\r
+           return computation;\r
+       }\r
+       \r
+       private void readBoundingBox(Graph graph) {\r
+               String[] parts = graph.get("bb").split(",");\r
+               double minX = Double.parseDouble(parts[0]);\r
+               double maxY = -Double.parseDouble(parts[1]);\r
+               double maxX = Double.parseDouble(parts[2]);\r
+               double minY = -Double.parseDouble(parts[3]);\r
+               bounds = new Rectangle2D.Double(minX, minY, maxX-minX, maxY-minY);\r
+       }\r
+       \r
+       @Override\r
+       public synchronized Rectangle2D getBounds() {\r
+           if(bounds == null)\r
+               System.err.println("bounds == null");\r
+               return bounds;\r
+       }\r
+       \r
+       @Override\r
+       public synchronized void draw(Graphics2D g, Rectangle2D area) {\r
+               for(DrawCommand command : commands)\r
+                       command.draw(g);\r
+       }\r
+       \r
+}\r