Skip to content

Commit cee3f4c

Browse files
Merge pull request #28 from EdNutting/nram-edit
NRAM Edit window fixes
2 parents 8078119 + 7700a64 commit cee3f4c

File tree

1 file changed

+57
-24
lines changed

1 file changed

+57
-24
lines changed

src/com/modsim/gui/MemEdit.java

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class MemEdit {
2828

2929
private NRAM nram = null;
3030

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

158+
/**
159+
* Handles a save action to save the data
160+
* @param e The event that triggered the action
161+
* @param shouldClose Whether the window should close after a successful save
162+
*/
163+
private void saveActionPerformed(ActionEvent e, boolean shouldClose) {
164+
Preferences prefs = Preferences.userNodeForPackage(MemEdit.class);
165+
FileDialog fd = new FileDialog(frame, "Save Hex-encoded data", FileDialog.SAVE);
166+
fd.setFilenameFilter(hexFilter);
167+
// can just append .hex if the user doesn't
168+
if (Main.sim.filePath.isEmpty()) {
169+
fd.setFile("*.hex");
170+
} else {
171+
int ind = Main.sim.filePath.lastIndexOf('/');
172+
fd.setFile(Main.sim.filePath.substring(ind + 1));
173+
}
174+
175+
fd.setDirectory(prefs.get("hex_fileDir", ""));
176+
fd.setVisible(true);
177+
178+
if (fd.getFile() != null) {
179+
String path = fd.getDirectory() + fd.getFile();
180+
181+
// Is the file being created with the correct extension?
182+
if (!path.endsWith(".hex")) {
183+
path = path + ".hex";
184+
}
185+
186+
HexWriter.writeFile(new File(path), nram);
187+
188+
if (shouldClose) {
189+
close();
190+
}
191+
}
192+
}
193+
158194
/**
159195
* Fills the file menu
160196
*/
@@ -209,34 +245,31 @@ else if (res == JOptionPane.NO_OPTION) {
209245
file.add(menuItem);
210246

211247
menuItem = new JMenuItem("Save Data");
212-
menuItem.setMnemonic(KeyEvent.VK_O);
248+
menuItem.setMnemonic(KeyEvent.VK_S);
213249
menuItem.setToolTipText("Saves the current NRAM contents to a hex data file");
214250
menuItem.addActionListener(new ActionListener() {
215251
public void actionPerformed(ActionEvent e) {
216-
Preferences prefs = Preferences.userNodeForPackage(MemEdit.class);
217-
FileDialog fd = new FileDialog(frame, "Save Hex-encoded data", FileDialog.SAVE);
218-
fd.setFilenameFilter(hexFilter);
219-
// can just append .hex if the user doesn't
220-
if (Main.sim.filePath.isEmpty()) {
221-
fd.setFile("*.hex");
222-
} else {
223-
int ind = Main.sim.filePath.lastIndexOf('/');
224-
fd.setFile(Main.sim.filePath.substring(ind + 1));
225-
}
226-
227-
fd.setDirectory(prefs.get("hex_fileDir", ""));
228-
fd.setVisible(true);
229-
230-
if (fd.getFile() != null) {
231-
String path = fd.getDirectory() + fd.getFile();
252+
saveActionPerformed(e, false);
253+
}
254+
});
255+
file.add(menuItem);
232256

233-
// Is the file being created with the correct extension?
234-
if (!path.endsWith(".hex")) {
235-
path = path + ".hex";
236-
}
257+
menuItem = new JMenuItem("Close without save");
258+
menuItem.setMnemonic(KeyEvent.VK_X);
259+
menuItem.setToolTipText("Closes the window without saving NRAM contents");
260+
menuItem.addActionListener(new ActionListener() {
261+
public void actionPerformed(ActionEvent e) {
262+
close();
263+
}
264+
});
265+
file.add(menuItem);
237266

238-
HexWriter.writeFile(new File(path), nram);
239-
}
267+
menuItem = new JMenuItem("Close with save");
268+
menuItem.setMnemonic(KeyEvent.VK_D);
269+
menuItem.setToolTipText("Saves the current NRAM contents to a hex data file and closes the window");
270+
menuItem.addActionListener(new ActionListener() {
271+
public void actionPerformed(ActionEvent e) {
272+
saveActionPerformed(e, true);
240273
}
241274
});
242275
file.add(menuItem);

0 commit comments

Comments
 (0)