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.drawable;
14 import java.awt.Graphics2D;
15 import java.awt.geom.Rectangle2D;
16 import java.util.concurrent.Semaphore;
17 import java.util.concurrent.TimeUnit;
19 import org.simantics.graphviz.Graph;
20 import org.simantics.graphviz.Graphs;
21 import org.simantics.graphviz.continuation.Computation;
22 import org.simantics.graphviz.continuation.Continuation;
23 import org.simantics.graphviz.internal.xdot.DrawCommand;
24 import org.simantics.graphviz.internal.xdot.DrawCommandParser;
27 * A drawable that draws a given graph.
29 * @author Hannu Niemist�
31 public class GraphDrawable implements Drawable {
33 private static String DEFAULT_ALGORITHM = "dot";
35 DrawCommand[] commands;
40 private String algorithm;
42 public GraphDrawable(Graph graph, String algorithm) {
43 setGraph(graph, algorithm);
46 public GraphDrawable(Graph graph) {
50 public GraphDrawable() {
51 commands = new DrawCommand[0];
52 bounds = new Rectangle2D.Double(0, 0, 100, 100);
56 public Graph getGraph() {
60 public String getAlgorithm() {
64 * Sets a new graph to be drawn. This operation may take a while. It can
65 * be called from any thread.
68 public void setGraph(Graph graph) {
69 setGraph(graph, DEFAULT_ALGORITHM);
73 * Sets a new graph to be drawn. This operation may take a while. It can
74 * be called from any thread.
77 public Computation<Graph> setGraph(Graph graph, String algorithm) {
79 this.algorithm = algorithm;
80 Computation<Graph> computation = Graphs.createXDot(graph, algorithm);
81 final Semaphore semaphore = new Semaphore(0);
82 computation.addContinuation(new Continuation<Graph>() {
84 public void succeeded(Graph xgraph) {
85 commands = DrawCommandParser.parse(xgraph);
86 readBoundingBox(xgraph);
91 public void failed(Exception exception) {
92 exception.printStackTrace();
97 semaphore.tryAcquire(5L, TimeUnit.SECONDS);
98 } catch (InterruptedException e) {
104 private void readBoundingBox(Graph graph) {
105 String[] parts = graph.get("bb").split(",");
106 double minX = Double.parseDouble(parts[0]);
107 double maxY = -Double.parseDouble(parts[1]);
108 double maxX = Double.parseDouble(parts[2]);
109 double minY = -Double.parseDouble(parts[3]);
110 bounds = new Rectangle2D.Double(minX, minY, maxX-minX, maxY-minY);
114 public synchronized Rectangle2D getBounds() {
116 System.err.println("bounds == null");
121 public synchronized void draw(Graphics2D g, Rectangle2D area) {
122 for(DrawCommand command : commands)