Skip to content

Commit

Permalink
Moved ContributedLibraryReleases out of LibrariesIndexTableModel and …
Browse files Browse the repository at this point in the history
…introduced its own special comparator. Will help with #4195
  • Loading branch information
Federico Fissore committed Nov 23, 2015
1 parent 6d03d26 commit bfeb994
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 73 deletions.
@@ -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;
}
}
}
}
@@ -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());
}

}
Expand Up @@ -204,7 +204,7 @@ public Component getTableCellRendererComponent(JTable table, Object value,
return component;
}

private LibrariesIndexTableModel.ContributedLibraryReleases editorValue;
private ContributedLibraryReleases editorValue;
private JTable parentTable;

@Override
Expand All @@ -217,12 +217,12 @@ public Component getTableCellEditorComponent(JTable table, Object value,
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());
Expand Down Expand Up @@ -263,7 +263,7 @@ public Component getTableCellEditorComponent(JTable table, Object value,
}

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);

Expand Down
Expand Up @@ -29,89 +29,22 @@

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;
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")
public class LibrariesIndexTableModel extends FilteredAbstractTableModel<ContributedLibrary> {

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"};
Expand Down Expand Up @@ -271,7 +204,7 @@ private void updateContributions() {
contributions.clear();
indexer.getIndex().getLibraries().forEach(this::applyFilterToLibrary);
indexer.getInstalledLibraries().forEach(this::applyFilterToLibrary);
Collections.sort(contributions);
Collections.sort(contributions, new ContributedLibraryReleasesComparator());
}

}
Expand Up @@ -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()));
Expand Down

0 comments on commit bfeb994

Please sign in to comment.