]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/GraphDrawable.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / drawable / GraphDrawable.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.graphviz.drawable;
13
14 import java.awt.Graphics2D;
15 import java.awt.geom.Rectangle2D;
16 import java.util.concurrent.Semaphore;
17 import java.util.concurrent.TimeUnit;
18
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
26 /**
27  * A drawable that draws a given graph.
28  * 
29  * @author Hannu Niemist�
30  */
31 public class GraphDrawable implements Drawable {
32
33         private static String DEFAULT_ALGORITHM = "dot";
34         
35         DrawCommand[] commands;
36         Rectangle2D bounds;
37         
38         
39         private Graph graph;
40         private String algorithm;
41         
42         public GraphDrawable(Graph graph, String algorithm) {
43                 setGraph(graph, algorithm);
44         }
45         
46         public GraphDrawable(Graph graph) {
47                 setGraph(graph);
48         }
49         
50         public GraphDrawable() {
51                 commands = new DrawCommand[0];
52                 bounds = new Rectangle2D.Double(0, 0, 100, 100);
53         }
54         
55         
56         public Graph getGraph() {
57                 return graph;
58         }
59         
60         public String getAlgorithm() {
61                 return algorithm;
62         }
63         /**
64          * Sets a new graph to be drawn. This operation may take a while. It can
65          * be called from any thread.
66          * @param graph
67          */
68         public void setGraph(Graph graph) {
69                 setGraph(graph, DEFAULT_ALGORITHM);
70         }
71         
72         /**
73          * Sets a new graph to be drawn. This operation may take a while. It can
74          * be called from any thread.
75          * @param graph
76          */
77         public Computation<Graph> setGraph(Graph graph, String algorithm) {
78             this.graph = graph;
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>() {
83             @Override
84             public void succeeded(Graph xgraph) {
85                 commands = DrawCommandParser.parse(xgraph);
86                 readBoundingBox(xgraph);
87                 semaphore.release();
88             }
89
90             @Override
91             public void failed(Exception exception) {
92                 exception.printStackTrace();
93                 semaphore.release();
94             }
95             });
96             try {
97                         semaphore.tryAcquire(5L, TimeUnit.SECONDS);
98                 } catch (InterruptedException e) {
99                         e.printStackTrace();
100                 }
101             return computation;
102         }
103         
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);
111         }
112         
113         @Override
114         public synchronized Rectangle2D getBounds() {
115             if(bounds == null)
116                 System.err.println("bounds == null");
117                 return bounds;
118         }
119         
120         @Override
121         public synchronized void draw(Graphics2D g, Rectangle2D area) {
122                 for(DrawCommand command : commands)
123                         command.draw(g);
124         }
125         
126 }