Skip to content

Commit abe6ff5

Browse files
bitroncmaglie
authored andcommitted
Moved onBoardOrPortChange() and related members/methods from Base to BaseNoGui.
1 parent 8687a78 commit abe6ff5

File tree

2 files changed

+88
-58
lines changed

2 files changed

+88
-58
lines changed

app/src/processing/app/Base.java

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,6 @@ public class Base {
7575

7676
static File buildFolder;
7777

78-
// these are static because they're used by Sketch
79-
static private File examplesFolder;
80-
static private File toolsFolder;
81-
82-
static private List<File> librariesFolders;
83-
8478
// classpath for all known libraries for p5
8579
// (both those in the p5/libs folder and those with lib subfolders
8680
// found in the sketchbook)
@@ -1161,7 +1155,7 @@ public void actionPerformed(ActionEvent e) {
11611155

11621156
// Add each of the subfolders of examples directly to the menu
11631157
try {
1164-
boolean found = addSketches(menu, examplesFolder, true);
1158+
boolean found = addSketches(menu, BaseNoGui.getExamplesFolder(), true);
11651159
if (found) menu.addSeparator();
11661160
} catch (IOException e) {
11671161
e.printStackTrace();
@@ -1245,7 +1239,7 @@ public void rebuildExamplesMenu(JMenu menu) {
12451239
menu.removeAll();
12461240

12471241
// Add examples from distribution "example" folder
1248-
boolean found = addSketches(menu, examplesFolder, false);
1242+
boolean found = addSketches(menu, BaseNoGui.getExamplesFolder(), false);
12491243
if (found) menu.addSeparator();
12501244

12511245
// Add examples from libraries
@@ -1275,39 +1269,7 @@ public LibraryList scanLibraries(File folder) throws IOException {
12751269
}
12761270

12771271
public void onBoardOrPortChange() {
1278-
TargetPlatform targetPlatform = getTargetPlatform();
1279-
if (targetPlatform == null)
1280-
return;
1281-
1282-
// Calculate paths for libraries and examples
1283-
examplesFolder = getContentFile("examples");
1284-
toolsFolder = getContentFile("tools");
1285-
1286-
File platformFolder = targetPlatform.getFolder();
1287-
librariesFolders = new ArrayList<File>();
1288-
librariesFolders.add(getContentFile("libraries"));
1289-
String core = getBoardPreferences().get("build.core");
1290-
if (core.contains(":")) {
1291-
String referencedCore = core.split(":")[0];
1292-
TargetPlatform referencedPlatform = getTargetPlatform(referencedCore, targetPlatform.getId());
1293-
if (referencedPlatform != null) {
1294-
File referencedPlatformFolder = referencedPlatform.getFolder();
1295-
librariesFolders.add(new File(referencedPlatformFolder, "libraries"));
1296-
}
1297-
}
1298-
librariesFolders.add(new File(platformFolder, "libraries"));
1299-
librariesFolders.add(getSketchbookLibrariesFolder());
1300-
1301-
// Scan for libraries in each library folder.
1302-
// Libraries located in the latest folders on the list can override
1303-
// other libraries with the same name.
1304-
try {
1305-
BaseNoGui.scanAndUpdateLibraries(librariesFolders);
1306-
} catch (IOException e) {
1307-
showWarning(_("Error"), _("Error loading libraries"), e);
1308-
}
1309-
1310-
BaseNoGui.populateImportToLibraryTable();
1272+
BaseNoGui.onBoardOrPortChange();
13111273

13121274
// Update editors status bar
13131275
for (Editor editor : editors)
@@ -1897,22 +1859,22 @@ static public LibraryList getLibraries() {
18971859

18981860

18991861
static public String getExamplesPath() {
1900-
return examplesFolder.getAbsolutePath();
1862+
return BaseNoGui.getExamplesPath();
19011863
}
19021864

19031865

19041866
static public List<File> getLibrariesPath() {
1905-
return librariesFolders;
1867+
return BaseNoGui.getLibrariesPath();
19061868
}
19071869

19081870

19091871
static public File getToolsFolder() {
1910-
return toolsFolder;
1872+
return BaseNoGui.getToolsFolder();
19111873
}
19121874

19131875

19141876
static public String getToolsPath() {
1915-
return toolsFolder.getAbsolutePath();
1877+
return BaseNoGui.getToolsPath();
19161878
}
19171879

19181880

@@ -1994,19 +1956,7 @@ static public File getSketchbookFolder() {
19941956

19951957

19961958
static public File getSketchbookLibrariesFolder() {
1997-
File libdir = new File(getSketchbookFolder(), "libraries");
1998-
if (!libdir.exists()) {
1999-
try {
2000-
libdir.mkdirs();
2001-
File readme = new File(libdir, "readme.txt");
2002-
FileWriter freadme = new FileWriter(readme);
2003-
freadme.write(_("For information on installing libraries, see: " +
2004-
"http://arduino.cc/en/Guide/Libraries\n"));
2005-
freadme.close();
2006-
} catch (Exception e) {
2007-
}
2008-
}
2009-
return libdir;
1959+
return BaseNoGui.getSketchbookLibrariesFolder();
20101960
}
20111961

20121962

app/src/processing/app/BaseNoGui.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
import java.io.File;
66
import java.io.FileInputStream;
7+
import java.io.FileWriter;
78
import java.io.IOException;
89
import java.io.InputStream;
10+
import java.util.ArrayList;
911
import java.util.Arrays;
1012
import java.util.Date;
1113
import java.util.HashMap;
@@ -47,12 +49,18 @@ public class BaseNoGui {
4749

4850
private static DiscoveryManager discoveryManager = new DiscoveryManager();
4951

52+
// these are static because they're used by Sketch
53+
static private File examplesFolder;
54+
static private File toolsFolder;
55+
5056
// maps #included files to their library folder
5157
public static Map<String, Library> importToLibraryTable;
5258

5359
// maps library name to their library folder
5460
static private LibraryList libraries;
5561

62+
static private List<File> librariesFolders;
63+
5664
static UserNotifier notifier = new BasicUserNotifier();
5765

5866
static public Map<String, TargetPackage> packages;
@@ -137,6 +145,14 @@ public static DiscoveryManager getDiscoveryManager() {
137145
return discoveryManager;
138146
}
139147

148+
static public File getExamplesFolder() {
149+
return examplesFolder;
150+
}
151+
152+
static public String getExamplesPath() {
153+
return examplesFolder.getAbsolutePath();
154+
}
155+
140156
static public File getHardwareFolder() {
141157
// calculate on the fly because it's needed by Preferences.init() to find
142158
// the boards.txt and programmers.txt preferences files (which happens
@@ -152,6 +168,10 @@ static public LibraryList getLibraries() {
152168
return libraries;
153169
}
154170

171+
static public List<File> getLibrariesPath() {
172+
return librariesFolders;
173+
}
174+
155175
/**
156176
* Return an InputStream for a file inside the Processing lib folder.
157177
*/
@@ -218,6 +238,22 @@ static public File getSketchbookHardwareFolder() {
218238
return new File(getSketchbookFolder(), "hardware");
219239
}
220240

241+
static public File getSketchbookLibrariesFolder() {
242+
File libdir = new File(getSketchbookFolder(), "libraries");
243+
if (!libdir.exists()) {
244+
try {
245+
libdir.mkdirs();
246+
File readme = new File(libdir, "readme.txt");
247+
FileWriter freadme = new FileWriter(readme);
248+
freadme.write(_("For information on installing libraries, see: " +
249+
"http://arduino.cc/en/Guide/Libraries\n"));
250+
freadme.close();
251+
} catch (Exception e) {
252+
}
253+
}
254+
return libdir;
255+
}
256+
221257
public static TargetBoard getTargetBoard() {
222258
String boardId = PreferencesData.get("board");
223259
return getTargetPlatform().getBoard(boardId);
@@ -249,6 +285,14 @@ static public TargetPlatform getTargetPlatform(String packageName,
249285
return p.get(platformName);
250286
}
251287

288+
static public File getToolsFolder() {
289+
return toolsFolder;
290+
}
291+
292+
static public String getToolsPath() {
293+
return toolsFolder.getAbsolutePath();
294+
}
295+
252296
static public LibraryList getUserLibs() {
253297
if (libraries == null)
254298
return new LibraryList();
@@ -370,6 +414,42 @@ static public String loadFile(File file) throws IOException {
370414
return PApplet.join(contents, "\n");
371415
}
372416

417+
static public void onBoardOrPortChange() {
418+
TargetPlatform targetPlatform = getTargetPlatform();
419+
if (targetPlatform == null)
420+
return;
421+
422+
// Calculate paths for libraries and examples
423+
examplesFolder = getContentFile("examples");
424+
toolsFolder = getContentFile("tools");
425+
426+
File platformFolder = targetPlatform.getFolder();
427+
librariesFolders = new ArrayList<File>();
428+
librariesFolders.add(getContentFile("libraries"));
429+
String core = getBoardPreferences().get("build.core");
430+
if (core.contains(":")) {
431+
String referencedCore = core.split(":")[0];
432+
TargetPlatform referencedPlatform = getTargetPlatform(referencedCore, targetPlatform.getId());
433+
if (referencedPlatform != null) {
434+
File referencedPlatformFolder = referencedPlatform.getFolder();
435+
librariesFolders.add(new File(referencedPlatformFolder, "libraries"));
436+
}
437+
}
438+
librariesFolders.add(new File(platformFolder, "libraries"));
439+
librariesFolders.add(getSketchbookLibrariesFolder());
440+
441+
// Scan for libraries in each library folder.
442+
// Libraries located in the latest folders on the list can override
443+
// other libraries with the same name.
444+
try {
445+
scanAndUpdateLibraries(librariesFolders);
446+
} catch (IOException e) {
447+
showWarning(_("Error"), _("Error loading libraries"), e);
448+
}
449+
450+
populateImportToLibraryTable();
451+
}
452+
373453
static public void populateImportToLibraryTable() {
374454
// Populate importToLibraryTable
375455
importToLibraryTable = new HashMap<String, Library>();

0 commit comments

Comments
 (0)