]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graphviz.ui/src/org/simantics/graphviz/ui/GraphvizComponent.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.graphviz.ui / src / org / simantics / graphviz / ui / GraphvizComponent.java
index f46194c3a8f20850546b75fa4ab2cada7acef7ac..6ba556da09d65f1076bb4108e9e2fb35dcca190d 100644 (file)
-/*******************************************************************************\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.awt.event.WindowAdapter;\r
-import java.awt.event.WindowEvent;\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.lang.reflect.InvocationTargetException;\r
-import java.lang.reflect.Method;\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.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Shell;\r
-import org.simantics.graphviz.Graph;\r
-import org.simantics.graphviz.Graphs;\r
-import org.simantics.graphviz.continuation.Computation;\r
-import org.simantics.graphviz.continuation.Continuation;\r
-import org.simantics.graphviz.drawable.GraphDrawable;\r
-import org.simantics.graphviz.drawable.ViewerCanvas;\r
-import org.simantics.utils.ui.SWTUtils;\r
-\r
-public class GraphvizComponent extends Composite {\r
-\r
-    Canvas canvas;\r
-    GraphDrawable drawable;\r
-    Graph graph;\r
-    AtomicBoolean initialized = new AtomicBoolean(false);\r
-\r
-    public GraphvizComponent(Composite parent, int style) {\r
-        super(parent, style | SWT.EMBEDDED | SWT.NO_BACKGROUND);\r
-\r
-        final Frame frame = SWT_AWT.new_Frame(this);\r
-        workaroundJava7FocusProblem(frame);\r
-        \r
-        SwingUtilities.invokeLater(new Runnable() {\r
-\r
-            @Override\r
-            public void run() {\r
-                try {\r
-                    drawable = new GraphDrawable();\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
-    private void workaroundJava7FocusProblem(Frame frame) {\r
-        String ver = System.getProperty("java.version");\r
-        if (ver.startsWith("1.7") || ver.startsWith("1.8")) {\r
-            try {\r
-                frame.addWindowListener(new Java7FocusFixListener(this, frame));\r
-            } catch (SecurityException e) {\r
-                e.printStackTrace();\r
-                //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));\r
-            } catch (NoSuchMethodException e) {\r
-                e.printStackTrace();\r
-                //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));\r
-            }\r
-        }\r
-    }\r
-\r
-    static class Java7FocusFixListener extends WindowAdapter {\r
-\r
-        Method shellSetActiveControl;\r
-        Control control;\r
-        Frame frame;\r
-\r
-        public Java7FocusFixListener(Control control, Frame frame) throws NoSuchMethodException, SecurityException {\r
-            this.shellSetActiveControl = Shell.class.getDeclaredMethod("setActiveControl", Control.class);\r
-            this.frame = frame;\r
-            this.control = control;\r
-        }\r
-\r
-        @Override\r
-        public void windowActivated(WindowEvent e) {\r
-            SWTUtils.asyncExec(control, new Runnable() {\r
-                @Override\r
-                public void run() {\r
-                    if (control.isDisposed())\r
-                        return;\r
-                    if (control.getDisplay().getFocusControl() == control) {\r
-                        try {\r
-                            boolean accessible = shellSetActiveControl.isAccessible();\r
-                            if (!accessible)\r
-                                shellSetActiveControl.setAccessible(true);\r
-                            shellSetActiveControl.invoke(control.getShell(), control);\r
-                            if (!accessible)\r
-                                shellSetActiveControl.setAccessible(false);\r
-                        } catch (SecurityException e) {\r
-                            e.printStackTrace();\r
-                            //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));\r
-                        } catch (IllegalArgumentException e) {\r
-                            e.printStackTrace();\r
-                            //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));\r
-                        } catch (IllegalAccessException e) {\r
-                            e.printStackTrace();\r
-                            //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));\r
-                        } catch (InvocationTargetException e) {\r
-                            e.getCause().printStackTrace();\r
-                            //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));\r
-                        }\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 void setGraph(Graph graph) {\r
-        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
-        waitUntilInitialized();\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
-        });\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
-                if (!isDisposed())\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 void save(File file) throws IOException {\r
-       if (drawable == null) {\r
-                       throw new IOException("Nothing to save");\r
-               }\r
-               Graph graph = drawable.getGraph();\r
-               String algo = drawable.getAlgorithm();\r
-       \r
-               String type = getExtension(file);\r
-               if (type == null)\r
-                       return;\r
-               Graphs.createImage(graph, algo, type, file);\r
-    }\r
-    \r
-    public String[] getFileExtensions() {\r
-       return new String[]{"*.svg","*.dot","*.eps", "*.jpg", "*.jpeg","*.pdf","*.png","*.ps"};\r
-    }\r
-    \r
-    public String[] getFileNames() {\r
-       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"};\r
-    }\r
-    \r
-    public static String getExtension(File file) {\r
-               String filename = file.getName();\r
-               int index = filename.lastIndexOf(".");\r
-               if (index < 0)\r
-                       return null;\r
-               return filename.substring(index+1).toLowerCase();\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.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+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.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Shell;
+import org.simantics.graphviz.Graph;
+import org.simantics.graphviz.Graphs;
+import org.simantics.graphviz.continuation.Computation;
+import org.simantics.graphviz.continuation.Continuation;
+import org.simantics.graphviz.drawable.GraphDrawable;
+import org.simantics.graphviz.drawable.ViewerCanvas;
+import org.simantics.utils.ui.SWTUtils;
+
+public class GraphvizComponent extends Composite {
+
+    Canvas canvas;
+    GraphDrawable drawable;
+    Graph graph;
+    AtomicBoolean initialized = new AtomicBoolean(false);
+
+    public GraphvizComponent(Composite parent, int style) {
+        super(parent, style | SWT.EMBEDDED | SWT.NO_BACKGROUND);
+
+        final Frame frame = SWT_AWT.new_Frame(this);
+        workaroundJava7FocusProblem(frame);
+        
+        SwingUtilities.invokeLater(new Runnable() {
+
+            @Override
+            public void run() {
+                try {
+                    drawable = new GraphDrawable();
+                    canvas = new ViewerCanvas(drawable);
+                    frame.add(canvas);
+                } finally {
+                    synchronized (initialized) {
+                        initialized.set(true);
+                        initialized.notifyAll();
+                    }
+                }
+            }
+
+        });
+    }
+    
+    private void workaroundJava7FocusProblem(Frame frame) {
+        String ver = System.getProperty("java.version");
+        if (ver.startsWith("1.7") || ver.startsWith("1.8")) {
+            try {
+                frame.addWindowListener(new Java7FocusFixListener(this, frame));
+            } catch (SecurityException e) {
+                e.printStackTrace();
+                //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
+            } catch (NoSuchMethodException e) {
+                e.printStackTrace();
+                //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
+            }
+        }
+    }
+
+    static class Java7FocusFixListener extends WindowAdapter {
+
+        Method shellSetActiveControl;
+        Control control;
+        Frame frame;
+
+        public Java7FocusFixListener(Control control, Frame frame) throws NoSuchMethodException, SecurityException {
+            this.shellSetActiveControl = Shell.class.getDeclaredMethod("setActiveControl", Control.class);
+            this.frame = frame;
+            this.control = control;
+        }
+
+        @Override
+        public void windowActivated(WindowEvent e) {
+            SWTUtils.asyncExec(control, new Runnable() {
+                @Override
+                public void run() {
+                    if (control.isDisposed())
+                        return;
+                    if (control.getDisplay().getFocusControl() == control) {
+                        try {
+                            boolean accessible = shellSetActiveControl.isAccessible();
+                            if (!accessible)
+                                shellSetActiveControl.setAccessible(true);
+                            shellSetActiveControl.invoke(control.getShell(), control);
+                            if (!accessible)
+                                shellSetActiveControl.setAccessible(false);
+                        } catch (SecurityException e) {
+                            e.printStackTrace();
+                            //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
+                        } catch (IllegalArgumentException e) {
+                            e.printStackTrace();
+                            //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
+                        } catch (IllegalAccessException e) {
+                            e.printStackTrace();
+                            //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
+                        } catch (InvocationTargetException e) {
+                            e.getCause().printStackTrace();
+                            //Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
+                        }
+                    }
+                }
+            });
+        }
+
+    }
+
+    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 void setGraph(Graph graph) {
+        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) {
+        waitUntilInitialized();
+        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() {
+                if (!isDisposed())
+                    redraw();
+            }
+
+        });
+        SwingUtilities.invokeLater(new Runnable() {
+
+            @Override
+            public void run() {
+                canvas.repaint();
+            }
+
+        });
+
+
+    }
+
+    public void requestFocus() {
+        if(canvas != null)
+            canvas.requestFocus();
+    }
+    
+    public void save(File file) throws IOException {
+       if (drawable == null) {
+                       throw new IOException("Nothing to save");
+               }
+               Graph graph = drawable.getGraph();
+               String algo = drawable.getAlgorithm();
+       
+               String type = getExtension(file);
+               if (type == null)
+                       return;
+               Graphs.createImage(graph, algo, type, file);
+    }
+    
+    public String[] getFileExtensions() {
+       return new String[]{"*.svg","*.dot","*.eps", "*.jpg", "*.jpeg","*.pdf","*.png","*.ps"};
+    }
+    
+    public String[] getFileNames() {
+       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"};
+    }
+    
+    public static String getExtension(File file) {
+               String filename = file.getName();
+               int index = filename.lastIndexOf(".");
+               if (index < 0)
+                       return null;
+               return filename.substring(index+1).toLowerCase();
+       }
+
+}