Fix duplication of user names in files list.
This commit is contained in:
parent
09abe21fed
commit
7bf7b8981c
@ -1,5 +1,10 @@
|
|||||||
package com.lyndir.masterpassword.gui.util;
|
package com.lyndir.masterpassword.gui.util;
|
||||||
|
|
||||||
|
import com.google.common.base.Predicates;
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import com.google.common.collect.Iterables;
|
||||||
|
import com.lyndir.lhunath.opal.system.logging.Logger;
|
||||||
|
import com.lyndir.lhunath.opal.system.util.ObjectUtils;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
@ -15,6 +20,8 @@ import javax.swing.event.ListSelectionListener;
|
|||||||
public class CollectionListModel<E> extends AbstractListModel<E>
|
public class CollectionListModel<E> extends AbstractListModel<E>
|
||||||
implements ComboBoxModel<E>, ListSelectionListener, Selectable<E, CollectionListModel<E>> {
|
implements ComboBoxModel<E>, ListSelectionListener, Selectable<E, CollectionListModel<E>> {
|
||||||
|
|
||||||
|
private static final Logger logger = Logger.get( CollectionListModel.class );
|
||||||
|
|
||||||
private final List<E> model = new LinkedList<>();
|
private final List<E> model = new LinkedList<>();
|
||||||
@Nullable
|
@Nullable
|
||||||
private JList<E> list;
|
private JList<E> list;
|
||||||
@ -51,16 +58,11 @@ public class CollectionListModel<E> extends AbstractListModel<E>
|
|||||||
* This operation will mutate the internal model to reflect the given model.
|
* This operation will mutate the internal model to reflect the given model.
|
||||||
* The given model will remain untouched and independent from this object.
|
* The given model will remain untouched and independent from this object.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({ "unchecked", "SuspiciousToArrayCall" })
|
@SuppressWarnings({ "Guava", "AssignmentToForLoopParameter" })
|
||||||
public synchronized void set(final Collection<? extends E> elements) {
|
public synchronized void set(final Iterable<? extends E> elements) {
|
||||||
set( (E[]) elements.toArray( new Object[0] ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("AssignmentToForLoopParameter")
|
|
||||||
public synchronized void set(final E... elements) {
|
|
||||||
ListIterator<E> oldIt = model.listIterator();
|
ListIterator<E> oldIt = model.listIterator();
|
||||||
for (int from = 0; oldIt.hasNext(); ++from) {
|
for (int from = 0; oldIt.hasNext(); ++from) {
|
||||||
int to = Arrays.binarySearch( elements, oldIt.next() );
|
int to = Iterables.indexOf( elements, Predicates.equalTo( oldIt.next() ) );
|
||||||
|
|
||||||
if (to != from) {
|
if (to != from) {
|
||||||
oldIt.remove();
|
oldIt.remove();
|
||||||
@ -69,19 +71,25 @@ public class CollectionListModel<E> extends AbstractListModel<E>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int to = 0; to < elements.length; ++to) {
|
int to = 0;
|
||||||
E newSite = elements[to];
|
for (final E newSite : elements) {
|
||||||
|
|
||||||
if ((to >= model.size()) || !Objects.equals( model.get( to ), newSite )) {
|
if ((to >= model.size()) || !Objects.equals( model.get( to ), newSite )) {
|
||||||
model.add( to, newSite );
|
model.add( to, newSite );
|
||||||
fireIntervalAdded( this, to, to );
|
fireIntervalAdded( this, to, to );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
++to;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((selectedItem == null) || !model.contains( selectedItem ))
|
if ((selectedItem == null) || !model.contains( selectedItem ))
|
||||||
setSelectedItem( getElementAt( 0 ) );
|
setSelectedItem( getElementAt( 0 ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public final synchronized void set(final E... elements) {
|
||||||
|
set( ImmutableList.copyOf( elements ) );
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings({ "unchecked", "SuspiciousMethodCalls" })
|
@SuppressWarnings({ "unchecked", "SuspiciousMethodCalls" })
|
||||||
public synchronized void setSelectedItem(@Nullable final Object newSelectedItem) {
|
public synchronized void setSelectedItem(@Nullable final Object newSelectedItem) {
|
||||||
|
Loading…
Reference in New Issue
Block a user