Skip to content

Commit 21de7bd

Browse files
bitroncmaglie
authored andcommitted
Moved some code from Sketch to SketchData.
1 parent b61f2a4 commit 21de7bd

File tree

2 files changed

+186
-145
lines changed

2 files changed

+186
-145
lines changed

app/src/processing/app/Sketch.java

Lines changed: 35 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,9 @@ public class Sketch {
5050

5151
private Editor editor;
5252

53-
/** main pde file for this sketch. */
54-
private File primaryFile;
55-
5653
/** true if any of the files have been modified. */
5754
private boolean modified;
5855

59-
/** folder that contains this sketch */
60-
private File folder;
61-
62-
/** data folder location for this sketch (may not exist yet) */
63-
private File dataFolder;
64-
65-
/** code folder location for this sketch (may not exist yet) */
66-
private File codeFolder;
67-
6856
private SketchCodeDocument current;
6957
private int currentIndex;
7058

@@ -85,14 +73,7 @@ public class Sketch {
8573
*/
8674
public Sketch(Editor _editor, File file) throws IOException {
8775
editor = _editor;
88-
data = new SketchData();
89-
primaryFile = file;
90-
91-
// get the name of the sketch by chopping .pde or .java
92-
// off of the main file name
93-
String mainFilename = primaryFile.getName();
94-
int suffixLength = getDefaultExtension().length() + 1;
95-
data.setName(mainFilename.substring(0, mainFilename.length() - suffixLength));
76+
data = new SketchData(file);
9677

9778
// lib/build must exist when the application is started
9879
// it is added to the CLASSPATH by default, but if it doesn't
@@ -113,9 +94,6 @@ public Sketch(Editor _editor, File file) throws IOException {
11394
tempBuildFolder = Base.getBuildFolder();
11495
//Base.addBuildFolderToClassPath();
11596

116-
folder = new File(file.getParent());
117-
//System.out.println("sketch dir is " + folder);
118-
11997
load();
12098
}
12199

@@ -135,60 +113,7 @@ public Sketch(Editor _editor, File file) throws IOException {
135113
* in which case the load happens each time "run" is hit.
136114
*/
137115
protected void load() throws IOException {
138-
codeFolder = new File(folder, "code");
139-
dataFolder = new File(folder, "data");
140-
141-
// get list of files in the sketch folder
142-
String list[] = folder.list();
143-
144-
// reset these because load() may be called after an
145-
// external editor event. (fix for 0099)
146-
data.clearCodeDocs();
147-
148-
List<String> extensions = getExtensions();
149-
150-
for (String filename : list) {
151-
// Ignoring the dot prefix files is especially important to avoid files
152-
// with the ._ prefix on Mac OS X. (You'll see this with Mac files on
153-
// non-HFS drives, i.e. a thumb drive formatted FAT32.)
154-
if (filename.startsWith(".")) continue;
155-
156-
// Don't let some wacko name a directory blah.pde or bling.java.
157-
if (new File(folder, filename).isDirectory()) continue;
158-
159-
// figure out the name without any extension
160-
String base = filename;
161-
// now strip off the .pde and .java extensions
162-
for (String extension : extensions) {
163-
if (base.toLowerCase().endsWith("." + extension)) {
164-
base = base.substring(0, base.length() - (extension.length() + 1));
165-
166-
// Don't allow people to use files with invalid names, since on load,
167-
// it would be otherwise possible to sneak in nasty filenames. [0116]
168-
if (Sketch.isSanitaryName(base)) {
169-
data.addCode(new SketchCodeDocument(new File(folder, filename)));
170-
} else {
171-
System.err.println(I18n.format("File name {0} is invalid: ignored", filename));
172-
}
173-
}
174-
}
175-
}
176-
177-
if (data.getCodeCount() == 0)
178-
throw new IOException(_("No valid code files found"));
179-
180-
// move the main class to the first tab
181-
// start at 1, if it's at zero, don't bother
182-
for (SketchCode code : data.getCodes()) {
183-
//if (code[i].file.getName().equals(mainFilename)) {
184-
if (code.getFile().equals(primaryFile)) {
185-
data.moveCodeToFront(code);
186-
break;
187-
}
188-
}
189-
190-
// sort the entries at the top
191-
data.sortCode();
116+
data.load();
192117

193118
// set the main file to be the current tab
194119
if (editor != null) {
@@ -335,7 +260,7 @@ protected void nameCode(String newName) {
335260
I18n.format(
336261
_("A file named \"{0}\" already exists in \"{1}\""),
337262
c.getFileName(),
338-
folder.getAbsolutePath()
263+
data.getFolder().getAbsolutePath()
339264
));
340265
return;
341266
}
@@ -364,7 +289,7 @@ protected void nameCode(String newName) {
364289
}
365290

366291

367-
File newFile = new File(folder, newName);
292+
File newFile = new File(data.getFolder(), newName);
368293
// if (newFile.exists()) { // yay! users will try anything
369294
// Base.showMessage("Nope",
370295
// "A file named \"" + newFile + "\" already exists\n" +
@@ -386,7 +311,7 @@ protected void nameCode(String newName) {
386311
if (currentIndex == 0) {
387312
// get the new folder name/location
388313
String folderName = newName.substring(0, newName.indexOf('.'));
389-
File newFolder = new File(folder.getParentFile(), folderName);
314+
File newFolder = new File(data.getFolder().getParentFile(), folderName);
390315
if (newFolder.exists()) {
391316
Base.showWarning(_("Cannot Rename"),
392317
I18n.format(
@@ -434,7 +359,7 @@ protected void nameCode(String newName) {
434359
}
435360

436361
// now rename the sketch folder and re-open
437-
boolean success = folder.renameTo(newFolder);
362+
boolean success = data.getFolder().renameTo(newFolder);
438363
if (!success) {
439364
Base.showWarning(_("Error"), _("Could not rename the sketch. (2)"), null);
440365
return;
@@ -479,7 +404,7 @@ protected void nameCode(String newName) {
479404
I18n.format(
480405
"Could not create the file \"{0}\" in \"{1}\"",
481406
newFile,
482-
folder.getAbsolutePath()
407+
data.getFolder().getAbsolutePath()
483408
), e);
484409
return;
485410
}
@@ -534,7 +459,7 @@ public void handleDeleteCode() {
534459
// to do a save on the handleNew()
535460

536461
// delete the entire sketch
537-
Base.removeDir(folder);
462+
Base.removeDir(data.getFolder());
538463

539464
// get the changes into the sketchbook menu
540465
//sketchbook.rebuildMenus();
@@ -680,10 +605,7 @@ public boolean accept(File dir, String name) {
680605
}
681606
}
682607

683-
for (SketchCode code : data.getCodes()) {
684-
if (code.isModified())
685-
code.save();
686-
}
608+
data.save();
687609
calcModified();
688610
return true;
689611
}
@@ -720,10 +642,10 @@ protected boolean saveAs() throws IOException {
720642

721643
if (isReadOnly() || isUntitled()) {
722644
// default to the sketchbook folder
723-
fd.setSelectedFile(new File(Base.getSketchbookFolder().getAbsolutePath(), folder.getName()));
645+
fd.setSelectedFile(new File(Base.getSketchbookFolder().getAbsolutePath(), data.getFolder().getName()));
724646
} else {
725647
// default to the parent folder of where this was
726-
fd.setSelectedFile(folder);
648+
fd.setSelectedFile(data.getFolder());
727649
}
728650

729651
int returnVal = fd.showSaveDialog(editor);
@@ -755,7 +677,7 @@ protected boolean saveAs() throws IOException {
755677
}
756678

757679
// check if the paths are identical
758-
if (newFolder.equals(folder)) {
680+
if (newFolder.equals(data.getFolder())) {
759681
// just use "save" here instead, because the user will have received a
760682
// message (from the operating system) about "do you want to replace?"
761683
return save();
@@ -764,7 +686,7 @@ protected boolean saveAs() throws IOException {
764686
// check to see if the user is trying to save this sketch inside itself
765687
try {
766688
String newPath = newFolder.getCanonicalPath() + File.separator;
767-
String oldPath = folder.getCanonicalPath() + File.separator;
689+
String oldPath = data.getFolder().getCanonicalPath() + File.separator;
768690

769691
if (newPath.indexOf(oldPath) == 0) {
770692
Base.showWarning(_("How very Borges of you"),
@@ -800,20 +722,20 @@ protected boolean saveAs() throws IOException {
800722
}
801723

802724
// re-copy the data folder (this may take a while.. add progress bar?)
803-
if (dataFolder.exists()) {
725+
if (data.getDataFolder().exists()) {
804726
File newDataFolder = new File(newFolder, "data");
805-
Base.copyDir(dataFolder, newDataFolder);
727+
Base.copyDir(data.getDataFolder(), newDataFolder);
806728
}
807729

808730
// re-copy the code folder
809-
if (codeFolder.exists()) {
731+
if (data.getCodeFolder().exists()) {
810732
File newCodeFolder = new File(newFolder, "code");
811-
Base.copyDir(codeFolder, newCodeFolder);
733+
Base.copyDir(data.getCodeFolder(), newCodeFolder);
812734
}
813735

814736
// copy custom applet.html file if one exists
815737
// http://dev.processing.org/bugs/show_bug.cgi?id=485
816-
File customHtml = new File(folder, "applet.html");
738+
File customHtml = new File(data.getFolder(), "applet.html");
817739
if (customHtml.exists()) {
818740
File newHtml = new File(newFolder, "applet.html");
819741
Base.copyFile(customHtml, newHtml);
@@ -912,19 +834,19 @@ public boolean addFile(File sourceFile) {
912834

913835
//if (!codeFolder.exists()) codeFolder.mkdirs();
914836
prepareCodeFolder();
915-
destFile = new File(codeFolder, filename);
837+
destFile = new File(data.getCodeFolder(), filename);
916838

917839
} else {
918-
for (String extension : getExtensions()) {
840+
for (String extension : data.getExtensions()) {
919841
String lower = filename.toLowerCase();
920842
if (lower.endsWith("." + extension)) {
921-
destFile = new File(this.folder, filename);
843+
destFile = new File(data.getFolder(), filename);
922844
codeExtension = extension;
923845
}
924846
}
925847
if (codeExtension == null) {
926848
prepareDataFolder();
927-
destFile = new File(dataFolder, filename);
849+
destFile = new File(data.getDataFolder(), filename);
928850
}
929851
}
930852

@@ -1511,14 +1433,14 @@ public boolean exportApplication(String destPath,
15111433
* but not its contents.
15121434
*/
15131435
protected void ensureExistence() {
1514-
if (folder.exists()) return;
1436+
if (data.getFolder().exists()) return;
15151437

15161438
Base.showWarning(_("Sketch Disappeared"),
15171439
_("The sketch folder has disappeared.\n " +
15181440
"Will attempt to re-save in the same location,\n" +
15191441
"but anything besides the code will be lost."), null);
15201442
try {
1521-
folder.mkdirs();
1443+
data.getFolder().mkdirs();
15221444
modified = true;
15231445

15241446
for (SketchCode code : data.getCodes()) {
@@ -1542,7 +1464,7 @@ protected void ensureExistence() {
15421464
* volumes or folders without appropriate permissions.
15431465
*/
15441466
public boolean isReadOnly() {
1545-
String apath = folder.getAbsolutePath();
1467+
String apath = data.getFolder().getAbsolutePath();
15461468
for (File folder : Base.getLibrariesPath()) {
15471469
if (apath.startsWith(folder.getAbsolutePath()))
15481470
return true;
@@ -1591,15 +1513,15 @@ public boolean isDefaultExtension(String what) {
15911513
* extensions.
15921514
*/
15931515
public boolean validExtension(String what) {
1594-
return getExtensions().contains(what);
1516+
return data.getExtensions().contains(what);
15951517
}
15961518

15971519

15981520
/**
15991521
* Returns the default extension for this editor setup.
16001522
*/
16011523
public String getDefaultExtension() {
1602-
return "ino";
1524+
return data.getDefaultExtension();
16031525
}
16041526

16051527
static private List<String> hiddenExtensions = Arrays.asList("ino", "pde");
@@ -1608,13 +1530,6 @@ public List<String> getHiddenExtensions() {
16081530
return hiddenExtensions;
16091531
}
16101532

1611-
/**
1612-
* Returns a String[] array of proper extensions.
1613-
*/
1614-
public List<String> getExtensions() {
1615-
return Arrays.asList("ino", "pde", "c", "cpp", "h");
1616-
}
1617-
16181533

16191534
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
16201535

@@ -1630,36 +1545,19 @@ public String getName() {
16301545
}
16311546

16321547

1633-
/**
1634-
* Returns a file object for the primary .pde of this sketch.
1635-
*/
1636-
public File getPrimaryFile() {
1637-
return primaryFile;
1638-
}
1639-
1640-
16411548
/**
16421549
* Returns path to the main .pde file for this sketch.
16431550
*/
16441551
public String getMainFilePath() {
1645-
return primaryFile.getAbsolutePath();
1646-
//return code[0].file.getAbsolutePath();
1552+
return data.getMainFilePath();
16471553
}
16481554

16491555

16501556
/**
16511557
* Returns the sketch folder.
16521558
*/
16531559
public File getFolder() {
1654-
return folder;
1655-
}
1656-
1657-
1658-
/**
1659-
* Returns the location of the sketch's data folder. (It may not exist yet.)
1660-
*/
1661-
public File getDataFolder() {
1662-
return dataFolder;
1560+
return data.getFolder();
16631561
}
16641562

16651563

@@ -1668,18 +1566,10 @@ public File getDataFolder() {
16681566
* it also returns the data folder, since it's likely about to be used.
16691567
*/
16701568
public File prepareDataFolder() {
1671-
if (!dataFolder.exists()) {
1672-
dataFolder.mkdirs();
1569+
if (!data.getDataFolder().exists()) {
1570+
data.getDataFolder().mkdirs();
16731571
}
1674-
return dataFolder;
1675-
}
1676-
1677-
1678-
/**
1679-
* Returns the location of the sketch's code folder. (It may not exist yet.)
1680-
*/
1681-
public File getCodeFolder() {
1682-
return codeFolder;
1572+
return data.getDataFolder();
16831573
}
16841574

16851575

@@ -1688,10 +1578,10 @@ public File getCodeFolder() {
16881578
* it also returns the code folder, since it's likely about to be used.
16891579
*/
16901580
public File prepareCodeFolder() {
1691-
if (!codeFolder.exists()) {
1692-
codeFolder.mkdirs();
1581+
if (!data.getCodeFolder().exists()) {
1582+
data.getCodeFolder().mkdirs();
16931583
}
1694-
return codeFolder;
1584+
return data.getCodeFolder();
16951585
}
16961586

16971587

0 commit comments

Comments
 (0)