Skip to content

Commit

Permalink
Open/Save signatures to file
Browse files Browse the repository at this point in the history
  • Loading branch information
apocist committed Aug 9, 2014
1 parent ceb5321 commit 0541f9d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@
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;
import javax.swing.BoxLayout;
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;
Expand All @@ -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.*;

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,15 @@ private void reExpandPaths(List<TreePath> 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);
Expand All @@ -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));
Expand All @@ -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;
Expand Down

0 comments on commit 0541f9d

Please sign in to comment.