Skip to content

Commit 6358071

Browse files
committed
Merge remote-tracking branch 'cmaglie/examples-menu-take2'
2 parents c58e2e6 + 096e545 commit 6358071

File tree

2 files changed

+139
-37
lines changed

2 files changed

+139
-37
lines changed

app/src/processing/app/Base.java

Lines changed: 138 additions & 37 deletions
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;
@@ -1053,30 +1049,6 @@ protected void rebuildSketchbookMenu(JMenu menu) {
10531049
}
10541050
}
10551051

1056-
public LibraryList getIDELibs() {
1057-
LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1058-
List<UserLibrary> libs = installedLibraries.stream()
1059-
.filter(CONTRIBUTED.negate())
1060-
.filter(RETIRED.negate())
1061-
.filter(COMPATIBLE)
1062-
.collect(Collectors.toList());
1063-
return new LibraryList(libs);
1064-
}
1065-
1066-
public LibraryList getIDERetiredLibs() {
1067-
LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1068-
List<UserLibrary> libs = installedLibraries.stream()
1069-
.filter(RETIRED)
1070-
.collect(Collectors.toList());
1071-
return new LibraryList(libs);
1072-
}
1073-
1074-
public LibraryList getUserLibs() {
1075-
LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1076-
List<UserLibrary> libs = installedLibraries.stream().filter(CONTRIBUTED).collect(Collectors.toList());
1077-
return new LibraryList(libs);
1078-
}
1079-
10801052
private List<ContributedLibrary> getSortedLibraries() {
10811053
List<ContributedLibrary> installedLibraries = new LinkedList<ContributedLibrary>(BaseNoGui.librariesIndexer.getInstalledLibraries());
10821054
Collections.sort(installedLibraries, new LibraryByTypeComparator());
@@ -1159,36 +1131,165 @@ public void rebuildExamplesMenu(JMenu menu) {
11591131
menu.addSeparator();
11601132
}
11611133

1134+
// Libraries can come from 4 locations: collect info about all four
1135+
File ideLibraryPath = BaseNoGui.getContentFile("libraries");
1136+
File sketchbookLibraryPath = BaseNoGui.getSketchbookLibrariesFolder();
1137+
File platformLibraryPath = null;
1138+
File referencedPlatformLibraryPath = null;
1139+
String platformName = null;
1140+
String boardId = null;
1141+
String referencedPlatformName = null;
1142+
String myArch = null;
1143+
TargetPlatform targetPlatform = BaseNoGui.getTargetPlatform();
1144+
if (targetPlatform != null) {
1145+
myArch = targetPlatform.getId();
1146+
boardId = BaseNoGui.getTargetBoard().getName();
1147+
platformName = targetPlatform.getPreferences().get("name");
1148+
platformLibraryPath = new File(targetPlatform.getFolder(), "libraries");
1149+
String core = BaseNoGui.getBoardPreferences().get("build.core", "arduino");
1150+
if (core.contains(":")) {
1151+
String refcore = core.split(":")[0];
1152+
TargetPlatform referencedPlatform = BaseNoGui.getTargetPlatform(refcore, myArch);
1153+
if (referencedPlatform != null) {
1154+
referencedPlatformName = referencedPlatform.getPreferences().get("name");
1155+
referencedPlatformLibraryPath = new File(referencedPlatform.getFolder(), "libraries");
1156+
}
1157+
}
1158+
}
1159+
1160+
// Divide the libraries into 7 lists, corresponding to the 4 locations
1161+
// with the retired IDE libs further divided into their own list, and
1162+
// any incompatible sketchbook libs further divided into their own list.
1163+
// The 7th list of "other" libraries should always be empty, but serves
1164+
// as a safety feature to prevent any library from vanishing.
1165+
LibraryList allLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1166+
LibraryList ideLibs = new LibraryList();
1167+
LibraryList retiredIdeLibs = new LibraryList();
1168+
LibraryList platformLibs = new LibraryList();
1169+
LibraryList referencedPlatformLibs = new LibraryList();
1170+
LibraryList sketchbookLibs = new LibraryList();
1171+
LibraryList sketchbookIncompatibleLibs = new LibraryList();
1172+
LibraryList otherLibs = new LibraryList();
1173+
for (UserLibrary lib : allLibraries) {
1174+
// Get the library's location - used for sorting into categories
1175+
File libraryLocation = lib.getInstalledFolder().getParentFile();
1176+
// Is this library compatible?
1177+
List<String> arch = lib.getArchitectures();
1178+
boolean compatible;
1179+
if (myArch == null || arch == null || arch.contains("*")) {
1180+
compatible = true;
1181+
} else {
1182+
compatible = arch.contains(myArch);
1183+
}
1184+
// IDE Libaries (including retired)
1185+
if (libraryLocation.equals(ideLibraryPath)) {
1186+
if (compatible) {
1187+
// only compatible IDE libs are shown
1188+
boolean retired = false;
1189+
List<String> types = lib.getTypes();
1190+
if (types != null) retired = types.contains("Retired");
1191+
if (retired) {
1192+
retiredIdeLibs.add(lib);
1193+
} else {
1194+
ideLibs.add(lib);
1195+
}
1196+
}
1197+
// Platform Libraries
1198+
} else if (libraryLocation.equals(platformLibraryPath)) {
1199+
// all platform libs are assumed to be compatible
1200+
platformLibs.add(lib);
1201+
// Referenced Platform Libraries
1202+
} else if (libraryLocation.equals(referencedPlatformLibraryPath)) {
1203+
// all referenced platform libs are assumed to be compatible
1204+
referencedPlatformLibs.add(lib);
1205+
// Sketchbook Libraries (including incompatible)
1206+
} else if (libraryLocation.equals(sketchbookLibraryPath)) {
1207+
if (compatible) {
1208+
// libraries promoted from sketchbook (behave as builtin)
1209+
if (lib.getTypes() != null && lib.getTypes().contains("Arduino")
1210+
&& lib.getArchitectures().contains("*")) {
1211+
ideLibs.add(lib);
1212+
} else {
1213+
sketchbookLibs.add(lib);
1214+
}
1215+
} else {
1216+
sketchbookIncompatibleLibs.add(lib);
1217+
}
1218+
// Other libraries of unknown type (should never occur)
1219+
} else {
1220+
otherLibs.add(lib);
1221+
}
1222+
}
1223+
11621224
// Add examples from libraries
1163-
LibraryList ideLibs = getIDELibs();
1164-
ideLibs.sort();
11651225
if (!ideLibs.isEmpty()) {
1166-
label = new JMenuItem(tr("Examples from Libraries"));
1226+
ideLibs.sort();
1227+
label = new JMenuItem(tr("Examples for any board"));
11671228
label.setEnabled(false);
11681229
menu.add(label);
11691230
}
11701231
for (UserLibrary lib : ideLibs) {
11711232
addSketchesSubmenu(menu, lib);
11721233
}
11731234

1174-
LibraryList retiredIdeLibs = getIDERetiredLibs();
1175-
retiredIdeLibs.sort();
11761235
if (!retiredIdeLibs.isEmpty()) {
1236+
retiredIdeLibs.sort();
11771237
JMenu retired = new JMenu(tr("RETIRED"));
11781238
menu.add(retired);
11791239
for (UserLibrary lib : retiredIdeLibs) {
11801240
addSketchesSubmenu(retired, lib);
11811241
}
11821242
}
11831243

1184-
LibraryList userLibs = getUserLibs();
1185-
if (userLibs.size() > 0) {
1244+
if (!platformLibs.isEmpty()) {
11861245
menu.addSeparator();
1187-
userLibs.sort();
1246+
platformLibs.sort();
1247+
label = new JMenuItem(I18n.format(tr("Examples for {0}"), boardId));
1248+
label.setEnabled(false);
1249+
menu.add(label);
1250+
for (UserLibrary lib : platformLibs) {
1251+
addSketchesSubmenu(menu, lib);
1252+
}
1253+
}
1254+
1255+
if (!referencedPlatformLibs.isEmpty()) {
1256+
menu.addSeparator();
1257+
referencedPlatformLibs.sort();
1258+
label = new JMenuItem(I18n.format(tr("Examples for {0}"), referencedPlatformName));
1259+
label.setEnabled(false);
1260+
menu.add(label);
1261+
for (UserLibrary lib : referencedPlatformLibs) {
1262+
addSketchesSubmenu(menu, lib);
1263+
}
1264+
}
1265+
1266+
if (!sketchbookLibs.isEmpty()) {
1267+
menu.addSeparator();
1268+
sketchbookLibs.sort();
11881269
label = new JMenuItem(tr("Examples from Custom Libraries"));
11891270
label.setEnabled(false);
11901271
menu.add(label);
1191-
for (UserLibrary lib : userLibs) {
1272+
for (UserLibrary lib : sketchbookLibs) {
1273+
addSketchesSubmenu(menu, lib);
1274+
}
1275+
}
1276+
1277+
if (!sketchbookIncompatibleLibs.isEmpty()) {
1278+
sketchbookIncompatibleLibs.sort();
1279+
JMenu incompatible = new JMenu(tr("INCOMPATIBLE"));
1280+
menu.add(incompatible);
1281+
for (UserLibrary lib : sketchbookIncompatibleLibs) {
1282+
addSketchesSubmenu(incompatible, lib);
1283+
}
1284+
}
1285+
1286+
if (!otherLibs.isEmpty()) {
1287+
menu.addSeparator();
1288+
otherLibs.sort();
1289+
label = new JMenuItem(tr("Examples from Other Libraries"));
1290+
label.setEnabled(false);
1291+
menu.add(label);
1292+
for (UserLibrary lib : otherLibs) {
11921293
addSketchesSubmenu(menu, lib);
11931294
}
11941295
}

build/shared/revisions.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ ARDUINO 1.6.12
33
[ide]
44
* Refactor Editor code, solving some long standing bugs and cleaning up everything. Thanks @matthijskooijman
55
* Add initial Cloud API integration. When plugging a new board, if an additional core is needed, the user will be prompted to install it
6+
* Improved examples menu organization. Thanks @PaulStoffregen
67

78
[core]
89
* avr: Add support for internal 2.56V and 2.56V ext. cap analog references on the ATtiny25/45/85. Thanks @mischnic

0 commit comments

Comments
 (0)