From 103f2e433aa383c1b74abe59fa67d1e9ff03ff6a Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Mon, 16 Mar 2015 12:34:26 +0100 Subject: [PATCH] Continuing improving library manager --- .../ui/ContributedLibraryTableCell.java | 58 +++++++++++++++---- .../ui/LibrariesIndexTableModel.java | 4 +- .../contributions/ui/LibraryManagerUI.java | 10 +++- .../filters/OnlyUpstreamReleasePredicate.java | 18 ++++++ .../contributions/ContributedLibrary.java | 29 ++++++++-- .../contributions/VersionComparator.java | 18 ++++++ 6 files changed, 114 insertions(+), 23 deletions(-) create mode 100644 app/src/cc/arduino/libraries/contributions/ui/filters/OnlyUpstreamReleasePredicate.java diff --git a/app/src/cc/arduino/libraries/contributions/ui/ContributedLibraryTableCell.java b/app/src/cc/arduino/libraries/contributions/ui/ContributedLibraryTableCell.java index e2843aa69..7ca176811 100644 --- a/app/src/cc/arduino/libraries/contributions/ui/ContributedLibraryTableCell.java +++ b/app/src/cc/arduino/libraries/contributions/ui/ContributedLibraryTableCell.java @@ -31,14 +31,18 @@ package cc.arduino.libraries.contributions.ui; import cc.arduino.libraries.contributions.ContributedLibrary; import cc.arduino.libraries.contributions.ContributedLibraryComparator; import cc.arduino.libraries.contributions.ui.LibrariesIndexTableModel.ContributedLibraryReleases; +import cc.arduino.libraries.contributions.ui.filters.BuiltInPredicate; import cc.arduino.libraries.contributions.ui.filters.InstalledPredicate; +import cc.arduino.libraries.contributions.ui.filters.OnlyUpstreamReleasePredicate; +import cc.arduino.packages.contributions.VersionComparator; import cc.arduino.ui.InstallerTableCell; import cc.arduino.utils.ReverseComparator; +import com.google.common.base.Function; import com.google.common.base.Predicates; import com.google.common.collect.Collections2; +import com.google.common.collect.Lists; import processing.app.Base; -import javax.sound.midi.Soundbank; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.event.HyperlinkEvent; @@ -168,7 +172,7 @@ public class ContributedLibraryTableCell extends InstallerTableCell { buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS)); buttonsPanel.setOpaque(false); - buttonsPanel.add(Box.createHorizontalStrut(20)); + buttonsPanel.add(Box.createHorizontalStrut(7)); buttonsPanel.add(downgradeChooser); buttonsPanel.add(Box.createHorizontalStrut(5)); buttonsPanel.add(downgradeButton); @@ -205,7 +209,7 @@ public class ContributedLibraryTableCell extends InstallerTableCell { panel.add(inactiveButtonsPanel); } - panel.add(Box.createVerticalStrut(10)); + panel.add(Box.createVerticalStrut(15)); } protected void onRemove(ContributedLibrary selectedLib) { @@ -222,7 +226,7 @@ public class ContributedLibraryTableCell extends InstallerTableCell { int column) { parentTable = table; setEnabled(false); - Component component = getUpdatedCellComponent(value, isSelected, row); + Component component = getUpdatedCellComponent(value, isSelected, row, false); if (row % 2 == 0) { component.setBackground(new Color(236, 241, 241)); //#ecf1f1 } else { @@ -248,29 +252,59 @@ public class ContributedLibraryTableCell extends InstallerTableCell { editorValue = (ContributedLibraryReleases) value; setEnabled(true); - List uninstalledLibraries = new LinkedList(Collections2.filter(editorValue.releases, Predicates.not(new InstalledPredicate()))); + final ContributedLibrary installedLibrary = editorValue.getInstalled(); + + List libraries = new LinkedList(Collections2.filter(editorValue.releases, new OnlyUpstreamReleasePredicate())); + List uninstalledLibraries = new LinkedList(Collections2.filter(libraries, Predicates.not(new InstalledPredicate()))); + + List installedBuiltIn = new LinkedList(Collections2.filter(libraries, Predicates.and(new InstalledPredicate(), new BuiltInPredicate()))); + + if (installedLibrary != null && !installedBuiltIn.contains(installedLibrary)) { + uninstalledLibraries.addAll(installedBuiltIn); + } + Collections.sort(uninstalledLibraries, new ReverseComparator(new ContributedLibraryComparator())); downgradeChooser.removeAllItems(); downgradeChooser.addItem(_("Select version")); - for (ContributedLibrary release : uninstalledLibraries) { + + final List uninstalledPreviousLibraries = Lists.newLinkedList(); + final List uninstalledNewerLibraries = Lists.newLinkedList(); + + Lists.newLinkedList(Lists.transform(uninstalledLibraries, new Function() { + @Override + public ContributedLibrary apply(ContributedLibrary input) { + if (installedLibrary == null || VersionComparator.VERSION_COMPARATOR.greaterThan(installedLibrary.getVersion(), input.getVersion())) { + uninstalledPreviousLibraries.add(input); + } else { + uninstalledNewerLibraries.add(input); + } + + return input; + } + })); + for (ContributedLibrary release : uninstalledNewerLibraries) { downgradeChooser.addItem(release); } - downgradeChooser.setVisible(editorValue.getInstalled() != null && uninstalledLibraries.size() > 1); - downgradeButton.setVisible(editorValue.getInstalled() != null && uninstalledLibraries.size() > 1); + for (ContributedLibrary release : uninstalledPreviousLibraries) { + downgradeChooser.addItem(release); + } + + downgradeChooser.setVisible(installedLibrary != null && (!uninstalledPreviousLibraries.isEmpty() || uninstalledNewerLibraries.size() > 1)); + downgradeButton.setVisible(installedLibrary != null && (!uninstalledPreviousLibraries.isEmpty() || uninstalledNewerLibraries.size() > 1)); versionToInstallChooser.removeAllItems(); for (ContributedLibrary release : uninstalledLibraries) { versionToInstallChooser.addItem(release); } - versionToInstallChooser.setVisible(editorValue.getInstalled() == null && uninstalledLibraries.size() > 1); + versionToInstallChooser.setVisible(installedLibrary == null && uninstalledLibraries.size() > 1); - Component component = getUpdatedCellComponent(value, true, row); + Component component = getUpdatedCellComponent(value, true, row, !installedBuiltIn.isEmpty()); component.setBackground(new Color(218, 227, 227)); //#dae3e3 return component; } - private Component getUpdatedCellComponent(Object value, boolean isSelected, int row) { + private Component getUpdatedCellComponent(Object value, boolean isSelected, int row, boolean hasBuiltInRelease) { ContributedLibraryReleases releases = (ContributedLibraryReleases) value; ContributedLibrary selectedLib = releases.getSelected(); ContributedLibrary installedLib = releases.getInstalled(); @@ -282,7 +316,7 @@ public class ContributedLibraryTableCell extends InstallerTableCell { upgradable = false; } else { installable = false; - removable = !installedLib.isReadOnly(); + removable = !installedLib.isReadOnly() && !hasBuiltInRelease; upgradable = new ContributedLibraryComparator().compare(selectedLib, installedLib) > 0; } if (installable) { diff --git a/app/src/cc/arduino/libraries/contributions/ui/LibrariesIndexTableModel.java b/app/src/cc/arduino/libraries/contributions/ui/LibrariesIndexTableModel.java index 5b221a3e9..f9f5cb7fb 100644 --- a/app/src/cc/arduino/libraries/contributions/ui/LibrariesIndexTableModel.java +++ b/app/src/cc/arduino/libraries/contributions/ui/LibrariesIndexTableModel.java @@ -55,9 +55,7 @@ public class LibrariesIndexTableModel extends FilteredAbstractTableModel { + + @Override + public boolean apply(ContributedLibrary input) { + return !(input instanceof UserLibrary); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof OnlyUpstreamReleasePredicate; + } +} diff --git a/arduino-core/src/cc/arduino/libraries/contributions/ContributedLibrary.java b/arduino-core/src/cc/arduino/libraries/contributions/ContributedLibrary.java index 1cdb03f48..91c302108 100644 --- a/arduino-core/src/cc/arduino/libraries/contributions/ContributedLibrary.java +++ b/arduino-core/src/cc/arduino/libraries/contributions/ContributedLibrary.java @@ -28,12 +28,12 @@ */ package cc.arduino.libraries.contributions; -import java.util.Comparator; -import java.util.List; - import cc.arduino.packages.contributions.DownloadableContribution; import processing.app.I18n; +import java.util.Comparator; +import java.util.List; + import static processing.app.I18n._; public abstract class ContributedLibrary extends DownloadableContribution { @@ -90,15 +90,14 @@ public abstract class ContributedLibrary extends DownloadableContribution { */ public boolean supportsArchitecture(String reqArch) { return getArchitectures().contains(reqArch) || - getArchitectures().contains("*"); + getArchitectures().contains("*"); } /** * Returns true if the library declares to support at least one of the * specified architectures. * - * @param reqArchs - * A List of architectures to check + * @param reqArchs A List of architectures to check * @return */ public boolean supportsArchitecture(List reqArchs) { @@ -146,4 +145,22 @@ public abstract class ContributedLibrary extends DownloadableContribution { return res; } + @Override + public boolean equals(Object obj) { + if (!(obj instanceof ContributedLibrary)) { + return false; + } + + String thisVersion = getVersion(); + String otherVersion = ((ContributedLibrary) obj).getVersion(); + + boolean versionEquals = thisVersion == null || otherVersion == null || thisVersion == otherVersion || thisVersion.equals(otherVersion); + + String thisName = getName(); + String otherName = ((ContributedLibrary) obj).getName(); + + boolean nameEquals = thisName == null || otherName == null || thisName == otherName || thisName.equals(otherName); + + return versionEquals && nameEquals; + } } diff --git a/arduino-core/src/cc/arduino/packages/contributions/VersionComparator.java b/arduino-core/src/cc/arduino/packages/contributions/VersionComparator.java index 6fb742efa..e89971585 100644 --- a/arduino-core/src/cc/arduino/packages/contributions/VersionComparator.java +++ b/arduino-core/src/cc/arduino/packages/contributions/VersionComparator.java @@ -53,6 +53,24 @@ public class VersionComparator implements Comparator { return versionA.compareTo(versionB); } + public boolean greaterThan(String a, String b) { + // null is always less than any other value + if (a == null && b == null) { + return false; + } + if (a == null) { + return false; + } + if (b == null) { + return true; + } + + Version versionA = valueOf(a); + Version versionB = valueOf(b); + + return versionA.greaterThan(versionB); + } + private Version valueOf(String ver) { String[] verParts = ver.split("\\."); if (verParts.length < 3) {