Skip to content

Commit 58e82a8

Browse files
committed
finishing template implementation
1 parent 1f1a46d commit 58e82a8

2 files changed

Lines changed: 46 additions & 23 deletions

File tree

app/src/processing/app/Mode.java

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -241,30 +241,48 @@ public File addTemplateFiles(File sketchFolder,
241241
File newbieFile =
242242
new File(sketchFolder, sketchName + "." + getDefaultExtension());
243243

244-
// First see if the user has overridden the default template
245-
File templateFolder = getUserTemplateFolder();
246-
if (!templateFolder.exists()) {
247-
// If not, see if the Mode has its own template
248-
templateFolder = getTemplateFolder();
249-
}
250-
if (templateFolder.exists()) {
251-
Util.copyDir(templateFolder, sketchFolder);
252-
File templateFile =
253-
new File(sketchFolder, "sketch." + getDefaultExtension());
254-
if (!templateFile.renameTo(newbieFile)) {
255-
throw new IOException("Error while assigning the sketch template.");
244+
try {
245+
// First see if the user has overridden the default template
246+
File templateFolder = checkSketchbookTemplate();
247+
248+
// Next see if the Mode has its own template
249+
if (templateFolder == null) {
250+
templateFolder = getTemplateFolder();
256251
}
257-
} else {
258-
if (!newbieFile.createNewFile()) {
259-
throw new IOException(newbieFile + " already exists.");
252+
if (templateFolder.exists()) {
253+
Util.copyDir(templateFolder, sketchFolder);
254+
File templateFile =
255+
new File(sketchFolder, "sketch." + getDefaultExtension());
256+
if (!templateFile.renameTo(newbieFile)) {
257+
System.err.println("Error while assigning the sketch template.");
258+
}
259+
} else {
260+
if (!newbieFile.createNewFile()) {
261+
System.err.println(newbieFile + " already exists.");
262+
}
260263
}
264+
} catch (Exception e) {
265+
// just spew out this error and try to recover below
266+
e.printStackTrace();
261267
}
262268
return newbieFile;
263269
}
264270

265271

266-
public File getUserTemplateFolder() {
267-
return new File(Base.getSketchbookTemplatesFolder(), getTitle());
272+
/**
273+
* See if the user has their own template for this Mode. If the default
274+
* extension is "pde", this will look for a file called sketch.pde to use
275+
* as the template for all sketches.
276+
*/
277+
protected File checkSketchbookTemplate() {
278+
File user = new File(Base.getSketchbookTemplatesFolder(), getTitle());
279+
if (user.exists()) {
280+
File template = new File(user, "sketch." + getDefaultExtension());
281+
if (template.exists() && template.canRead()) {
282+
return user;
283+
}
284+
}
285+
return null;
268286
}
269287

270288

todo.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,20 @@ X https://github.com/processing/processing/issues/4606
2323
X change PdeInputHandler constructor
2424
X check whether this breaks other Modes before releasing
2525
X added extra constructor to keep things working
26-
_ allow modes to specify their own base file name
27-
_ need to move "is this a sketch?" handling into Mode
28-
_ favicon handling for p5jsMode
29-
_ move template subst code to Util
30-
_ in Mode: TokenMarker getTokenMarker(SketchCode code)
31-
_ passes through to no args version if not overridden
3226
X change PdeKeywords to PdeTokenMarker
3327
X doesn't seem to cause any breakage, but will keep an eye out
3428
X added getSketchbookTemplatesFolder() to Base
29+
o move template subst code to Util
30+
X in Mode: TokenMarker getTokenMarker(SketchCode code)
31+
X passes through to no args version if not overridden
32+
_ note this in the docs
33+
X implement templates
34+
_ write short docs
35+
_ https://github.com/processing/processing/issues/4306
36+
_ https://github.com/processing/processing/issues/4352
37+
38+
_ allow modes to specify their own base file name
39+
_ need to move "is this a sketch?" handling into Mode
3540

3641
_ modify line number color when no lines extend that far?
3742
_ https://github.com/processing/processing/pull/4560

0 commit comments

Comments
 (0)