]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz.ui/src/org/simantics/graphviz/ui/GraphvizComponent2.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graphviz.ui / src / org / simantics / graphviz / ui / GraphvizComponent2.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.graphviz.ui;\r
13 \r
14 import java.awt.Canvas;\r
15 import java.awt.Frame;\r
16 import java.util.concurrent.atomic.AtomicBoolean;\r
17 \r
18 import javax.swing.SwingUtilities;\r
19 \r
20 import org.eclipse.swt.SWT;\r
21 import org.eclipse.swt.awt.SWT_AWT;\r
22 import org.eclipse.swt.widgets.Composite;\r
23 import org.simantics.graphviz.Graph;\r
24 import org.simantics.graphviz.continuation.Computation;\r
25 import org.simantics.graphviz.continuation.Continuation;\r
26 import org.simantics.graphviz.drawable.GraphDrawable2;\r
27 import org.simantics.graphviz.drawable.ViewerCanvas;\r
28 \r
29 public class GraphvizComponent2 extends Composite {\r
30 \r
31     Canvas canvas;\r
32     GraphDrawable2 drawable;\r
33     Graph graph;\r
34     AtomicBoolean initialized = new AtomicBoolean(false);\r
35     Frame frame;\r
36 \r
37     public GraphvizComponent2(Composite parent, int style) {\r
38         super(parent, style | SWT.EMBEDDED | SWT.NO_BACKGROUND);\r
39 \r
40         frame = SWT_AWT.new_Frame(this);\r
41 \r
42         SwingUtilities.invokeLater(new Runnable() {\r
43 \r
44             @Override\r
45             public void run() {\r
46                 try {\r
47                     drawable = new GraphDrawable2();\r
48                     canvas = new ViewerCanvas(drawable);\r
49                     frame.add(canvas);\r
50                 } finally {\r
51                     synchronized (initialized) {\r
52                         initialized.set(true);\r
53                         initialized.notifyAll();\r
54                     }\r
55                 }\r
56             }\r
57 \r
58         });\r
59     }\r
60 \r
61     public void waitUntilInitialized() {\r
62         try {\r
63             synchronized (initialized) {\r
64                 while (!initialized.get())\r
65                     initialized.wait();\r
66             }\r
67         } catch (InterruptedException e) {\r
68             throw new Error("GraphvizComponent AWT population interrupted for class " + this, e);\r
69         }\r
70     }\r
71 \r
72     /**\r
73      * Sets a new graph to be drawn. This operation may take a while. It can\r
74      * be called from any thread. Automatically redraws the component.\r
75      * @param graph\r
76      */\r
77     public Computation<Graph> setGraph(Graph graph) {\r
78         return setGraph(graph, "dot");\r
79     }\r
80 \r
81     /**\r
82      * Sets a new graph to be drawn. This operation may take a while. It can\r
83      * be called from any thread. Automatically redraws the component.\r
84      * @param graph\r
85      */\r
86     public Computation<Graph> setGraph(Graph graph, String algorithm) {\r
87         Computation<Graph> computation = drawable.setGraph(graph, algorithm);\r
88         computation.addContinuation(new Continuation<Graph>() {\r
89             \r
90             @Override\r
91             public void succeeded(Graph result) {\r
92                 if (isDisposed())\r
93                     return;\r
94                 \r
95                 fit();\r
96             }\r
97             \r
98             @Override\r
99             public void failed(Exception exception) {\r
100             }\r
101         });\r
102         return computation;\r
103     }\r
104 \r
105     /**\r
106      * Fits the content of the canvas to the component.\r
107      * Can be called from any thread.\r
108      */\r
109     public void fit() {\r
110         ((ViewerCanvas)canvas).fit();\r
111         getDisplay().asyncExec(new Runnable() {\r
112 \r
113             @Override\r
114             public void run() {\r
115                 redraw();\r
116             }\r
117 \r
118         });\r
119         SwingUtilities.invokeLater(new Runnable() {\r
120 \r
121             @Override\r
122             public void run() {\r
123                 canvas.repaint();\r
124             }\r
125 \r
126         });\r
127 \r
128 \r
129     }\r
130 \r
131     public void requestFocus() {\r
132         if(canvas != null)\r
133             canvas.requestFocus();\r
134     }\r
135     \r
136     public Frame getFrame() {\r
137         return frame;\r
138     }\r
139     \r
140     public Canvas getCanvas() {\r
141         return canvas;\r
142     }\r
143     \r
144     public GraphDrawable2 getDrawable() {\r
145         return drawable;\r
146     }\r
147 \r
148 }\r