-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.graphviz.ui;\r
-\r
-import java.awt.Canvas;\r
-import java.awt.Frame;\r
-import java.util.concurrent.atomic.AtomicBoolean;\r
-\r
-import javax.swing.SwingUtilities;\r
-\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.awt.SWT_AWT;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.simantics.graphviz.Graph;\r
-import org.simantics.graphviz.continuation.Computation;\r
-import org.simantics.graphviz.continuation.Continuation;\r
-import org.simantics.graphviz.drawable.GraphDrawable2;\r
-import org.simantics.graphviz.drawable.ViewerCanvas;\r
-\r
-public class GraphvizComponent2 extends Composite {\r
-\r
- Canvas canvas;\r
- GraphDrawable2 drawable;\r
- Graph graph;\r
- AtomicBoolean initialized = new AtomicBoolean(false);\r
- Frame frame;\r
-\r
- public GraphvizComponent2(Composite parent, int style) {\r
- super(parent, style | SWT.EMBEDDED | SWT.NO_BACKGROUND);\r
-\r
- frame = SWT_AWT.new_Frame(this);\r
-\r
- SwingUtilities.invokeLater(new Runnable() {\r
-\r
- @Override\r
- public void run() {\r
- try {\r
- drawable = new GraphDrawable2();\r
- canvas = new ViewerCanvas(drawable);\r
- frame.add(canvas);\r
- } finally {\r
- synchronized (initialized) {\r
- initialized.set(true);\r
- initialized.notifyAll();\r
- }\r
- }\r
- }\r
-\r
- });\r
- }\r
-\r
- public void waitUntilInitialized() {\r
- try {\r
- synchronized (initialized) {\r
- while (!initialized.get())\r
- initialized.wait();\r
- }\r
- } catch (InterruptedException e) {\r
- throw new Error("GraphvizComponent AWT population interrupted for class " + this, e);\r
- }\r
- }\r
-\r
- /**\r
- * Sets a new graph to be drawn. This operation may take a while. It can\r
- * be called from any thread. Automatically redraws the component.\r
- * @param graph\r
- */\r
- public Computation<Graph> setGraph(Graph graph) {\r
- return setGraph(graph, "dot");\r
- }\r
-\r
- /**\r
- * Sets a new graph to be drawn. This operation may take a while. It can\r
- * be called from any thread. Automatically redraws the component.\r
- * @param graph\r
- */\r
- public Computation<Graph> setGraph(Graph graph, String algorithm) {\r
- Computation<Graph> computation = drawable.setGraph(graph, algorithm);\r
- computation.addContinuation(new Continuation<Graph>() {\r
- \r
- @Override\r
- public void succeeded(Graph result) {\r
- if (isDisposed())\r
- return;\r
- \r
- fit();\r
- }\r
- \r
- @Override\r
- public void failed(Exception exception) {\r
- }\r
- });\r
- return computation;\r
- }\r
-\r
- /**\r
- * Fits the content of the canvas to the component.\r
- * Can be called from any thread.\r
- */\r
- public void fit() {\r
- ((ViewerCanvas)canvas).fit();\r
- getDisplay().asyncExec(new Runnable() {\r
-\r
- @Override\r
- public void run() {\r
- redraw();\r
- }\r
-\r
- });\r
- SwingUtilities.invokeLater(new Runnable() {\r
-\r
- @Override\r
- public void run() {\r
- canvas.repaint();\r
- }\r
-\r
- });\r
-\r
-\r
- }\r
-\r
- public void requestFocus() {\r
- if(canvas != null)\r
- canvas.requestFocus();\r
- }\r
- \r
- public Frame getFrame() {\r
- return frame;\r
- }\r
- \r
- public Canvas getCanvas() {\r
- return canvas;\r
- }\r
- \r
- public GraphDrawable2 getDrawable() {\r
- return drawable;\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.graphviz.ui;
+
+import java.awt.Canvas;
+import java.awt.Frame;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import javax.swing.SwingUtilities;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.awt.SWT_AWT;
+import org.eclipse.swt.widgets.Composite;
+import org.simantics.graphviz.Graph;
+import org.simantics.graphviz.continuation.Computation;
+import org.simantics.graphviz.continuation.Continuation;
+import org.simantics.graphviz.drawable.GraphDrawable2;
+import org.simantics.graphviz.drawable.ViewerCanvas;
+
+public class GraphvizComponent2 extends Composite {
+
+ Canvas canvas;
+ GraphDrawable2 drawable;
+ Graph graph;
+ AtomicBoolean initialized = new AtomicBoolean(false);
+ Frame frame;
+
+ public GraphvizComponent2(Composite parent, int style) {
+ super(parent, style | SWT.EMBEDDED | SWT.NO_BACKGROUND);
+
+ frame = SWT_AWT.new_Frame(this);
+
+ SwingUtilities.invokeLater(new Runnable() {
+
+ @Override
+ public void run() {
+ try {
+ drawable = new GraphDrawable2();
+ canvas = new ViewerCanvas(drawable);
+ frame.add(canvas);
+ } finally {
+ synchronized (initialized) {
+ initialized.set(true);
+ initialized.notifyAll();
+ }
+ }
+ }
+
+ });
+ }
+
+ public void waitUntilInitialized() {
+ try {
+ synchronized (initialized) {
+ while (!initialized.get())
+ initialized.wait();
+ }
+ } catch (InterruptedException e) {
+ throw new Error("GraphvizComponent AWT population interrupted for class " + this, e);
+ }
+ }
+
+ /**
+ * Sets a new graph to be drawn. This operation may take a while. It can
+ * be called from any thread. Automatically redraws the component.
+ * @param graph
+ */
+ public Computation<Graph> setGraph(Graph graph) {
+ return setGraph(graph, "dot");
+ }
+
+ /**
+ * Sets a new graph to be drawn. This operation may take a while. It can
+ * be called from any thread. Automatically redraws the component.
+ * @param graph
+ */
+ public Computation<Graph> setGraph(Graph graph, String algorithm) {
+ Computation<Graph> computation = drawable.setGraph(graph, algorithm);
+ computation.addContinuation(new Continuation<Graph>() {
+
+ @Override
+ public void succeeded(Graph result) {
+ if (isDisposed())
+ return;
+
+ fit();
+ }
+
+ @Override
+ public void failed(Exception exception) {
+ }
+ });
+ return computation;
+ }
+
+ /**
+ * Fits the content of the canvas to the component.
+ * Can be called from any thread.
+ */
+ public void fit() {
+ ((ViewerCanvas)canvas).fit();
+ getDisplay().asyncExec(new Runnable() {
+
+ @Override
+ public void run() {
+ redraw();
+ }
+
+ });
+ SwingUtilities.invokeLater(new Runnable() {
+
+ @Override
+ public void run() {
+ canvas.repaint();
+ }
+
+ });
+
+
+ }
+
+ public void requestFocus() {
+ if(canvas != null)
+ canvas.requestFocus();
+ }
+
+ public Frame getFrame() {
+ return frame;
+ }
+
+ public Canvas getCanvas() {
+ return canvas;
+ }
+
+ public GraphDrawable2 getDrawable() {
+ return drawable;
+ }
+
+}