add template support for Modes

This commit is contained in:
Ben Fry
2016-08-07 08:34:42 -04:00
parent 16534d388c
commit 7e142e8b54
4 changed files with 45 additions and 7 deletions
+5
View File
@@ -1094,12 +1094,17 @@ public class Base {
// Make the directory for the new sketch
newbieDir.mkdirs();
// Add any template files from the Mode itself
File newbieFile = nextMode.addTemplateFiles(newbieDir, newbieName);
/*
// Make an empty pde file
File newbieFile =
new File(newbieDir, newbieName + "." + nextMode.getDefaultExtension()); //$NON-NLS-1$
if (!newbieFile.createNewFile()) {
throw new IOException(newbieFile + " already exists.");
}
*/
// Create sketch properties file if it's not the default mode.
if (!nextMode.equals(getDefaultMode())) {
+37
View File
@@ -226,6 +226,43 @@ public abstract class Mode {
}
/**
* Add files to a folder to create an empty sketch. This can be overridden
* to add template files to a sketch for Modes that need them.
*
* @param sketchFolder the directory where the new sketch should live
* @param sketchName the name of the new sketch
* @return the main file for the sketch to be opened via handleOpen()
* @throws IOException if the file somehow already exists
*/
public File addTemplateFiles(File sketchFolder,
String sketchName) throws IOException {
// Make an empty .pde file
File newbieFile =
new File(sketchFolder, sketchName + "." + getDefaultExtension());
File templateFolder = getTemplateFolder();
if (templateFolder.exists()) {
Util.copyDir(templateFolder, sketchFolder);
File templateFile =
new File(sketchFolder, "sketch." + getDefaultExtension());
if (!templateFile.renameTo(newbieFile)) {
throw new IOException("Error while assigning the sketch template.");
}
} else {
if (!newbieFile.createNewFile()) {
throw new IOException(newbieFile + " already exists.");
}
}
return newbieFile;
}
public File getTemplateFolder() {
return getContentFile("template");
}
/**
* Return the pretty/printable/menu name for this mode. This is separate from
* the single word name of the folder that contains this mode. It could even