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;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * A drawable that draws a given graph.
31 * @author Hannu Niemist�
33 public class GraphDrawable implements Drawable {
34 private static final Logger LOGGER = LoggerFactory.getLogger(GraphDrawable.class);
36 private static String DEFAULT_ALGORITHM = "dot";
38 DrawCommand[] commands;
43 private String algorithm;
45 public GraphDrawable(Graph graph, String algorithm) {
46 setGraph(graph, algorithm);
49 public GraphDrawable(Graph graph) {
53 public GraphDrawable() {
54 commands = new DrawCommand[0];
55 bounds = new Rectangle2D.Double(0, 0, 100, 100);
59 public Graph getGraph() {
63 public String getAlgorithm() {
67 * Sets a new graph to be drawn. This operation may take a while. It can
68 * be called from any thread.
71 public void setGraph(Graph graph) {
72 setGraph(graph, DEFAULT_ALGORITHM);
76 * Sets a new graph to be drawn. This operation may take a while. It can
77 * be called from any thread.
80 public Computation<Graph> setGraph(Graph graph, String algorithm) {
82 this.algorithm = algorithm;
83 Computation<Graph> computation = Graphs.createXDot(graph, algorithm);
84 final Semaphore semaphore = new Semaphore(0);
85 computation.addContinuation(new Continuation<Graph>() {
87 public void succeeded(Graph xgraph) {
88 commands = DrawCommandParser.parse(xgraph);
89 readBoundingBox(xgraph);
94 public void failed(Exception exception) {
95 exception.printStackTrace();
100 semaphore.tryAcquire(5L, TimeUnit.SECONDS);
101 } catch (InterruptedException e) {
107 private void readBoundingBox(Graph graph) {
108 String[] parts = graph.get("bb").split(",");
109 double minX = Double.parseDouble(parts[0]);
110 double maxY = -Double.parseDouble(parts[1]);
111 double maxX = Double.parseDouble(parts[2]);
112 double minY = -Double.parseDouble(parts[3]);
113 bounds = new Rectangle2D.Double(minX, minY, maxX-minX, maxY-minY);
117 public synchronized Rectangle2D getBounds() {
119 LOGGER.warn("bounds == null");
124 public synchronized void draw(Graphics2D g, Rectangle2D area) {
125 for(DrawCommand command : commands)