GuiChoose - reverting lists sorting inside GUI components

This commit is contained in:
Maxmtg
2013-02-17 13:07:43 +00:00
parent be6e8fbf25
commit dd8c0bb899
8 changed files with 135 additions and 128 deletions

View File

@@ -46,11 +46,11 @@ public class ChooseColorEffect extends SpellEffect {
if ((tgt == null) || p.canBeTargetedBy(sa)) {
if (sa.getActivatingPlayer().isHuman()) {
if (sa.hasParam("OrColors")) {
final List<String> o = GuiChoose.oneOrMany("Choose a color or colors",
Constant.Color.ONLY_COLORS);
String[] choices = Constant.Color.ONLY_COLORS;
final List<String> o = GuiChoose.getChoices("Choose a color or colors", 1, choices.length, choices);
card.setChosenColor(new ArrayList<String>(o));
} else if (sa.hasParam("TwoColors")) {
final List<String> o = GuiChoose.amount("Choose two colors", Constant.Color.ONLY_COLORS, 2);
final List<String> o = GuiChoose.getChoices("Choose two colors", 2, 2, Constant.Color.ONLY_COLORS);
card.setChosenColor(new ArrayList<String>(o));
} else {
final Object o = GuiChoose.one("Choose a color", Constant.Color.ONLY_COLORS);

View File

@@ -258,7 +258,7 @@ public class DigEffect extends SpellEffect {
break;
}
if (changeValid.length() > 0) {
GuiChoose.one("Computer picked: ", chosen);
GuiChoose.show("Computer picked: ", chosen);
}
movedCards.add(chosen);
valid.remove(chosen);

View File

@@ -32,6 +32,8 @@ import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import forge.deck.io.DeckFileHeader;
import forge.deck.io.DeckSerializer;
import forge.gui.deckeditor.tables.TableSorter;
@@ -293,6 +295,19 @@ public class Deck extends DeckBase implements Iterable<Entry<DeckSection, CardPo
}
};
public static final Predicate<Deck> AI_KNOWS_HOW_TO_PLAY_ALL_CARDS = new Predicate<Deck>() {
@Override
public boolean apply(Deck d) {
for(Entry<DeckSection, CardPool> cp: d) {
for(Entry<CardPrinted, Integer> e : cp.getValue()) {
if ( e.getKey().getCard().getRemAIDecks() )
return false;
}
}
return true;
}
};
/* (non-Javadoc)
* @see java.lang.Iterable#iterator()
*/

View File

@@ -13,8 +13,6 @@ import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.JDialog;
@@ -25,8 +23,6 @@ import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import com.google.common.collect.Lists;
import forge.Card;
import forge.card.spellability.SpellAbility;
import forge.gui.match.CMatchUI;
@@ -49,7 +45,7 @@ import forge.item.CardPrinted;
// Single ok button, disabled until left box has specified number of items remaining
@SuppressWarnings("serial")
public class DualListBox<T extends Comparable<? super T>> extends FPanel {
public class DualListBox<T> extends FPanel {
private final FList sourceList;
private final UnsortedListModel<T> sourceListModel;
@@ -71,8 +67,7 @@ public class DualListBox<T extends Comparable<? super T>> extends FPanel {
private boolean sideboardingMode = false;
private boolean showCard = true;
public DualListBox(int remainingSources, String label, List<T> sourceElements, List<T> destElements,
boolean startSorted, Comparator<T> sortComparator) {
public DualListBox(int remainingSources, List<T> sourceElements, List<T> destElements ) {
targetRemainingSources = remainingSources;
sourceListModel = new UnsortedListModel<T>();
sourceList = new FList(sourceListModel);
@@ -166,7 +161,7 @@ public class DualListBox<T extends Comparable<? super T>> extends FPanel {
centerPanel.add(removeButton);
centerPanel.add(removeAllButton);
orderedLabel = new FLabel.Builder().text(label).build();
orderedLabel = new FLabel.Builder().build();
FPanel rightPanel = new FPanel(new BorderLayout());
rightPanel.setSize(300, 300);
@@ -182,11 +177,6 @@ public class DualListBox<T extends Comparable<? super T>> extends FPanel {
_addListListeners(destList);
if (destElements != null && !destElements.isEmpty()) {
if (startSorted) {
// it would be nice if we could sort in place, but it might mess up our callers
destElements = Lists.newArrayList(destElements);
Collections.sort(destElements, sortComparator);
}
addDestinationElements(destElements);
SwingUtilities.invokeLater(new Runnable() {
@@ -198,10 +188,6 @@ public class DualListBox<T extends Comparable<? super T>> extends FPanel {
}
if (sourceElements != null && !sourceElements.isEmpty()) {
if (startSorted) {
sourceElements = Lists.newArrayList(sourceElements);
Collections.sort(sourceElements, sortComparator);
}
addSourceElements(sourceElements);
SwingUtilities.invokeLater(new Runnable() {
@@ -215,6 +201,10 @@ public class DualListBox<T extends Comparable<? super T>> extends FPanel {
_setButtonState();
}
public void setSecondColumnLabelText(String label) {
orderedLabel.setText(label);
}
public void setSideboardMode( boolean isSideboardMode) {
sideboardingMode = isSideboardMode;
if (sideboardingMode) {

View File

@@ -1,8 +1,9 @@
package forge.gui;
import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Collection;
import java.util.List;
import javax.swing.JDialog;
@@ -14,7 +15,6 @@ import javax.swing.event.ListSelectionListener;
import forge.Card;
import forge.gui.match.CMatchUI;
import forge.item.CardPrinted;
import forge.item.InventoryItem;
/**
@@ -22,105 +22,113 @@ import forge.item.InventoryItem;
*
*/
public class GuiChoose {
public static <T extends Comparable<? super T>> T oneOrNone(String message, List<T> choices, boolean sort, Comparator<T> sortComparator) {
if ((choices == null) || (choices.size() == 0)) {
return null;
}
final List<T> choice = _getChoices(message, 0, 1, choices, sort, sortComparator);
return choice.isEmpty() ? null : choice.get(0);
}
public static <T extends Comparable<? super T>> T oneOrNone(String message, List<T> choices) {
return oneOrNone(message, choices, false, null);
}
public static <T extends Comparable<? super T>> T oneOrNone(String message, T[] choices) {
if ((choices == null)) { return null; }
return oneOrNone(message, Arrays.asList(choices));
}
/**
* Presents a list of choices where the player must choose exactly *amount* options.
* Convenience for getChoices(message, 0, 1, choices).
*
* @param <T>
* is automatically inferred.
* @param message
* the message to display to the player.
* a {@link java.lang.String} object.
* @param choices
* the choices to choose from.
* @param amount
* the amount of options that must be chosen.
* @return
* 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 extends Comparable<? super T>> List<T> amount(String message, List<T> choices, int amt, boolean sort, Comparator<T> sortComparator) {
if (null == choices || 0 == choices.size() || 0 == amt) {
public static <T> T oneOrNone(final String message, final T[] choices) {
if ((choices == null) || (choices.length == 0)) {
return null;
}
final List<T> choice = _getChoices(message, amt, amt, choices, sort, sortComparator);
assert choice.size() == amt;
return choice;
}
public static <T extends Comparable<? super T>> List<T> amount(String message, List<T> choices, int amt) {
return amount(message, choices, amt, false, null);
}
public static <T extends Comparable<? super T>> List<T> amount(String message, T[] choices, int amt) {
if ((choices == null)) { return null; }
return amount(message, Arrays.asList(choices), amt);
}
final List<T> choice = GuiChoose.getChoices(message, 0, 1, choices);
return choice.isEmpty() ? null : choice.get(0);
} // getChoiceOptional(String,T...)
public static <T extends Comparable<? super T>> T one(String message, List<T> choices, boolean sort, Comparator<T> sortComparator) {
List<T> choice = amount(message, choices, 1, sort, sortComparator);
if (null == choice || choice.isEmpty()) {
public static <T> T oneOrNone(final String message, final Collection<T> choices) {
if ((choices == null) || choices.isEmpty()) {
return null;
}
final List<T> choice = GuiChoose.getChoices(message, 0, 1, choices);
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 one(final String message, final T[] choices) {
final List<T> choice = GuiChoose.getChoices(message, 1, 1, choices);
assert choice.size() == 1;
return choice.get(0);
} // getChoice()
public static <T> T one(final String message, final Collection<T> choices) {
if ((choices == null) || (choices.size() == 0)) {
return null;
}
final List<T> choice = GuiChoose.getChoices(message, 1, 1, choices);
assert choice.size() == 1;
return choice.get(0);
}
public static <T extends Comparable<? super T>> T one(String message, List<T> choices) {
return one(message, choices, false, null);
}
public static <T extends Comparable<? super T>> T one(String message, T[] choices) {
if ((choices == null)) { return null; }
return one(message, Arrays.asList(choices));
}
// Nothing to choose here. Code uses this to just show a card.
public static Card one(String message, Card singleChoice) {
List<Card> choices = Arrays.asList(singleChoice);
return one(message, choices);
}
public static <T extends Comparable<? super T>> List<T> noneOrMany(String message, List<T> choices, boolean sort, Comparator<T> sortComparator) {
if ((choices == null) || (choices.size() == 0)) {
return null;
}
final List<T> choice = _getChoices(message, 0, choices.size(), choices, sort, sortComparator);
return choice.isEmpty() ? null : choice;
}
public static <T extends Comparable<? super T>> List<T> noneOrMany(String message, List<T> choices) {
return noneOrMany(message, choices, false, null);
}
public static <T extends Comparable<? super T>> List<T> oneOrMany(String message, List<T> choices, boolean sort, Comparator<T> sortComparator) {
if ((choices == null) || (choices.size() == 0)) {
return null;
}
final List<T> choice = _getChoices(message, 1, choices.size(), choices, sort, sortComparator);
return choice.isEmpty() ? null : choice;
}
public static <T extends Comparable<? super T>> List<T> oneOrMany(String message, List<T> choices) {
return oneOrMany(message, choices, false, null);
}
public static <T extends Comparable<? super T>> List<T> oneOrMany(String message, T[] choices) {
if ((choices == null)) { return null; }
return oneOrMany(message, Arrays.asList(choices));
public static <T> List<T> noneOrMany(final String message, final Collection<T> choices) {
return GuiChoose.getChoices(message, 0, choices.size(), choices);
}
// returned Object will never be null
private static <T extends Comparable<? super T>> List<T> _getChoices(String message, int min, int max, List<T> choices,
boolean sort, Comparator<T> sortComparator) {
return _getChoices(new ListChooser<T>(message, min, max, choices, sort, sortComparator));
/**
* <p>
* getChoices.
* </p>
*
* @param <T>
* a T object.
* @param message
* a {@link java.lang.String} object.
* @param min
* a int.
* @param max
* a int.
* @param choices
* a T object.
* @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));
return getChoices(c);
}
private static <T extends Comparable<? super T>> List<T> _getChoices(ListChooser<T> c) {
public static <T> List<T> getChoices(final String message, final int min, final int max, final Collection<T> choices) {
final ListChooser<T> c = new ListChooser<T>(message, min, max, choices);
return getChoices(c);
}
// Nothing to choose here. Code uses this to just show a card.
public static Card show(final String message, final Card singleChoice) {
List<Card> choices = new ArrayList<Card>();
choices.add(singleChoice);
return one(message, choices);
}
private static <T> List<T> getChoices(final ListChooser<T> c) {
final JList list = c.getJList();
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(final ListSelectionEvent ev) {
if (list.getSelectedValue() instanceof Card) {
CMatchUI.SINGLETON_INSTANCE.setCard((Card) list.getSelectedValue());
GuiUtils.clearPanelSelections();
GuiUtils.setPanelSelection((Card) list.getSelectedValue());
}
@@ -129,31 +137,28 @@ public class GuiChoose {
}
}
});
c.show();
GuiUtils.clearPanelSelections();
return c.getSelectedValues();
} // getChoice()
public static <T> List<T> order(final String title, final String top, int remainingObjects,
final List<T> sourceChoices, List<T> destChoices, Card referenceCard) {
return order(title, top, remainingObjects, sourceChoices, destChoices, referenceCard, false);
}
public static <T extends Comparable<? super T>> List<T> order(String title, String top, int remainingObjects,
List<T> sourceChoices, List<T> destChoices, Card referenceCard, boolean sort, Comparator<T> sortComparator) {
return _order(title, top, remainingObjects, sourceChoices, destChoices, referenceCard, sort, sortComparator, false);
}
public static <T extends Comparable<? super T>> List<T> order(String title, String top, int remainingObjects,
List<T> sourceChoices, List<T> destChoices, Card referenceCard) {
return order(title, top, remainingObjects, sourceChoices, destChoices, referenceCard, false, null);
}
public static List<CardPrinted> sideboard(List<CardPrinted> sideboard, List<CardPrinted> deck) {
return _order("Sideboard", "Main Deck", sideboard.size(), sideboard, deck, null, true, null, true);
public static <T> List<T> sideboard(List<T> sideboard, List<T> deck) {
return order("Sideboard", "Main Deck", sideboard.size(), sideboard, deck, null, true);
}
private static <T extends Comparable<? super T>> List<T> _order(
String title, String top, int remainingObjects, List<T> sourceChoices, List<T> destChoices,
Card referenceCard, boolean startSorted, Comparator<T> sortComparator, boolean sideboardingMode) {
public static <T> List<T> order(final String title, final String top, int remainingObjects,
final List<T> sourceChoices, List<T> destChoices, Card referenceCard, boolean sideboardingMode) {
// An input box for handling the order of choices.
final JFrame frame = new JFrame();
DualListBox<T> dual = new DualListBox<T>(remainingObjects, top, sourceChoices, destChoices, startSorted, sortComparator);
DualListBox<T> dual = new DualListBox<T>(remainingObjects, sourceChoices, destChoices);
dual.setSideboardMode(sideboardingMode);
dual.setSecondColumnLabelText(top);
frame.setLayout(new BorderLayout());
frame.setSize(dual.getPreferredSize());
@@ -168,12 +173,10 @@ public class GuiChoose {
dialog.setLocationRelativeTo(null);
dialog.pack();
dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
if (referenceCard != null) {
CMatchUI.SINGLETON_INSTANCE.setCard(referenceCard);
// MARKED FOR UPDATE
}
dialog.setVisible(true);
List<T> objects = dual.getOrderedList();
@@ -182,4 +185,6 @@ public class GuiChoose {
GuiUtils.clearPanelSelections();
return objects;
}
}

View File

@@ -24,9 +24,6 @@ import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.AbstractAction;
@@ -42,6 +39,8 @@ import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import com.google.common.collect.Lists;
/**
* A simple class that shows a list of choices in a dialog. Two properties
* influence the behavior of a list chooser: minSelection and maxSelection.
@@ -65,7 +64,7 @@ import javax.swing.event.ListSelectionListener;
* @author Forge
* @version $Id$
*/
public class ListChooser<T extends Comparable<? super T>> {
public class ListChooser<T> {
// Data and number of choices for the list
private List<T> list;
@@ -83,15 +82,11 @@ public class ListChooser<T extends Comparable<? super T>> {
private JOptionPane optionPane;
private Action ok, cancel;
public ListChooser(final String title, final int minChoices, final int maxChoices,
final List<T> list, boolean startSorted, Comparator<T> sortComparator) {
public ListChooser(final String title, final int minChoices, final int maxChoices, final Iterable<T> list) {
this.title = title;
this.minChoices = minChoices;
this.maxChoices = maxChoices;
this.list = new ArrayList<T>(list);
if (startSorted) {
Collections.sort(this.list, sortComparator);
}
this.list = Lists.newArrayList(list);
this.jList = new JList(new ChooserListModel());
this.ok = new CloseAction(JOptionPane.OK_OPTION, "OK");
this.ok.setEnabled(minChoices == 0);

View File

@@ -57,6 +57,7 @@ import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
@@ -555,7 +556,8 @@ public class QuestWinLose extends ControlWinLose {
}
}
final ListChooser<GameFormat> ch = new ListChooser<GameFormat>("Choose bonus booster format", 1, 1, formats, true, null);
Collections.sort(formats);
final ListChooser<GameFormat> ch = new ListChooser<GameFormat>("Choose bonus booster format", 1, 1, formats);
ch.show(pref);
final GameFormat selected = ch.getSelectedValue();

View File

@@ -15,7 +15,7 @@ public class ListChooserTest {
*/
@Test(groups = { "UnitTest", "fast" }, timeOut = 1000, enabled = false)
public void listChooserTest1() {
final ListChooser<String> c = new ListChooser<String>("choose a or b", 0, 2, Arrays.asList("a", "b"), false, null);
final ListChooser<String> c = new ListChooser<String>("choose a or b", 0, 2, Arrays.asList("a", "b"));
System.out.println(c.show());
for (final String s : c.getSelectedValues()) {
System.out.println(s);