From: Hannu Niemistö Date: Fri, 2 Jun 2017 18:13:11 +0000 (+0300) Subject: (refs #7272) Support updating named Graphviz windows X-Git-Tag: v1.31.0~340^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F93%2F593%2F2;p=simantics%2Fplatform.git (refs #7272) Support updating named Graphviz windows Change-Id: I72080ec1cba333cf9639a9801814e5fc100a8309 --- 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(); + } }