Skip to content

Commit

Permalink
Merge pull request #28 from EdNutting/nram-edit
Browse files Browse the repository at this point in the history
NRAM Edit window fixes
  • Loading branch information
samtherussell authored Oct 1, 2018
2 parents 8078119 + 7700a64 commit cee3f4c
Showing 1 changed file with 57 additions and 24 deletions.
81 changes: 57 additions & 24 deletions src/com/modsim/gui/MemEdit.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class MemEdit {

private NRAM nram = null;

public final JDialog frame = new JDialog(Main.ui.frame, "Memory Viewer");
public final JDialog frame = new JDialog(Main.ui.frame, "Memory Viewer", Dialog.ModalityType.MODELESS);
private final JMenuBar menu = new JMenuBar();
private final FilenameFilter hexFilter = new FilenameFilter() {
@Override
Expand Down Expand Up @@ -155,6 +155,42 @@ public void jumpTo(int adr) {
scroll.setValue(adr / memView.getCellsPerRow());
}

/**
* Handles a save action to save the data
* @param e The event that triggered the action
* @param shouldClose Whether the window should close after a successful save
*/
private void saveActionPerformed(ActionEvent e, boolean shouldClose) {
Preferences prefs = Preferences.userNodeForPackage(MemEdit.class);
FileDialog fd = new FileDialog(frame, "Save Hex-encoded data", FileDialog.SAVE);
fd.setFilenameFilter(hexFilter);
// can just append .hex if the user doesn't
if (Main.sim.filePath.isEmpty()) {
fd.setFile("*.hex");
} else {
int ind = Main.sim.filePath.lastIndexOf('/');
fd.setFile(Main.sim.filePath.substring(ind + 1));
}

fd.setDirectory(prefs.get("hex_fileDir", ""));
fd.setVisible(true);

if (fd.getFile() != null) {
String path = fd.getDirectory() + fd.getFile();

// Is the file being created with the correct extension?
if (!path.endsWith(".hex")) {
path = path + ".hex";
}

HexWriter.writeFile(new File(path), nram);

if (shouldClose) {
close();
}
}
}

/**
* Fills the file menu
*/
Expand Down Expand Up @@ -209,34 +245,31 @@ else if (res == JOptionPane.NO_OPTION) {
file.add(menuItem);

menuItem = new JMenuItem("Save Data");
menuItem.setMnemonic(KeyEvent.VK_O);
menuItem.setMnemonic(KeyEvent.VK_S);
menuItem.setToolTipText("Saves the current NRAM contents to a hex data file");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Preferences prefs = Preferences.userNodeForPackage(MemEdit.class);
FileDialog fd = new FileDialog(frame, "Save Hex-encoded data", FileDialog.SAVE);
fd.setFilenameFilter(hexFilter);
// can just append .hex if the user doesn't
if (Main.sim.filePath.isEmpty()) {
fd.setFile("*.hex");
} else {
int ind = Main.sim.filePath.lastIndexOf('/');
fd.setFile(Main.sim.filePath.substring(ind + 1));
}

fd.setDirectory(prefs.get("hex_fileDir", ""));
fd.setVisible(true);

if (fd.getFile() != null) {
String path = fd.getDirectory() + fd.getFile();
saveActionPerformed(e, false);
}
});
file.add(menuItem);

// Is the file being created with the correct extension?
if (!path.endsWith(".hex")) {
path = path + ".hex";
}
menuItem = new JMenuItem("Close without save");
menuItem.setMnemonic(KeyEvent.VK_X);
menuItem.setToolTipText("Closes the window without saving NRAM contents");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
close();
}
});
file.add(menuItem);

HexWriter.writeFile(new File(path), nram);
}
menuItem = new JMenuItem("Close with save");
menuItem.setMnemonic(KeyEvent.VK_D);
menuItem.setToolTipText("Saves the current NRAM contents to a hex data file and closes the window");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveActionPerformed(e, true);
}
});
file.add(menuItem);
Expand Down

0 comments on commit cee3f4c

Please sign in to comment.