From dd8c0bb89964d86bbe7412faf1e2939d05d4dfad Mon Sep 17 00:00:00 2001 From: Maxmtg Date: Sun, 17 Feb 2013 13:07:43 +0000 Subject: [PATCH] GuiChoose - reverting lists sorting inside GUI components --- .../ability/effects/ChooseColorEffect.java | 6 +- .../forge/card/ability/effects/DigEffect.java | 2 +- src/main/java/forge/deck/Deck.java | 15 ++ src/main/java/forge/gui/DualListBox.java | 24 +-- src/main/java/forge/gui/GuiChoose.java | 195 +++++++++--------- src/main/java/forge/gui/ListChooser.java | 15 +- .../java/forge/gui/match/QuestWinLose.java | 4 +- src/test/java/forge/gui/ListChooserTest.java | 2 +- 8 files changed, 135 insertions(+), 128 deletions(-) diff --git a/src/main/java/forge/card/ability/effects/ChooseColorEffect.java b/src/main/java/forge/card/ability/effects/ChooseColorEffect.java index e6dd3a1faaf..787b6d4cb92 100644 --- a/src/main/java/forge/card/ability/effects/ChooseColorEffect.java +++ b/src/main/java/forge/card/ability/effects/ChooseColorEffect.java @@ -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 o = GuiChoose.oneOrMany("Choose a color or colors", - Constant.Color.ONLY_COLORS); + String[] choices = Constant.Color.ONLY_COLORS; + final List o = GuiChoose.getChoices("Choose a color or colors", 1, choices.length, choices); card.setChosenColor(new ArrayList(o)); } else if (sa.hasParam("TwoColors")) { - final List o = GuiChoose.amount("Choose two colors", Constant.Color.ONLY_COLORS, 2); + final List o = GuiChoose.getChoices("Choose two colors", 2, 2, Constant.Color.ONLY_COLORS); card.setChosenColor(new ArrayList(o)); } else { final Object o = GuiChoose.one("Choose a color", Constant.Color.ONLY_COLORS); diff --git a/src/main/java/forge/card/ability/effects/DigEffect.java b/src/main/java/forge/card/ability/effects/DigEffect.java index fae7afab625..f4326488589 100644 --- a/src/main/java/forge/card/ability/effects/DigEffect.java +++ b/src/main/java/forge/card/ability/effects/DigEffect.java @@ -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); diff --git a/src/main/java/forge/deck/Deck.java b/src/main/java/forge/deck/Deck.java index 56972557740..78bae7ef9ef 100644 --- a/src/main/java/forge/deck/Deck.java +++ b/src/main/java/forge/deck/Deck.java @@ -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; @@ -292,6 +294,19 @@ public class Deck extends DeckBase implements Iterable AI_KNOWS_HOW_TO_PLAY_ALL_CARDS = new Predicate() { + @Override + public boolean apply(Deck d) { + for(Entry cp: d) { + for(Entry e : cp.getValue()) { + if ( e.getKey().getCard().getRemAIDecks() ) + return false; + } + } + return true; + } + }; /* (non-Javadoc) * @see java.lang.Iterable#iterator() diff --git a/src/main/java/forge/gui/DualListBox.java b/src/main/java/forge/gui/DualListBox.java index 24b67719a3d..8400a8344c2 100644 --- a/src/main/java/forge/gui/DualListBox.java +++ b/src/main/java/forge/gui/DualListBox.java @@ -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> extends FPanel { +public class DualListBox extends FPanel { private final FList sourceList; private final UnsortedListModel sourceListModel; @@ -71,8 +67,7 @@ public class DualListBox> extends FPanel { private boolean sideboardingMode = false; private boolean showCard = true; - public DualListBox(int remainingSources, String label, List sourceElements, List destElements, - boolean startSorted, Comparator sortComparator) { + public DualListBox(int remainingSources, List sourceElements, List destElements ) { targetRemainingSources = remainingSources; sourceListModel = new UnsortedListModel(); sourceList = new FList(sourceListModel); @@ -166,7 +161,7 @@ public class DualListBox> 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> 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> 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> extends FPanel { _setButtonState(); } + public void setSecondColumnLabelText(String label) { + orderedLabel.setText(label); + } + public void setSideboardMode( boolean isSideboardMode) { sideboardingMode = isSideboardMode; if (sideboardingMode) { diff --git a/src/main/java/forge/gui/GuiChoose.java b/src/main/java/forge/gui/GuiChoose.java index f00e45c5b8e..e1f64ae92fa 100644 --- a/src/main/java/forge/gui/GuiChoose.java +++ b/src/main/java/forge/gui/GuiChoose.java @@ -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 oneOrNone(String message, List choices, boolean sort, Comparator sortComparator) { - if ((choices == null) || (choices.size() == 0)) { - return null; - } - final List choice = _getChoices(message, 0, 1, choices, sort, sortComparator); - return choice.isEmpty() ? null : choice.get(0); - } - public static > T oneOrNone(String message, List choices) { - return oneOrNone(message, choices, false, null); - } - public static > 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 + * 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 > List amount(String message, List choices, int amt, boolean sort, Comparator sortComparator) { - if (null == choices || 0 == choices.size() || 0 == amt) { + public static T oneOrNone(final String message, final T[] choices) { + if ((choices == null) || (choices.length == 0)) { return null; } - final List choice = _getChoices(message, amt, amt, choices, sort, sortComparator); - assert choice.size() == amt; - return choice; - } - public static > List amount(String message, List choices, int amt) { - return amount(message, choices, amt, false, null); - } - public static > List amount(String message, T[] choices, int amt) { - if ((choices == null)) { return null; } - return amount(message, Arrays.asList(choices), amt); - } + final List choice = GuiChoose.getChoices(message, 0, 1, choices); + return choice.isEmpty() ? null : choice.get(0); + } // getChoiceOptional(String,T...) - public static > T one(String message, List choices, boolean sort, Comparator sortComparator) { - List choice = amount(message, choices, 1, sort, sortComparator); - if (null == choice || choice.isEmpty()) { + public static T oneOrNone(final String message, final Collection choices) { + if ((choices == null) || choices.isEmpty()) { return null; } + final List choice = GuiChoose.getChoices(message, 0, 1, choices); + return choice.isEmpty() ? null : choice.get(0); + } // getChoiceOptional(String,T...) + + // returned Object will never be null + /** + *

+ * getChoice. + *

+ * + * @param + * a T object. + * @param message + * a {@link java.lang.String} object. + * @param choices + * a T object. + * @return a T object. + */ + public static T one(final String message, final T[] choices) { + final List choice = GuiChoose.getChoices(message, 1, 1, choices); + assert choice.size() == 1; + return choice.get(0); + } // getChoice() + + public static T one(final String message, final Collection choices) { + if ((choices == null) || (choices.size() == 0)) { + return null; + } + final List choice = GuiChoose.getChoices(message, 1, 1, choices); + assert choice.size() == 1; return choice.get(0); } - public static > T one(String message, List choices) { - return one(message, choices, false, null); - } - public static > 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 choices = Arrays.asList(singleChoice); - return one(message, choices); - } - public static > List noneOrMany(String message, List choices, boolean sort, Comparator sortComparator) { - if ((choices == null) || (choices.size() == 0)) { - return null; - } - final List choice = _getChoices(message, 0, choices.size(), choices, sort, sortComparator); - return choice.isEmpty() ? null : choice; - } - public static > List noneOrMany(String message, List choices) { - return noneOrMany(message, choices, false, null); - } - - public static > List oneOrMany(String message, List choices, boolean sort, Comparator sortComparator) { - if ((choices == null) || (choices.size() == 0)) { - return null; - } - final List choice = _getChoices(message, 1, choices.size(), choices, sort, sortComparator); - return choice.isEmpty() ? null : choice; - } - public static > List oneOrMany(String message, List choices) { - return oneOrMany(message, choices, false, null); - } - public static > List oneOrMany(String message, T[] choices) { - if ((choices == null)) { return null; } - return oneOrMany(message, Arrays.asList(choices)); + public static List noneOrMany(final String message, final Collection choices) { + return GuiChoose.getChoices(message, 0, choices.size(), choices); } // returned Object will never be null - private static > List _getChoices(String message, int min, int max, List choices, - boolean sort, Comparator sortComparator) { - return _getChoices(new ListChooser(message, min, max, choices, sort, sortComparator)); + /** + *

+ * getChoices. + *

+ * + * @param + * 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 List getChoices(final String message, final int min, final int max, final T[] choices) { + final ListChooser c = new ListChooser(message, min, max, Arrays.asList(choices)); + return getChoices(c); } - private static > List _getChoices(ListChooser c) { + + public static List getChoices(final String message, final int min, final int max, final Collection choices) { + final ListChooser c = new ListChooser(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 choices = new ArrayList(); + choices.add(singleChoice); + return one(message, choices); + } + + private static List getChoices(final ListChooser 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 List order(final String title, final String top, int remainingObjects, + final List sourceChoices, List destChoices, Card referenceCard) { + return order(title, top, remainingObjects, sourceChoices, destChoices, referenceCard, false); } - public static > List order(String title, String top, int remainingObjects, - List sourceChoices, List destChoices, Card referenceCard, boolean sort, Comparator sortComparator) { - return _order(title, top, remainingObjects, sourceChoices, destChoices, referenceCard, sort, sortComparator, false); - } - public static > List order(String title, String top, int remainingObjects, - List sourceChoices, List destChoices, Card referenceCard) { - return order(title, top, remainingObjects, sourceChoices, destChoices, referenceCard, false, null); - } - public static List sideboard(List sideboard, List deck) { - return _order("Sideboard", "Main Deck", sideboard.size(), sideboard, deck, null, true, null, true); + public static List sideboard(List sideboard, List deck) { + return order("Sideboard", "Main Deck", sideboard.size(), sideboard, deck, null, true); } - private static > List _order( - String title, String top, int remainingObjects, List sourceChoices, List destChoices, - Card referenceCard, boolean startSorted, Comparator sortComparator, boolean sideboardingMode) { + + public static List order(final String title, final String top, int remainingObjects, + final List sourceChoices, List destChoices, Card referenceCard, boolean sideboardingMode) { // An input box for handling the order of choices. final JFrame frame = new JFrame(); - DualListBox dual = new DualListBox(remainingObjects, top, sourceChoices, destChoices, startSorted, sortComparator); + DualListBox dual = new DualListBox(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 objects = dual.getOrderedList(); @@ -182,4 +185,6 @@ public class GuiChoose { GuiUtils.clearPanelSelections(); return objects; } + } + diff --git a/src/main/java/forge/gui/ListChooser.java b/src/main/java/forge/gui/ListChooser.java index d576e6fee58..084d3572fba 100644 --- a/src/main/java/forge/gui/ListChooser.java +++ b/src/main/java/forge/gui/ListChooser.java @@ -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> { +public class ListChooser { // Data and number of choices for the list private List list; @@ -83,15 +82,11 @@ public class ListChooser> { private JOptionPane optionPane; private Action ok, cancel; - public ListChooser(final String title, final int minChoices, final int maxChoices, - final List list, boolean startSorted, Comparator sortComparator) { + public ListChooser(final String title, final int minChoices, final int maxChoices, final Iterable list) { this.title = title; this.minChoices = minChoices; this.maxChoices = maxChoices; - this.list = new ArrayList(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); diff --git a/src/main/java/forge/gui/match/QuestWinLose.java b/src/main/java/forge/gui/match/QuestWinLose.java index 4f2282d750d..996c6429fe6 100644 --- a/src/main/java/forge/gui/match/QuestWinLose.java +++ b/src/main/java/forge/gui/match/QuestWinLose.java @@ -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 ch = new ListChooser("Choose bonus booster format", 1, 1, formats, true, null); + Collections.sort(formats); + final ListChooser ch = new ListChooser("Choose bonus booster format", 1, 1, formats); ch.show(pref); final GameFormat selected = ch.getSelectedValue(); diff --git a/src/test/java/forge/gui/ListChooserTest.java b/src/test/java/forge/gui/ListChooserTest.java index 04db9bc30ae..2ceb758250e 100644 --- a/src/test/java/forge/gui/ListChooserTest.java +++ b/src/test/java/forge/gui/ListChooserTest.java @@ -15,7 +15,7 @@ public class ListChooserTest { */ @Test(groups = { "UnitTest", "fast" }, timeOut = 1000, enabled = false) public void listChooserTest1() { - final ListChooser c = new ListChooser("choose a or b", 0, 2, Arrays.asList("a", "b"), false, null); + final ListChooser c = new ListChooser("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);