From 8e5a04f6a9cadb365e3db29c9dcc52869bca5396 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Fri, 13 Mar 2015 17:30:48 +0100 Subject: [PATCH] Various improvements on library manager --- .../ui/ContributedLibraryTableCell.java | 61 +++++++++++++------ .../contributions/ui/LibraryInstaller.java | 8 ++- .../contributions/ui/LibraryManagerUI.java | 4 +- .../ui/ContributedPlatformTableCell.java | 2 +- .../contributions/ContributedLibrary.java | 7 ++- .../ContributedLibraryComparator.java | 15 +++++ .../cc/arduino/utils/ReverseComparator.java | 17 ++++++ 7 files changed, 87 insertions(+), 27 deletions(-) create mode 100644 arduino-core/src/cc/arduino/libraries/contributions/ContributedLibraryComparator.java create mode 100644 arduino-core/src/cc/arduino/utils/ReverseComparator.java diff --git a/app/src/cc/arduino/libraries/contributions/ui/ContributedLibraryTableCell.java b/app/src/cc/arduino/libraries/contributions/ui/ContributedLibraryTableCell.java index a14ae0b60..e2843aa69 100644 --- a/app/src/cc/arduino/libraries/contributions/ui/ContributedLibraryTableCell.java +++ b/app/src/cc/arduino/libraries/contributions/ui/ContributedLibraryTableCell.java @@ -29,10 +29,16 @@ 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.InstalledPredicate; import cc.arduino.ui.InstallerTableCell; +import cc.arduino.utils.ReverseComparator; +import com.google.common.base.Predicates; +import com.google.common.collect.Collections2; import processing.app.Base; +import javax.sound.midi.Soundbank; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.event.HyperlinkEvent; @@ -45,6 +51,9 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; import static processing.app.I18n._; import static processing.app.I18n.format; @@ -59,6 +68,7 @@ public class ContributedLibraryTableCell extends InstallerTableCell { private Component removeButtonPlaceholder; private Component installButtonPlaceholder; private JComboBox downgradeChooser; + private JComboBox versionToInstallChooser; private JButton downgradeButton; private JPanel buttonsPanel; private Component removeButtonStrut; @@ -121,8 +131,7 @@ public class ContributedLibraryTableCell extends InstallerTableCell { downgradeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - ContributedLibrary selected; - selected = (ContributedLibrary) downgradeChooser.getSelectedItem(); + ContributedLibrary selected = (ContributedLibrary) downgradeChooser.getSelectedItem(); onInstall(selected, editorValue.getInstalled()); } }); @@ -139,6 +148,16 @@ public class ContributedLibraryTableCell extends InstallerTableCell { } }); + versionToInstallChooser = new JComboBox(); + versionToInstallChooser.addItem("-"); + versionToInstallChooser.setMaximumSize(versionToInstallChooser.getPreferredSize()); + versionToInstallChooser.addItemListener(new ItemListener() { + @Override + public void itemStateChanged(ItemEvent e) { + editorValue.select((ContributedLibrary) versionToInstallChooser.getSelectedItem()); + } + }); + panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); @@ -156,6 +175,8 @@ public class ContributedLibraryTableCell extends InstallerTableCell { buttonsPanel.add(Box.createHorizontalGlue()); + buttonsPanel.add(versionToInstallChooser); + buttonsPanel.add(Box.createHorizontalStrut(5)); buttonsPanel.add(installButton); buttonsPanel.add(Box.createHorizontalStrut(5)); buttonsPanel.add(removeButton); @@ -227,17 +248,22 @@ public class ContributedLibraryTableCell extends InstallerTableCell { editorValue = (ContributedLibraryReleases) value; setEnabled(true); + List uninstalledLibraries = new LinkedList(Collections2.filter(editorValue.releases, Predicates.not(new InstalledPredicate()))); + Collections.sort(uninstalledLibraries, new ReverseComparator(new ContributedLibraryComparator())); + downgradeChooser.removeAllItems(); downgradeChooser.addItem(_("Select version")); - boolean visible = false; - for (ContributedLibrary release : editorValue.releases) { - if (release.isInstalled()) - continue; + for (ContributedLibrary release : uninstalledLibraries) { downgradeChooser.addItem(release); - visible = true; } - downgradeChooser.setVisible(visible && editorValue.releases.size() > 1); - downgradeButton.setVisible(visible && editorValue.releases.size() > 1); + downgradeChooser.setVisible(editorValue.getInstalled() != null && uninstalledLibraries.size() > 1); + downgradeButton.setVisible(editorValue.getInstalled() != null && uninstalledLibraries.size() > 1); + + versionToInstallChooser.removeAllItems(); + for (ContributedLibrary release : uninstalledLibraries) { + versionToInstallChooser.addItem(release); + } + versionToInstallChooser.setVisible(editorValue.getInstalled() == null && uninstalledLibraries.size() > 1); Component component = getUpdatedCellComponent(value, true, row); component.setBackground(new Color(218, 227, 227)); //#dae3e3 @@ -257,12 +283,14 @@ public class ContributedLibraryTableCell extends InstallerTableCell { } else { installable = false; removable = !installedLib.isReadOnly(); - upgradable = (selectedLib != installedLib); + upgradable = new ContributedLibraryComparator().compare(selectedLib, installedLib) > 0; } - if (installable) + if (installable) { installButton.setText(_("Install")); - if (upgradable) - installButton.setText(_("Upgrade")); + } + if (upgradable) { + installButton.setText(_("Update")); + } installButton.setVisible(installable || upgradable); installButtonPlaceholder.setVisible(!(installable || upgradable)); removeButton.setVisible(removable); @@ -327,13 +355,6 @@ public class ContributedLibraryTableCell extends InstallerTableCell { description.setText(desc); description.setBackground(Color.WHITE); - // If the selected lib is available from repository... - if (url != null) { - removeButton.setText(_("Remove")); - } else { - removeButton.setText(_("Delete")); - } - // for modelToView to work, the text area has to be sized. It doesn't // matter if it's visible or not. diff --git a/app/src/cc/arduino/libraries/contributions/ui/LibraryInstaller.java b/app/src/cc/arduino/libraries/contributions/ui/LibraryInstaller.java index 2c659aff6..46d5f7a78 100644 --- a/app/src/cc/arduino/libraries/contributions/ui/LibraryInstaller.java +++ b/app/src/cc/arduino/libraries/contributions/ui/LibraryInstaller.java @@ -68,7 +68,7 @@ public class LibraryInstaller { File tmpFile = new File(outputFile.getAbsolutePath() + ".tmp"); try { downloader.download(url, tmpFile, progress, - _("Downloading libraries index...")); + _("Downloading libraries index...")); } catch (InterruptedException e) { // Download interrupted... just exit return; @@ -121,7 +121,7 @@ public class LibraryInstaller { // Step 3: Remove replaced library and move installed one to the correct location // TODO: Fix progress bar... - if (replacedLib != null) { + if (replacedLib != null && !replacedLib.isReadOnly()) { remove(replacedLib); } File destFolder = new File(libsFolder, lib.getName()); @@ -133,6 +133,10 @@ public class LibraryInstaller { } public void remove(ContributedLibrary lib) throws IOException { + if (lib.isReadOnly()) { + throw new IllegalArgumentException("Can't delete a built-in library"); + } + final MultiStepProgress progress = new MultiStepProgress(2); // Step 1: Remove library diff --git a/app/src/cc/arduino/libraries/contributions/ui/LibraryManagerUI.java b/app/src/cc/arduino/libraries/contributions/ui/LibraryManagerUI.java index eb0d4a2a9..6ec00c90f 100644 --- a/app/src/cc/arduino/libraries/contributions/ui/LibraryManagerUI.java +++ b/app/src/cc/arduino/libraries/contributions/ui/LibraryManagerUI.java @@ -65,8 +65,8 @@ public class LibraryManagerUI extends InstallerJDialog { } @Override - protected void onRemove(ContributedLibrary installedPlatform) { - onRemovePressed(installedPlatform); + protected void onRemove(ContributedLibrary library) { + onRemovePressed(library); } }; } diff --git a/app/src/cc/arduino/packages/contributions/ui/ContributedPlatformTableCell.java b/app/src/cc/arduino/packages/contributions/ui/ContributedPlatformTableCell.java index 3405d2f34..386e9bd3b 100644 --- a/app/src/cc/arduino/packages/contributions/ui/ContributedPlatformTableCell.java +++ b/app/src/cc/arduino/packages/contributions/ui/ContributedPlatformTableCell.java @@ -254,7 +254,7 @@ public class ContributedPlatformTableCell extends InstallerTableCell { if (installable) installButton.setText(_("Install")); if (upgradable) - installButton.setText(_("Upgrade")); + installButton.setText(_("Update")); installButton.setVisible(installable || upgradable); removeButton.setVisible(removable); diff --git a/arduino-core/src/cc/arduino/libraries/contributions/ContributedLibrary.java b/arduino-core/src/cc/arduino/libraries/contributions/ContributedLibrary.java index 9b5a16581..1cdb03f48 100644 --- a/arduino-core/src/cc/arduino/libraries/contributions/ContributedLibrary.java +++ b/arduino-core/src/cc/arduino/libraries/contributions/ContributedLibrary.java @@ -32,6 +32,9 @@ import java.util.Comparator; import java.util.List; import cc.arduino.packages.contributions.DownloadableContribution; +import processing.app.I18n; + +import static processing.app.I18n._; public abstract class ContributedLibrary extends DownloadableContribution { @@ -109,9 +112,9 @@ public abstract class ContributedLibrary extends DownloadableContribution { @Override public String toString() { - return getVersion(); + return I18n.format(_("Version {0}"), getVersion()); } - + public String info() { String res = ""; res += " ContributedLibrary : " + getName() + "\n"; diff --git a/arduino-core/src/cc/arduino/libraries/contributions/ContributedLibraryComparator.java b/arduino-core/src/cc/arduino/libraries/contributions/ContributedLibraryComparator.java new file mode 100644 index 000000000..5da63ac38 --- /dev/null +++ b/arduino-core/src/cc/arduino/libraries/contributions/ContributedLibraryComparator.java @@ -0,0 +1,15 @@ +package cc.arduino.libraries.contributions; + +import cc.arduino.packages.contributions.VersionComparator; + +import java.util.Comparator; + +public class ContributedLibraryComparator implements Comparator { + + @Override + public int compare(ContributedLibrary lib1, ContributedLibrary lib2) { + return VersionComparator.VERSION_COMPARATOR.compare(lib1.getVersion(), lib2.getVersion()); + } + + +} diff --git a/arduino-core/src/cc/arduino/utils/ReverseComparator.java b/arduino-core/src/cc/arduino/utils/ReverseComparator.java new file mode 100644 index 000000000..50c0ccd45 --- /dev/null +++ b/arduino-core/src/cc/arduino/utils/ReverseComparator.java @@ -0,0 +1,17 @@ +package cc.arduino.utils; + +import java.util.Comparator; + +public class ReverseComparator implements Comparator { + + private final Comparator orig; + + public ReverseComparator(Comparator orig) { + this.orig = orig; + } + + @Override + public int compare(T t, T t1) { + return -1 * orig.compare(t, t1); + } +}