Skip to content

Commit eee54f1

Browse files
committed
Merge branch 'examplesmenu' of git://github.com/PaulStoffregen/Arduino into HEAD
2 parents 7f63012 + 9b324bc commit eee54f1

File tree

2 files changed

+131
-37
lines changed

2 files changed

+131
-37
lines changed

app/src/processing/app/Base.java

+130-37
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@
8080
*/
8181
public class Base {
8282

83-
public static final Predicate<UserLibrary> CONTRIBUTED = library -> library.getTypes() == null || library.getTypes().isEmpty() || library.getTypes().contains("Contributed");
84-
public static final Predicate<UserLibrary> RETIRED = library -> library.getTypes() != null && library.getTypes().contains("Retired");
85-
public static final Predicate<UserLibrary> COMPATIBLE = library -> library.getArchitectures() != null && (library.getArchitectures().contains("*") || library.getArchitectures().contains(BaseNoGui.getTargetPlatform().getId()));
86-
8783
private static final int RECENT_SKETCHES_MAX_SIZE = 10;
8884

8985
private static boolean commandLine;
@@ -1102,30 +1098,6 @@ protected void rebuildSketchbookMenu(JMenu menu) {
11021098
}
11031099
}
11041100

1105-
public LibraryList getIDELibs() {
1106-
LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1107-
List<UserLibrary> libs = installedLibraries.stream()
1108-
.filter(CONTRIBUTED.negate())
1109-
.filter(RETIRED.negate())
1110-
.filter(COMPATIBLE)
1111-
.collect(Collectors.toList());
1112-
return new LibraryList(libs);
1113-
}
1114-
1115-
public LibraryList getIDERetiredLibs() {
1116-
LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1117-
List<UserLibrary> libs = installedLibraries.stream()
1118-
.filter(RETIRED)
1119-
.collect(Collectors.toList());
1120-
return new LibraryList(libs);
1121-
}
1122-
1123-
public LibraryList getUserLibs() {
1124-
LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1125-
List<UserLibrary> libs = installedLibraries.stream().filter(CONTRIBUTED).collect(Collectors.toList());
1126-
return new LibraryList(libs);
1127-
}
1128-
11291101
private List<ContributedLibrary> getSortedLibraries() {
11301102
List<ContributedLibrary> installedLibraries = new LinkedList<ContributedLibrary>(BaseNoGui.librariesIndexer.getInstalledLibraries());
11311103
Collections.sort(installedLibraries, new LibraryByTypeComparator());
@@ -1208,36 +1180,157 @@ public void rebuildExamplesMenu(JMenu menu) {
12081180
menu.addSeparator();
12091181
}
12101182

1183+
// Libraries can come from 4 locations: collect info about all four
1184+
File ideLibraryPath = BaseNoGui.getContentFile("libraries");
1185+
File sketchbookLibraryPath = BaseNoGui.getSketchbookLibrariesFolder();
1186+
File platformLibraryPath = null;
1187+
File referencedPlatformLibraryPath = null;
1188+
String platformName = null;
1189+
String referencedPlatformName = null;
1190+
String myArch = null;
1191+
TargetPlatform targetPlatform = BaseNoGui.getTargetPlatform();
1192+
if (targetPlatform != null) {
1193+
myArch = targetPlatform.getId();
1194+
platformName = targetPlatform.getPreferences().get("name");
1195+
platformLibraryPath = new File(targetPlatform.getFolder(), "libraries");
1196+
String core = BaseNoGui.getBoardPreferences().get("build.core", "arduino");
1197+
if (core.contains(":")) {
1198+
String refcore = core.split(":")[0];
1199+
TargetPlatform referencedPlatform = BaseNoGui.getTargetPlatform(refcore, myArch);
1200+
if (referencedPlatform != null) {
1201+
referencedPlatformName = referencedPlatform.getPreferences().get("name");
1202+
referencedPlatformLibraryPath = new File(referencedPlatform.getFolder(), "libraries");
1203+
}
1204+
}
1205+
}
1206+
1207+
// Divide the libraries into 7 lists, corresponding to the 4 locations
1208+
// with the retired IDE libs further divided into their own list, and
1209+
// any incompatible sketchbook libs further divided into their own list.
1210+
// The 7th list of "other" libraries should always be empty, but serves
1211+
// as a safety feature to prevent any library from vanishing.
1212+
LibraryList allLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1213+
LibraryList ideLibs = new LibraryList();
1214+
LibraryList retiredIdeLibs = new LibraryList();
1215+
LibraryList platformLibs = new LibraryList();
1216+
LibraryList referencedPlatformLibs = new LibraryList();
1217+
LibraryList sketchbookLibs = new LibraryList();
1218+
LibraryList sketchbookIncompatibleLibs = new LibraryList();
1219+
LibraryList otherLibs = new LibraryList();
1220+
for (UserLibrary lib : allLibraries) {
1221+
// Get the library's location - used for sorting into categories
1222+
File libraryLocation = lib.getInstalledFolder().getParentFile();
1223+
// Is this library compatible?
1224+
List<String> arch = lib.getArchitectures();
1225+
boolean compatible;
1226+
if (myArch == null || arch == null || arch.contains("*")) {
1227+
compatible = true;
1228+
} else {
1229+
compatible = arch.contains(myArch);
1230+
}
1231+
// IDE Libaries (including retired)
1232+
if (libraryLocation.equals(ideLibraryPath)) {
1233+
if (compatible) {
1234+
// only compatible IDE libs are shown
1235+
boolean retired = false;
1236+
List<String> types = lib.getTypes();
1237+
if (types != null) retired = types.contains("Retired");
1238+
if (retired) {
1239+
retiredIdeLibs.add(lib);
1240+
} else {
1241+
ideLibs.add(lib);
1242+
}
1243+
}
1244+
// Platform Libraries
1245+
} else if (libraryLocation.equals(platformLibraryPath)) {
1246+
// all platform libs are assumed to be compatible
1247+
platformLibs.add(lib);
1248+
// Referenced Platform Libraries
1249+
} else if (libraryLocation.equals(referencedPlatformLibraryPath)) {
1250+
// all referenced platform libs are assumed to be compatible
1251+
referencedPlatformLibs.add(lib);
1252+
// Sketchbook Libraries (including incompatible)
1253+
} else if (libraryLocation.equals(sketchbookLibraryPath)) {
1254+
if (compatible) {
1255+
sketchbookLibs.add(lib);
1256+
} else {
1257+
sketchbookIncompatibleLibs.add(lib);
1258+
}
1259+
// Other libraries of unknown type (should never occur)
1260+
} else {
1261+
otherLibs.add(lib);
1262+
}
1263+
}
1264+
12111265
// Add examples from libraries
1212-
LibraryList ideLibs = getIDELibs();
1213-
ideLibs.sort();
12141266
if (!ideLibs.isEmpty()) {
1215-
label = new JMenuItem(tr("Examples from Libraries"));
1267+
ideLibs.sort();
1268+
label = new JMenuItem(tr("Examples from Built-in Libraries"));
12161269
label.setEnabled(false);
12171270
menu.add(label);
12181271
}
12191272
for (UserLibrary lib : ideLibs) {
12201273
addSketchesSubmenu(menu, lib);
12211274
}
12221275

1223-
LibraryList retiredIdeLibs = getIDERetiredLibs();
1224-
retiredIdeLibs.sort();
12251276
if (!retiredIdeLibs.isEmpty()) {
1277+
retiredIdeLibs.sort();
12261278
JMenu retired = new JMenu(tr("RETIRED"));
12271279
menu.add(retired);
12281280
for (UserLibrary lib : retiredIdeLibs) {
12291281
addSketchesSubmenu(retired, lib);
12301282
}
12311283
}
12321284

1233-
LibraryList userLibs = getUserLibs();
1234-
if (userLibs.size() > 0) {
1285+
if (!platformLibs.isEmpty()) {
12351286
menu.addSeparator();
1236-
userLibs.sort();
1287+
platformLibs.sort();
1288+
label = new JMenuItem(I18n.format(tr("Examples from {0} Libraries"), platformName));
1289+
label.setEnabled(false);
1290+
menu.add(label);
1291+
for (UserLibrary lib : platformLibs) {
1292+
addSketchesSubmenu(menu, lib);
1293+
}
1294+
}
1295+
1296+
if (!referencedPlatformLibs.isEmpty()) {
1297+
menu.addSeparator();
1298+
referencedPlatformLibs.sort();
1299+
label = new JMenuItem(I18n.format(tr("Examples from {0} Libraries"), referencedPlatformName));
1300+
label.setEnabled(false);
1301+
menu.add(label);
1302+
for (UserLibrary lib : referencedPlatformLibs) {
1303+
addSketchesSubmenu(menu, lib);
1304+
}
1305+
}
1306+
1307+
if (!sketchbookLibs.isEmpty()) {
1308+
menu.addSeparator();
1309+
sketchbookLibs.sort();
12371310
label = new JMenuItem(tr("Examples from Custom Libraries"));
12381311
label.setEnabled(false);
12391312
menu.add(label);
1240-
for (UserLibrary lib : userLibs) {
1313+
for (UserLibrary lib : sketchbookLibs) {
1314+
addSketchesSubmenu(menu, lib);
1315+
}
1316+
}
1317+
1318+
if (!sketchbookIncompatibleLibs.isEmpty()) {
1319+
sketchbookIncompatibleLibs.sort();
1320+
JMenu incompatible = new JMenu(tr("INCOMPATIBLE"));
1321+
menu.add(incompatible);
1322+
for (UserLibrary lib : sketchbookIncompatibleLibs) {
1323+
addSketchesSubmenu(incompatible, lib);
1324+
}
1325+
}
1326+
1327+
if (!otherLibs.isEmpty()) {
1328+
menu.addSeparator();
1329+
otherLibs.sort();
1330+
label = new JMenuItem(tr("Examples from Other Libraries"));
1331+
label.setEnabled(false);
1332+
menu.add(label);
1333+
for (UserLibrary lib : otherLibs) {
12411334
addSketchesSubmenu(menu, lib);
12421335
}
12431336
}

build/shared/revisions.txt

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ARDUINO 1.6.11
88
(see https://github.com/arduino/Arduino/issues/5186 for details)
99
* avrdude: reverted to version 6.0.1, until all discovered regressions are solved
1010
(see https://github.com/arduino/Arduino/issues?q=is%3Aissue+is%3Aopen+label%3A%22Component%3A+Avrdude+6.3%22 for details)
11+
* Improved how examples are grouped in the menus. Thanks @PaulStoffregen
1112

1213
ARDUINO 1.6.10 - 2016.07.26
1314

0 commit comments

Comments
 (0)