From bfeb994974a74a0af0226651a6d2d394ea0c2779 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Mon, 23 Nov 2015 16:06:32 +0100 Subject: [PATCH] Moved ContributedLibraryReleases out of LibrariesIndexTableModel and introduced its own special comparator. Will help with #4195 --- .../ui/ContributedLibraryReleases.java | 106 ++++++++++++++++++ .../ContributedLibraryReleasesComparator.java | 41 +++++++ .../ui/ContributedLibraryTableCell.java | 8 +- .../ui/LibrariesIndexTableModel.java | 69 +----------- .../ui/FilteredAbstractTableModel.java | 2 +- 5 files changed, 153 insertions(+), 73 deletions(-) create mode 100644 app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleases.java create mode 100644 app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleasesComparator.java diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleases.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleases.java new file mode 100644 index 000000000..a1eb1424d --- /dev/null +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleases.java @@ -0,0 +1,106 @@ +/* + * This file is part of Arduino. + * + * Copyright 2015 Arduino LLC (http://www.arduino.cc/) + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ + +package cc.arduino.contributions.libraries.ui; + +import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator; +import cc.arduino.contributions.filters.InstalledPredicate; +import cc.arduino.contributions.libraries.ContributedLibrary; +import cc.arduino.contributions.ui.FilteredAbstractTableModel; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; + +public class ContributedLibraryReleases { + + private final ContributedLibrary library; + private final List releases; + private final List versions; + + private ContributedLibrary selected; + + public ContributedLibraryReleases(ContributedLibrary library) { + this.library = library; + this.versions = new LinkedList<>(); + this.releases = new LinkedList<>(); + this.selected = null; + add(library); + } + + public ContributedLibrary getLibrary() { + return library; + } + + public List getReleases() { + return releases; + } + + public boolean shouldContain(ContributedLibrary lib) { + return lib.getName().equals(library.getName()); + } + + public void add(ContributedLibrary library) { + releases.add(library); + String version = library.getParsedVersion(); + if (version != null) { + versions.add(version); + } + selected = getLatest(); + } + + public ContributedLibrary getInstalled() { + List installedReleases = releases.stream().filter(new InstalledPredicate()).collect(Collectors.toList()); + Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator()); + + if (installedReleases.isEmpty()) { + return null; + } + + return installedReleases.get(0); + } + + public ContributedLibrary getLatest() { + return FilteredAbstractTableModel.getLatestOf(releases); + } + + public ContributedLibrary getSelected() { + return selected; + } + + public void select(ContributedLibrary value) { + for (ContributedLibrary plat : releases) { + if (plat == value) { + selected = plat; + return; + } + } + } +} diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleasesComparator.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleasesComparator.java new file mode 100644 index 000000000..14bf736dc --- /dev/null +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleasesComparator.java @@ -0,0 +1,41 @@ +/* + * This file is part of Arduino. + * + * Copyright 2015 Arduino LLC (http://www.arduino.cc/) + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ + +package cc.arduino.contributions.libraries.ui; + +import java.util.Comparator; + +public class ContributedLibraryReleasesComparator implements Comparator { + + @Override + public int compare(ContributedLibraryReleases o1, ContributedLibraryReleases o2) { + return o1.getLibrary().getName().compareToIgnoreCase(o2.getLibrary().getName()); + } + +} diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java index 901bfa3a3..54cd56718 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java @@ -204,7 +204,7 @@ public class ContributedLibraryTableCell extends InstallerTableCell { return component; } - private LibrariesIndexTableModel.ContributedLibraryReleases editorValue; + private ContributedLibraryReleases editorValue; private JTable parentTable; @Override @@ -217,12 +217,12 @@ public class ContributedLibraryTableCell extends InstallerTableCell { boolean isSelected, int row, int column) { parentTable = table; - editorValue = (LibrariesIndexTableModel.ContributedLibraryReleases) value; + editorValue = (ContributedLibraryReleases) value; setEnabled(true); final ContributedLibrary installed = editorValue.getInstalled(); - List releases = editorValue.releases.stream().filter(new OnlyUpstreamReleasePredicate()).collect(Collectors.toList()); + List releases = editorValue.getReleases().stream().filter(new OnlyUpstreamReleasePredicate()).collect(Collectors.toList()); List uninstalledReleases = releases.stream().filter(new InstalledPredicate().negate()).collect(Collectors.toList()); List installedBuiltIn = releases.stream().filter(new InstalledPredicate()).filter(new BuiltInPredicate()).collect(Collectors.toList()); @@ -263,7 +263,7 @@ public class ContributedLibraryTableCell extends InstallerTableCell { } private Component getUpdatedCellComponent(Object value, boolean isSelected, int row, boolean hasBuiltInRelease) { - LibrariesIndexTableModel.ContributedLibraryReleases releases = (LibrariesIndexTableModel.ContributedLibraryReleases) value; + ContributedLibraryReleases releases = (ContributedLibraryReleases) value; JTextPane description = makeNewDescription(panel); diff --git a/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java b/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java index 5ea308e5d..1814cafb8 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java +++ b/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java @@ -29,8 +29,6 @@ package cc.arduino.contributions.libraries.ui; -import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator; -import cc.arduino.contributions.filters.InstalledPredicate; import cc.arduino.contributions.libraries.ContributedLibrary; import cc.arduino.contributions.libraries.LibrariesIndexer; import cc.arduino.contributions.packages.ContributedPlatform; @@ -38,10 +36,8 @@ import cc.arduino.contributions.ui.FilteredAbstractTableModel; import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedList; import java.util.List; import java.util.function.Predicate; -import java.util.stream.Collectors; import java.util.stream.Stream; @SuppressWarnings("serial") @@ -49,69 +45,6 @@ public class LibrariesIndexTableModel extends FilteredAbstractTableModel { - - public final String name; - public final List releases; - public final List versions; - - public ContributedLibrary selected; - - public ContributedLibraryReleases(ContributedLibrary library) { - this.name = library.getName(); - this.versions = new LinkedList<>(); - this.releases = new LinkedList<>(); - this.selected = null; - add(library); - } - - public boolean shouldContain(ContributedLibrary lib) { - return lib.getName().equals(name); - } - - public void add(ContributedLibrary library) { - releases.add(library); - String version = library.getParsedVersion(); - if (version != null) { - versions.add(version); - } - selected = getLatest(); - } - - public ContributedLibrary getInstalled() { - List installedReleases = releases.stream().filter(new InstalledPredicate()).collect(Collectors.toList()); - Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator()); - - if (installedReleases.isEmpty()) { - return null; - } - - return installedReleases.get(0); - } - - public ContributedLibrary getLatest() { - return getLatestOf(releases); - } - - public ContributedLibrary getSelected() { - return selected; - } - - public void select(ContributedLibrary value) { - for (ContributedLibrary plat : releases) { - if (plat == value) { - selected = plat; - return; - } - } - } - - @Override - public int compareTo(ContributedLibraryReleases o) { - return name.compareToIgnoreCase(o.name); - } - } - private final List contributions = new ArrayList<>(); private final String[] columnNames = {"Description"}; @@ -271,7 +204,7 @@ public class LibrariesIndexTableModel extends FilteredAbstractTableModel extends AbstractTableModel { abstract public void updateIndexFilter(String[] filters, Stream> additionalFilters); - protected static T getLatestOf(List contribs) { + public static T getLatestOf(List contribs) { contribs = new LinkedList<>(contribs); final VersionComparator versionComparator = new VersionComparator(); Collections.sort(contribs, (contrib1, contrib2) -> versionComparator.compare(contrib1.getParsedVersion(), contrib2.getParsedVersion()));