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