import java.io.InputStream;
import java.io.PrintStream;
import java.util.Arrays;
+import java.util.HashMap;
import javax.swing.SwingUtilities;
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));
+ }
+ });
+ }
+
}
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;
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;
* 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<String, JViewer> VIEWER_MAP = new HashMap<String, JViewer>();
+
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;
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;
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();
+ }
}