|
8 | 8 | import java.awt.event.ActionEvent; |
9 | 9 | import java.awt.event.ActionListener; |
10 | 10 | import java.awt.event.KeyEvent; |
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
11 | 13 | import java.io.PrintWriter; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Paths; |
12 | 16 |
|
13 | 17 | import javax.swing.BorderFactory; |
14 | 18 | import javax.swing.Box; |
15 | 19 | import javax.swing.BoxLayout; |
16 | 20 | import javax.swing.ImageIcon; |
17 | 21 | import javax.swing.JButton; |
18 | 22 | import javax.swing.JDialog; |
| 23 | +import javax.swing.JFileChooser; |
19 | 24 | import javax.swing.JFrame; |
20 | 25 | import javax.swing.JLabel; |
21 | 26 | import javax.swing.JList; |
22 | 27 | import javax.swing.JMenu; |
23 | 28 | import javax.swing.JMenuBar; |
24 | 29 | import javax.swing.JMenuItem; |
| 30 | +import javax.swing.JOptionPane; |
25 | 31 | import javax.swing.JPanel; |
26 | 32 | import javax.swing.JScrollPane; |
27 | 33 | import javax.swing.JTabbedPane; |
|
30 | 36 | import javax.swing.ListSelectionModel; |
31 | 37 | import javax.swing.SwingUtilities; |
32 | 38 | import javax.swing.WindowConstants; |
| 39 | +import javax.swing.filechooser.FileFilter; |
| 40 | +import javax.swing.filechooser.FileNameExtensionFilter; |
| 41 | + |
33 | 42 | import com.inverseinnovations.VisualMALSignatureDesigner.BuildingBlock.*; |
34 | 43 | import com.inverseinnovations.VisualMALSignatureDesigner.BuildingBlock.Filter.*; |
35 | 44 |
|
@@ -59,7 +68,72 @@ public void run(){ |
59 | 68 | JMenu menu = new JMenu("File");//Build the first menu. |
60 | 69 |
|
61 | 70 | //a group of JMenuItems |
62 | | - JMenuItem menuItem = new JMenuItem("Generate Script"); |
| 71 | + JMenuItem menuItem = new JMenuItem("Open"); |
| 72 | + menuItem.addActionListener(new ActionListener(){ |
| 73 | + public void actionPerformed(ActionEvent e){ |
| 74 | + final JFileChooser fc = new JFileChooser(System.getProperty("user.dir")); |
| 75 | + FileFilter filter = new FileNameExtensionFilter("VisualMSD file", new String[] {"vmsd"}); |
| 76 | + fc.addChoosableFileFilter(filter); |
| 77 | + fc.setFileFilter(filter); |
| 78 | + |
| 79 | + File dir = new File(System.getProperty("user.dir")); |
| 80 | + if(dir.isDirectory() && dir.canRead()){ |
| 81 | + fc.setCurrentDirectory(dir); |
| 82 | + int returnVal = fc.showOpenDialog(blockFrame); |
| 83 | + if (returnVal == JFileChooser.APPROVE_OPTION) { |
| 84 | + File file = fc.getSelectedFile(); |
| 85 | + if(file.isFile()){ |
| 86 | + //load file |
| 87 | + BuildingBlock block = null; |
| 88 | + try { |
| 89 | + block = blocks.bytesToNode(Files.readAllBytes(Paths.get(file.getPath()))); |
| 90 | + } catch (IOException e1) {JOptionPane.showMessageDialog(blockFrame, "ERROR: Cannot load file!");} |
| 91 | + if(block != null){ |
| 92 | + blocks.setRootNode(block); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + }); |
| 99 | + menu.add(menuItem); |
| 100 | + //a group of JMenuItems |
| 101 | + menuItem = new JMenuItem("Save"); |
| 102 | + menuItem.addActionListener(new ActionListener(){ |
| 103 | + public void actionPerformed(ActionEvent e){ |
| 104 | + final JFileChooser fc = new JFileChooser(System.getProperty("user.dir") + "sig.vmsd"); |
| 105 | + FileFilter filter = new FileNameExtensionFilter("VisualMSD file", new String[] {"vmsd"}); |
| 106 | + fc.setSelectedFile(new File("sig.vmsd")); |
| 107 | + fc.addChoosableFileFilter(filter); |
| 108 | + fc.setFileFilter(filter); |
| 109 | + |
| 110 | + File dir = new File(System.getProperty("user.dir")); |
| 111 | + if(dir.isDirectory() && dir.canWrite()){ |
| 112 | + fc.setCurrentDirectory(dir); |
| 113 | + int returnVal = fc.showSaveDialog(blockFrame); |
| 114 | + if (returnVal == JFileChooser.APPROVE_OPTION) { |
| 115 | + File file = fc.getSelectedFile(); |
| 116 | + BuildingBlock block = blocks.getRootNode(); |
| 117 | + if(file.exists()){ |
| 118 | + if(file.canWrite()){ |
| 119 | + try { |
| 120 | + Files.write(Paths.get(file.getPath()), blocks.nodeToBytes(block)); |
| 121 | + JOptionPane.showMessageDialog(blockFrame, "Saved"); |
| 122 | + } catch (IOException e1) {JOptionPane.showMessageDialog(blockFrame, "ERROR: Cannot save file!");} |
| 123 | + }else{JOptionPane.showMessageDialog(blockFrame, "File is not rewritable");} |
| 124 | + } |
| 125 | + else{ |
| 126 | + try { |
| 127 | + Files.write(Paths.get(file.getPath()), blocks.nodeToBytes(block)); |
| 128 | + JOptionPane.showMessageDialog(blockFrame, "Saved"); |
| 129 | + } catch (IOException e1) {JOptionPane.showMessageDialog(blockFrame, "ERROR: Cannot save file!");} |
| 130 | + } |
| 131 | + } |
| 132 | + }else{JOptionPane.showMessageDialog(blockFrame, "Dir is not writable");} |
| 133 | + } |
| 134 | + }); |
| 135 | + menu.add(menuItem); |
| 136 | + menuItem = new JMenuItem("Generate Script"); |
63 | 137 | menuItem.addActionListener(new ActionListener(){ |
64 | 138 | public void actionPerformed(ActionEvent e){ |
65 | 139 | final JDialog d = new JDialog(blockFrame, "MSD Script", true); |
|
0 commit comments