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