@@ -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
0 commit comments