diff --git a/Gvisual/src/gvisual/ToolbarBuilder.java b/Gvisual/src/gvisual/ToolbarBuilder.java index 6220142..69a71e7 100644 --- a/Gvisual/src/gvisual/ToolbarBuilder.java +++ b/Gvisual/src/gvisual/ToolbarBuilder.java @@ -12,9 +12,6 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.util.List; -import java.util.function.Supplier; -import java.util.logging.Level; -import java.util.logging.Logger; import org.apache.commons.io.FileUtils; @@ -31,8 +28,6 @@ */ public final class ToolbarBuilder { - private static final Logger LOGGER = Logger.getLogger(ToolbarBuilder.class.getName()); - /** Callback interface for the host to supply live graph + Edge data. */ public interface GraphContext { /** Current graph (may be null before a file is loaded). */ @@ -111,71 +106,35 @@ private static void addModeButtons(JPanel panel, GraphContext ctx) { /* ---- Snapshot ---- */ private static void addSnapshotButton(JPanel panel, GraphContext ctx) { - JButton btn = new JButton( - "
Take a snapshot
Use this to
take and image
of the current view
of the graph
"); - btn.setPreferredSize(new Dimension(140, 100)); - btn.addActionListener(e -> { - JFileChooser fc; - int count = 0; - do { - if (count != 0) { - JOptionPane.showMessageDialog(null, - "File with same name already exists!!!", "Error!!", 1); - } - count++; - fc = new JFileChooser(System.getProperty("user.dir")); - fc.showSaveDialog(null); - } while (fc.getSelectedFile() != null && fc.getSelectedFile().exists()); - - if (fc.getSelectedFile() == null) return; - File curFile = new File(fc.getSelectedFile().toString() + ".png"); - try { - curFile.createNewFile(); - } catch (IOException ex) { - LOGGER.log(Level.SEVERE, null, ex); - } - PNGDump dumper = new PNGDump(); - try { - dumper.dumpComponent(curFile, ctx.getVisualizationViewer()); - } catch (IOException ex) { - LOGGER.log(Level.SEVERE, "Snapshot failed", ex); - } - }); - panel.add(btn); + ExportActions.addExportButton(panel, null, + "
Take a snapshot
Use this to
take an image
of the current view
of the graph
", + "Save Snapshot", + () -> "snapshot_" + ctx.getTimestamp() + ".png", + new String[]{".png"}, + outFile -> { + PNGDump dumper = new PNGDump(); + dumper.dumpComponent(outFile, ctx.getVisualizationViewer()); + return "Snapshot saved!\nFile: " + outFile.getName(); + }); } /* ---- Edge-list CSV export ---- */ private static void addEdgelistExportButton(JPanel panel) { - JButton btn = new JButton( - "
Export edgelist
Export the graph
Edge list in
CSV format.
"); - btn.setPreferredSize(new Dimension(140, 100)); - btn.addActionListener(e -> { - JFileChooser fc; - int count = 0; - do { - if (count != 0) { - JOptionPane.showMessageDialog(null, - "File with same name already exists!!!", "Error!!", 1); - } - count++; - fc = new JFileChooser(System.getProperty("user.dir")); - fc.showSaveDialog(null); - } while (fc.getSelectedFile() != null && fc.getSelectedFile().exists()); - - if (fc.getSelectedFile() == null) return; - try { - fc.getSelectedFile().createNewFile(); - } catch (IOException ex) { - LOGGER.log(Level.SEVERE, null, ex); - } - try { - FileUtils.copyFile(new File("./graph.txt"), fc.getSelectedFile()); - } catch (IOException ex) { - LOGGER.log(Level.SEVERE, null, ex); - } - }); - panel.add(btn); + ExportActions.addExportButton(panel, null, + "
Export edgelist
Export the graph
Edge list in
CSV format.
", + "Export Edge List", + () -> "edgelist.csv", + new String[]{".csv"}, + outFile -> { + File source = new File("./graph.txt"); + if (!source.exists()) { + throw new FileNotFoundException( + "Edge-list source file (graph.txt) not found in working directory."); + } + FileUtils.copyFile(source, outFile); + return "Edge list exported!\nFile: " + outFile.getName(); + }); } /* ---- GraphML export ---- */