@@ -50,21 +50,9 @@ public class Sketch {
50
50
51
51
private Editor editor ;
52
52
53
- /** main pde file for this sketch. */
54
- private File primaryFile ;
55
-
56
53
/** true if any of the files have been modified. */
57
54
private boolean modified ;
58
55
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
-
68
56
private SketchCodeDocument current ;
69
57
private int currentIndex ;
70
58
@@ -85,14 +73,7 @@ public class Sketch {
85
73
*/
86
74
public Sketch (Editor _editor , File file ) throws IOException {
87
75
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 );
96
77
97
78
// lib/build must exist when the application is started
98
79
// 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 {
113
94
tempBuildFolder = Base .getBuildFolder ();
114
95
//Base.addBuildFolderToClassPath();
115
96
116
- folder = new File (file .getParent ());
117
- //System.out.println("sketch dir is " + folder);
118
-
119
97
load ();
120
98
}
121
99
@@ -135,60 +113,7 @@ public Sketch(Editor _editor, File file) throws IOException {
135
113
* in which case the load happens each time "run" is hit.
136
114
*/
137
115
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 ();
192
117
193
118
// set the main file to be the current tab
194
119
if (editor != null ) {
@@ -335,7 +260,7 @@ protected void nameCode(String newName) {
335
260
I18n .format (
336
261
_ ("A file named \" {0}\" already exists in \" {1}\" " ),
337
262
c .getFileName (),
338
- folder .getAbsolutePath ()
263
+ data . getFolder () .getAbsolutePath ()
339
264
));
340
265
return ;
341
266
}
@@ -364,7 +289,7 @@ protected void nameCode(String newName) {
364
289
}
365
290
366
291
367
- File newFile = new File (folder , newName );
292
+ File newFile = new File (data . getFolder () , newName );
368
293
// if (newFile.exists()) { // yay! users will try anything
369
294
// Base.showMessage("Nope",
370
295
// "A file named \"" + newFile + "\" already exists\n" +
@@ -386,7 +311,7 @@ protected void nameCode(String newName) {
386
311
if (currentIndex == 0 ) {
387
312
// get the new folder name/location
388
313
String folderName = newName .substring (0 , newName .indexOf ('.' ));
389
- File newFolder = new File (folder .getParentFile (), folderName );
314
+ File newFolder = new File (data . getFolder () .getParentFile (), folderName );
390
315
if (newFolder .exists ()) {
391
316
Base .showWarning (_ ("Cannot Rename" ),
392
317
I18n .format (
@@ -434,7 +359,7 @@ protected void nameCode(String newName) {
434
359
}
435
360
436
361
// now rename the sketch folder and re-open
437
- boolean success = folder .renameTo (newFolder );
362
+ boolean success = data . getFolder () .renameTo (newFolder );
438
363
if (!success ) {
439
364
Base .showWarning (_ ("Error" ), _ ("Could not rename the sketch. (2)" ), null );
440
365
return ;
@@ -479,7 +404,7 @@ protected void nameCode(String newName) {
479
404
I18n .format (
480
405
"Could not create the file \" {0}\" in \" {1}\" " ,
481
406
newFile ,
482
- folder .getAbsolutePath ()
407
+ data . getFolder () .getAbsolutePath ()
483
408
), e );
484
409
return ;
485
410
}
@@ -534,7 +459,7 @@ public void handleDeleteCode() {
534
459
// to do a save on the handleNew()
535
460
536
461
// delete the entire sketch
537
- Base .removeDir (folder );
462
+ Base .removeDir (data . getFolder () );
538
463
539
464
// get the changes into the sketchbook menu
540
465
//sketchbook.rebuildMenus();
@@ -680,10 +605,7 @@ public boolean accept(File dir, String name) {
680
605
}
681
606
}
682
607
683
- for (SketchCode code : data .getCodes ()) {
684
- if (code .isModified ())
685
- code .save ();
686
- }
608
+ data .save ();
687
609
calcModified ();
688
610
return true ;
689
611
}
@@ -720,10 +642,10 @@ protected boolean saveAs() throws IOException {
720
642
721
643
if (isReadOnly () || isUntitled ()) {
722
644
// 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 ()));
724
646
} else {
725
647
// default to the parent folder of where this was
726
- fd .setSelectedFile (folder );
648
+ fd .setSelectedFile (data . getFolder () );
727
649
}
728
650
729
651
int returnVal = fd .showSaveDialog (editor );
@@ -755,7 +677,7 @@ protected boolean saveAs() throws IOException {
755
677
}
756
678
757
679
// check if the paths are identical
758
- if (newFolder .equals (folder )) {
680
+ if (newFolder .equals (data . getFolder () )) {
759
681
// just use "save" here instead, because the user will have received a
760
682
// message (from the operating system) about "do you want to replace?"
761
683
return save ();
@@ -764,7 +686,7 @@ protected boolean saveAs() throws IOException {
764
686
// check to see if the user is trying to save this sketch inside itself
765
687
try {
766
688
String newPath = newFolder .getCanonicalPath () + File .separator ;
767
- String oldPath = folder .getCanonicalPath () + File .separator ;
689
+ String oldPath = data . getFolder () .getCanonicalPath () + File .separator ;
768
690
769
691
if (newPath .indexOf (oldPath ) == 0 ) {
770
692
Base .showWarning (_ ("How very Borges of you" ),
@@ -800,20 +722,20 @@ protected boolean saveAs() throws IOException {
800
722
}
801
723
802
724
// re-copy the data folder (this may take a while.. add progress bar?)
803
- if (dataFolder .exists ()) {
725
+ if (data . getDataFolder () .exists ()) {
804
726
File newDataFolder = new File (newFolder , "data" );
805
- Base .copyDir (dataFolder , newDataFolder );
727
+ Base .copyDir (data . getDataFolder () , newDataFolder );
806
728
}
807
729
808
730
// re-copy the code folder
809
- if (codeFolder .exists ()) {
731
+ if (data . getCodeFolder () .exists ()) {
810
732
File newCodeFolder = new File (newFolder , "code" );
811
- Base .copyDir (codeFolder , newCodeFolder );
733
+ Base .copyDir (data . getCodeFolder () , newCodeFolder );
812
734
}
813
735
814
736
// copy custom applet.html file if one exists
815
737
// 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" );
817
739
if (customHtml .exists ()) {
818
740
File newHtml = new File (newFolder , "applet.html" );
819
741
Base .copyFile (customHtml , newHtml );
@@ -912,19 +834,19 @@ public boolean addFile(File sourceFile) {
912
834
913
835
//if (!codeFolder.exists()) codeFolder.mkdirs();
914
836
prepareCodeFolder ();
915
- destFile = new File (codeFolder , filename );
837
+ destFile = new File (data . getCodeFolder () , filename );
916
838
917
839
} else {
918
- for (String extension : getExtensions ()) {
840
+ for (String extension : data . getExtensions ()) {
919
841
String lower = filename .toLowerCase ();
920
842
if (lower .endsWith ("." + extension )) {
921
- destFile = new File (this . folder , filename );
843
+ destFile = new File (data . getFolder () , filename );
922
844
codeExtension = extension ;
923
845
}
924
846
}
925
847
if (codeExtension == null ) {
926
848
prepareDataFolder ();
927
- destFile = new File (dataFolder , filename );
849
+ destFile = new File (data . getDataFolder () , filename );
928
850
}
929
851
}
930
852
@@ -1511,14 +1433,14 @@ public boolean exportApplication(String destPath,
1511
1433
* but not its contents.
1512
1434
*/
1513
1435
protected void ensureExistence () {
1514
- if (folder .exists ()) return ;
1436
+ if (data . getFolder () .exists ()) return ;
1515
1437
1516
1438
Base .showWarning (_ ("Sketch Disappeared" ),
1517
1439
_ ("The sketch folder has disappeared.\n " +
1518
1440
"Will attempt to re-save in the same location,\n " +
1519
1441
"but anything besides the code will be lost." ), null );
1520
1442
try {
1521
- folder .mkdirs ();
1443
+ data . getFolder () .mkdirs ();
1522
1444
modified = true ;
1523
1445
1524
1446
for (SketchCode code : data .getCodes ()) {
@@ -1542,7 +1464,7 @@ protected void ensureExistence() {
1542
1464
* volumes or folders without appropriate permissions.
1543
1465
*/
1544
1466
public boolean isReadOnly () {
1545
- String apath = folder .getAbsolutePath ();
1467
+ String apath = data . getFolder () .getAbsolutePath ();
1546
1468
for (File folder : Base .getLibrariesPath ()) {
1547
1469
if (apath .startsWith (folder .getAbsolutePath ()))
1548
1470
return true ;
@@ -1591,15 +1513,15 @@ public boolean isDefaultExtension(String what) {
1591
1513
* extensions.
1592
1514
*/
1593
1515
public boolean validExtension (String what ) {
1594
- return getExtensions ().contains (what );
1516
+ return data . getExtensions ().contains (what );
1595
1517
}
1596
1518
1597
1519
1598
1520
/**
1599
1521
* Returns the default extension for this editor setup.
1600
1522
*/
1601
1523
public String getDefaultExtension () {
1602
- return "ino" ;
1524
+ return data . getDefaultExtension () ;
1603
1525
}
1604
1526
1605
1527
static private List <String > hiddenExtensions = Arrays .asList ("ino" , "pde" );
@@ -1608,13 +1530,6 @@ public List<String> getHiddenExtensions() {
1608
1530
return hiddenExtensions ;
1609
1531
}
1610
1532
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
-
1618
1533
1619
1534
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1620
1535
@@ -1630,36 +1545,19 @@ public String getName() {
1630
1545
}
1631
1546
1632
1547
1633
- /**
1634
- * Returns a file object for the primary .pde of this sketch.
1635
- */
1636
- public File getPrimaryFile () {
1637
- return primaryFile ;
1638
- }
1639
-
1640
-
1641
1548
/**
1642
1549
* Returns path to the main .pde file for this sketch.
1643
1550
*/
1644
1551
public String getMainFilePath () {
1645
- return primaryFile .getAbsolutePath ();
1646
- //return code[0].file.getAbsolutePath();
1552
+ return data .getMainFilePath ();
1647
1553
}
1648
1554
1649
1555
1650
1556
/**
1651
1557
* Returns the sketch folder.
1652
1558
*/
1653
1559
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 ();
1663
1561
}
1664
1562
1665
1563
@@ -1668,18 +1566,10 @@ public File getDataFolder() {
1668
1566
* it also returns the data folder, since it's likely about to be used.
1669
1567
*/
1670
1568
public File prepareDataFolder () {
1671
- if (!dataFolder .exists ()) {
1672
- dataFolder .mkdirs ();
1569
+ if (!data . getDataFolder () .exists ()) {
1570
+ data . getDataFolder () .mkdirs ();
1673
1571
}
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 ();
1683
1573
}
1684
1574
1685
1575
@@ -1688,10 +1578,10 @@ public File getCodeFolder() {
1688
1578
* it also returns the code folder, since it's likely about to be used.
1689
1579
*/
1690
1580
public File prepareCodeFolder () {
1691
- if (!codeFolder .exists ()) {
1692
- codeFolder .mkdirs ();
1581
+ if (!data . getCodeFolder () .exists ()) {
1582
+ data . getCodeFolder () .mkdirs ();
1693
1583
}
1694
- return codeFolder ;
1584
+ return data . getCodeFolder () ;
1695
1585
}
1696
1586
1697
1587
0 commit comments