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.awt.event.WindowAdapter;
17 import java.awt.event.WindowEvent;
19 import java.io.IOException;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 import java.util.concurrent.atomic.AtomicBoolean;
24 import javax.swing.SwingUtilities;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.awt.SWT_AWT;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Shell;
31 import org.simantics.graphviz.Graph;
32 import org.simantics.graphviz.Graphs;
33 import org.simantics.graphviz.continuation.Computation;
34 import org.simantics.graphviz.continuation.Continuation;
35 import org.simantics.graphviz.drawable.GraphDrawable;
36 import org.simantics.graphviz.drawable.ViewerCanvas;
37 import org.simantics.utils.ui.SWTUtils;
39 public class GraphvizComponent extends Composite {
42 GraphDrawable drawable;
44 AtomicBoolean initialized = new AtomicBoolean(false);
46 public GraphvizComponent(Composite parent, int style) {
47 super(parent, style | SWT.EMBEDDED | SWT.NO_BACKGROUND);
49 final Frame frame = SWT_AWT.new_Frame(this);
50 workaroundJava7FocusProblem(frame);
52 SwingUtilities.invokeLater(new Runnable() {
57 drawable = new GraphDrawable();
58 canvas = new ViewerCanvas(drawable);
61 synchronized (initialized) {
62 initialized.set(true);
63 initialized.notifyAll();
71 private void workaroundJava7FocusProblem(Frame frame) {
72 String ver = System.getProperty("java.version");
73 if (ver.startsWith("1.7") || ver.startsWith("1.8")) {
75 frame.addWindowListener(new Java7FocusFixListener(this, frame));
76 } catch (SecurityException e) {
78 //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
79 } catch (NoSuchMethodException e) {
81 //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
86 static class Java7FocusFixListener extends WindowAdapter {
88 Method shellSetActiveControl;
92 public Java7FocusFixListener(Control control, Frame frame) throws NoSuchMethodException, SecurityException {
93 this.shellSetActiveControl = Shell.class.getDeclaredMethod("setActiveControl", Control.class);
95 this.control = control;
99 public void windowActivated(WindowEvent e) {
100 SWTUtils.asyncExec(control, new Runnable() {
103 if (control.isDisposed())
105 if (control.getDisplay().getFocusControl() == control) {
107 boolean accessible = shellSetActiveControl.isAccessible();
109 shellSetActiveControl.setAccessible(true);
110 shellSetActiveControl.invoke(control.getShell(), control);
112 shellSetActiveControl.setAccessible(false);
113 } catch (SecurityException e) {
115 //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
116 } catch (IllegalArgumentException e) {
118 //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
119 } catch (IllegalAccessException e) {
121 //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
122 } catch (InvocationTargetException e) {
123 e.getCause().printStackTrace();
124 //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
133 public void waitUntilInitialized() {
135 synchronized (initialized) {
136 while (!initialized.get())
139 } catch (InterruptedException e) {
140 throw new Error("GraphvizComponent AWT population interrupted for class " + this, e);
145 * Sets a new graph to be drawn. This operation may take a while. It can
146 * be called from any thread. Automatically redraws the component.
149 public void setGraph(Graph graph) {
150 setGraph(graph, "dot");
154 * Sets a new graph to be drawn. This operation may take a while. It can
155 * be called from any thread. Automatically redraws the component.
158 public Computation<Graph> setGraph(Graph graph, String algorithm) {
159 waitUntilInitialized();
160 Computation<Graph> computation = drawable.setGraph(graph, algorithm);
161 computation.addContinuation(new Continuation<Graph>() {
164 public void succeeded(Graph result) {
172 public void failed(Exception exception) {
180 * Fits the content of the canvas to the component.
181 * Can be called from any thread.
184 ((ViewerCanvas)canvas).fit();
185 getDisplay().asyncExec(new Runnable() {
194 SwingUtilities.invokeLater(new Runnable() {
206 public void requestFocus() {
208 canvas.requestFocus();
211 public void save(File file) throws IOException {
212 if (drawable == null) {
213 throw new IOException("Nothing to save");
215 Graph graph = drawable.getGraph();
216 String algo = drawable.getAlgorithm();
218 String type = getExtension(file);
221 Graphs.createImage(graph, algo, type, file);
224 public String[] getFileExtensions() {
225 return new String[]{"*.svg","*.dot","*.eps", "*.jpg", "*.jpeg","*.pdf","*.png","*.ps"};
228 public String[] getFileNames() {
229 return new String[]{"Scalable Vector Graphics Image", "DOT Image", "Encapsulated PostScript Image","JPG Image","JPG Image","Portable Document Format Image","Portable Network Graphics Image","PostScript Image"};
232 public static String getExtension(File file) {
233 String filename = file.getName();
234 int index = filename.lastIndexOf(".");
237 return filename.substring(index+1).toLowerCase();