1
- package gui ;
1
+ package com . modsim . gui ;
2
2
3
- import java .awt .BorderLayout ;
3
+ import java .awt .* ;
4
4
import java .awt .event .ActionEvent ;
5
5
import java .awt .event .ActionListener ;
6
6
import java .awt .event .AdjustmentEvent ;
11
11
import java .awt .event .MouseAdapter ;
12
12
import java .awt .event .MouseWheelEvent ;
13
13
import java .io .File ;
14
+ import java .io .FilenameFilter ;
15
+ import java .util .prefs .Preferences ;
14
16
15
- import javax .swing .BoxLayout ;
16
- import javax .swing .JButton ;
17
- import javax .swing .JDialog ;
18
- import javax .swing .JFileChooser ;
19
- import javax .swing .JLabel ;
20
- import javax .swing .JMenu ;
21
- import javax .swing .JMenuBar ;
22
- import javax .swing .JMenuItem ;
23
- import javax .swing .JPanel ;
24
- import javax .swing .JScrollBar ;
25
- import javax .swing .JTextField ;
17
+ import javax .swing .*;
26
18
import javax .swing .filechooser .FileNameExtensionFilter ;
27
19
28
- import simulator .Main ;
29
- import util .BinData ;
30
- import util .HexReader ;
31
- import util .HexWriter ;
32
- import modules .NRAM ;
20
+ import com .modsim .Main ;
21
+ import com .modsim .util .BinData ;
22
+ import com .modsim .util .HexReader ;
23
+ import com .modsim .util .HexWriter ;
24
+ import com .modsim .modules .NRAM ;
25
+ import com .modsim .util .XMLReader ;
33
26
34
27
public class MemEdit {
35
28
36
- private NRAM targ = null ;
29
+ private NRAM nram = null ;
37
30
38
31
public final JDialog frame = new JDialog (Main .ui .frame , "Memory Viewer" );
39
32
private final JMenuBar menu = new JMenuBar ();
40
- private final JFileChooser filePick = new JFileChooser ();
41
- private final FileNameExtensionFilter hexFilter = new FileNameExtensionFilter ("Hex files" , "hex" );
33
+ private final FilenameFilter hexFilter = new FilenameFilter () {
34
+ @ Override
35
+ public boolean accept (File dir , String name ) {
36
+ return name .endsWith (".hex" );
37
+ }
38
+ };
42
39
private final JScrollBar scroll = new JScrollBar (JScrollBar .VERTICAL );
43
- private final JTextField jumpadr = new JTextField ();
40
+ private final JTextField jumpAdr = new JTextField ();
44
41
45
42
private final MemView memView ;
46
43
@@ -56,7 +53,7 @@ public MemEdit() {
56
53
JPanel adrStrip = new JPanel ();
57
54
adrStrip .setLayout (new BoxLayout (adrStrip , BoxLayout .LINE_AXIS ));
58
55
adrStrip .add (new JLabel (" Jump to: " ));
59
- adrStrip .add (jumpadr );
56
+ adrStrip .add (jumpAdr );
60
57
61
58
JButton goBtn = new JButton ("Go" );
62
59
JButton lastBtn = new JButton ("Last Changed" );
@@ -87,7 +84,7 @@ public void actionPerformed(ActionEvent e) {
87
84
int adr ;
88
85
89
86
try {
90
- adr = Integer .parseInt (jumpadr .getText (), 16 );
87
+ adr = Integer .parseInt (jumpAdr .getText (), 16 );
91
88
}
92
89
catch (NumberFormatException nfe ) {
93
90
return ; // Fail silently.
@@ -99,7 +96,7 @@ public void actionPerformed(ActionEvent e) {
99
96
}
100
97
};
101
98
102
- jumpadr .addActionListener (jumpListener );
99
+ jumpAdr .addActionListener (jumpListener );
103
100
goBtn .addActionListener (jumpListener );
104
101
105
102
lastBtn .addActionListener (new ActionListener () {
@@ -119,7 +116,7 @@ public void show(NRAM nram) {
119
116
frame .setSize (300 , 700 );
120
117
frame .setLocation (800 , 100 );
121
118
frame .setVisible (true );
122
- targ = nram ;
119
+ this . nram = nram ;
123
120
124
121
update ();
125
122
}
@@ -135,6 +132,7 @@ public void close() {
135
132
* Updates the view of the memory contents
136
133
*/
137
134
public void update () {
135
+ frame .setTitle ("NRAM " + nram .label );
138
136
memView .setUpdated (updAdr );
139
137
memView .repaint ();
140
138
}
@@ -145,7 +143,7 @@ public void update() {
145
143
* @return The stored byte
146
144
*/
147
145
public int getByte (int adr ) {
148
- BinData [] bits = targ .read (adr );
146
+ BinData [] bits = nram .read (adr );
149
147
return (bits [1 ].getUInt () << 4 ) | (bits [0 ].getUInt ());
150
148
}
151
149
@@ -169,12 +167,40 @@ private void addFileMenu() {
169
167
menuItem .setToolTipText ("Load a hex data file into the module (replaces the current contents)" );
170
168
menuItem .addActionListener (new ActionListener () {
171
169
public void actionPerformed (ActionEvent e ) {
172
- filePick .setFileFilter (hexFilter );
173
- int r = filePick .showOpenDialog (frame );
170
+ Preferences prefs = Preferences .userNodeForPackage (MemEdit .class );
171
+ FileDialog fd = new FileDialog (frame , "Load Hex-encoded data" , FileDialog .LOAD );
172
+
173
+ fd .setFilenameFilter (hexFilter );
174
+ fd .setFile ("*.hex" ); // FilenameFilter doesn't work on Windows
175
+
176
+ fd .setDirectory (prefs .get ("hex_fileDir" , "" ));
177
+ fd .setVisible (true );
178
+
179
+ if (fd .getFile () != null ) {
180
+ String path = fd .getDirectory () + fd .getFile ();
174
181
175
- if (r == JFileChooser .APPROVE_OPTION ) {
176
- File file = filePick .getSelectedFile ();
177
- HexReader .readFile (file , targ );
182
+ // Loop till we get a valid input
183
+ while (!path .endsWith (".hex" )) {
184
+ int res = JOptionPane .showConfirmDialog (frame , "Data load: Warning" ,
185
+ "That doesn't appear to be a hex file. Try and open anyway?" ,
186
+ JOptionPane .YES_NO_CANCEL_OPTION );
187
+
188
+ if (res == JOptionPane .YES_OPTION ) break ;
189
+ else if (res == JOptionPane .NO_OPTION ) {
190
+ fd .setFile ("*.hex" );
191
+ fd .setVisible (true );
192
+
193
+ if (fd .getFile () == null ) return ;
194
+ path = fd .getDirectory () + fd .getFile ();
195
+ }
196
+ else {
197
+ return ;
198
+ }
199
+ }
200
+
201
+ prefs .put ("hex_fileDir" , fd .getDirectory ());
202
+ File file = new File (path );
203
+ HexReader .readFile (file , nram );
178
204
updAdr = -1 ;
179
205
update ();
180
206
}
@@ -187,22 +213,29 @@ public void actionPerformed(ActionEvent e) {
187
213
menuItem .setToolTipText ("Saves the current NRAM contents to a hex data file" );
188
214
menuItem .addActionListener (new ActionListener () {
189
215
public void actionPerformed (ActionEvent e ) {
190
- filePick .setFileFilter (hexFilter );
191
- int r = filePick .showSaveDialog (frame );
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 );
192
229
193
- if (r == JFileChooser . APPROVE_OPTION ) {
194
- File file = filePick . getSelectedFile ();
230
+ if (fd . getFile () != null ) {
231
+ String path = fd . getDirectory () + fd . getFile ();
195
232
196
233
// Is the file being created with the correct extension?
197
- if (!file .getName ().endsWith (".hex" )) {
198
- File extFile = new File (file .getPath () + ".hex" );
199
- if (! extFile .exists ()) {
200
- // Rename the destination file if it doesn't exist.
201
- file = extFile ;
202
- }
234
+ if (!path .endsWith (".hex" )) {
235
+ path = path + ".hex" ;
203
236
}
204
237
205
- HexWriter .writeFile (file , targ );
238
+ HexWriter .writeFile (new File ( path ), nram );
206
239
}
207
240
}
208
241
});
0 commit comments