|
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;
|
@@ -1053,30 +1049,6 @@ protected void rebuildSketchbookMenu(JMenu menu) {
|
1053 | 1049 | }
|
1054 | 1050 | }
|
1055 | 1051 |
|
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 |
| - |
1080 | 1052 | private List<ContributedLibrary> getSortedLibraries() {
|
1081 | 1053 | List<ContributedLibrary> installedLibraries = new LinkedList<ContributedLibrary>(BaseNoGui.librariesIndexer.getInstalledLibraries());
|
1082 | 1054 | Collections.sort(installedLibraries, new LibraryByTypeComparator());
|
@@ -1159,36 +1131,165 @@ public void rebuildExamplesMenu(JMenu menu) {
|
1159 | 1131 | menu.addSeparator();
|
1160 | 1132 | }
|
1161 | 1133 |
|
| 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 | + |
1162 | 1224 | // Add examples from libraries
|
1163 |
| - LibraryList ideLibs = getIDELibs(); |
1164 |
| - ideLibs.sort(); |
1165 | 1225 | if (!ideLibs.isEmpty()) {
|
1166 |
| - label = new JMenuItem(tr("Examples from Libraries")); |
| 1226 | + ideLibs.sort(); |
| 1227 | + label = new JMenuItem(tr("Examples for any board")); |
1167 | 1228 | label.setEnabled(false);
|
1168 | 1229 | menu.add(label);
|
1169 | 1230 | }
|
1170 | 1231 | for (UserLibrary lib : ideLibs) {
|
1171 | 1232 | addSketchesSubmenu(menu, lib);
|
1172 | 1233 | }
|
1173 | 1234 |
|
1174 |
| - LibraryList retiredIdeLibs = getIDERetiredLibs(); |
1175 |
| - retiredIdeLibs.sort(); |
1176 | 1235 | if (!retiredIdeLibs.isEmpty()) {
|
| 1236 | + retiredIdeLibs.sort(); |
1177 | 1237 | JMenu retired = new JMenu(tr("RETIRED"));
|
1178 | 1238 | menu.add(retired);
|
1179 | 1239 | for (UserLibrary lib : retiredIdeLibs) {
|
1180 | 1240 | addSketchesSubmenu(retired, lib);
|
1181 | 1241 | }
|
1182 | 1242 | }
|
1183 | 1243 |
|
1184 |
| - LibraryList userLibs = getUserLibs(); |
1185 |
| - if (userLibs.size() > 0) { |
| 1244 | + if (!platformLibs.isEmpty()) { |
1186 | 1245 | 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(); |
1188 | 1269 | label = new JMenuItem(tr("Examples from Custom Libraries"));
|
1189 | 1270 | label.setEnabled(false);
|
1190 | 1271 | 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) { |
1192 | 1293 | addSketchesSubmenu(menu, lib);
|
1193 | 1294 | }
|
1194 | 1295 | }
|
|
0 commit comments