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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.graphviz.ui;
\r
14 import java.awt.Canvas;
\r
15 import java.awt.Frame;
\r
16 import java.util.concurrent.atomic.AtomicBoolean;
\r
18 import javax.swing.SwingUtilities;
\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
29 public class GraphvizComponent2 extends Composite {
\r
32 GraphDrawable2 drawable;
\r
34 AtomicBoolean initialized = new AtomicBoolean(false);
\r
37 public GraphvizComponent2(Composite parent, int style) {
\r
38 super(parent, style | SWT.EMBEDDED | SWT.NO_BACKGROUND);
\r
40 frame = SWT_AWT.new_Frame(this);
\r
42 SwingUtilities.invokeLater(new Runnable() {
\r
47 drawable = new GraphDrawable2();
\r
48 canvas = new ViewerCanvas(drawable);
\r
51 synchronized (initialized) {
\r
52 initialized.set(true);
\r
53 initialized.notifyAll();
\r
61 public void waitUntilInitialized() {
\r
63 synchronized (initialized) {
\r
64 while (!initialized.get())
\r
67 } catch (InterruptedException e) {
\r
68 throw new Error("GraphvizComponent AWT population interrupted for class " + this, e);
\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
77 public Computation<Graph> setGraph(Graph graph) {
\r
78 return setGraph(graph, "dot");
\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
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
91 public void succeeded(Graph result) {
\r
99 public void failed(Exception exception) {
\r
102 return computation;
\r
106 * Fits the content of the canvas to the component.
\r
107 * Can be called from any thread.
\r
109 public void fit() {
\r
110 ((ViewerCanvas)canvas).fit();
\r
111 getDisplay().asyncExec(new Runnable() {
\r
114 public void run() {
\r
119 SwingUtilities.invokeLater(new Runnable() {
\r
122 public void run() {
\r
131 public void requestFocus() {
\r
133 canvas.requestFocus();
\r
136 public Frame getFrame() {
\r
140 public Canvas getCanvas() {
\r
144 public GraphDrawable2 getDrawable() {
\r