3232// dealing with renaming
3333// before sketch save/rename, remove it from the recent list
3434// after sketch save/rename add it to the list
35- // need to do this whether or not
35+ // need to do this whether or not
3636
3737public class Recent {
38-
3938 static final String FILENAME = "recent.txt" ;
40- static final String VERSION = "1 " ;
41-
39+ static final String VERSION = "2 " ;
40+
4241 Base base ;
43-
4442 File file ;
4543 int count ;
4644 ArrayList <Record > records ;
45+ JMenu menu ;
4746
4847
4948 public Recent (Base base ) {
5049 this .base = base ;
5150 count = Preferences .getInteger ("recent.count" );
5251 file = Base .getSettingsFile (FILENAME );
52+ menu = new JMenu ("Recent Sketches" );
53+
5354 try {
5455 load ();
5556 } catch (IOException e ) {
5657 e .printStackTrace ();
5758 }
5859 }
59-
60-
60+
61+
6162 protected void load () throws IOException {
6263 records = new ArrayList <Record >();
6364 if (file .exists ()) {
@@ -66,12 +67,19 @@ protected void load() throws IOException {
6667 if (version != null && version .equals (VERSION )) {
6768 String line = null ;
6869 while ((line = reader .readLine ()) != null ) {
69- String [] pieces = PApplet .split (line , '\t' );
70- Record record = new Record (pieces [0 ], new EditorState (pieces [1 ]));
71- records .add (record );
70+ // String[] pieces = PApplet.split(line, '\t');
71+ // Record record = new Record(pieces[0], new EditorState(pieces[1]));
72+ // Record record = new Record(pieces[0]); //, new EditorState(pieces[1]));
73+ // records.add(record);
74+ if (new File (line ).exists ()) { // don't add ghost entries
75+ records .add (new Record (line ));
76+ } else {
77+ Base .log (this , "ghost file: " + line );
78+ }
7279 }
7380 }
7481 }
82+ updateMenu ();
7583 }
7684
7785
@@ -80,144 +88,203 @@ protected void save() {
8088 writer .println (VERSION );
8189 for (Record record : records ) {
8290// System.out.println(record.getPath() + "\t" + record.getState());
83- writer .println (record .getPath () + "\t " + record .getState ());
84- // if (record.sketch == null) {
85- // writer.println(record.path + "\t" + record.state);
86- // } else {
87- // writer.println(record.)
88- // }
91+ // writer.println(record.path + "\t" + record.getState());
92+ writer .println (record .path ); // + "\t" + record.getState());
8993 }
9094 writer .flush ();
9195 writer .close ();
9296// System.out.println();
97+ updateMenu ();
9398 }
94-
95-
96- public JMenu createMenu () {
97- JMenu menu = new JMenu ("Recent Sketches" );
98- updateMenu (menu );
99+
100+
101+ public JMenu getMenu () {
99102 return menu ;
100103 }
101-
102-
103- public void updateMenu (JMenu menu ) {
104+
105+
106+ private void updateMenu () {
104107 menu .removeAll ();
105108 for (final Record rec : records ) {
106109 JMenuItem item = new JMenuItem (rec .getName ());
107110 item .addActionListener (new ActionListener () {
108111 public void actionPerformed (ActionEvent e ) {
109- handleOpen (rec );
112+ // Base will call handle() (below) which will cause this entry to
113+ // be removed from the list and re-added to the end. If already
114+ // opened, Base will bring the window forward, and also call handle()
115+ // so that it's re-queued to the newest slot in the Recent menu.
116+ base .handleOpen (rec .path );
117+ // if (rec.sketch == null) {
118+ // // this will later call 'add' to put it back on the stack
119+ // base.handleOpen(rec.path); //, rec.state);
120+ //// int index = findRecord(rec);
121+ //// if (index != -1) {
122+ //// records.remove(index); // remove from the list
123+ //// save(); // write the recent file with the latest
124+ //// }
125+ // } else {
126+ //// System.out.println("sketch not null in handleOpen: " + record.getPath());
127+ // }
110128 }
111129 });
112130 menu .add (item );
113131 }
114132 }
115-
116-
117- /** Opened from the recent sketches menu. */
118- synchronized void handleOpen (Record record ) {
119- if (record .sketch == null ) {
120- // Editor editor = base.handleOpen(record.path, record.state);
121- base .handleOpen (record .path , record .state );
122- int index = findRecord (record );
123- if (index != -1 ) {
124- records .remove (index ); // remove from the list
125- // if (editor != null) {
126- // record.sketch = editor.getSketch();
127- // records.add(record); // move to the end of the line
128- // }
129- save (); // write the recent file with the latest
130- }
131- } else {
132- // System.out.println("sketch not null in handleOpen: " + record.getPath());
133- }
134- // otherwise the other handleOpen() will be called once it opens
135- }
136-
137-
138- /** Called by Base when a new sketch is opened. */
139- synchronized void handleOpen (Editor editor , JMenu menu ) {
140- // System.out.println("handleOpen editor " + editor.getSketch().getMainFilePath());
141- Record newRec = new Record (editor , editor .getSketch ());
142- // If this is already in the menu, remove it
143- int index = findRecord (newRec );
133+
134+
135+ synchronized void remove (Editor editor ) {
136+ int index = findRecord (editor .getSketch ().getMainFilePath ());
144137 if (index != -1 ) {
145138 records .remove (index );
146139 }
147- if (records .size () == count ) {
148- records .remove (0 ); // remove the first entry
149- }
150- records .add (newRec );
151- updateMenu (menu );
152- save ();
153140 }
154-
155-
156- int findRecord (Record rec ) {
157- int index = records .indexOf (rec );
158- if (index != -1 ) {
159- return index ;
141+
142+
143+ // synchronized void handleRename(String oldPath, String newPath) {
144+ // int index = findRecord(oldPath);
145+ // if (index != -1) {
146+ // Record rec = records.get(index);
147+ // rec.path = newPath;
148+ // save();
149+ // } else {
150+ // Base.log(this, "Could not find " + oldPath +
151+ // " in list of recent sketches to replace with " + newPath);
152+ // }
153+ // }
154+
155+
156+ // /** Opened from the recent sketches menu. */
157+ // synchronized void handleOpen(Record record) {
158+ // if (record.sketch == null) {
159+ //// Editor editor = base.handleOpen(record.path, record.state);
160+ // base.handleOpen(record.path, record.state);
161+ // int index = findRecord(record);
162+ // if (index != -1) {
163+ // records.remove(index); // remove from the list
164+ //// if (editor != null) {
165+ //// record.sketch = editor.getSketch();
166+ //// records.add(record); // move to the end of the line
167+ //// }
168+ // save(); // write the recent file with the latest
169+ // }
170+ // } else {
171+ //// System.out.println("sketch not null in handleOpen: " + record.getPath());
172+ // }
173+ // // otherwise the other handleOpen() will be called once it opens
174+ // }
175+
176+
177+ /**
178+ * Called by Base when a new sketch is opened, to add the sketch to the last
179+ * entry on the Recent queue. If the sketch is already in the list, it is
180+ * first removed so it doesn't show up multiple times.
181+ */
182+ synchronized void handle (Editor editor ) {
183+ if (!editor .getSketch ().isUntitled ()) {
184+ // If this is already in the menu, remove it
185+ remove (editor );
186+
187+ if (records .size () == count ) {
188+ records .remove (0 ); // remove the first entry
189+ }
190+
191+ // Record newRec = new Record(editor, editor.getSketch());
192+ // records.add(newRec);
193+ records .add (new Record (editor ));
194+ // updateMenu();
195+ save ();
160196 }
161- index = 0 ;
162- for (Record r : records ) {
163- // System.out.println("checking " + r + "\n against " + rec);
164- if (r .equals (rec )) {
165- return index ;
197+ }
198+
199+
200+ int findRecord (String path ) {
201+ for (int i = 0 ; i < records .size (); i ++) {
202+ if (path .equals (records .get (i ).path )) {
203+ return i ;
166204 }
167- index ++;
168205 }
169206 return -1 ;
170207 }
171-
172-
208+
209+
210+ // int findRecord(Record rec) {
211+ // int index = records.indexOf(rec);
212+ // if (index != -1) {
213+ // return index;
214+ // }
215+ // return findRecord(rec.path);
216+ //// index = 0;
217+ //// for (Record r : records) {
218+ //// System.out.println("checking " + r + "\n against " + rec);
219+ //// if (r.equals(rec)) {
220+ //// return index;
221+ //// }
222+ //// index++;
223+ //// }
224+ //// return -1;
225+ // }
226+
227+
173228 class Record {
174229 String path ; // if not loaded, this is non-null
175- EditorState state ; // if not loaded, this is non-null
230+ // EditorState state; // if not loaded, this is non-null
176231
177232 /**
178- * If currently loaded, this is non-null, and takes precedence over the
233+ * If currently loaded, this is non-null, and takes precedence over the
179234 * path and state information, which will instead be stored actively by
180235 * the actual sketch object.
181236 */
182- Sketch sketch ;
183237 Editor editor ;
238+ // Sketch sketch;
184239
185- Record (String path , EditorState state ) {
240+ // Record(String path, EditorState state) {
241+ // this.path = path;
242+ // this.state = state;
243+ // }
244+
245+ Record (String path ) {
186246 this .path = path ;
187- this .state = state ;
188247 }
189-
190- Record (Editor editor , Sketch sketch ) {
248+
249+ // Record(Editor editor, Sketch sketch) {
250+ // this.editor = editor;
251+ // this.sketch = sketch;
252+ // }
253+
254+ Record (Editor editor ) {
191255 this .editor = editor ;
192- this .sketch = sketch ;
256+ this .path = editor . getSketch (). getMainFilePath () ;
193257 }
194258
195259 String getName () {
196- if (sketch != null ) {
197- return sketch .getName ();
260+ if (editor != null ) {
261+ return editor . getSketch () .getName ();
198262 }
199263 // Get the filename of the .pde (or .js or .py...)
200264 String name = path .substring (path .lastIndexOf (File .separatorChar ) + 1 );
201265 // Return the name with the extension removed
202266 return name .substring (0 , name .indexOf ('.' ));
203267 }
204268
205- String getPath () {
206- if (sketch != null ) {
207- return sketch .getMainFilePath ();
208- } else {
209- return path ;
210- }
211- }
269+ // String getPath() {
270+ // if (sketch != null) {
271+ // return sketch.getMainFilePath();
272+ // } else {
273+ // return path;
274+ // }
275+ // }
212276
213- EditorState getState () {
214- //return sketch != null ? sketch.getEditor() state;
215- return sketch == null ? state : editor .getEditorState ();
216- }
217-
218- public boolean equals (Object o ) {
219- Record r = (Record ) o ;
220- return getPath ().equals (r .getPath ());
221- }
277+ // EditorState getState() {
278+ //// return sketch == null ? state : editor.getEditorState();
279+ // if (editor != null) { // update state if editor is open
280+ // state = editor.getEditorState();
281+ // }
282+ // return state;
283+ // }
284+
285+ // public boolean equals(Object o) {
286+ // Record r = (Record) o;
287+ // return getPath().equals(r.getPath());
288+ // }
222289 }
223290}
0 commit comments