1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.graphviz.drawable;
\r
14 import java.awt.Graphics2D;
\r
15 import java.awt.geom.Rectangle2D;
\r
16 import java.util.concurrent.Semaphore;
\r
17 import java.util.concurrent.TimeUnit;
\r
19 import org.simantics.graphviz.Graph;
\r
20 import org.simantics.graphviz.Graphs;
\r
21 import org.simantics.graphviz.continuation.Computation;
\r
22 import org.simantics.graphviz.continuation.Continuation;
\r
23 import org.simantics.graphviz.internal.xdot.DrawCommand;
\r
24 import org.simantics.graphviz.internal.xdot.DrawCommandParser;
\r
27 * A drawable that draws a given graph.
\r
29 * @author Hannu Niemist�
\r
31 public class GraphDrawable implements Drawable {
\r
33 private static String DEFAULT_ALGORITHM = "dot";
\r
35 DrawCommand[] commands;
\r
39 private Graph graph;
\r
40 private String algorithm;
\r
42 public GraphDrawable(Graph graph, String algorithm) {
\r
43 setGraph(graph, algorithm);
\r
46 public GraphDrawable(Graph graph) {
\r
50 public GraphDrawable() {
\r
51 commands = new DrawCommand[0];
\r
52 bounds = new Rectangle2D.Double(0, 0, 100, 100);
\r
56 public Graph getGraph() {
\r
60 public String getAlgorithm() {
\r
64 * Sets a new graph to be drawn. This operation may take a while. It can
\r
65 * be called from any thread.
\r
68 public void setGraph(Graph graph) {
\r
69 setGraph(graph, DEFAULT_ALGORITHM);
\r
73 * Sets a new graph to be drawn. This operation may take a while. It can
\r
74 * be called from any thread.
\r
77 public Computation<Graph> setGraph(Graph graph, String algorithm) {
\r
79 this.algorithm = algorithm;
\r
80 Computation<Graph> computation = Graphs.createXDot(graph, algorithm);
\r
81 final Semaphore semaphore = new Semaphore(0);
\r
82 computation.addContinuation(new Continuation<Graph>() {
\r
84 public void succeeded(Graph xgraph) {
\r
85 commands = DrawCommandParser.parse(xgraph);
\r
86 readBoundingBox(xgraph);
\r
87 semaphore.release();
\r
91 public void failed(Exception exception) {
\r
92 exception.printStackTrace();
\r
93 semaphore.release();
\r
97 semaphore.tryAcquire(5L, TimeUnit.SECONDS);
\r
98 } catch (InterruptedException e) {
\r
99 e.printStackTrace();
\r
101 return computation;
\r
104 private void readBoundingBox(Graph graph) {
\r
105 String[] parts = graph.get("bb").split(",");
\r
106 double minX = Double.parseDouble(parts[0]);
\r
107 double maxY = -Double.parseDouble(parts[1]);
\r
108 double maxX = Double.parseDouble(parts[2]);
\r
109 double minY = -Double.parseDouble(parts[3]);
\r
110 bounds = new Rectangle2D.Double(minX, minY, maxX-minX, maxY-minY);
\r
114 public synchronized Rectangle2D getBounds() {
\r
116 System.err.println("bounds == null");
\r
121 public synchronized void draw(Graphics2D g, Rectangle2D area) {
\r
122 for(DrawCommand command : commands)
\r