|
25 | 25 | import cc.arduino.contributions.BuiltInCoreIsNewerCheck;
|
26 | 26 | import cc.arduino.contributions.DownloadableContributionVersionComparator;
|
27 | 27 | import cc.arduino.contributions.VersionHelper;
|
28 |
| -import cc.arduino.contributions.libraries.ContributedLibrary; |
29 |
| -import cc.arduino.contributions.libraries.LibrariesIndexer; |
30 |
| -import cc.arduino.contributions.libraries.LibraryInstaller; |
| 28 | +import cc.arduino.contributions.libraries.*; |
31 | 29 | import cc.arduino.contributions.libraries.ui.LibraryManagerUI;
|
32 | 30 | import cc.arduino.contributions.packages.ContributedPlatform;
|
33 | 31 | import cc.arduino.contributions.packages.ContributionInstaller;
|
@@ -1195,6 +1193,13 @@ public LibraryList getUserLibs() {
|
1195 | 1193 | return new LibraryList(libs);
|
1196 | 1194 | }
|
1197 | 1195 |
|
| 1196 | + private List<ContributedLibrary> getSortedLibraries() { |
| 1197 | + List<ContributedLibrary> installedLibraries = new LinkedList<ContributedLibrary>(BaseNoGui.librariesIndexer.getInstalledLibraries()); |
| 1198 | + Collections.sort(installedLibraries, new LibraryByTypeComparator()); |
| 1199 | + Collections.sort(installedLibraries, new LibraryOfSameTypeComparator()); |
| 1200 | + return installedLibraries; |
| 1201 | + } |
| 1202 | + |
1198 | 1203 | public void rebuildImportMenu(JMenu importMenu) {
|
1199 | 1204 | if (importMenu == null)
|
1200 | 1205 | return;
|
@@ -1225,33 +1230,35 @@ public void actionPerformed(ActionEvent e) {
|
1225 | 1230 | TargetPlatform targetPlatform = getTargetPlatform();
|
1226 | 1231 |
|
1227 | 1232 | if (targetPlatform != null) {
|
1228 |
| - LibraryList ideLibs = getIDELibs(); |
1229 |
| - LibraryList userLibs = getUserLibs(); |
1230 |
| - try { |
1231 |
| - // Find the current target. Get the platform, and then select the |
1232 |
| - // correct name and core path. |
1233 |
| - String platformNameLabel; |
1234 |
| - platformNameLabel = StringUtils.capitalize(targetPlatform.getContainerPackage().getId()) + "/" + StringUtils.capitalize(targetPlatform.getId()); |
1235 |
| - platformNameLabel = I18n.format(_("{0} libraries"), platformNameLabel); |
1236 |
| - JMenuItem platformItem = new JMenuItem(_(platformNameLabel)); |
1237 |
| - platformItem.setEnabled(false); |
1238 |
| - importMenu.add(platformItem); |
1239 |
| - |
1240 |
| - if (ideLibs.size() > 0) { |
1241 |
| - addLibraries(importMenu, ideLibs); |
1242 |
| - } |
1243 |
| - |
1244 |
| - if (userLibs.size() > 0) { |
1245 |
| - if (ideLibs.size() > 0) { |
| 1233 | + List<ContributedLibrary> libs = getSortedLibraries(); |
| 1234 | + String lastLibType = null; |
| 1235 | + for (ContributedLibrary lib : libs) { |
| 1236 | + if (lastLibType == null || !lastLibType.equals(lib.getTypes().get(0))) { |
| 1237 | + if (lastLibType != null) { |
1246 | 1238 | importMenu.addSeparator();
|
1247 | 1239 | }
|
1248 |
| - JMenuItem contributedLibraryItem = new JMenuItem(_("Contributed libraries")); |
1249 |
| - contributedLibraryItem.setEnabled(false); |
1250 |
| - importMenu.add(contributedLibraryItem); |
1251 |
| - addLibraries(importMenu, userLibs); |
| 1240 | + lastLibType = lib.getTypes().get(0); |
| 1241 | + JMenuItem platformItem = new JMenuItem(I18n.format(_("{0} libraries"), lastLibType)); |
| 1242 | + platformItem.setEnabled(false); |
| 1243 | + importMenu.add(platformItem); |
1252 | 1244 | }
|
1253 |
| - } catch (IOException e) { |
1254 |
| - e.printStackTrace(); |
| 1245 | + |
| 1246 | + AbstractAction action = new AbstractAction(lib.getName()) { |
| 1247 | + public void actionPerformed(ActionEvent event) { |
| 1248 | + UserLibrary l = (UserLibrary) getValue("library"); |
| 1249 | + try { |
| 1250 | + activeEditor.getSketch().importLibrary(l); |
| 1251 | + } catch (IOException e) { |
| 1252 | + showWarning(_("Error"), I18n.format("Unable to list header files in {0}", l.getSrcFolder()), e); |
| 1253 | + } |
| 1254 | + } |
| 1255 | + }; |
| 1256 | + action.putValue("library", lib); |
| 1257 | + |
| 1258 | + // Add new element at the bottom |
| 1259 | + JMenuItem item = new JMenuItem(action); |
| 1260 | + item.putClientProperty("library", lib); |
| 1261 | + importMenu.add(item); |
1255 | 1262 | }
|
1256 | 1263 | }
|
1257 | 1264 | }
|
|
0 commit comments