From 0541f9d898490b6c99ba3e9671a3138f19abc129 Mon Sep 17 00:00:00 2001 From: Matthew 'Apocist' Davis Date: Sat, 9 Aug 2014 03:47:20 -0400 Subject: [PATCH] Open/Save signatures to file --- .../BlockWindow.java | 76 ++++++++++++++++++- .../DynamicTree.java | 29 ++++++- 2 files changed, 102 insertions(+), 3 deletions(-) diff --git a/src/com/inverseinnovations/VisualMALSignatureDesigner/BlockWindow.java b/src/com/inverseinnovations/VisualMALSignatureDesigner/BlockWindow.java index 8c847e8..0d4688b 100644 --- a/src/com/inverseinnovations/VisualMALSignatureDesigner/BlockWindow.java +++ b/src/com/inverseinnovations/VisualMALSignatureDesigner/BlockWindow.java @@ -8,7 +8,11 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; +import java.io.File; +import java.io.IOException; import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Paths; import javax.swing.BorderFactory; import javax.swing.Box; @@ -16,12 +20,14 @@ import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; +import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; +import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTabbedPane; @@ -30,6 +36,9 @@ import javax.swing.ListSelectionModel; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; +import javax.swing.filechooser.FileFilter; +import javax.swing.filechooser.FileNameExtensionFilter; + import com.inverseinnovations.VisualMALSignatureDesigner.BuildingBlock.*; import com.inverseinnovations.VisualMALSignatureDesigner.BuildingBlock.Filter.*; @@ -59,7 +68,72 @@ public void run(){ JMenu menu = new JMenu("File");//Build the first menu. //a group of JMenuItems - JMenuItem menuItem = new JMenuItem("Generate Script"); + JMenuItem menuItem = new JMenuItem("Open"); + menuItem.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + final JFileChooser fc = new JFileChooser(System.getProperty("user.dir")); + FileFilter filter = new FileNameExtensionFilter("VisualMSD file", new String[] {"vmsd"}); + fc.addChoosableFileFilter(filter); + fc.setFileFilter(filter); + + File dir = new File(System.getProperty("user.dir")); + if(dir.isDirectory() && dir.canRead()){ + fc.setCurrentDirectory(dir); + int returnVal = fc.showOpenDialog(blockFrame); + if (returnVal == JFileChooser.APPROVE_OPTION) { + File file = fc.getSelectedFile(); + if(file.isFile()){ + //load file + BuildingBlock block = null; + try { + block = blocks.bytesToNode(Files.readAllBytes(Paths.get(file.getPath()))); + } catch (IOException e1) {JOptionPane.showMessageDialog(blockFrame, "ERROR: Cannot load file!");} + if(block != null){ + blocks.setRootNode(block); + } + } + } + } + } + }); + menu.add(menuItem); + //a group of JMenuItems + menuItem = new JMenuItem("Save"); + menuItem.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + final JFileChooser fc = new JFileChooser(System.getProperty("user.dir") + "sig.vmsd"); + FileFilter filter = new FileNameExtensionFilter("VisualMSD file", new String[] {"vmsd"}); + fc.setSelectedFile(new File("sig.vmsd")); + fc.addChoosableFileFilter(filter); + fc.setFileFilter(filter); + + File dir = new File(System.getProperty("user.dir")); + if(dir.isDirectory() && dir.canWrite()){ + fc.setCurrentDirectory(dir); + int returnVal = fc.showSaveDialog(blockFrame); + if (returnVal == JFileChooser.APPROVE_OPTION) { + File file = fc.getSelectedFile(); + BuildingBlock block = blocks.getRootNode(); + if(file.exists()){ + if(file.canWrite()){ + try { + Files.write(Paths.get(file.getPath()), blocks.nodeToBytes(block)); + JOptionPane.showMessageDialog(blockFrame, "Saved"); + } catch (IOException e1) {JOptionPane.showMessageDialog(blockFrame, "ERROR: Cannot save file!");} + }else{JOptionPane.showMessageDialog(blockFrame, "File is not rewritable");} + } + else{ + try { + Files.write(Paths.get(file.getPath()), blocks.nodeToBytes(block)); + JOptionPane.showMessageDialog(blockFrame, "Saved"); + } catch (IOException e1) {JOptionPane.showMessageDialog(blockFrame, "ERROR: Cannot save file!");} + } + } + }else{JOptionPane.showMessageDialog(blockFrame, "Dir is not writable");} + } + }); + menu.add(menuItem); + menuItem = new JMenuItem("Generate Script"); menuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ final JDialog d = new JDialog(blockFrame, "MSD Script", true); diff --git a/src/com/inverseinnovations/VisualMALSignatureDesigner/DynamicTree.java b/src/com/inverseinnovations/VisualMALSignatureDesigner/DynamicTree.java index fe7c126..f67907d 100644 --- a/src/com/inverseinnovations/VisualMALSignatureDesigner/DynamicTree.java +++ b/src/com/inverseinnovations/VisualMALSignatureDesigner/DynamicTree.java @@ -214,8 +214,15 @@ private void reExpandPaths(List expPaths){ } public BuildingBlock cloneNode(BuildingBlock node){ - ByteArrayOutputStream bos = new ByteArrayOutputStream(); BuildingBlock theReturn = null; + + byte[] bytes = nodeToBytes(node); + theReturn = bytesToNode(bytes); + return theReturn; + } + + public byte[] nodeToBytes(BuildingBlock node){ + ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(bos); @@ -229,7 +236,11 @@ public BuildingBlock cloneNode(BuildingBlock node){ } catch (IOException e) {} } - byte[] bytes = bos.toByteArray(); + return bos.toByteArray(); + } + + public BuildingBlock bytesToNode(byte[] bytes){ + BuildingBlock theReturn = null; ObjectInputStream ois; try { ois = new ObjectInputStream(new ByteArrayInputStream(bytes)); @@ -240,6 +251,20 @@ public BuildingBlock cloneNode(BuildingBlock node){ return theReturn; } + public void setRootNode(BuildingBlock node){ + if(node != null){ + if(node instanceof InitSignature){ + rootNode = node; + treeModel = new DefaultTreeModel(rootNode); + tree.setModel(treeModel); + rootNode.reinit(Main); + reload(); + Main.ImageWindow.update(); + } + else{System.out.println("Not a root node");} + } + } + class MyTreeModelListener implements TreeModelListener { public void treeNodesChanged(TreeModelEvent e) { DefaultMutableTreeNode node;