Skip to content

Commit

Permalink
Merge branch 'az/saveas'
Browse files Browse the repository at this point in the history
  • Loading branch information
aitjcize committed Aug 19, 2011
2 parents 2a95728 + d56568a commit 6ece38b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 27 deletions.
86 changes: 62 additions & 24 deletions src/org/zeroxlab/aster/AsterMainPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,40 @@ public void actionPerformed(ActionEvent ev) {
JMenuItem saveItem = new JMenuItem();
saveItem.setAction(new AbstractAction() {
public void actionPerformed(ActionEvent ev) {
try {
if (mCmdManager.getSaved()) {
try {
mCmdManager.dump(mActionList.getModel().toArray(),
mCmdManager.getFile().getAbsolutePath(),
true);
} catch (IOException e) {
e.printStackTrace();
}
} else {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(AsterMainPanel.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
mCmdManager.dump(mActionList.getModel().toArray(),
file.getAbsolutePath());
try {
mCmdManager.dump(mActionList.getModel().toArray(),
file.getAbsolutePath(), false);
} catch (IOException e) {
JOptionPane pane = new JOptionPane(
"File exists! Do you want to overwrite?");
Object[] options = new String[] { "Yes", "No" };
pane.setOptions(options);
JDialog dialog = pane.createDialog(new JFrame(), "Confirm");
dialog.show();
Object obj = pane.getValue();
if (options[0].equals(obj)) {
try {
mCmdManager.dump(mActionList.getModel().toArray(),
file.getAbsolutePath(), true);
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
} catch (IOException e) {
System.err.println(e.toString());
}
}
});
Expand All @@ -217,25 +241,39 @@ public void actionPerformed(ActionEvent ev) {
fileMenu.add(saveItem);

// Save As
//JMenuItem saveItem = new JMenuItem();
//saveItem.setAction(new AbstractAction() {
// public void actionPerformed(ActionEvent ev) {
// try {
// final JFileChooser fc = new JFileChooser();
// int returnVal = fc.showSaveDialog(AsterMainPanel.this);
// if (returnVal == JFileChooser.APPROVE_OPTION) {
// File file = fc.getSelectedFile();
// mCmdManager.dump(mActionList.getModel().toArray(),
// file.getAbsolutePath());
// }
// } catch (IOException e) {
// System.err.println(e.toString());
// }
// }
// });
//saveItem.setText("Save As...");
//saveItem.setMnemonic(KeyEvent.VK_S);
//fileMenu.add(saveItem);
JMenuItem saveAsItem = new JMenuItem();
saveAsItem.setAction(new AbstractAction() {
public void actionPerformed(ActionEvent ev) {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(AsterMainPanel.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
mCmdManager.dump(mActionList.getModel().toArray(),
file.getAbsolutePath(), false);
} catch (IOException e) {
JOptionPane pane = new JOptionPane(
"File exists! Do you want to overwrite?");
Object[] options = new String[] { "Yes", "No" };
pane.setOptions(options);
JDialog dialog = pane.createDialog(new JFrame(), "Confirm");
dialog.show();
Object obj = pane.getValue();
if (options[0].equals(obj)) {
try {
mCmdManager.dump(mActionList.getModel().toArray(),
file.getAbsolutePath(), true);
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
}
});
saveAsItem.setText("Save As...");
saveAsItem.setMnemonic(KeyEvent.VK_A);
fileMenu.add(saveAsItem);

// Recall
JMenuItem recallItem = new JMenuItem();
Expand Down
26 changes: 24 additions & 2 deletions src/org/zeroxlab/aster/cmds/AsterCommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@

public class AsterCommandManager {

private File mCwd;
private File mCwd = null;
private File mFile = null;
private static Stack<String> mPathStack = new Stack<String>();
private static ChimpChat mChimpChat;
private static IChimpDevice mImpl;
Expand All @@ -70,6 +71,14 @@ public void cdCwd() {
System.setProperty("user.dir", mCwd.getAbsolutePath());
}

public File getFile() {
return mFile;
}

public boolean getSaved() {
return mFile != null;
}

private void zipDir(File prefix, String dir, ZipOutputStream zos)
throws FileNotFoundException, IOException {
File dirfile = new File(dir);
Expand Down Expand Up @@ -224,7 +233,7 @@ public AsterCommand.ExecutionResult runLocal(String astfile)
return new AsterCommand.ExecutionResult(true, "");
}

public void dump(AsterCommand[] cmds, String filename)
public void dump(AsterCommand[] cmds, String filename, boolean overwrite)
throws IOException {
if (!filename.endsWith(".ast"))
filename += ".ast";
Expand All @@ -239,13 +248,23 @@ public void dump(AsterCommand[] cmds, String filename)
out.close();

try {
File outfile = new File(filename);
if (outfile.exists()) {
if (overwrite) {
outfile.delete();
} else {
throw new IOException(String.format("File `%s' exists", filename));
}
}
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(filename));
zipDir(root, root.getAbsolutePath(), zos);
zos.close();
deleteDir(root);
} catch(FileNotFoundException e) {
throw new IOException(e);
}

mFile = new File(filename);
}

public AsterCommand[] load(String zipfile)
Expand Down Expand Up @@ -291,7 +310,10 @@ public AsterCommand[] load(String zipfile)
System.out.println(e);
e.printStackTrace();
}

mFile = new File(zipfile);
AsterCommand[] cmd_array = new AsterCommand[cmds.size()];

return cmds.toArray(cmd_array);
}
}
2 changes: 1 addition & 1 deletion src/org/zeroxlab/aster/cmds/CmdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static public void main(String argv[]) {
}

try {
manager.dump(cmds, "cmds.ast");
manager.dump(cmds, "cmds.ast", true);
cmds = manager.load("cmds.ast");
for (AsterCommand c: cmds) {
System.out.printf("%s", c.toScript());
Expand Down

0 comments on commit 6ece38b

Please sign in to comment.