-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathRecent.java
More file actions
411 lines (349 loc) · 12.9 KB
/
Recent.java
File metadata and controls
411 lines (349 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-19 The Processing Foundation
Copyright (c) 2011-12 Ben Fry and Casey Reas
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.app.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import processing.app.Base;
import processing.app.Language;
import processing.app.Library;
import processing.app.Messages;
import processing.app.Mode;
import processing.app.Preferences;
import processing.core.PApplet;
// TODO this isn't pretty... probably better to do an internal instance fancy thing
// dealing with renaming
// before sketch save/rename, remove it from the recent list
// after sketch save/rename add it to the list
// (this is the more straightforward model, otherwise has lots of weird edge cases)
public class Recent {
static final String FILENAME = "recent.txt";
static final String VERSION = "2";
static Base base;
static File file;
static List<Record> records;
/** actual menu used in the primary menu bar */
static JMenu mainMenu;
/** copy of the menu to use in the toolbar */
static JMenu toolbarMenu;
static public void init(Base b) {
base = b;
file = Base.getSettingsFile(FILENAME);
mainMenu = new JMenu(Language.text("menu.file.recent"));
toolbarMenu = new JMenu(Language.text("menu.file.open"));
try {
load();
} catch (IOException e) {
e.printStackTrace();
}
}
static protected void load() throws IOException {
records = new ArrayList<>();
if (file.exists()) {
BufferedReader reader = PApplet.createReader(file);
String version = reader.readLine();
if (version != null && version.equals(VERSION)) {
String line = null;
while ((line = reader.readLine()) != null) {
// String[] pieces = PApplet.split(line, '\t');
// Record record = new Record(pieces[0], new EditorState(pieces[1]));
// Record record = new Record(pieces[0]); //, new EditorState(pieces[1]));
// records.add(record);
if (new File(line).exists()) { // don't add ghost entries
records.add(new Record(line));
} else {
Messages.log("ghost file: " + line);
}
}
}
reader.close();
}
updateMenu(mainMenu);
updateMenu(toolbarMenu);
}
static protected void save() {
file.setWritable(true, false);
PrintWriter writer = PApplet.createWriter(file);
writer.println(VERSION);
for (Record record : records) {
// System.out.println(record.getPath() + "\t" + record.getState());
// writer.println(record.path + "\t" + record.getState());
writer.println(record.path); // + "\t" + record.getState());
}
writer.flush();
writer.close();
updateMenu(mainMenu);
updateMenu(toolbarMenu);
}
static public JMenu getMenu() {
return mainMenu;
}
static public JMenu getToolbarMenu() {
return toolbarMenu;
}
static private void updateMenu(JMenu menu) {
menu.removeAll();
String sketchbookPath = Base.getSketchbookFolder().getAbsolutePath();
for (Record rec : records) {
updateMenuRecord(menu, rec, sketchbookPath);
}
}
static private void updateMenuRecord(JMenu menu, final Record rec,
String sketchbookPath) {
try {
String recPath = new File(rec.getPath()).getParent();
String purtyPath = null;
if (recPath.startsWith(sketchbookPath)) {
purtyPath = "sketchbook \u2192 " +
recPath.substring(sketchbookPath.length() + 1);
} else {
List<Mode> modes = base.getModeList();
for (Mode mode : modes) {
File examplesFolder = mode.getExamplesFolder();
String examplesPath = examplesFolder.getAbsolutePath();
if (recPath.startsWith(examplesPath)) {
String modePrefix = mode.getTitle() + " ";
if (mode.getTitle().equals("Standard")) {
modePrefix = ""; // "Standard examples" is dorky
}
purtyPath = modePrefix + "examples \u2192 " +
recPath.substring(examplesPath.length() + 1);
break;
}
if (mode.coreLibraries != null) {
for (Library lib : mode.coreLibraries) {
examplesFolder = lib.getExamplesFolder();
examplesPath = examplesFolder.getAbsolutePath();
if (recPath.startsWith(examplesPath)) {
purtyPath = lib.getName() + " examples \u2192 " +
recPath.substring(examplesPath.length() + 1);
break;
}
}
}
if (mode.contribLibraries != null) {
for (Library lib : mode.contribLibraries) {
examplesFolder = lib.getExamplesFolder();
examplesPath = examplesFolder.getAbsolutePath();
if (recPath.startsWith(examplesPath)) {
purtyPath = lib.getName() + " examples \u2192 " +
recPath.substring(examplesPath.length() + 1);
break;
}
}
}
}
}
if (purtyPath == null) {
String homePath = System.getProperty("user.home");
if (recPath.startsWith(homePath)) {
// Not localized, but this is gravy. It'll work on OS X & EN Windows
String desktopPath = homePath + File.separator + "Desktop";
if (recPath.startsWith(desktopPath)) {
purtyPath = "Desktop \u2192 " + recPath.substring(desktopPath.length() + 1);
} else {
//purtyPath = "\u2302 \u2192 " + recPath.substring(homePath.length() + 1);
//purtyPath = "Home \u2192 " + recPath.substring(homePath.length() + 1);
String userName = new File(homePath).getName();
//purtyPath = "\u2302 " + userName + " \u2192 " + recPath.substring(homePath.length() + 1);
purtyPath = userName + " \u2192 " + recPath.substring(homePath.length() + 1);
}
} else {
purtyPath = recPath;
}
}
// JMenuItem item = new JMenuItem(rec.getName() + " | " + purtyPath);
JMenuItem item = new JMenuItem(purtyPath);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Base will call handle() (below) which will cause this entry to
// be removed from the list and re-added to the end. If already
// opened, Base will bring the window forward, and also call handle()
// so that it's re-queued to the newest slot in the Recent menu.
base.handleOpen(rec.path);
// if (rec.sketch == null) {
// // this will later call 'add' to put it back on the stack
// base.handleOpen(rec.path); //, rec.state);
//// int index = findRecord(rec);
//// if (index != -1) {
//// records.remove(index); // remove from the list
//// save(); // write the recent file with the latest
//// }
// } else {
//// System.out.println("sketch not null in handleOpen: " + record.getPath());
// }
}
});
//menu.add(item);
menu.insert(item, 0);
} catch (Exception e) {
// Strange things can happen... report them for the geeky and move on:
// https://github.com/processing/processing/issues/2463
e.printStackTrace();
}
}
synchronized static public void remove(Editor editor) {
int index = findRecord(editor.getSketch().getMainFilePath());
if (index != -1) {
System.out.println("removing " + editor.getSketch().getMainFilePath());
new Exception().printStackTrace(System.out);
records.remove(index);
}
}
// synchronized void handleRename(String oldPath, String newPath) {
// int index = findRecord(oldPath);
// if (index != -1) {
// Record rec = records.get(index);
// rec.path = newPath;
// save();
// } else {
// Base.log(this, "Could not find " + oldPath +
// " in list of recent sketches to replace with " + newPath);
// }
// }
// /** Opened from the recent sketches menu. */
// synchronized void handleOpen(Record record) {
// if (record.sketch == null) {
//// Editor editor = base.handleOpen(record.path, record.state);
// base.handleOpen(record.path, record.state);
// int index = findRecord(record);
// if (index != -1) {
// records.remove(index); // remove from the list
//// if (editor != null) {
//// record.sketch = editor.getSketch();
//// records.add(record); // move to the end of the line
//// }
// save(); // write the recent file with the latest
// }
// } else {
//// System.out.println("sketch not null in handleOpen: " + record.getPath());
// }
// // otherwise the other handleOpen() will be called once it opens
// }
/**
* Called by Base when a new sketch is opened, to add the sketch to the last
* entry on the Recent queue. If the sketch is already in the list, it is
* first removed so it doesn't show up multiple times.
*/
synchronized static public void append(Editor editor) {
if (!editor.getSketch().isUntitled()) {
// If this sketch is already in the menu, remove it
remove(editor);
// If the list is full, remove the first entry
if (records.size() == Preferences.getInteger("recent.count")) {
records.remove(0); // remove the first entry
}
// new Exception("adding to recent: " + editor.getSketch().getMainFilePath()).printStackTrace(System.out);
// Record newRec = new Record(editor, editor.getSketch());
// records.add(newRec);
records.add(new Record(editor));
// updateMenu();
save();
// } else {
// new Exception("NOT adding to recent: " + editor.getSketch().getMainFilePath()).printStackTrace(System.out);
}
}
synchronized static public void rename(Editor editor, String oldPath) {
if (records.size() == Preferences.getInteger("recent.count")) {
records.remove(0); // remove the first entry
}
int index = findRecord(oldPath);
//check if record exists
if (index != -1) {
records.remove(index);
}
records.add(new Record(editor));
save();
}
static int findRecord(String path) {
for (int i = 0; i < records.size(); i++) {
if (path.equals(records.get(i).path)) {
return i;
}
}
return -1;
}
// int findRecord(Record rec) {
// int index = records.indexOf(rec);
// if (index != -1) {
// return index;
// }
// return findRecord(rec.path);
//// index = 0;
//// for (Record r : records) {
//// System.out.println("checking " + r + "\n against " + rec);
//// if (r.equals(rec)) {
//// return index;
//// }
//// index++;
//// }
//// return -1;
// }
static class Record {
String path; // if not loaded, this is non-null
// EditorState state; // if not loaded, this is non-null
// Sketch sketch;
// Record(String path, EditorState state) {
// this.path = path;
// this.state = state;
// }
Record(String path) {
this.path = path;
}
// Record(Editor editor, Sketch sketch) {
// this.editor = editor;
// this.sketch = sketch;
// }
Record(Editor editor) {
this(editor.getSketch().getMainFilePath());
}
String getName() {
// Get the filename of the .pde (or .js or .py...)
String name = path.substring(path.lastIndexOf(File.separatorChar) + 1);
// Return the name with the extension removed
return name.substring(0, name.indexOf('.'));
}
String getPath() {
return path;
}
// String getPath() {
// if (sketch != null) {
// return sketch.getMainFilePath();
// } else {
// return path;
// }
// }
// EditorState getState() {
//// return sketch == null ? state : editor.getEditorState();
// if (editor != null) { // update state if editor is open
// state = editor.getEditorState();
// }
// return state;
// }
// public boolean equals(Object o) {
// Record r = (Record) o;
// return getPath().equals(r.getPath());
// }
}
}