Moved ContributedLibraryReleases out of LibrariesIndexTableModel and introduced its own special comparator. Will help with #4195

This commit is contained in:
Federico Fissore 2015-11-23 16:06:32 +01:00
parent 6d03d268f9
commit bfeb994974
5 changed files with 153 additions and 73 deletions

View File

@ -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<ContributedLibrary> releases;
private final List<String> 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<ContributedLibrary> 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<ContributedLibrary> 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;
}
}
}
}

View File

@ -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<ContributedLibraryReleases> {
@Override
public int compare(ContributedLibraryReleases o1, ContributedLibraryReleases o2) {
return o1.getLibrary().getName().compareToIgnoreCase(o2.getLibrary().getName());
}
}

View File

@ -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<ContributedLibrary> releases = editorValue.releases.stream().filter(new OnlyUpstreamReleasePredicate()).collect(Collectors.toList());
List<ContributedLibrary> releases = editorValue.getReleases().stream().filter(new OnlyUpstreamReleasePredicate()).collect(Collectors.toList());
List<ContributedLibrary> uninstalledReleases = releases.stream().filter(new InstalledPredicate().negate()).collect(Collectors.toList());
List<ContributedLibrary> 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);

View File

@ -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<Contrib
public final static int DESCRIPTION_COL = 0;
public static class ContributedLibraryReleases implements Comparable<ContributedLibraryReleases> {
public final String name;
public final List<ContributedLibrary> releases;
public final List<String> 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<ContributedLibrary> 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<ContributedLibraryReleases> contributions = new ArrayList<>();
private final String[] columnNames = {"Description"};
@ -271,7 +204,7 @@ public class LibrariesIndexTableModel extends FilteredAbstractTableModel<Contrib
contributions.clear();
indexer.getIndex().getLibraries().forEach(this::applyFilterToLibrary);
indexer.getInstalledLibraries().forEach(this::applyFilterToLibrary);
Collections.sort(contributions);
Collections.sort(contributions, new ContributedLibraryReleasesComparator());
}
}

View File

@ -43,7 +43,7 @@ public abstract class FilteredAbstractTableModel<T> extends AbstractTableModel {
abstract public void updateIndexFilter(String[] filters, Stream<Predicate<T>> additionalFilters);
protected static <T extends DownloadableContribution> T getLatestOf(List<T> contribs) {
public static <T extends DownloadableContribution> T getLatestOf(List<T> contribs) {
contribs = new LinkedList<>(contribs);
final VersionComparator versionComparator = new VersionComparator();
Collections.sort(contribs, (contrib1, contrib2) -> versionComparator.compare(contrib1.getParsedVersion(), contrib2.getParsedVersion()));