mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-29 18:52:13 +01:00
LibraryManagerUI: library types order fixed
This commit is contained in:
parent
1d6e710eac
commit
f13fe9a4f7
@ -1,24 +1,22 @@
|
||||
package cc.arduino.contributions.libraries;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class LibraryByTypeComparator implements Comparator<ContributedLibrary> {
|
||||
|
||||
private final List<String> types;
|
||||
private final LibraryTypeComparator libraryTypeComparator;
|
||||
|
||||
public LibraryByTypeComparator() {
|
||||
this("Arduino", "Recommended", "Contributed");
|
||||
this(new LibraryTypeComparator());
|
||||
}
|
||||
|
||||
public LibraryByTypeComparator(String... types) {
|
||||
this.types = Arrays.asList(types);
|
||||
public LibraryByTypeComparator(LibraryTypeComparator libraryTypeComparator) {
|
||||
this.libraryTypeComparator = libraryTypeComparator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(ContributedLibrary o1, ContributedLibrary o2) {
|
||||
return types.indexOf(o1.getTypes().get(0)) - types.indexOf(o2.getTypes().get(0));
|
||||
return libraryTypeComparator.compare(o1.getTypes().get(0), o2.getTypes().get(0));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
package cc.arduino.contributions.libraries;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class LibraryTypeComparator implements Comparator<String> {
|
||||
|
||||
private final List<String> types;
|
||||
|
||||
public LibraryTypeComparator() {
|
||||
this("Arduino", "Recommended", "Contributed");
|
||||
}
|
||||
|
||||
public LibraryTypeComparator(String... types) {
|
||||
this.types = Arrays.asList(types);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(String o1, String o2) {
|
||||
if (types.contains(o1) && types.contains(o2)) {
|
||||
return types.indexOf(o1) - types.indexOf(o2);
|
||||
} else if (types.contains(o1)) {
|
||||
return -1;
|
||||
} else if (types.contains(o2)) {
|
||||
return 1;
|
||||
}
|
||||
return o1.compareTo(o2);
|
||||
}
|
||||
|
||||
}
|
@ -31,6 +31,7 @@ package cc.arduino.contributions.libraries.ui;
|
||||
import cc.arduino.contributions.libraries.ContributedLibrary;
|
||||
import cc.arduino.contributions.libraries.LibrariesIndexer;
|
||||
import cc.arduino.contributions.libraries.LibraryInstaller;
|
||||
import cc.arduino.contributions.libraries.LibraryTypeComparator;
|
||||
import cc.arduino.contributions.packages.DownloadableContribution;
|
||||
import cc.arduino.contributions.ui.InstallerJDialogUncaughtExceptionHandler;
|
||||
import cc.arduino.contributions.ui.*;
|
||||
@ -41,7 +42,7 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Collection;
|
||||
import java.util.*;
|
||||
|
||||
import static processing.app.I18n._;
|
||||
|
||||
@ -160,7 +161,8 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
|
||||
typeChooser.removeAllItems();
|
||||
typeChooser.addItem(new DropdownAllItem());
|
||||
typeChooser.addItem(new DropdownInstalledLibraryItem(indexer.getIndex()));
|
||||
Collection<String> types = indexer.getIndex().getTypes();
|
||||
java.util.List<String> types = new LinkedList<String>(indexer.getIndex().getTypes());
|
||||
Collections.sort(types, new LibraryTypeComparator());
|
||||
for (String type : types) {
|
||||
typeChooser.addItem(new DropdownLibraryOfTypeItem(type));
|
||||
}
|
||||
|
@ -0,0 +1,19 @@
|
||||
package cc.arduino.contributions.libraries;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class LibraryOfTypeComparatorTest {
|
||||
|
||||
@Test
|
||||
public void testSort() throws Exception {
|
||||
List<String> strings = Arrays.asList("Arduino", "Contributed", "Recommended", "Recommended", "Other", "1yetanother", "Arduino", "Recommended", "Contributed", "Recommended");
|
||||
Collections.sort(strings, new LibraryTypeComparator());
|
||||
assertEquals(Arrays.asList("Arduino", "Arduino", "Recommended", "Recommended", "Recommended", "Recommended", "Contributed", "Contributed", "1yetanother", "Other"), strings);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user