From 71577067a4470a88e72886916a8cb2a55c46ddb3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Fri, 2 Jun 2017 21:13:11 +0300 Subject: [PATCH] (refs #7272) Support updating named Graphviz windows Change-Id: I72080ec1cba333cf9639a9801814e5fc100a8309 --- .../scl/Visualization/GGraph.scl | 3 ++ .../scl/Visualization/Graphviz.scl | 5 ++- .../src/org/simantics/graphviz/Graphs.java | 9 +++++ .../simantics/graphviz/drawable/JViewer.java | 37 ++++++++++++++++++- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/bundles/org.simantics.graphviz/scl/Visualization/GGraph.scl b/bundles/org.simantics.graphviz/scl/Visualization/GGraph.scl index 58bdc41f2..3179ab74d 100644 --- a/bundles/org.simantics.graphviz/scl/Visualization/GGraph.scl +++ b/bundles/org.simantics.graphviz/scl/Visualization/GGraph.scl @@ -30,3 +30,6 @@ showGGraph (GGraph graph _ _) = showGraph graph showGGraphWithAlgorithm :: String -> GGraph a e -> () showGGraphWithAlgorithm algorithm (GGraph graph _ _) = showGraphWithAlgorithm graph algorithm + +showGGraphWithNamedWindow :: String -> String -> GGraph a e -> () +showGGraphWithNamedWindow windowName algorithm (GGraph graph _ _) = showGraphWithNamedWindow windowName graph algorithm diff --git a/bundles/org.simantics.graphviz/scl/Visualization/Graphviz.scl b/bundles/org.simantics.graphviz/scl/Visualization/Graphviz.scl index 47fa99b69..85db2d7cb 100644 --- a/bundles/org.simantics.graphviz/scl/Visualization/Graphviz.scl +++ b/bundles/org.simantics.graphviz/scl/Visualization/Graphviz.scl @@ -8,4 +8,7 @@ importJava "org.simantics.graphviz.Graphs" where showGraph :: Graph -> () @JavaName show - showGraphWithAlgorithm :: Graph -> String -> () \ No newline at end of file + showGraphWithAlgorithm :: Graph -> String -> () + + @JavaName showWithNamedWindow + showGraphWithNamedWindow :: String -> Graph -> String -> () \ No newline at end of file diff --git a/bundles/org.simantics.graphviz/src/org/simantics/graphviz/Graphs.java b/bundles/org.simantics.graphviz/src/org/simantics/graphviz/Graphs.java index b0b82f9e8..330aec503 100644 --- a/bundles/org.simantics.graphviz/src/org/simantics/graphviz/Graphs.java +++ b/bundles/org.simantics.graphviz/src/org/simantics/graphviz/Graphs.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.util.Arrays; +import java.util.HashMap; import javax.swing.SwingUtilities; @@ -146,4 +147,12 @@ public class Graphs { show(graph, "dot"); } + public static void showWithNamedWindow(final String windowName, final Graph graph, final String algorithm) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + JViewer.getOrCreateViewer(windowName, new GraphDrawable(graph, algorithm)); + } + }); + } + } diff --git a/bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/JViewer.java b/bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/JViewer.java index 6f972d746..2543a7a66 100644 --- a/bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/JViewer.java +++ b/bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/JViewer.java @@ -33,6 +33,7 @@ import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.io.File; import java.io.IOException; +import java.util.HashMap; import javax.swing.AbstractAction; import javax.swing.JFileChooser; @@ -41,6 +42,7 @@ import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; +import javax.swing.SwingUtilities; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileView; @@ -51,12 +53,16 @@ import org.simantics.graphviz.Graphs; * A stand alone frame that shows and makes it * possible to navigate a given Drawable. * - * @author Hannu Niemistö + * @author Hannu Niemist� */ public class JViewer extends JFrame { + private static final HashMap VIEWER_MAP = new HashMap(); + private static final long serialVersionUID = 3022682167091092801L; Drawable drawable; ViewerCanvas canvas = new ViewerCanvas(); + + private String windowName; class ViewerCanvas extends Canvas { private static final long serialVersionUID = -5330090168115281754L; @@ -205,6 +211,12 @@ public class JViewer extends JFrame { setVisible(true); } + public void updateDrawable(Drawable drawable) { + this.drawable = drawable; + canvas.fit(); + canvas.repaint(); + } + public class ExitAction extends AbstractAction { private static final long serialVersionUID = 5680421985865931443L; @@ -324,5 +336,26 @@ public class JViewer extends JFrame { return null; } } - + + public static void getOrCreateViewer(String windowName, GraphDrawable graphDrawable) { + JViewer viewer = VIEWER_MAP.get(windowName); + if(viewer == null) { + viewer = new JViewer(graphDrawable); + viewer.setTitle(windowName); + viewer.windowName = windowName; + VIEWER_MAP.put(windowName, viewer); + viewer.toFront(); + } + else { + viewer.updateDrawable(graphDrawable); + viewer.toFront(); + } + } + + @Override + public void dispose() { + if(windowName != null) + VIEWER_MAP.remove(windowName); + super.dispose(); + } } -- 2.43.2