|
80 | 80 | */
|
81 | 81 | public class Base {
|
82 | 82 |
|
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 |
| - |
87 | 83 | private static final int RECENT_SKETCHES_MAX_SIZE = 10;
|
88 | 84 |
|
89 | 85 | private static boolean commandLine;
|
@@ -1102,30 +1098,6 @@ protected void rebuildSketchbookMenu(JMenu menu) {
|
1102 | 1098 | }
|
1103 | 1099 | }
|
1104 | 1100 |
|
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 |
| - |
1129 | 1101 | private List<ContributedLibrary> getSortedLibraries() {
|
1130 | 1102 | List<ContributedLibrary> installedLibraries = new LinkedList<ContributedLibrary>(BaseNoGui.librariesIndexer.getInstalledLibraries());
|
1131 | 1103 | Collections.sort(installedLibraries, new LibraryByTypeComparator());
|
@@ -1208,36 +1180,157 @@ public void rebuildExamplesMenu(JMenu menu) {
|
1208 | 1180 | menu.addSeparator();
|
1209 | 1181 | }
|
1210 | 1182 |
|
| 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 | + |
1211 | 1265 | // Add examples from libraries
|
1212 |
| - LibraryList ideLibs = getIDELibs(); |
1213 |
| - ideLibs.sort(); |
1214 | 1266 | if (!ideLibs.isEmpty()) {
|
1215 |
| - label = new JMenuItem(tr("Examples from Libraries")); |
| 1267 | + ideLibs.sort(); |
| 1268 | + label = new JMenuItem(tr("Examples from Built-in Libraries")); |
1216 | 1269 | label.setEnabled(false);
|
1217 | 1270 | menu.add(label);
|
1218 | 1271 | }
|
1219 | 1272 | for (UserLibrary lib : ideLibs) {
|
1220 | 1273 | addSketchesSubmenu(menu, lib);
|
1221 | 1274 | }
|
1222 | 1275 |
|
1223 |
| - LibraryList retiredIdeLibs = getIDERetiredLibs(); |
1224 |
| - retiredIdeLibs.sort(); |
1225 | 1276 | if (!retiredIdeLibs.isEmpty()) {
|
| 1277 | + retiredIdeLibs.sort(); |
1226 | 1278 | JMenu retired = new JMenu(tr("RETIRED"));
|
1227 | 1279 | menu.add(retired);
|
1228 | 1280 | for (UserLibrary lib : retiredIdeLibs) {
|
1229 | 1281 | addSketchesSubmenu(retired, lib);
|
1230 | 1282 | }
|
1231 | 1283 | }
|
1232 | 1284 |
|
1233 |
| - LibraryList userLibs = getUserLibs(); |
1234 |
| - if (userLibs.size() > 0) { |
| 1285 | + if (!platformLibs.isEmpty()) { |
1235 | 1286 | 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(); |
1237 | 1310 | label = new JMenuItem(tr("Examples from Custom Libraries"));
|
1238 | 1311 | label.setEnabled(false);
|
1239 | 1312 | 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) { |
1241 | 1334 | addSketchesSubmenu(menu, lib);
|
1242 | 1335 | }
|
1243 | 1336 | }
|
|
0 commit comments