@@ -73,7 +73,10 @@ public class Sketch {
73
73
74
74
/** code folder location for this sketch (may not exist yet) */
75
75
private File codeFolder ;
76
-
76
+
77
+ /** configuration folder location for this sketch (may not exist yet) */
78
+ private File configFolder ;
79
+
77
80
private SketchCode current ;
78
81
private int currentIndex ;
79
82
/**
@@ -160,15 +163,24 @@ public Sketch(Editor editor, String path) throws IOException {
160
163
protected void load () throws IOException {
161
164
codeFolder = new File (folder , "code" );
162
165
dataFolder = new File (folder , "data" );
166
+ configFolder = new File (folder , "configuration" );
163
167
164
168
// get list of files in the sketch folder
165
169
String list [] = folder .list ();
170
+ // get list of files in the library configuration folder
171
+ String list1 [] = configFolder .list ();
166
172
167
173
// reset these because load() may be called after an
168
174
// external editor event. (fix for 0099)
169
175
codeCount = 0 ;
170
-
171
- code = new SketchCode [list .length ];
176
+
177
+ // Add files from library configuration folder if exists
178
+ if (configFolder .exists ()) {
179
+ code = new SketchCode [list .length +list1 .length ];
180
+ }
181
+ else {
182
+ code = new SketchCode [list .length ];
183
+ }
172
184
173
185
String [] extensions = getExtensions ();
174
186
@@ -203,6 +215,38 @@ protected void load() throws IOException {
203
215
if (codeCount == 0 )
204
216
throw new IOException (_ ("No valid code files found" ));
205
217
218
+ //Check library configuration folder to create tabs for library configuration files
219
+ if (configFolder .exists ()) {
220
+ for (String filename : list1 ) {
221
+ // Ignoring the dot prefix files is especially important to avoid files
222
+ // with the ._ prefix on Mac OS X. (You'll see this with Mac files on
223
+ // non-HFS drives, i.e. a thumb drive formatted FAT32.)
224
+ if (filename .startsWith ("." )) continue ;
225
+
226
+ // Don't let some wacko name a directory blah.pde or bling.java.
227
+ if (new File (configFolder , filename ).isDirectory ()) continue ;
228
+
229
+ // figure out the name without any extension
230
+ String base = filename ;
231
+ // now strip off the .pde and .java extensions
232
+ for (String extension : extensions ) {
233
+ if (base .toLowerCase ().endsWith ("." + extension )) {
234
+ base = base .substring (0 , base .length () - (extension .length () + 1 ));
235
+
236
+ // Don't allow people to use files with invalid names, since on load,
237
+ // it would be otherwise possible to sneak in nasty filenames. [0116]
238
+ if (Sketch .isSanitaryName (base )) {
239
+ code [codeCount ++] = new SketchCode (new File (configFolder , filename ), extension );
240
+ // Don't forget to declare it as configuration file
241
+ code [codeCount -1 ].setConfig (true );
242
+ } else {
243
+ editor .console .message (I18n .format ("File name {0} is invalid: ignored" , filename ), true , false );
244
+ }
245
+ }
246
+ }
247
+ }
248
+ }
249
+
206
250
// Remove any code that wasn't proper
207
251
code = (SketchCode []) PApplet .subset (code , 0 , codeCount );
208
252
@@ -878,7 +922,14 @@ protected boolean saveAs() throws IOException {
878
922
879
923
// now make a fresh copy of the folder
880
924
newFolder .mkdirs ();
881
-
925
+
926
+ // If exists, make a copy of the configuration folder
927
+ File newConfigFolder = new File (newFolder , "configuration" );
928
+ if (configFolder .exists ()) {
929
+ Base .copyDir (configFolder , newConfigFolder );
930
+ }
931
+
932
+
882
933
// grab the contents of the current tab before saving
883
934
// first get the contents of the editor text area
884
935
if (current .isModified ()) {
@@ -887,10 +938,17 @@ protected boolean saveAs() throws IOException {
887
938
888
939
// save the other tabs to their new location
889
940
for (int i = 1 ; i < codeCount ; i ++) {
890
- File newFile = new File (newFolder , code [i ].getFileName ());
941
+ // Library configuration file is not saved in the sketch folder but in the configuration folder
942
+ File newFile ;
943
+ if (code [i ].isConfig () && configFolder .exists ()) {
944
+ newFile = new File (newConfigFolder , code [i ].getFileName ());
945
+ }
946
+ else {
947
+ newFile = new File (newFolder , code [i ].getFileName ());
948
+ }
891
949
code [i ].saveAs (newFile );
892
950
}
893
-
951
+
894
952
// re-copy the data folder (this may take a while.. add progress bar?)
895
953
if (dataFolder .exists ()) {
896
954
File newDataFolder = new File (newFolder , "data" );
@@ -1117,12 +1175,28 @@ public void importLibrary(File jarPath) throws IOException {
1117
1175
1118
1176
String list [] = Base .headerListFromIncludePath (jarPath );
1119
1177
1178
+ // If exists, make a copy of the library configuration file in the sketch folder
1179
+ File LibConfigFolder = new File (jarPath , "configuration" );
1180
+ if (LibConfigFolder .exists ()) {
1181
+ System .out .println ("Library configuration files found" );
1182
+ File NewLibConfigFolder = (new File (folder ,"configuration" ));
1183
+ Base .copyDir (LibConfigFolder , NewLibConfigFolder );
1184
+ // Update editor tabs
1185
+ load ();
1186
+ // Need to switch between tabs to refresh the current content
1187
+ if (code [1 ].fileExists ()) {
1188
+ setCurrentCode (1 );
1189
+ }
1190
+ setCurrentCode (0 );
1191
+ }
1192
+
1120
1193
// import statements into the main sketch file (code[0])
1121
1194
// if the current code is a .java file, insert into current
1122
1195
//if (current.flavor == PDE) {
1123
1196
if (hasDefaultExtension (current )) {
1124
1197
setCurrentCode (0 );
1125
1198
}
1199
+
1126
1200
// could also scan the text in the file to see if each import
1127
1201
// statement is already in there, but if the user has the import
1128
1202
// commented out, then this will be a problem.
@@ -1136,6 +1210,7 @@ public void importLibrary(File jarPath) throws IOException {
1136
1210
buffer .append (editor .getText ());
1137
1211
editor .setText (buffer .toString ());
1138
1212
editor .setSelection (0 , 0 ); // scroll to start
1213
+
1139
1214
setModified (true );
1140
1215
}
1141
1216
@@ -1405,7 +1480,20 @@ public void preprocess(String buildPath, PdePreprocessor preprocessor) throws Ru
1405
1480
// shtuff so that unicode bunk is properly handled
1406
1481
String filename = sc .getFileName (); //code[i].name + ".java";
1407
1482
try {
1408
- Base .saveFile (sc .getProgram (), new File (buildPath , filename ));
1483
+ // Is it a Library configuration file ?
1484
+ if (sc .isConfig ()) {
1485
+ File TempLibConfigFolder = new File (buildPath , "configuration" );
1486
+ // Create the Library Configuration folder if it doesn't exist
1487
+ if (!TempLibConfigFolder .exists ()){
1488
+ TempLibConfigFolder .mkdir ();
1489
+ }
1490
+ // Copy file in configuration folder
1491
+ Base .saveFile (sc .getProgram (), new File (TempLibConfigFolder .getAbsolutePath (), filename ));
1492
+ }
1493
+ else {
1494
+ // Copy file in build folder
1495
+ Base .saveFile (sc .getProgram (), new File (buildPath , filename ));
1496
+ }
1409
1497
} catch (IOException e ) {
1410
1498
e .printStackTrace ();
1411
1499
throw new RunnerException (I18n .format (_ ("Problem moving {0} to the build folder" ), filename ));
@@ -1962,6 +2050,25 @@ public File prepareCodeFolder() {
1962
2050
return codeFolder ;
1963
2051
}
1964
2052
2053
+ /**
2054
+ * Returns the location of the sketch's configuration folder. (It may not exist yet.)
2055
+ */
2056
+ public File getConfigFolder () {
2057
+ return configFolder ;
2058
+ }
2059
+
2060
+
2061
+ /**
2062
+ * Create the config folder if it does not exist already. As a convenience,
2063
+ * it also returns the config folder, since it's likely about to be used.
2064
+ */
2065
+ public File prepareConfigFolder () {
2066
+ if (!configFolder .exists ()) {
2067
+ configFolder .mkdirs ();
2068
+ }
2069
+ return configFolder ;
2070
+ }
2071
+
1965
2072
1966
2073
public String getClassPath () {
1967
2074
return classPath ;
0 commit comments