Commiting overloads to have GuiChoose lists sorted

This commit is contained in:
Maxmtg
2013-02-17 13:22:07 +00:00
parent dd8c0bb899
commit b585ddfcb7
2 changed files with 87 additions and 3 deletions

View File

@@ -4,6 +4,8 @@ import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.JDialog;
@@ -105,7 +107,7 @@ public class GuiChoose {
* @return a {@link java.util.List} object.
*/
public static <T> List<T> getChoices(final String message, final int min, final int max, final T[] choices) {
final ListChooser<T> c = new ListChooser<T>(message, min, max, Arrays.asList(choices));
final ListChooser<T> c = new ListChooser<T>(message, min, max, choices);
return getChoices(c);
}
@@ -186,5 +188,81 @@ public class GuiChoose {
return objects;
}
/**
* Convenience for getChoices(message, 0, 1, choices).
*
* @param <T>
* is automatically inferred.
* @param message
* a {@link java.lang.String} object.
* @param choices
* a T object.
* @return null if choices is missing, empty, or if the users' choices are
* empty; otherwise, returns the first item in the List returned by
* getChoices.
* @see #getChoices(String, int, int, Object...)
*/
public static <T> T sortedOneOrNone(final String message, final T[] choices, Comparator<T> comparer) {
if ((choices == null) || (choices.length == 0)) {
return null;
}
final List<T> choice = GuiChoose.sortedGetChoices(message, 0, 1, choices, comparer);
return choice.isEmpty() ? null : choice.get(0);
} // getChoiceOptional(String,T...)
public static <T> T sortedOneOrNone(final String message, final List<T> choices, Comparator<T> comparer) {
if ((choices == null) || choices.isEmpty()) {
return null;
}
final List<T> choice = GuiChoose.sortedGetChoices(message, 0, 1, choices, comparer);
return choice.isEmpty() ? null : choice.get(0);
} // getChoiceOptional(String,T...)
// returned Object will never be null
/**
* <p>
* getChoice.
* </p>
*
* @param <T>
* a T object.
* @param message
* a {@link java.lang.String} object.
* @param choices
* a T object.
* @return a T object.
*/
public static <T> T sortedOne(final String message, final T[] choices, Comparator<T> comparer) {
final List<T> choice = GuiChoose.sortedGetChoices(message, 1, 1, choices, comparer);
assert choice.size() == 1;
return choice.get(0);
} // getChoice()
public static <T> T sortedOne(final String message, final List<T> choices, Comparator<T> comparer) {
if ((choices == null) || (choices.size() == 0)) {
return null;
}
final List<T> choice = GuiChoose.sortedGetChoices(message, 1, 1, choices, comparer);
assert choice.size() == 1;
return choice.get(0);
}
public static <T> List<T> sortedNoneOrMany(final String message, final List<T> choices, Comparator<T> comparer) {
return GuiChoose.sortedGetChoices(message, 0, choices.size(), choices, comparer);
}
// If comparer is NULL, T must be comparable. Otherwise you'll get an exception from inside the Arrays.sort() routine
public static <T> List<T> sortedGetChoices(final String message, final int min, final int max, final T[] choices, Comparator<T> comparer) {
Arrays.sort(choices, comparer);
final ListChooser<T> c = new ListChooser<T>(message, min, max, choices);
return getChoices(c);
}
public static <T> List<T> sortedGetChoices(final String message, final int min, final int max, final List<T> choices, Comparator<T> comparer) {
Collections.sort(choices, comparer);
final ListChooser<T> c = new ListChooser<T>(message, min, max, choices);
return getChoices(c);
}
}

View File

@@ -24,6 +24,8 @@ import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.util.AbstractList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.swing.AbstractAction;
@@ -82,11 +84,15 @@ public class ListChooser<T> {
private JOptionPane optionPane;
private Action ok, cancel;
public ListChooser(final String title, final int minChoices, final int maxChoices, final Iterable<T> list) {
public ListChooser(final String title, final int minChoices, final int maxChoices, final T[] list) {
this(title, minChoices, maxChoices, Arrays.asList(list));
}
public ListChooser(final String title, final int minChoices, final int maxChoices, final Collection<T> list) {
this.title = title;
this.minChoices = minChoices;
this.maxChoices = maxChoices;
this.list = Lists.newArrayList(list);
this.list = list.getClass().isInstance(List.class) ? (List<T>)list : Lists.newArrayList(list);
this.jList = new JList(new ChooserListModel());
this.ok = new CloseAction(JOptionPane.OK_OPTION, "OK");
this.ok.setEnabled(minChoices == 0);