1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

Moved some code from Sketch to SketchData.

This commit is contained in:
Claudio Indellicati 2014-04-07 18:20:17 +02:00 committed by Cristian Maglie
parent b61f2a419f
commit 21de7bdea3
2 changed files with 186 additions and 145 deletions

View File

@ -50,21 +50,9 @@ public class Sketch {
private Editor editor; private Editor editor;
/** main pde file for this sketch. */
private File primaryFile;
/** true if any of the files have been modified. */ /** true if any of the files have been modified. */
private boolean modified; private boolean modified;
/** folder that contains this sketch */
private File folder;
/** data folder location for this sketch (may not exist yet) */
private File dataFolder;
/** code folder location for this sketch (may not exist yet) */
private File codeFolder;
private SketchCodeDocument current; private SketchCodeDocument current;
private int currentIndex; private int currentIndex;
@ -85,14 +73,7 @@ public class Sketch {
*/ */
public Sketch(Editor _editor, File file) throws IOException { public Sketch(Editor _editor, File file) throws IOException {
editor = _editor; editor = _editor;
data = new SketchData(); data = new SketchData(file);
primaryFile = file;
// get the name of the sketch by chopping .pde or .java
// off of the main file name
String mainFilename = primaryFile.getName();
int suffixLength = getDefaultExtension().length() + 1;
data.setName(mainFilename.substring(0, mainFilename.length() - suffixLength));
// lib/build must exist when the application is started // lib/build must exist when the application is started
// it is added to the CLASSPATH by default, but if it doesn't // it is added to the CLASSPATH by default, but if it doesn't
@ -113,9 +94,6 @@ public class Sketch {
tempBuildFolder = Base.getBuildFolder(); tempBuildFolder = Base.getBuildFolder();
//Base.addBuildFolderToClassPath(); //Base.addBuildFolderToClassPath();
folder = new File(file.getParent());
//System.out.println("sketch dir is " + folder);
load(); load();
} }
@ -135,60 +113,7 @@ public class Sketch {
* in which case the load happens each time "run" is hit. * in which case the load happens each time "run" is hit.
*/ */
protected void load() throws IOException { protected void load() throws IOException {
codeFolder = new File(folder, "code"); data.load();
dataFolder = new File(folder, "data");
// get list of files in the sketch folder
String list[] = folder.list();
// reset these because load() may be called after an
// external editor event. (fix for 0099)
data.clearCodeDocs();
List<String> extensions = getExtensions();
for (String filename : list) {
// Ignoring the dot prefix files is especially important to avoid files
// with the ._ prefix on Mac OS X. (You'll see this with Mac files on
// non-HFS drives, i.e. a thumb drive formatted FAT32.)
if (filename.startsWith(".")) continue;
// Don't let some wacko name a directory blah.pde or bling.java.
if (new File(folder, filename).isDirectory()) continue;
// figure out the name without any extension
String base = filename;
// now strip off the .pde and .java extensions
for (String extension : extensions) {
if (base.toLowerCase().endsWith("." + extension)) {
base = base.substring(0, base.length() - (extension.length() + 1));
// Don't allow people to use files with invalid names, since on load,
// it would be otherwise possible to sneak in nasty filenames. [0116]
if (Sketch.isSanitaryName(base)) {
data.addCode(new SketchCodeDocument(new File(folder, filename)));
} else {
System.err.println(I18n.format("File name {0} is invalid: ignored", filename));
}
}
}
}
if (data.getCodeCount() == 0)
throw new IOException(_("No valid code files found"));
// move the main class to the first tab
// start at 1, if it's at zero, don't bother
for (SketchCode code : data.getCodes()) {
//if (code[i].file.getName().equals(mainFilename)) {
if (code.getFile().equals(primaryFile)) {
data.moveCodeToFront(code);
break;
}
}
// sort the entries at the top
data.sortCode();
// set the main file to be the current tab // set the main file to be the current tab
if (editor != null) { if (editor != null) {
@ -335,7 +260,7 @@ public class Sketch {
I18n.format( I18n.format(
_("A file named \"{0}\" already exists in \"{1}\""), _("A file named \"{0}\" already exists in \"{1}\""),
c.getFileName(), c.getFileName(),
folder.getAbsolutePath() data.getFolder().getAbsolutePath()
)); ));
return; return;
} }
@ -364,7 +289,7 @@ public class Sketch {
} }
File newFile = new File(folder, newName); File newFile = new File(data.getFolder(), newName);
// if (newFile.exists()) { // yay! users will try anything // if (newFile.exists()) { // yay! users will try anything
// Base.showMessage("Nope", // Base.showMessage("Nope",
// "A file named \"" + newFile + "\" already exists\n" + // "A file named \"" + newFile + "\" already exists\n" +
@ -386,7 +311,7 @@ public class Sketch {
if (currentIndex == 0) { if (currentIndex == 0) {
// get the new folder name/location // get the new folder name/location
String folderName = newName.substring(0, newName.indexOf('.')); String folderName = newName.substring(0, newName.indexOf('.'));
File newFolder = new File(folder.getParentFile(), folderName); File newFolder = new File(data.getFolder().getParentFile(), folderName);
if (newFolder.exists()) { if (newFolder.exists()) {
Base.showWarning(_("Cannot Rename"), Base.showWarning(_("Cannot Rename"),
I18n.format( I18n.format(
@ -434,7 +359,7 @@ public class Sketch {
} }
// now rename the sketch folder and re-open // now rename the sketch folder and re-open
boolean success = folder.renameTo(newFolder); boolean success = data.getFolder().renameTo(newFolder);
if (!success) { if (!success) {
Base.showWarning(_("Error"), _("Could not rename the sketch. (2)"), null); Base.showWarning(_("Error"), _("Could not rename the sketch. (2)"), null);
return; return;
@ -479,7 +404,7 @@ public class Sketch {
I18n.format( I18n.format(
"Could not create the file \"{0}\" in \"{1}\"", "Could not create the file \"{0}\" in \"{1}\"",
newFile, newFile,
folder.getAbsolutePath() data.getFolder().getAbsolutePath()
), e); ), e);
return; return;
} }
@ -534,7 +459,7 @@ public class Sketch {
// to do a save on the handleNew() // to do a save on the handleNew()
// delete the entire sketch // delete the entire sketch
Base.removeDir(folder); Base.removeDir(data.getFolder());
// get the changes into the sketchbook menu // get the changes into the sketchbook menu
//sketchbook.rebuildMenus(); //sketchbook.rebuildMenus();
@ -680,10 +605,7 @@ public class Sketch {
} }
} }
for (SketchCode code : data.getCodes()) { data.save();
if (code.isModified())
code.save();
}
calcModified(); calcModified();
return true; return true;
} }
@ -720,10 +642,10 @@ public class Sketch {
if (isReadOnly() || isUntitled()) { if (isReadOnly() || isUntitled()) {
// default to the sketchbook folder // default to the sketchbook folder
fd.setSelectedFile(new File(Base.getSketchbookFolder().getAbsolutePath(), folder.getName())); fd.setSelectedFile(new File(Base.getSketchbookFolder().getAbsolutePath(), data.getFolder().getName()));
} else { } else {
// default to the parent folder of where this was // default to the parent folder of where this was
fd.setSelectedFile(folder); fd.setSelectedFile(data.getFolder());
} }
int returnVal = fd.showSaveDialog(editor); int returnVal = fd.showSaveDialog(editor);
@ -755,7 +677,7 @@ public class Sketch {
} }
// check if the paths are identical // check if the paths are identical
if (newFolder.equals(folder)) { if (newFolder.equals(data.getFolder())) {
// just use "save" here instead, because the user will have received a // just use "save" here instead, because the user will have received a
// message (from the operating system) about "do you want to replace?" // message (from the operating system) about "do you want to replace?"
return save(); return save();
@ -764,7 +686,7 @@ public class Sketch {
// check to see if the user is trying to save this sketch inside itself // check to see if the user is trying to save this sketch inside itself
try { try {
String newPath = newFolder.getCanonicalPath() + File.separator; String newPath = newFolder.getCanonicalPath() + File.separator;
String oldPath = folder.getCanonicalPath() + File.separator; String oldPath = data.getFolder().getCanonicalPath() + File.separator;
if (newPath.indexOf(oldPath) == 0) { if (newPath.indexOf(oldPath) == 0) {
Base.showWarning(_("How very Borges of you"), Base.showWarning(_("How very Borges of you"),
@ -800,20 +722,20 @@ public class Sketch {
} }
// re-copy the data folder (this may take a while.. add progress bar?) // re-copy the data folder (this may take a while.. add progress bar?)
if (dataFolder.exists()) { if (data.getDataFolder().exists()) {
File newDataFolder = new File(newFolder, "data"); File newDataFolder = new File(newFolder, "data");
Base.copyDir(dataFolder, newDataFolder); Base.copyDir(data.getDataFolder(), newDataFolder);
} }
// re-copy the code folder // re-copy the code folder
if (codeFolder.exists()) { if (data.getCodeFolder().exists()) {
File newCodeFolder = new File(newFolder, "code"); File newCodeFolder = new File(newFolder, "code");
Base.copyDir(codeFolder, newCodeFolder); Base.copyDir(data.getCodeFolder(), newCodeFolder);
} }
// copy custom applet.html file if one exists // copy custom applet.html file if one exists
// http://dev.processing.org/bugs/show_bug.cgi?id=485 // http://dev.processing.org/bugs/show_bug.cgi?id=485
File customHtml = new File(folder, "applet.html"); File customHtml = new File(data.getFolder(), "applet.html");
if (customHtml.exists()) { if (customHtml.exists()) {
File newHtml = new File(newFolder, "applet.html"); File newHtml = new File(newFolder, "applet.html");
Base.copyFile(customHtml, newHtml); Base.copyFile(customHtml, newHtml);
@ -912,19 +834,19 @@ public class Sketch {
//if (!codeFolder.exists()) codeFolder.mkdirs(); //if (!codeFolder.exists()) codeFolder.mkdirs();
prepareCodeFolder(); prepareCodeFolder();
destFile = new File(codeFolder, filename); destFile = new File(data.getCodeFolder(), filename);
} else { } else {
for (String extension : getExtensions()) { for (String extension : data.getExtensions()) {
String lower = filename.toLowerCase(); String lower = filename.toLowerCase();
if (lower.endsWith("." + extension)) { if (lower.endsWith("." + extension)) {
destFile = new File(this.folder, filename); destFile = new File(data.getFolder(), filename);
codeExtension = extension; codeExtension = extension;
} }
} }
if (codeExtension == null) { if (codeExtension == null) {
prepareDataFolder(); prepareDataFolder();
destFile = new File(dataFolder, filename); destFile = new File(data.getDataFolder(), filename);
} }
} }
@ -1511,14 +1433,14 @@ public class Sketch {
* but not its contents. * but not its contents.
*/ */
protected void ensureExistence() { protected void ensureExistence() {
if (folder.exists()) return; if (data.getFolder().exists()) return;
Base.showWarning(_("Sketch Disappeared"), Base.showWarning(_("Sketch Disappeared"),
_("The sketch folder has disappeared.\n " + _("The sketch folder has disappeared.\n " +
"Will attempt to re-save in the same location,\n" + "Will attempt to re-save in the same location,\n" +
"but anything besides the code will be lost."), null); "but anything besides the code will be lost."), null);
try { try {
folder.mkdirs(); data.getFolder().mkdirs();
modified = true; modified = true;
for (SketchCode code : data.getCodes()) { for (SketchCode code : data.getCodes()) {
@ -1542,7 +1464,7 @@ public class Sketch {
* volumes or folders without appropriate permissions. * volumes or folders without appropriate permissions.
*/ */
public boolean isReadOnly() { public boolean isReadOnly() {
String apath = folder.getAbsolutePath(); String apath = data.getFolder().getAbsolutePath();
for (File folder : Base.getLibrariesPath()) { for (File folder : Base.getLibrariesPath()) {
if (apath.startsWith(folder.getAbsolutePath())) if (apath.startsWith(folder.getAbsolutePath()))
return true; return true;
@ -1591,7 +1513,7 @@ public class Sketch {
* extensions. * extensions.
*/ */
public boolean validExtension(String what) { public boolean validExtension(String what) {
return getExtensions().contains(what); return data.getExtensions().contains(what);
} }
@ -1599,7 +1521,7 @@ public class Sketch {
* Returns the default extension for this editor setup. * Returns the default extension for this editor setup.
*/ */
public String getDefaultExtension() { public String getDefaultExtension() {
return "ino"; return data.getDefaultExtension();
} }
static private List<String> hiddenExtensions = Arrays.asList("ino", "pde"); static private List<String> hiddenExtensions = Arrays.asList("ino", "pde");
@ -1608,13 +1530,6 @@ public class Sketch {
return hiddenExtensions; return hiddenExtensions;
} }
/**
* Returns a String[] array of proper extensions.
*/
public List<String> getExtensions() {
return Arrays.asList("ino", "pde", "c", "cpp", "h");
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@ -1630,20 +1545,11 @@ public class Sketch {
} }
/**
* Returns a file object for the primary .pde of this sketch.
*/
public File getPrimaryFile() {
return primaryFile;
}
/** /**
* Returns path to the main .pde file for this sketch. * Returns path to the main .pde file for this sketch.
*/ */
public String getMainFilePath() { public String getMainFilePath() {
return primaryFile.getAbsolutePath(); return data.getMainFilePath();
//return code[0].file.getAbsolutePath();
} }
@ -1651,15 +1557,7 @@ public class Sketch {
* Returns the sketch folder. * Returns the sketch folder.
*/ */
public File getFolder() { public File getFolder() {
return folder; return data.getFolder();
}
/**
* Returns the location of the sketch's data folder. (It may not exist yet.)
*/
public File getDataFolder() {
return dataFolder;
} }
@ -1668,18 +1566,10 @@ public class Sketch {
* it also returns the data folder, since it's likely about to be used. * it also returns the data folder, since it's likely about to be used.
*/ */
public File prepareDataFolder() { public File prepareDataFolder() {
if (!dataFolder.exists()) { if (!data.getDataFolder().exists()) {
dataFolder.mkdirs(); data.getDataFolder().mkdirs();
} }
return dataFolder; return data.getDataFolder();
}
/**
* Returns the location of the sketch's code folder. (It may not exist yet.)
*/
public File getCodeFolder() {
return codeFolder;
} }
@ -1688,10 +1578,10 @@ public class Sketch {
* it also returns the code folder, since it's likely about to be used. * it also returns the code folder, since it's likely about to be used.
*/ */
public File prepareCodeFolder() { public File prepareCodeFolder() {
if (!codeFolder.exists()) { if (!data.getCodeFolder().exists()) {
codeFolder.mkdirs(); data.getCodeFolder().mkdirs();
} }
return codeFolder; return data.getCodeFolder();
} }

View File

@ -1,12 +1,29 @@
package processing.app; package processing.app;
import static processing.app.I18n._;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
public class SketchData { public class SketchData {
/** main pde file for this sketch. */
private File primaryFile;
/** folder that contains this sketch */
private File folder;
/** data folder location for this sketch (may not exist yet) */
private File dataFolder;
/** code folder location for this sketch (may not exist yet) */
private File codeFolder;
/** /**
* Name of sketch, which is the name of main file (without .pde or .java * Name of sketch, which is the name of main file (without .pde or .java
* extension) * extension)
@ -22,6 +39,99 @@ public class SketchData {
} }
}; };
SketchData(File file) {
primaryFile = file;
// get the name of the sketch by chopping .pde or .java
// off of the main file name
String mainFilename = primaryFile.getName();
int suffixLength = getDefaultExtension().length() + 1;
name = mainFilename.substring(0, mainFilename.length() - suffixLength);
folder = new File(file.getParent());
//System.out.println("sketch dir is " + folder);
}
/**
* Build the list of files.
* <P>
* Generally this is only done once, rather than
* each time a change is made, because otherwise it gets to be
* a nightmare to keep track of what files went where, because
* not all the data will be saved to disk.
* <P>
* This also gets called when the main sketch file is renamed,
* because the sketch has to be reloaded from a different folder.
* <P>
* Another exception is when an external editor is in use,
* in which case the load happens each time "run" is hit.
*/
protected void load() throws IOException {
codeFolder = new File(folder, "code");
dataFolder = new File(folder, "data");
// get list of files in the sketch folder
String list[] = folder.list();
// reset these because load() may be called after an
// external editor event. (fix for 0099)
// codeDocs = new SketchCodeDoc[list.length];
clearCodeDocs();
// data.setCodeDocs(codeDocs);
List<String> extensions = getExtensions();
for (String filename : list) {
// Ignoring the dot prefix files is especially important to avoid files
// with the ._ prefix on Mac OS X. (You'll see this with Mac files on
// non-HFS drives, i.e. a thumb drive formatted FAT32.)
if (filename.startsWith(".")) continue;
// Don't let some wacko name a directory blah.pde or bling.java.
if (new File(folder, filename).isDirectory()) continue;
// figure out the name without any extension
String base = filename;
// now strip off the .pde and .java extensions
for (String extension : extensions) {
if (base.toLowerCase().endsWith("." + extension)) {
base = base.substring(0, base.length() - (extension.length() + 1));
// Don't allow people to use files with invalid names, since on load,
// it would be otherwise possible to sneak in nasty filenames. [0116]
if (Sketch.isSanitaryName(base)) {
addCode(new SketchCodeDocument(new File(folder, filename)));
} else {
System.err.println(I18n.format("File name {0} is invalid: ignored", filename));
}
}
}
}
if (getCodeCount() == 0)
throw new IOException(_("No valid code files found"));
// move the main class to the first tab
// start at 1, if it's at zero, don't bother
for (SketchCode code : getCodes()) {
//if (code[i].file.getName().equals(mainFilename)) {
if (code.getFile().equals(primaryFile)) {
moveCodeToFront(code);
break;
}
}
// sort the entries at the top
sortCode();
}
public void save() throws IOException {
for (SketchCode code : getCodes()) {
if (code.isModified())
code.save();
}
}
public int getCodeCount() { public int getCodeCount() {
return codes.size(); return codes.size();
} }
@ -30,6 +140,35 @@ public class SketchData {
return codes.toArray(new SketchCode[0]); return codes.toArray(new SketchCode[0]);
} }
/**
* Returns the default extension for this editor setup.
*/
public String getDefaultExtension() {
return "ino";
}
/**
* Returns a String[] array of proper extensions.
*/
public List<String> getExtensions() {
return Arrays.asList("ino", "pde", "c", "cpp", "h");
}
/**
* Returns a file object for the primary .pde of this sketch.
*/
public File getPrimaryFile() {
return primaryFile;
}
/**
* Returns path to the main .pde file for this sketch.
*/
public String getMainFilePath() {
return primaryFile.getAbsolutePath();
//return code[0].file.getAbsolutePath();
}
public void addCode(SketchCode sketchCode) { public void addCode(SketchCode sketchCode) {
codes.add(sketchCode); codes.add(sketchCode);
} }
@ -89,4 +228,16 @@ public class SketchData {
public void clearCodeDocs() { public void clearCodeDocs() {
codes.clear(); codes.clear();
} }
public File getFolder() {
return folder;
}
public File getDataFolder() {
return dataFolder;
}
public File getCodeFolder() {
return codeFolder;
}
} }