diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleases.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleases.java index a1eb1424d..b5e6e8e34 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleases.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleases.java @@ -37,6 +37,7 @@ import cc.arduino.contributions.ui.FilteredAbstractTableModel; import java.util.Collections; import java.util.LinkedList; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; public class ContributedLibraryReleases { @@ -76,15 +77,15 @@ public class ContributedLibraryReleases { selected = getLatest(); } - public ContributedLibrary getInstalled() { + public Optional getInstalled() { List installedReleases = releases.stream().filter(new InstalledPredicate()).collect(Collectors.toList()); Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator()); if (installedReleases.isEmpty()) { - return null; + return Optional.empty(); } - return installedReleases.get(0); + return Optional.of(installedReleases.get(0)); } public ContributedLibrary getLatest() { diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java index 22cc695db..5b0aa2fe4 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java @@ -36,6 +36,7 @@ import java.awt.Component; import java.util.Collections; import java.util.LinkedList; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import javax.swing.JComboBox; @@ -85,7 +86,7 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell { setEnabled(true); - final ContributedLibrary installed = editorValue.getInstalled(); + final Optional mayInstalled = editorValue.getInstalled(); List releases = editorValue.getReleases().stream() .filter(new OnlyUpstreamReleasePredicate()) @@ -97,7 +98,7 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell { .filter(new InstalledPredicate()).filter(new BuiltInPredicate()) .collect(Collectors.toList()); - if (installed != null && !installedBuiltIn.contains(installed)) { + if (mayInstalled.isPresent() && !installedBuiltIn.contains(mayInstalled.get())) { uninstalledReleases.addAll(installedBuiltIn); } @@ -111,8 +112,8 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell { final List uninstalledNewerReleases = new LinkedList<>(); uninstalledReleases.stream().forEach(input -> { - if (installed == null - || VersionComparator.greaterThan(installed, input)) { + if (!mayInstalled.isPresent() + || VersionComparator.greaterThan(mayInstalled.get(), input)) { uninstalledPreviousReleases.add(input); } else { uninstalledNewerReleases.add(input); @@ -122,18 +123,18 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell { uninstalledPreviousReleases.forEach(editorCell.downgradeChooser::addItem); editorCell.downgradeChooser - .setVisible(installed != null + .setVisible(mayInstalled.isPresent() && (!uninstalledPreviousReleases.isEmpty() || uninstalledNewerReleases.size() > 1)); editorCell.downgradeButton - .setVisible(installed != null + .setVisible(mayInstalled.isPresent() && (!uninstalledPreviousReleases.isEmpty() || uninstalledNewerReleases.size() > 1)); editorCell.versionToInstallChooser.removeAllItems(); uninstalledReleases.forEach(editorCell.versionToInstallChooser::addItem); editorCell.versionToInstallChooser - .setVisible(installed == null && uninstalledReleases.size() > 1); + .setVisible(!mayInstalled.isPresent() && uninstalledReleases.size() > 1); editorCell.setBackground(new Color(218, 227, 227)); // #dae3e3 return editorCell; @@ -153,7 +154,7 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell { } protected void onInstall(ContributedLibrary selected, - ContributedLibrary installed) { + Optional mayInstalled) { // Empty } diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellJPanel.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellJPanel.java index 5df53d4e8..b3bda52c1 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellJPanel.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellJPanel.java @@ -7,6 +7,7 @@ import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Insets; +import java.util.Optional; import javax.swing.Box; import javax.swing.BoxLayout; @@ -116,16 +117,16 @@ public class ContributedLibraryTableCellJPanel extends JPanel { return; ContributedLibrary selected = releases.getSelected(); - ContributedLibrary installed = releases.getInstalled(); + Optional mayInstalled = releases.getInstalled(); boolean installable, upgradable; - if (installed == null) { + if (!mayInstalled.isPresent()) { installable = true; upgradable = false; } else { installable = false; upgradable = new DownloadableContributionVersionComparator() - .compare(selected, installed) > 0; + .compare(selected, mayInstalled.get()) > 0; } if (installable) { installButton.setText(tr("Install")); @@ -151,7 +152,7 @@ public class ContributedLibraryTableCellJPanel extends JPanel { // Library name... desc += format("{0}", name); - if (installed != null && installed.isReadOnly()) { + if (mayInstalled.isPresent() && mayInstalled.get().isReadOnly()) { desc += " Built-In "; } @@ -162,8 +163,8 @@ public class ContributedLibraryTableCellJPanel extends JPanel { } // ...version. - if (installed != null) { - String installedVer = installed.getParsedVersion(); + if (mayInstalled.isPresent()) { + String installedVer = mayInstalled.get().getParsedVersion(); if (installedVer == null) { desc += " " + tr("Version unknown"); } else { @@ -172,7 +173,7 @@ public class ContributedLibraryTableCellJPanel extends JPanel { } desc += ""; - if (installed != null) { + if (mayInstalled.isPresent()) { desc += " INSTALLED"; } diff --git a/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java b/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java index aa8f8505f..0ec5f5365 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java +++ b/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java @@ -38,6 +38,7 @@ import java.awt.event.ActionListener; import java.util.Collection; import java.util.Collections; import java.util.LinkedList; +import java.util.Optional; import java.util.function.Predicate; import javax.swing.Box; @@ -80,11 +81,11 @@ public class LibraryManagerUI extends InstallerJDialog { protected InstallerTableCell createCellEditor() { return new ContributedLibraryTableCellEditor() { @Override - protected void onInstall(ContributedLibrary selectedLibrary, ContributedLibrary installedLibrary) { - if (selectedLibrary.isReadOnly()) { - onRemovePressed(installedLibrary); + protected void onInstall(ContributedLibrary selectedLibrary, Optional mayInstalledLibrary) { + if (selectedLibrary.isReadOnly() && mayInstalledLibrary.isPresent()) { + onRemovePressed(mayInstalledLibrary.get()); } else { - onInstallPressed(selectedLibrary, installedLibrary); + onInstallPressed(selectedLibrary, mayInstalledLibrary); } } @@ -212,12 +213,12 @@ public class LibraryManagerUI extends InstallerJDialog { installerThread.start(); } - public void onInstallPressed(final ContributedLibrary lib, final ContributedLibrary replaced) { + public void onInstallPressed(final ContributedLibrary lib, final Optional mayReplaced) { clearErrorMessage(); installerThread = new Thread(() -> { try { setProgressVisible(true, tr("Installing...")); - installer.install(lib, replaced, this::setProgress); + installer.install(lib, mayReplaced, this::setProgress); onIndexesUpdated(); // TODO: Do a better job in refreshing only the needed element //getContribModel().updateLibrary(lib); } catch (Exception e) { diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 4b2c6f90d..a8678e6e0 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -358,11 +358,11 @@ public class Base { System.exit(1); } - ContributedLibrary installed = indexer.getIndex().getInstalled(libraryToInstallParts[0]); - if (selected.isReadOnly()) { - libraryInstaller.remove(installed, progressListener); + Optional mayInstalled = indexer.getIndex().getInstalled(libraryToInstallParts[0]); + if (selected.isReadOnly() && mayInstalled.isPresent()) { + libraryInstaller.remove(mayInstalled.get(), progressListener); } else { - libraryInstaller.install(selected, installed, progressListener); + libraryInstaller.install(selected, mayInstalled, progressListener); } } diff --git a/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndex.java b/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndex.java index a78b6b637..4e6d4da38 100644 --- a/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndex.java +++ b/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndex.java @@ -91,14 +91,14 @@ public abstract class LibrariesIndex { return types; } - public ContributedLibrary getInstalled(String name) { + public Optional getInstalled(String name) { List installedReleases = find(name).stream().filter(new InstalledPredicate()).collect(Collectors.toList()); Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator()); if (installedReleases.isEmpty()) { - return null; + return Optional.empty(); } - return installedReleases.get(0); + return Optional.of(installedReleases.get(0)); } } diff --git a/arduino-core/src/cc/arduino/contributions/libraries/LibraryInstaller.java b/arduino-core/src/cc/arduino/contributions/libraries/LibraryInstaller.java index 12ac6519a..3fa1f6de3 100644 --- a/arduino-core/src/cc/arduino/contributions/libraries/LibraryInstaller.java +++ b/arduino-core/src/cc/arduino/contributions/libraries/LibraryInstaller.java @@ -43,6 +43,7 @@ import processing.app.helpers.FileUtils; import java.io.File; import java.io.IOException; import java.net.URL; +import java.util.Optional; import static processing.app.I18n.tr; @@ -82,7 +83,7 @@ public class LibraryInstaller { rescanLibraryIndex(progress, progressListener); } - public synchronized void install(ContributedLibrary lib, ContributedLibrary replacedLib, ProgressListener progressListener) throws Exception { + public synchronized void install(ContributedLibrary lib, Optional mayReplacedLib, ProgressListener progressListener) throws Exception { if (lib.isInstalled()) { System.out.println(I18n.format(tr("Library is already installed: {0} version {1}"), lib.getName(), lib.getParsedVersion())); return; @@ -119,7 +120,9 @@ public class LibraryInstaller { // Step 3: Remove replaced library and move installed one to the correct location // TODO: Fix progress bar... - remove(replacedLib, progressListener); + if (mayReplacedLib.isPresent()) { + remove(mayReplacedLib.get(), progressListener); + } File destFolder = new File(libsFolder, lib.getName().replaceAll(" ", "_")); tmpFolder.renameTo(destFolder); progress.stepDone(); @@ -129,7 +132,7 @@ public class LibraryInstaller { } public synchronized void remove(ContributedLibrary lib, ProgressListener progressListener) throws IOException { - if (lib == null || lib.isReadOnly()) { + if (lib.isReadOnly()) { return; }