From 49dc0e2e59e71fe8d5eeb726d4a3deb8e5b8588b Mon Sep 17 00:00:00 2001 From: elcnesh Date: Mon, 11 May 2015 14:22:08 +0000 Subject: [PATCH] Cleanup: use Lists rather than arrays whenever applicable (removes some methods) --- .../java/forge/ai/PlayerControllerAi.java | 4 +- .../src/main/java/forge/util/EnumUtil.java | 8 + .../src/main/java/forge/game/PlanarDice.java | 4 + .../ability/effects/CountersRemoveEffect.java | 5 +- .../java/forge/game/card/CounterType.java | 4 + .../java/forge/game/player/DelayedReveal.java | 12 +- .../forge/game/player/PlayerController.java | 4 +- .../src/main/java/forge/GuiDesktop.java | 4 +- .../src/main/java/forge/control/FControl.java | 2 +- .../java/forge/deckchooser/FDeckChooser.java | 128 ++++++++-------- .../src/main/java/forge/gui/GuiDialog.java | 9 +- .../src/main/java/forge/gui/ImportDialog.java | 7 +- .../src/main/java/forge/gui/ListChooser.java | 10 +- .../forge/screens/deckeditor/SEditorIO.java | 5 +- .../CEditorQuestDraftingProcess.java | 6 +- .../main/java/forge/screens/home/VLobby.java | 9 +- .../home/quest/CSubmenuQuestDraft.java | 10 +- .../java/forge/screens/match/CMatchUI.java | 19 ++- .../screens/match/QuestDraftWinLose.java | 4 +- .../workshop/controllers/CCardScript.java | 7 +- .../main/java/forge/toolbox/FComboBox.java | 4 + .../main/java/forge/toolbox/FOptionPane.java | 27 ++-- .../util/PlayerControllerForTests.java | 4 +- forge-gui-mobile/src/forge/GuiMobile.java | 4 +- .../src/forge/assets/AssetsDownloader.java | 15 +- .../src/forge/card/GameEntityPicker.java | 4 +- .../src/forge/deck/FDeckChooser.java | 3 +- .../src/forge/deck/FDeckEditor.java | 5 +- .../src/forge/deck/FDeckImportDialog.java | 6 +- .../constructed/ConstructedScreen.java | 7 +- .../forge/screens/match/MatchController.java | 114 ++++++++------- .../src/forge/toolbox/FOptionPane.java | 30 ++-- .../src/forge/toolbox/GuiDialog.java | 17 ++- .../src/forge/toolbox/ListChooser.java | 10 +- .../main/java/forge/interfaces/IGuiBase.java | 4 +- .../main/java/forge/interfaces/IGuiGame.java | 24 ++- .../java/forge/match/AbstractGuiGame.java | 72 ++++----- .../forge/match/input/InputSelectTargets.java | 54 +++---- .../main/java/forge/net/ProtocolMethod.java | 11 +- .../java/forge/net/server/NetGuiGame.java | 10 +- .../planarconquest/ConquestController.java | 3 +- .../forge/player/PlayerControllerHuman.java | 138 +++++++++--------- .../main/java/forge/util/gui/SOptionPane.java | 14 +- 43 files changed, 450 insertions(+), 391 deletions(-) diff --git a/forge-ai/src/main/java/forge/ai/PlayerControllerAi.java b/forge-ai/src/main/java/forge/ai/PlayerControllerAi.java index 7cde8cb93a1..28911ad5166 100644 --- a/forge-ai/src/main/java/forge/ai/PlayerControllerAi.java +++ b/forge-ai/src/main/java/forge/ai/PlayerControllerAi.java @@ -242,7 +242,7 @@ public class PlayerControllerAi extends PlayerController { } @Override - public void reveal(Collection cards, ZoneType zone, PlayerView owner, String messagePrefix) { + public void reveal(List cards, ZoneType zone, PlayerView owner, String messagePrefix) { // We don't know how to reveal cards to AI } @@ -583,7 +583,7 @@ public class PlayerControllerAi extends PlayerController { } @Override - public CounterType chooseCounterType(Collection options, SpellAbility sa, String prompt) { + public CounterType chooseCounterType(List options, SpellAbility sa, String prompt) { // may write a smarter AI if you need to (with calls to AI-clas for given API ability) // TODO: ArsenalNut (06 Feb 12)computer needs diff --git a/forge-core/src/main/java/forge/util/EnumUtil.java b/forge-core/src/main/java/forge/util/EnumUtil.java index 57451aca4cf..e8fe486e5e5 100644 --- a/forge-core/src/main/java/forge/util/EnumUtil.java +++ b/forge-core/src/main/java/forge/util/EnumUtil.java @@ -7,6 +7,14 @@ public final class EnumUtil { private EnumUtil() { } + /** + * Get the names of the values of an enum type. + * + * @param enumType + * an {@link Enum} type. + * @return an {@link ImmutableList} of strings representing the names of the + * enum's values. + */ public static ImmutableList getNames(final Class> enumType) { final ImmutableList.Builder builder = ImmutableList.builder(); for (final Enum type : enumType.getEnumConstants()) { diff --git a/forge-game/src/main/java/forge/game/PlanarDice.java b/forge-game/src/main/java/forge/game/PlanarDice.java index a951a97f7f2..2d2ec53eb4d 100644 --- a/forge-game/src/main/java/forge/game/PlanarDice.java +++ b/forge-game/src/main/java/forge/game/PlanarDice.java @@ -5,6 +5,8 @@ import forge.game.trigger.TriggerType; import java.util.HashMap; +import com.google.common.collect.ImmutableList; + /** * Represents the planar dice for Planechase games. * @@ -59,4 +61,6 @@ public enum PlanarDice { throw new RuntimeException("Element " + value + " not found in PlanarDice enum"); } + + public static final ImmutableList values = ImmutableList.copyOf(values()); } diff --git a/forge-game/src/main/java/forge/game/ability/effects/CountersRemoveEffect.java b/forge-game/src/main/java/forge/game/ability/effects/CountersRemoveEffect.java index a4c1f1427ae..2a95051a6eb 100644 --- a/forge-game/src/main/java/forge/game/ability/effects/CountersRemoveEffect.java +++ b/forge-game/src/main/java/forge/game/ability/effects/CountersRemoveEffect.java @@ -12,8 +12,11 @@ import forge.game.zone.Zone; import forge.game.zone.ZoneType; import java.util.Map; + import org.apache.commons.lang3.tuple.Pair; +import com.google.common.collect.ImmutableList; + public class CountersRemoveEffect extends SpellAbilityEffect { @Override protected String getStackDescription(SpellAbility sa) { @@ -93,7 +96,7 @@ public class CountersRemoveEffect extends SpellAbilityEffect { while (cntToRemove > 0 && tgtCard.hasCounters()) { final Map tgtCounters = tgtCard.getCounters(); - CounterType chosenType = pc.chooseCounterType(tgtCounters.keySet(), sa, "Select type of counters to remove"); + CounterType chosenType = pc.chooseCounterType(ImmutableList.copyOf(tgtCounters.keySet()), sa, "Select type of counters to remove"); String prompt = "Select the number of " + chosenType.getName() + " counters to remove"; int chosenAmount = pc.chooseNumber(sa, prompt, 1, Math.min(cntToRemove, tgtCounters.get(chosenType))); diff --git a/forge-game/src/main/java/forge/game/card/CounterType.java b/forge-game/src/main/java/forge/game/card/CounterType.java index d7b8ae77ffb..00e8bfc45ee 100644 --- a/forge-game/src/main/java/forge/game/card/CounterType.java +++ b/forge-game/src/main/java/forge/game/card/CounterType.java @@ -18,6 +18,8 @@ package forge.game.card; +import com.google.common.collect.ImmutableList; + /** * The class Counters. * @@ -311,4 +313,6 @@ public enum CounterType { final String replacedName = name.replace("/", "").replaceAll("\\+", "p").replaceAll("\\-", "m").toUpperCase(); return Enum.valueOf(CounterType.class, replacedName); } + + public static final ImmutableList values = ImmutableList.copyOf(values()); } diff --git a/forge-game/src/main/java/forge/game/player/DelayedReveal.java b/forge-game/src/main/java/forge/game/player/DelayedReveal.java index c5008d48eca..6d6850ef76b 100644 --- a/forge-game/src/main/java/forge/game/player/DelayedReveal.java +++ b/forge-game/src/main/java/forge/game/player/DelayedReveal.java @@ -1,11 +1,11 @@ package forge.game.player; import java.io.Serializable; -import java.util.Collection; import forge.game.card.Card; import forge.game.card.CardView; import forge.game.zone.ZoneType; +import forge.trackable.TrackableCollection; /** * Stores information to reveal cards after a delay unless those cards can be @@ -14,22 +14,22 @@ import forge.game.zone.ZoneType; public class DelayedReveal implements Serializable { private static final long serialVersionUID = 5516713460440436615L; - private final Collection cards; + private final TrackableCollection cards; private final ZoneType zone; private final PlayerView owner; private final String messagePrefix; - public DelayedReveal(Iterable cards0, ZoneType zone0, PlayerView owner0) { + public DelayedReveal(final Iterable cards0, final ZoneType zone0, final PlayerView owner0) { this(cards0, zone0, owner0, null); } - public DelayedReveal(Iterable cards0, ZoneType zone0, PlayerView owner0, String messagePrefix0) { + public DelayedReveal(final Iterable cards0, final ZoneType zone0, final PlayerView owner0, final String messagePrefix0) { cards = CardView.getCollection(cards0); zone = zone0; owner = owner0; messagePrefix = messagePrefix0; } - public Collection getCards() { + public TrackableCollection getCards() { return cards; } @@ -45,7 +45,7 @@ public class DelayedReveal implements Serializable { return messagePrefix; } - public void remove(CardView card) { + public void remove(final CardView card) { cards.remove(card); } diff --git a/forge-game/src/main/java/forge/game/player/PlayerController.java b/forge-game/src/main/java/forge/game/player/PlayerController.java index b76a8547c0f..531ec44478c 100644 --- a/forge-game/src/main/java/forge/game/player/PlayerController.java +++ b/forge-game/src/main/java/forge/game/player/PlayerController.java @@ -141,7 +141,7 @@ public abstract class PlayerController { reveal(cards, zone, owner, null); } public abstract void reveal(CardCollectionView cards, ZoneType zone, Player owner, String messagePrefix); - public abstract void reveal(Collection cards, ZoneType zone, PlayerView owner, String messagePrefix); + public abstract void reveal(List cards, ZoneType zone, PlayerView owner, String messagePrefix); /** Shows message to player to reveal chosen cardName, creatureType, number etc. AI must analyze API to understand what that is */ public abstract void notifyOfValue(SpellAbility saSource, GameObject realtedTarget, String value); @@ -192,7 +192,7 @@ public abstract class PlayerController { public abstract PaperCard chooseSinglePaperCard(SpellAbility sa, String message, Predicate cpp, String name); public abstract List chooseColors(String message, SpellAbility sa, int min, int max, List options); - public abstract CounterType chooseCounterType(Collection options, SpellAbility sa, String prompt); + public abstract CounterType chooseCounterType(List options, SpellAbility sa, String prompt); public abstract boolean confirmPayment(CostPart costPart, String string); public abstract ReplacementEffect chooseSingleReplacementEffect(String prompt, List possibleReplacers, HashMap runParams); diff --git a/forge-gui-desktop/src/main/java/forge/GuiDesktop.java b/forge-gui-desktop/src/main/java/forge/GuiDesktop.java index 34667798e02..0ffd221f107 100644 --- a/forge-gui-desktop/src/main/java/forge/GuiDesktop.java +++ b/forge-gui-desktop/src/main/java/forge/GuiDesktop.java @@ -132,12 +132,12 @@ public class GuiDesktop implements IGuiBase { } @Override - public int showOptionDialog(final String message, final String title, final FSkinProp icon, final String[] options, final int defaultOption) { + public int showOptionDialog(final String message, final String title, final FSkinProp icon, final List options, final int defaultOption) { return FOptionPane.showOptionDialog(message, title, icon == null ? null : FSkin.getImage(icon), options, defaultOption); } @Override - public String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final String[] inputOptions) { + public String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final List inputOptions) { return FOptionPane.showInputDialog(message, title, icon == null ? null : FSkin.getImage(icon), initialInput, inputOptions); } diff --git a/forge-gui-desktop/src/main/java/forge/control/FControl.java b/forge-gui-desktop/src/main/java/forge/control/FControl.java index 0a192f44c20..f5240fdbe51 100644 --- a/forge-gui-desktop/src/main/java/forge/control/FControl.java +++ b/forge-gui-desktop/src/main/java/forge/control/FControl.java @@ -126,7 +126,7 @@ public enum FControl implements KeyEventDispatcher { public void windowClosing(final WindowEvent e) { switch (closeAction) { case NONE: //prompt user for close action if not previously specified - final String[] options = {"Close Screen", "Exit Forge", "Cancel"}; + final List options = ImmutableList.of("Close Screen", "Exit Forge", "Cancel"); final int reply = FOptionPane.showOptionDialog( "Forge now supports navigation tabs which allow closing and switching between different screens with ease. " + "As a result, you no longer need to use the X button in the upper right to close the current screen and go back." diff --git a/forge-gui-desktop/src/main/java/forge/deckchooser/FDeckChooser.java b/forge-gui-desktop/src/main/java/forge/deckchooser/FDeckChooser.java index 8c48cedef5d..fb39c6ac7c3 100644 --- a/forge-gui-desktop/src/main/java/forge/deckchooser/FDeckChooser.java +++ b/forge-gui-desktop/src/main/java/forge/deckchooser/FDeckChooser.java @@ -1,5 +1,19 @@ package forge.deckchooser; +import java.awt.Dimension; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.swing.JOptionPane; +import javax.swing.JPanel; + +import net.miginfocom.swing.MigLayout; + +import org.apache.commons.lang3.StringUtils; + +import com.google.common.collect.ImmutableList; + import forge.FThreads; import forge.UiCommand; import forge.deck.ColorDeckGenerator; @@ -24,16 +38,6 @@ import forge.quest.QuestUtil; import forge.screens.match.controllers.CDetailPicture; import forge.toolbox.FLabel; import forge.toolbox.FOptionPane; -import net.miginfocom.swing.MigLayout; - -import org.apache.commons.lang3.StringUtils; - -import javax.swing.*; - -import java.awt.Dimension; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; @SuppressWarnings("serial") public class FDeckChooser extends JPanel implements IDecksComboBoxListener { @@ -58,9 +62,9 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener { final FDeckChooser chooser = new FDeckChooser(cDetailPicture, forAi); chooser.initialize(defaultDeckType); chooser.populate(); - Dimension parentSize = JOptionPane.getRootFrame().getSize(); + final Dimension parentSize = JOptionPane.getRootFrame().getSize(); chooser.setMinimumSize(new Dimension((int)(parentSize.getWidth() / 2), (int)parentSize.getHeight() - 200)); - final FOptionPane optionPane = new FOptionPane(null, title, null, chooser, new String[] { "OK", "Cancel" }, 0); + final FOptionPane optionPane = new FOptionPane(null, title, null, chooser, ImmutableList.of("OK", "Cancel"), 0); optionPane.setDefaultFocus(chooser); chooser.lstDecks.setItemActivateCommand(new UiCommand() { @Override @@ -69,7 +73,7 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener { } }); optionPane.setVisible(true); - int dialogResult = optionPane.getResult(); + final int dialogResult = optionPane.getResult(); optionPane.dispose(); if (dialogResult == 0) { return chooser.getDeck(); @@ -81,9 +85,8 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener { lstDecks = new DeckManager(GameType.Constructed, cDetailPicture); setOpaque(false); isAi = forAi; - UiCommand cmdViewDeck = new UiCommand() { - @Override - public void run() { + final UiCommand cmdViewDeck = new UiCommand() { + @Override public void run() { if (selectedDeckType != DeckType.COLOR_DECK && selectedDeckType != DeckType.THEME_DECK) { FDeckViewer.show(getDeck()); } @@ -96,16 +99,16 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener { public void initialize() { initialize(DeckType.COLOR_DECK); } - public void initialize(DeckType defaultDeckType) { + public void initialize(final DeckType defaultDeckType) { initialize(null, defaultDeckType); } - public void initialize(FPref savedStateSetting, DeckType defaultDeckType) { + public void initialize(final FPref savedStateSetting, final DeckType defaultDeckType) { stateSetting = savedStateSetting; selectedDeckType = defaultDeckType; } public DeckType getSelectedDeckType() { return selectedDeckType; } - public void setSelectedDeckType(DeckType selectedDeckType0) { + public void setSelectedDeckType(final DeckType selectedDeckType0) { refreshDecksList(selectedDeckType0, false, null); } @@ -247,8 +250,8 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener { // Special branch for quest events if (selectedDeckType == DeckType.QUEST_OPPONENT_DECK) { - QuestEvent event = DeckgenUtil.getQuestEvent(lstDecks.getSelectedItem().getName()); - RegisteredPlayer result = new RegisteredPlayer(event.getEventDeck()); + final QuestEvent event = DeckgenUtil.getQuestEvent(lstDecks.getSelectedItem().getName()); + final RegisteredPlayer result = new RegisteredPlayer(event.getEventDeck()); if (event instanceof QuestEventChallenge) { result.setStartingLife(((QuestEventChallenge) event).getAiLife()); } @@ -283,7 +286,7 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener { return isAi; } - public void setIsAi(boolean isAiDeck) { + public void setIsAi(final boolean isAiDeck) { isAi = isAiDeck; } @@ -317,7 +320,7 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener { refreshDecksList(ev.getDeckType(), false, ev); } - private void refreshDecksList(DeckType deckType, boolean forceRefresh, DecksComboBoxEvent ev) { + private void refreshDecksList(final DeckType deckType, final boolean forceRefresh, final DecksComboBoxEvent ev) { if (decksComboBox == null) { return; } // Not yet populated if (selectedDeckType == deckType && !forceRefresh) { return; } selectedDeckType = deckType; @@ -330,29 +333,29 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener { lstDecks.setCaption(deckType.toString()); switch (deckType) { - case CUSTOM_DECK: - updateCustom(); - break; - case COLOR_DECK: - updateColors(); - break; - case THEME_DECK: - updateThemes(); - break; - case QUEST_OPPONENT_DECK: - updateQuestEvents(); - break; - case PRECONSTRUCTED_DECK: - updatePrecons(); - break; - case RANDOM_DECK: - updateRandom(); - break; - case NET_DECK: - updateNetDecks(); - break; - default: - break; //other deck types not currently supported here + case CUSTOM_DECK: + updateCustom(); + break; + case COLOR_DECK: + updateColors(); + break; + case THEME_DECK: + updateThemes(); + break; + case QUEST_OPPONENT_DECK: + updateQuestEvents(); + break; + case PRECONSTRUCTED_DECK: + updatePrecons(); + break; + case RANDOM_DECK: + updateRandom(); + break; + case NET_DECK: + updateNetDecks(); + break; + default: + break; //other deck types not currently supported here } } @@ -367,7 +370,7 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener { } private String getState() { - StringBuilder state = new StringBuilder(); + final StringBuilder state = new StringBuilder(); if (decksComboBox.getDeckType() == null || decksComboBox.getDeckType() == DeckType.NET_DECK) { //handle special case of net decks if (netDeckCategory == null) { return ""; } @@ -381,15 +384,14 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener { return state.toString(); } - private void joinSelectedDecks(StringBuilder state, String delimiter) { - Iterable selectedDecks = lstDecks.getSelectedItems(); + private void joinSelectedDecks(final StringBuilder state, final String delimiter) { + final Iterable selectedDecks = lstDecks.getSelectedItems(); boolean isFirst = true; if (selectedDecks != null) { - for (DeckProxy deck : selectedDecks) { + for (final DeckProxy deck : selectedDecks) { if (isFirst) { isFirst = false; - } - else { + } else { state.append(delimiter); } state.append(deck.toString()); @@ -398,14 +400,14 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener { } public void restoreSavedState() { - DeckType oldDeckType = selectedDeckType; + final DeckType oldDeckType = selectedDeckType; if (stateSetting == null) { //if can't restore saved state, just refresh deck list refreshDecksList(oldDeckType, true, null); return; } - String savedState = prefs.getPref(stateSetting); + final String savedState = prefs.getPref(stateSetting); refreshDecksList(getDeckTypeFromSavedState(savedState), true, null); if (!lstDecks.setSelectedStrings(getSelectedDecksFromSavedState(savedState))) { //if can't select old decks, just refresh deck list @@ -413,36 +415,32 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener { } } - private DeckType getDeckTypeFromSavedState(String savedState) { + private DeckType getDeckTypeFromSavedState(final String savedState) { try { if (StringUtils.isBlank(savedState)) { return selectedDeckType; - } - else { - String deckType = savedState.split(";")[0]; + } else { + final String deckType = savedState.split(";")[0]; if (deckType.startsWith(NetDeckCategory.PREFIX)) { netDeckCategory = NetDeckCategory.selectAndLoad(lstDecks.getGameType(), deckType.substring(NetDeckCategory.PREFIX.length())); return DeckType.NET_DECK; } return DeckType.valueOf(deckType); } - } - catch (IllegalArgumentException ex) { + } catch (final IllegalArgumentException ex) { System.err.println(ex.getMessage() + ". Using default : " + selectedDeckType); return selectedDeckType; } } - private List getSelectedDecksFromSavedState(String savedState) { + private List getSelectedDecksFromSavedState(final String savedState) { try { if (StringUtils.isBlank(savedState)) { return new ArrayList(); - } - else { + } else { return Arrays.asList(savedState.split(";")[1].split(SELECTED_DECK_DELIMITER)); } - } - catch (Exception ex) { + } catch (final Exception ex) { System.err.println(ex + " [savedState=" + savedState + "]"); return new ArrayList(); } diff --git a/forge-gui-desktop/src/main/java/forge/gui/GuiDialog.java b/forge-gui-desktop/src/main/java/forge/gui/GuiDialog.java index 8882746c89c..73e6b9955b8 100644 --- a/forge-gui-desktop/src/main/java/forge/gui/GuiDialog.java +++ b/forge-gui-desktop/src/main/java/forge/gui/GuiDialog.java @@ -1,11 +1,14 @@ package forge.gui; +import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; import org.apache.commons.lang3.StringUtils; +import com.google.common.collect.ImmutableList; + import forge.FThreads; import forge.game.card.CardView; import forge.screens.match.CMatchUI; @@ -16,9 +19,9 @@ import forge.toolbox.FOptionPane; * */ public class GuiDialog { - private static final String[] defaultConfirmOptions = { "Yes", "No" }; + private static final ImmutableList defaultConfirmOptions = ImmutableList.of("Yes", "No"); - public static boolean confirm(final CardView c, final String question, final boolean defaultIsYes, final String[] options, final CMatchUI matchUI) { + public static boolean confirm(final CardView c, final String question, final boolean defaultIsYes, final List options, final CMatchUI matchUI) { final Callable confirmTask = new Callable() { @Override public final Boolean call() { if (matchUI != null && c != null) { @@ -27,7 +30,7 @@ public class GuiDialog { final String title = c == null ? "Question" : c + " - Ability"; final String questionToUse = StringUtils.isBlank(question) ? "Activate card's ability?" : question; - final String[] opts = options == null ? defaultConfirmOptions : options; + final List opts = options == null ? defaultConfirmOptions : options; final int answer = FOptionPane.showOptionDialog(questionToUse, title, FOptionPane.QUESTION_ICON, opts, defaultIsYes ? 0 : 1); return Boolean.valueOf(answer == 0); }}; diff --git a/forge-gui-desktop/src/main/java/forge/gui/ImportDialog.java b/forge-gui-desktop/src/main/java/forge/gui/ImportDialog.java index cacd1027c25..b83d8bf7033 100644 --- a/forge-gui-desktop/src/main/java/forge/gui/ImportDialog.java +++ b/forge-gui-desktop/src/main/java/forge/gui/ImportDialog.java @@ -55,6 +55,8 @@ import net.miginfocom.swing.MigLayout; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; +import com.google.common.collect.ImmutableList; + import forge.UiCommand; import forge.assets.FSkinProp; import forge.error.BugReporter; @@ -91,6 +93,8 @@ public class ImportDialog { // volatile since it is checked from multiple threads private volatile boolean _cancel; + private static final ImmutableList fixOrContinue = ImmutableList.of("Whoops, let me fix that!", "Continue with the import, I know what I'm doing."); + @SuppressWarnings("serial") public ImportDialog(final String forcedSrcDir, final Runnable onDialogClose) { this.forcedSrcDir = forcedSrcDir; @@ -636,8 +640,7 @@ public class ImportDialog { sb.append("will come up again the next time you start Forge in order to migrate the remaining files
"); sb.append("unless you move or delete them manually."); - final String[] options = { "Whoops, let me fix that!", "Continue with the import, I know what I'm doing." }; - final int chosen = FOptionPane.showOptionDialog(sb.toString(), "Migration warning", FOptionPane.WARNING_ICON, options); + final int chosen = FOptionPane.showOptionDialog(sb.toString(), "Migration warning", FOptionPane.WARNING_ICON, fixOrContinue); if (chosen != 1) { // i.e. option 0 was chosen or the dialog was otherwise closed diff --git a/forge-gui-desktop/src/main/java/forge/gui/ListChooser.java b/forge-gui-desktop/src/main/java/forge/gui/ListChooser.java index 717ff648201..267e3e912d6 100644 --- a/forge-gui-desktop/src/main/java/forge/gui/ListChooser.java +++ b/forge-gui-desktop/src/main/java/forge/gui/ListChooser.java @@ -37,6 +37,7 @@ import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import com.google.common.base.Function; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import forge.FThreads; @@ -88,12 +89,11 @@ public class ListChooser { this.list = list.getClass().isInstance(List.class) ? (List)list : Lists.newArrayList(list); this.lstChoices = new FList(new ChooserListModel()); - String[] options; + final ImmutableList options; if (minChoices == 0) { - options = new String[] {"OK","Cancel"}; - } - else { - options = new String[] {"OK"}; + options = ImmutableList.of("OK","Cancel"); + } else { + options = ImmutableList.of("OK"); } if (maxChoices == 1 || minChoices == -1) { diff --git a/forge-gui-desktop/src/main/java/forge/screens/deckeditor/SEditorIO.java b/forge-gui-desktop/src/main/java/forge/screens/deckeditor/SEditorIO.java index a93a9403b27..090a68d1ac7 100644 --- a/forge-gui-desktop/src/main/java/forge/screens/deckeditor/SEditorIO.java +++ b/forge-gui-desktop/src/main/java/forge/screens/deckeditor/SEditorIO.java @@ -12,6 +12,8 @@ import forge.toolbox.FOptionPane; import org.apache.commons.lang3.StringUtils; +import com.google.common.collect.ImmutableList; + /** * Handles editor preferences saving and loading. * @@ -69,6 +71,7 @@ public class SEditorIO { return true; } + private final static ImmutableList confirmSaveOptions = ImmutableList.of("Save", "Don't Save", "Cancel"); /** * Prompts to save changes if necessary. * @@ -82,7 +85,7 @@ public class SEditorIO { } final int choice = FOptionPane.showOptionDialog("Save changes to current deck?", "Save Changes?", - FOptionPane.QUESTION_ICON, new String[] {"Save", "Don't Save", "Cancel"}); + FOptionPane.QUESTION_ICON, confirmSaveOptions); if (choice == -1 || choice == 2) { return false; } diff --git a/forge-gui-desktop/src/main/java/forge/screens/deckeditor/controllers/CEditorQuestDraftingProcess.java b/forge-gui-desktop/src/main/java/forge/screens/deckeditor/controllers/CEditorQuestDraftingProcess.java index 43ddf4fd4b3..7fb45352065 100644 --- a/forge-gui-desktop/src/main/java/forge/screens/deckeditor/controllers/CEditorQuestDraftingProcess.java +++ b/forge-gui-desktop/src/main/java/forge/screens/deckeditor/controllers/CEditorQuestDraftingProcess.java @@ -19,6 +19,8 @@ package forge.screens.deckeditor.controllers; import java.util.Map.Entry; +import com.google.common.collect.ImmutableList; + import forge.assets.FSkinProp; import forge.card.MagicColor; import forge.deck.Deck; @@ -71,6 +73,8 @@ public class CEditorQuestDraftingProcess extends ACEditorBase leaveOrCancel = ImmutableList.of("Leave", "Cancel"); + //========== Constructor /** @@ -290,7 +294,7 @@ public class CEditorQuestDraftingProcess extends ACEditorBase genderOptions = ImmutableList.of("Male", "Female", "Any"), + typeOptions = ImmutableList.of("Fantasy", "Generic", "Any"); final String getNewName() { final String title = "Get new random name"; final String message = "What type of name do you want to generate?"; final SkinImage icon = FOptionPane.QUESTION_ICON; - final String[] genderOptions = new String[]{ "Male", "Female", "Any" }; - final String[] typeOptions = new String[]{ "Fantasy", "Generic", "Any" }; final int genderIndex = FOptionPane.showOptionDialog(message, title, icon, genderOptions, 2); if (genderIndex < 0) { @@ -723,8 +724,8 @@ public class VLobby implements IUpdateable { return null; } - final String gender = genderOptions[genderIndex]; - final String type = typeOptions[typeIndex]; + final String gender = genderOptions.get(genderIndex); + final String type = typeOptions.get(typeIndex); String confirmMsg, newName; final List usedNames = getPlayerNames(); diff --git a/forge-gui-desktop/src/main/java/forge/screens/home/quest/CSubmenuQuestDraft.java b/forge-gui-desktop/src/main/java/forge/screens/home/quest/CSubmenuQuestDraft.java index 82693129416..aa531afccce 100644 --- a/forge-gui-desktop/src/main/java/forge/screens/home/quest/CSubmenuQuestDraft.java +++ b/forge-gui-desktop/src/main/java/forge/screens/home/quest/CSubmenuQuestDraft.java @@ -13,6 +13,8 @@ import java.util.List; import javax.swing.JRadioButton; import javax.swing.SwingUtilities; +import com.google.common.collect.ImmutableList; + import forge.GuiBase; import forge.Singletons; import forge.UiCommand; @@ -139,7 +141,7 @@ public enum CSubmenuQuestDraft implements ICDoc { final boolean shouldQuit = FOptionPane.showOptionDialog("If you leave now, this tournament will be forever gone." + "\nYou will keep the cards you drafted, but will receive no other prizes." - + "\n\nWould you still like to quit the tournament?", "Really Quit?", FSkin.getImage(FSkinProp.ICO_WARNING).scale(2.0), new String[] { "Yes", "No" }, 1) == 0; + + "\n\nWould you still like to quit the tournament?", "Really Quit?", FSkin.getImage(FSkinProp.ICO_WARNING).scale(2.0), ImmutableList.of("Yes", "No"), 1) == 0; if (!shouldQuit) { return; } @@ -149,7 +151,7 @@ public enum CSubmenuQuestDraft implements ICDoc { if (draft.playerHasMatchesLeft()) { final boolean shouldQuit = FOptionPane.showOptionDialog("You have matches left to play!\nLeaving the tournament early will forfeit your potential future winnings." + "\nYou will still receive winnings as if you conceded your next match and you will keep the cards you drafted." - + "\n\nWould you still like to quit the tournament?", "Really Quit?", FSkin.getImage(FSkinProp.ICO_WARNING).scale(2.0), new String[] { "Yes", "No" }, 1) == 0; + + "\n\nWould you still like to quit the tournament?", "Really Quit?", FSkin.getImage(FSkinProp.ICO_WARNING).scale(2.0), ImmutableList.of("Yes", "No"), 1) == 0; if (!shouldQuit) { return; } @@ -242,7 +244,7 @@ public enum CSubmenuQuestDraft implements ICDoc { } - final boolean saveDraft = FOptionPane.showOptionDialog("Would you like to save this draft to the regular draft mode?", "Save Draft?", FSkin.getImage(FSkinProp.ICO_QUESTION).scale(2.0), new String[] { "Yes", "No" }, 0) == 0; + final boolean saveDraft = FOptionPane.showOptionDialog("Would you like to save this draft to the regular draft mode?", "Save Draft?", FSkin.getImage(FSkinProp.ICO_QUESTION).scale(2.0), ImmutableList.of("Yes", "No"), 0) == 0; if (saveDraft) { draft.saveToRegularDraft(); @@ -525,7 +527,7 @@ public enum CSubmenuQuestDraft implements ICDoc { return; } - final boolean okayToEnter = FOptionPane.showOptionDialog("This tournament costs " + draftEvent.getEntryFee() + " credits to enter.\nAre you sure you wish to enter?", "Enter Draft Tournament?", FSkin.getImage(FSkinProp.ICO_QUEST_GOLD), new String[] { "Yes", "No" }, 1) == 0; + final boolean okayToEnter = FOptionPane.showOptionDialog("This tournament costs " + draftEvent.getEntryFee() + " credits to enter.\nAre you sure you wish to enter?", "Enter Draft Tournament?", FSkin.getImage(FSkinProp.ICO_QUEST_GOLD), ImmutableList.of("Yes", "No"), 1) == 0; if (!okayToEnter) { return; diff --git a/forge-gui-desktop/src/main/java/forge/screens/match/CMatchUI.java b/forge-gui-desktop/src/main/java/forge/screens/match/CMatchUI.java index 23d50996fea..393da79f49e 100644 --- a/forge-gui-desktop/src/main/java/forge/screens/match/CMatchUI.java +++ b/forge-gui-desktop/src/main/java/forge/screens/match/CMatchUI.java @@ -326,8 +326,11 @@ public final class CMatchUI } public void setCard(final CardView c, final boolean isInAltState) { - FThreads.assertExecutedByEdt(true); - cDetailPicture.showCard(c, isInAltState); + FThreads.invokeInEdtNowOrLater(new Runnable() { + @Override public void run() { + cDetailPicture.showCard(c, isInAltState); + } + }); } public void setCard(final InventoryItem item) { @@ -819,17 +822,17 @@ public final class CMatchUI } @Override - public int showOptionDialog(final String message, final String title, final FSkinProp icon, final String[] options, final int defaultOption) { + public int showOptionDialog(final String message, final String title, final FSkinProp icon, final List options, final int defaultOption) { return FOptionPane.showOptionDialog(message, title, icon == null ? null : FSkin.getImage(icon), options, defaultOption); } @Override - public String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final String[] inputOptions) { + public String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final List inputOptions) { return FOptionPane.showInputDialog(message, title, icon == null ? null : FSkin.getImage(icon), initialInput, inputOptions); } @Override - public List getChoices(final String message, final int min, final int max, final Collection choices, final T selected, final Function display) { + public List getChoices(final String message, final int min, final int max, final List choices, final T selected, final Function display) { /*if ((choices != null && !choices.isEmpty() && choices.iterator().next() instanceof GameObject) || selected instanceof GameObject) { System.err.println("Warning: GameObject passed to GUI! Printing stack trace."); Thread.dumpStack(); @@ -854,7 +857,7 @@ public final class CMatchUI } @Override - public GameEntityView chooseSingleEntityForEffect(final String title, final Collection optionList, final DelayedReveal delayedReveal, final boolean isOptional) { + public GameEntityView chooseSingleEntityForEffect(final String title, final List optionList, final DelayedReveal delayedReveal, final boolean isOptional) { if (delayedReveal != null) { reveal(delayedReveal.getMessagePrefix(), delayedReveal.getCards()); //TODO: Merge this into search dialog } @@ -984,13 +987,13 @@ public final class CMatchUI } @Override - public boolean confirm(final CardView c, final String question, final boolean defaultIsYes, final String[] options) { + public boolean confirm(final CardView c, final String question, final boolean defaultIsYes, final List options) { return GuiDialog.confirm(c, question, defaultIsYes, options, this); } @Override public boolean showConfirmDialog(final String message, final String title, final String yesButtonText, final String noButtonText, final boolean defaultYes) { - final String[] options = {yesButtonText, noButtonText}; + final List options = ImmutableList.of(yesButtonText, noButtonText); final int reply = SOptionPane.showOptionDialog(message, title, SOptionPane.QUESTION_ICON, options, defaultYes ? 0 : 1); return reply == 0; } diff --git a/forge-gui-desktop/src/main/java/forge/screens/match/QuestDraftWinLose.java b/forge-gui-desktop/src/main/java/forge/screens/match/QuestDraftWinLose.java index 793b056f4ca..64571fcf8c6 100644 --- a/forge-gui-desktop/src/main/java/forge/screens/match/QuestDraftWinLose.java +++ b/forge-gui-desktop/src/main/java/forge/screens/match/QuestDraftWinLose.java @@ -19,6 +19,8 @@ package forge.screens.match; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import com.google.common.collect.ImmutableList; + import forge.assets.FSkinProp; import forge.game.GameView; import forge.match.NextGameDecision; @@ -104,7 +106,7 @@ public class QuestDraftWinLose extends ControlWinLose { @Override public void actionPerformed(final ActionEvent e) { if (warningString == null || - FOptionPane.showOptionDialog(warningString, warningCaption, FSkin.getImage(FSkinProp.ICO_WARNING).scale(2), new String[] { "Yes", "No" }, 1) == 0) { + FOptionPane.showOptionDialog(warningString, warningCaption, FSkin.getImage(FSkinProp.ICO_WARNING).scale(2), ImmutableList.of("Yes", "No"), 1) == 0) { matchUI.getGameController().nextGameDecision(NextGameDecision.QUIT); QuestDraftUtils.matchInProgress = false; QuestDraftUtils.continueMatches(matchUI); diff --git a/forge-gui-desktop/src/main/java/forge/screens/workshop/controllers/CCardScript.java b/forge-gui-desktop/src/main/java/forge/screens/workshop/controllers/CCardScript.java index 9e0fa9d2c50..2ec4f296da7 100644 --- a/forge-gui-desktop/src/main/java/forge/screens/workshop/controllers/CCardScript.java +++ b/forge-gui-desktop/src/main/java/forge/screens/workshop/controllers/CCardScript.java @@ -11,6 +11,8 @@ import javax.swing.event.DocumentListener; import javax.swing.text.Style; import javax.swing.text.StyledDocument; +import com.google.common.collect.ImmutableList; + import forge.Singletons; import forge.card.CardDb; import forge.card.CardRules; @@ -117,6 +119,7 @@ public enum CCardScript implements ICDoc { return (currentScriptInfo != null && isTextDirty); } + private static final ImmutableList switchAwayOptions = ImmutableList.of("Save", "Don't Save", "Cancel"); public boolean canSwitchAway(final boolean isCardChanging) { if (switchInProgress) { return false; } if (!hasChanges()) { return true; } @@ -124,10 +127,10 @@ public enum CCardScript implements ICDoc { switchInProgress = true; Singletons.getControl().ensureScreenActive(FScreen.WORKSHOP_SCREEN); //ensure Workshop is active before showing dialog final int choice = FOptionPane.showOptionDialog( - "Save changes to " + currentCard + "?", + String.format("Save changes to %s?", currentCard), "Save Changes?", FOptionPane.QUESTION_ICON, - new String[] {"Save", "Don't Save", "Cancel"}); + switchAwayOptions); switchInProgress = false; if (choice == -1 || choice == 2) { return false; } diff --git a/forge-gui-desktop/src/main/java/forge/toolbox/FComboBox.java b/forge-gui-desktop/src/main/java/forge/toolbox/FComboBox.java index 691419f3604..dec663ea096 100644 --- a/forge-gui-desktop/src/main/java/forge/toolbox/FComboBox.java +++ b/forge-gui-desktop/src/main/java/forge/toolbox/FComboBox.java @@ -5,6 +5,7 @@ import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.LayoutManager; +import java.util.List; import java.util.Vector; import javax.swing.ComboBoxModel; @@ -51,6 +52,9 @@ public class FComboBox extends SkinnedComboBox implements IComboBox { super(items); initialize(); } + public FComboBox(final List items) { + this(new Vector<>(items)); + } public FComboBox(final Vector items) { super(items); initialize(); diff --git a/forge-gui-desktop/src/main/java/forge/toolbox/FOptionPane.java b/forge-gui-desktop/src/main/java/forge/toolbox/FOptionPane.java index 367acc292a1..46bf42477c5 100644 --- a/forge-gui-desktop/src/main/java/forge/toolbox/FOptionPane.java +++ b/forge-gui-desktop/src/main/java/forge/toolbox/FOptionPane.java @@ -7,6 +7,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; +import java.util.List; import javax.swing.JComponent; import javax.swing.JOptionPane; @@ -14,6 +15,8 @@ import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.text.StyleConstants; +import com.google.common.collect.ImmutableList; + import forge.assets.FSkinProp; import forge.toolbox.FSkin.SkinImage; import forge.view.FDialog; @@ -46,7 +49,7 @@ public class FOptionPane extends FDialog { } public static void showMessageDialog(final String message, final String title, final SkinImage icon) { - showOptionDialog(message, title, icon, new String[] {"OK"}, 0); + showOptionDialog(message, title, icon, ImmutableList.of("OK"), 0); } public static boolean showConfirmDialog(final String message) { @@ -66,16 +69,16 @@ public class FOptionPane extends FDialog { } public static boolean showConfirmDialog(final String message, final String title, final String yesButtonText, final String noButtonText, final boolean defaultYes) { - final String[] options = {yesButtonText, noButtonText}; + final List options = ImmutableList.of(yesButtonText, noButtonText); final int reply = FOptionPane.showOptionDialog(message, title, QUESTION_ICON, options, defaultYes ? 0 : 1); return (reply == 0); } - public static int showOptionDialog(final String message, final String title, final SkinImage icon, final String[] options) { + public static int showOptionDialog(final String message, final String title, final SkinImage icon, final List options) { return showOptionDialog(message, title, icon, options, 0); } - public static int showOptionDialog(final String message, final String title, final SkinImage icon, final String[] options, final int defaultOption) { + public static int showOptionDialog(final String message, final String title, final SkinImage icon, final List options, final int defaultOption) { final FOptionPane optionPane = new FOptionPane(message, title, icon, null, options, defaultOption); optionPane.setVisible(true); final int dialogResult = optionPane.result; @@ -96,21 +99,20 @@ public class FOptionPane extends FDialog { } @SuppressWarnings("unchecked") - public static T showInputDialog(final String message, final String title, final SkinImage icon, final T initialInput, final T[] inputOptions) { + public static T showInputDialog(final String message, final String title, final SkinImage icon, final String initialInput, final List inputOptions) { final JComponent inputField; FTextField txtInput = null; FComboBox cbInput = null; if (inputOptions == null) { txtInput = new FTextField.Builder().text(initialInput.toString()).build(); inputField = txtInput; - } - else { + } else { cbInput = new FComboBox(inputOptions); cbInput.setSelectedItem(initialInput); inputField = cbInput; } - final FOptionPane optionPane = new FOptionPane(message, title, icon, inputField, new String[] {"OK", "Cancel"}, -1); + final FOptionPane optionPane = new FOptionPane(message, title, icon, inputField, ImmutableList.of("OK", "Cancel"), -1); optionPane.setDefaultFocus(inputField); inputField.addKeyListener(new KeyAdapter() { //hook so pressing Enter on field accepts dialog @Override @@ -136,7 +138,7 @@ public class FOptionPane extends FDialog { private int result = -1; //default result to -1, indicating dialog closed without choosing option private final FButton[] buttons; - public FOptionPane(final String message, final String title, final SkinImage icon, final Component comp, final String[] options, final int defaultOption) { + public FOptionPane(final String message, final String title, final SkinImage icon, final Component comp, final List options, final int defaultOption) { this.setTitle(title); final int padding = 10; @@ -184,18 +186,19 @@ public class FOptionPane extends FDialog { } //determine size of buttons - final int optionCount = options.length; + final int optionCount = options.size(); final FButton btnMeasure = new FButton(); //use blank button to aid in measurement final FontMetrics metrics = JOptionPane.getRootFrame().getGraphics().getFontMetrics(btnMeasure.getFont()); int maxTextWidth = 0; buttons = new FButton[optionCount]; for (int i = 0; i < optionCount; i++) { - final int textWidth = metrics.stringWidth(options[i]); + final String option = options.get(i); + final int textWidth = metrics.stringWidth(option); if (textWidth > maxTextWidth) { maxTextWidth = textWidth; } - buttons[i] = new FButton(options[i]); + buttons[i] = new FButton(option); } this.pack(); //resize dialog to fit component and title to help determine button layout diff --git a/forge-gui-desktop/src/test/java/forge/gamesimulationtests/util/PlayerControllerForTests.java b/forge-gui-desktop/src/test/java/forge/gamesimulationtests/util/PlayerControllerForTests.java index 53753a20aab..3d4319a34c2 100644 --- a/forge-gui-desktop/src/test/java/forge/gamesimulationtests/util/PlayerControllerForTests.java +++ b/forge-gui-desktop/src/test/java/forge/gamesimulationtests/util/PlayerControllerForTests.java @@ -238,7 +238,7 @@ public class PlayerControllerForTests extends PlayerController { } @Override - public void reveal(Collection cards, ZoneType zone, PlayerView owner, String messagePrefix) { + public void reveal(List cards, ZoneType zone, PlayerView owner, String messagePrefix) { //nothing needs to be done here } @@ -499,7 +499,7 @@ public class PlayerControllerForTests extends PlayerController { } @Override - public CounterType chooseCounterType(Collection options, SpellAbility sa, String prompt) { + public CounterType chooseCounterType(List options, SpellAbility sa, String prompt) { return Iterables.getFirst(options, CounterType.P1P1); } diff --git a/forge-gui-mobile/src/forge/GuiMobile.java b/forge-gui-mobile/src/forge/GuiMobile.java index ed5ae32edd5..bbb00e77db0 100644 --- a/forge-gui-mobile/src/forge/GuiMobile.java +++ b/forge-gui-mobile/src/forge/GuiMobile.java @@ -141,7 +141,7 @@ public class GuiMobile implements IGuiBase { } @Override - public int showOptionDialog(final String message, final String title, final FSkinProp icon, final String[] options, final int defaultOption) { + public int showOptionDialog(final String message, final String title, final FSkinProp icon, final List options, final int defaultOption) { return new WaitCallback() { @Override public void run() { @@ -151,7 +151,7 @@ public class GuiMobile implements IGuiBase { } @Override - public String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final String[] inputOptions) { + public String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final List inputOptions) { return new WaitCallback() { @Override public void run() { diff --git a/forge-gui-mobile/src/forge/assets/AssetsDownloader.java b/forge-gui-mobile/src/forge/assets/AssetsDownloader.java index 16f33482a2a..9c692158239 100644 --- a/forge-gui-mobile/src/forge/assets/AssetsDownloader.java +++ b/forge-gui-mobile/src/forge/assets/AssetsDownloader.java @@ -3,11 +3,13 @@ package forge.assets; import java.io.File; import java.io.IOException; import java.net.URL; +import java.util.List; import org.apache.commons.lang3.StringUtils; import com.badlogic.gdx.Application.ApplicationType; import com.badlogic.gdx.Gdx; +import com.google.common.collect.ImmutableList; import forge.FThreads; import forge.Forge; @@ -20,6 +22,9 @@ import forge.util.gui.SOptionPane; public class AssetsDownloader { public static final boolean SHARE_DESKTOP_ASSETS = true; //change to false to test downloading separate assets for desktop version + private final static ImmutableList downloadIgnoreExit = ImmutableList.of("Download", "Ignore", "Exit"); + private final static ImmutableList downloadExit = ImmutableList.of("Download", "Exit"); + //if not sharing desktop assets, check whether assets are up to date public static void checkForUpdates(final SplashScreen splashScreen) { if (Gdx.app.getType() == ApplicationType.Desktop && SHARE_DESKTOP_ASSETS) { return; } @@ -105,16 +110,16 @@ public class AssetsDownloader { else { message += "so it's highly recommended that you connect to wifi first."; } - String[] options; + final List options; message += "\n\n"; if (canIgnoreDownload) { message += "If you choose to ignore this download, you may miss out on card fixes or experience other problems."; - options = new String[] { "Download", "Ignore", "Exit" }; - } - else { + options = downloadIgnoreExit; + } else { message += "This download is mandatory to start the app since you haven't previously downloaded these files."; - options = new String[] { "Download", "Exit" }; + options = downloadExit; } + switch (SOptionPane.showOptionDialog(message, "", null, options)) { case 1: if (!canIgnoreDownload) { diff --git a/forge-gui-mobile/src/forge/card/GameEntityPicker.java b/forge-gui-mobile/src/forge/card/GameEntityPicker.java index 02cd70e2a2d..41de7117376 100644 --- a/forge-gui-mobile/src/forge/card/GameEntityPicker.java +++ b/forge-gui-mobile/src/forge/card/GameEntityPicker.java @@ -4,6 +4,8 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import com.google.common.collect.ImmutableList; + import forge.Graphics; import forge.assets.FImage; import forge.assets.FSkinFont; @@ -30,7 +32,7 @@ public class GameEntityPicker extends TabPageScreen { setHeight(FOptionPane.getMaxDisplayObjHeight()); optionPane = new FOptionPane(null, title, null, this, - isOptional ? new String[] { "OK", "Cancel" } : new String[] { "OK" }, 0, new Callback() { + isOptional ? ImmutableList.of("OK", "Cancel") : ImmutableList.of("OK"), 0, new Callback() { @Override public void run(Integer result) { if (result == 0) { diff --git a/forge-gui-mobile/src/forge/deck/FDeckChooser.java b/forge-gui-mobile/src/forge/deck/FDeckChooser.java index 1b0e8244917..dd474184bb1 100644 --- a/forge-gui-mobile/src/forge/deck/FDeckChooser.java +++ b/forge-gui-mobile/src/forge/deck/FDeckChooser.java @@ -43,6 +43,7 @@ import forge.util.storage.IStorage; import org.apache.commons.lang3.StringUtils; import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment; +import com.google.common.collect.ImmutableList; import java.util.ArrayList; import java.util.Arrays; @@ -97,7 +98,7 @@ public class FDeckChooser extends FScreen { container.add(deckChooser.lstDecks); container.setHeight(FOptionPane.getMaxDisplayObjHeight()); - deckChooser.optionPane = new FOptionPane(null, title, null, container, new String[] { "OK", "Cancel" }, 0, new Callback() { + deckChooser.optionPane = new FOptionPane(null, title, null, container, ImmutableList.of("OK", "Cancel"), 0, new Callback() { @Override public void run(Integer result) { if (result == 0) { diff --git a/forge-gui-mobile/src/forge/deck/FDeckEditor.java b/forge-gui-mobile/src/forge/deck/FDeckEditor.java index a3417ef5422..17b67a67b15 100644 --- a/forge-gui-mobile/src/forge/deck/FDeckEditor.java +++ b/forge-gui-mobile/src/forge/deck/FDeckEditor.java @@ -12,6 +12,7 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment; import com.badlogic.gdx.math.Vector2; import com.google.common.base.Predicates; import com.google.common.base.Supplier; +import com.google.common.collect.ImmutableList; import forge.Forge; import forge.Graphics; @@ -479,6 +480,8 @@ public class FDeckEditor extends TabPageScreen { } } + private final static ImmutableList onCloseOptions = ImmutableList.of("Save", "Don't Save", "Cancel"); + @Override public void onClose(final Callback canCloseCallback) { if (editorType.getController().isSaved() || canCloseCallback == null) { @@ -486,7 +489,7 @@ public class FDeckEditor extends TabPageScreen { return; } FOptionPane.showOptionDialog("Save changes to current deck?", "", - FOptionPane.QUESTION_ICON, new String[] {"Save", "Don't Save", "Cancel"}, new Callback() { + FOptionPane.QUESTION_ICON, onCloseOptions, new Callback() { @Override public void run(Integer result) { if (result == 0) { diff --git a/forge-gui-mobile/src/forge/deck/FDeckImportDialog.java b/forge-gui-mobile/src/forge/deck/FDeckImportDialog.java index 13784df8d8c..2197e1dbd88 100644 --- a/forge-gui-mobile/src/forge/deck/FDeckImportDialog.java +++ b/forge-gui-mobile/src/forge/deck/FDeckImportDialog.java @@ -19,6 +19,8 @@ package forge.deck; import java.util.List; +import com.google.common.collect.ImmutableList; + import forge.FThreads; import forge.Forge; import forge.Graphics; @@ -48,6 +50,8 @@ public class FDeckImportDialog extends FDialog { private final boolean showOptions; private final DeckImportController controller; + private final static ImmutableList importOrCancel = ImmutableList.of("Import", "Cancel"); + public FDeckImportDialog(final boolean replacingDeck, final Callback callback0) { super("Import from Clipboard", 2); @@ -74,7 +78,7 @@ public class FDeckImportDialog extends FDialog { } } if (sb.length() > 0) { - if (SOptionPane.showOptionDialog("The following cards cannot be imported due to misspelling, set restrictions, or not being in Forge yet:\n\n" + sb.toString(), "Import remaining cards?", SOptionPane.INFORMATION_ICON, new String[] { "Import", "Cancel" }) == 1) { + if (SOptionPane.showOptionDialog("The following cards cannot be imported due to misspelling, set restrictions, or not being in Forge yet:\n\n" + sb.toString(), "Import remaining cards?", SOptionPane.INFORMATION_ICON, importOrCancel) == 1) { return; } } diff --git a/forge-gui-mobile/src/forge/screens/constructed/ConstructedScreen.java b/forge-gui-mobile/src/forge/screens/constructed/ConstructedScreen.java index 939308049c2..280a9831c24 100644 --- a/forge-gui-mobile/src/forge/screens/constructed/ConstructedScreen.java +++ b/forge-gui-mobile/src/forge/screens/constructed/ConstructedScreen.java @@ -5,6 +5,7 @@ import java.util.*; import org.apache.commons.lang3.StringUtils; import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment; +import com.google.common.collect.ImmutableList; import forge.FThreads; import forge.Forge; @@ -859,12 +860,12 @@ public class ConstructedScreen extends LaunchScreen { return usedAvatars; } + private static final ImmutableList genderOptions = ImmutableList.of("Male", "Female", "Any"); + private static final ImmutableList typeOptions = ImmutableList.of("Fantasy", "Generic", "Any"); private final void getNewName(final Callback callback) { final String title = "Get new random name"; final String message = "What type of name do you want to generate?"; final FSkinImage icon = FOptionPane.QUESTION_ICON; - final String[] genderOptions = new String[]{ "Male", "Female", "Any" }; - final String[] typeOptions = new String[]{ "Fantasy", "Generic", "Any" }; FOptionPane.showOptionDialog(message, title, icon, genderOptions, 2, new Callback() { @Override @@ -882,7 +883,7 @@ public class ConstructedScreen extends LaunchScreen { return; } - generateRandomName(genderOptions[genderIndex], typeOptions[typeIndex], getPlayerNames(), title, callback); + generateRandomName(genderOptions.get(genderIndex), typeOptions.get(typeIndex), getPlayerNames(), title, callback); } }); } diff --git a/forge-gui-mobile/src/forge/screens/match/MatchController.java b/forge-gui-mobile/src/forge/screens/match/MatchController.java index a60ca7115c1..bdfa55ed732 100644 --- a/forge-gui-mobile/src/forge/screens/match/MatchController.java +++ b/forge-gui-mobile/src/forge/screens/match/MatchController.java @@ -10,6 +10,7 @@ import java.util.Map.Entry; import org.apache.commons.lang3.StringUtils; import com.google.common.base.Function; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import forge.Forge; @@ -82,7 +83,7 @@ public class MatchController extends AbstractGuiGame { } public static FImage getPlayerAvatar(final PlayerView p) { - String lp = p.getLobbyPlayerName(); + final String lp = p.getLobbyPlayerName(); FImage avatar = avatarImages.get(lp); if (avatar == null) { if (StringUtils.isEmpty(p.getAvatarCardImageKey())) { @@ -96,9 +97,9 @@ public class MatchController extends AbstractGuiGame { } @Override - public void refreshCardDetails(Iterable cards) { + public void refreshCardDetails(final Iterable cards) { //ensure cards appear in the correct row of the field - for (VPlayerPanel pnl : view.getPlayerPanels().values()) { + for (final VPlayerPanel pnl : view.getPlayerPanels().values()) { pnl.getField().update(); } } @@ -123,12 +124,12 @@ public class MatchController extends AbstractGuiGame { //add special object that pauses game if screen touched view.add(new FDisplayObject() { @Override - public void draw(Graphics g) { + public void draw(final Graphics g) { //don't draw anything } @Override - public void buildTouchListeners(float screenX, float screenY, ArrayList listeners) { + public void buildTouchListeners(final float screenX, final float screenY, final ArrayList listeners) { if (screenY < view.getHeight() - VPrompt.HEIGHT) { hostedMatch.pause(); } @@ -146,6 +147,7 @@ public class MatchController extends AbstractGuiGame { view.getPrompt(player).setMessage(message); } + @Override public void updateButtons(final PlayerView owner, final String label1, final String label2, final boolean enable1, final boolean enable2, final boolean focus1) { final VPrompt prompt = view.getPrompt(owner); final FButton btn1 = prompt.getBtnOk(), btn2 = prompt.getBtnCancel(); @@ -194,7 +196,7 @@ public class MatchController extends AbstractGuiGame { @Override public void disableOverlay() { } - + @Override public void enableOverlay() { } @@ -233,15 +235,15 @@ public class MatchController extends AbstractGuiGame { @Override public Object showManaPool(final PlayerView player) { - VPlayerPanel playerPanel = view.getPlayerPanel(player); - InfoTab oldSelectedTab = playerPanel.getSelectedTab(); + final VPlayerPanel playerPanel = view.getPlayerPanel(player); + final InfoTab oldSelectedTab = playerPanel.getSelectedTab(); playerPanel.setSelectedTab(playerPanel.getManaPoolTab()); return oldSelectedTab; } @Override public void hideManaPool(final PlayerView player, final Object zoneToRestore) { - VPlayerPanel playerPanel = view.getPlayerPanel(player); + final VPlayerPanel playerPanel = view.getPlayerPanel(player); if (zoneToRestore == playerPanel.getManaPoolTab()) { return; //if mana pool was selected previously, we don't need to switch back to anything } @@ -252,38 +254,37 @@ public class MatchController extends AbstractGuiGame { } @Override - public boolean openZones(Collection zones, Map players) { + public boolean openZones(final Collection zones, final Map players) { if (zones.size() == 1) { - ZoneType zoneType = zones.iterator().next(); + final ZoneType zoneType = zones.iterator().next(); switch (zoneType) { - case Battlefield: - case Command: - players.clear(); //clear since no zones need to be restored - return true; //Battlefield is always open - default: - //open zone tab for given zone if needed - boolean result = true; - for (PlayerView player : players.keySet()) { - VPlayerPanel playerPanel = view.getPlayerPanel(player); - players.put(player, playerPanel.getSelectedTab()); //backup selected tab before changing it - InfoTab zoneTab = playerPanel.getZoneTab(zoneType); - if (zoneTab == null) { - result = false; + case Battlefield: + case Command: + players.clear(); //clear since no zones need to be restored + return true; //Battlefield is always open + default: + //open zone tab for given zone if needed + boolean result = true; + for (final PlayerView player : players.keySet()) { + final VPlayerPanel playerPanel = view.getPlayerPanel(player); + players.put(player, playerPanel.getSelectedTab()); //backup selected tab before changing it + final InfoTab zoneTab = playerPanel.getZoneTab(zoneType); + if (zoneTab == null) { + result = false; + } else { + playerPanel.setSelectedTab(zoneTab); + } } - else { - playerPanel.setSelectedTab(zoneTab); - } - } - return result; + return result; } } return false; } @Override - public void restoreOldZones(Map playersToRestoreZonesFor) { - for (Entry player : playersToRestoreZonesFor.entrySet()) { - VPlayerPanel playerPanel = view.getPlayerPanel(player.getKey()); + public void restoreOldZones(final Map playersToRestoreZonesFor) { + for (final Entry player : playersToRestoreZonesFor.entrySet()) { + final VPlayerPanel playerPanel = view.getPlayerPanel(player.getKey()); playerPanel.setSelectedTab((InfoTab)player.getValue()); } } @@ -293,33 +294,33 @@ public class MatchController extends AbstractGuiGame { return new WaitCallback>() { @Override public void run() { - VAssignDamage v = new VAssignDamage(attacker, blockers, damage, defender, overrideOrder, this); + final VAssignDamage v = new VAssignDamage(attacker, blockers, damage, defender, overrideOrder, this); v.show(); } }.invokeAndWait(); } @Override - public void updateManaPool(Iterable manaPoolUpdate) { - for (PlayerView p : manaPoolUpdate) { + public void updateManaPool(final Iterable manaPoolUpdate) { + for (final PlayerView p : manaPoolUpdate) { view.getPlayerPanel(p).updateManaPool(); } } @Override - public void updateLives(Iterable livesUpdate) { - for (PlayerView p : livesUpdate) { + public void updateLives(final Iterable livesUpdate) { + for (final PlayerView p : livesUpdate) { view.getPlayerPanel(p).updateLife(); } } @Override - public void updateZones(Iterable zonesToUpdate) { + public void updateZones(final Iterable zonesToUpdate) { view.updateZones(zonesToUpdate); } @Override - public void updateCards(Iterable cards) { + public void updateCards(final Iterable cards) { for (final CardView card : cards) { view.updateSingleCard(card); } @@ -332,9 +333,9 @@ public class MatchController extends AbstractGuiGame { } private static void actuateMatchPreferences() { - ForgePreferences prefs = FModel.getPreferences(); + final ForgePreferences prefs = FModel.getPreferences(); - VPhaseIndicator fvAi = view.getTopPlayerPanel().getPhaseIndicator(); + final VPhaseIndicator fvAi = view.getTopPlayerPanel().getPhaseIndicator(); fvAi.getLabel(PhaseType.UPKEEP).setStopAtPhase(prefs.getPrefBoolean(FPref.PHASE_AI_UPKEEP)); fvAi.getLabel(PhaseType.DRAW).setStopAtPhase(prefs.getPrefBoolean(FPref.PHASE_AI_DRAW)); fvAi.getLabel(PhaseType.MAIN1).setStopAtPhase(prefs.getPrefBoolean(FPref.PHASE_AI_MAIN1)); @@ -348,7 +349,7 @@ public class MatchController extends AbstractGuiGame { fvAi.getLabel(PhaseType.END_OF_TURN).setStopAtPhase(prefs.getPrefBoolean(FPref.PHASE_AI_EOT)); fvAi.getLabel(PhaseType.CLEANUP).setStopAtPhase(prefs.getPrefBoolean(FPref.PHASE_AI_CLEANUP)); - VPhaseIndicator fvHuman = view.getBottomPlayerPanel().getPhaseIndicator(); + final VPhaseIndicator fvHuman = view.getBottomPlayerPanel().getPhaseIndicator(); fvHuman.getLabel(PhaseType.UPKEEP).setStopAtPhase(prefs.getPrefBoolean(FPref.PHASE_HUMAN_UPKEEP)); fvHuman.getLabel(PhaseType.DRAW).setStopAtPhase(prefs.getPrefBoolean(FPref.PHASE_HUMAN_DRAW)); fvHuman.getLabel(PhaseType.MAIN1).setStopAtPhase(prefs.getPrefBoolean(FPref.PHASE_HUMAN_MAIN1)); @@ -364,9 +365,9 @@ public class MatchController extends AbstractGuiGame { } public static void writeMatchPreferences() { - ForgePreferences prefs = FModel.getPreferences(); + final ForgePreferences prefs = FModel.getPreferences(); - VPhaseIndicator fvAi = view.getTopPlayerPanel().getPhaseIndicator(); + final VPhaseIndicator fvAi = view.getTopPlayerPanel().getPhaseIndicator(); prefs.setPref(FPref.PHASE_AI_UPKEEP, String.valueOf(fvAi.getLabel(PhaseType.UPKEEP).getStopAtPhase())); prefs.setPref(FPref.PHASE_AI_DRAW, String.valueOf(fvAi.getLabel(PhaseType.DRAW).getStopAtPhase())); prefs.setPref(FPref.PHASE_AI_MAIN1, String.valueOf(fvAi.getLabel(PhaseType.MAIN1).getStopAtPhase())); @@ -380,7 +381,7 @@ public class MatchController extends AbstractGuiGame { prefs.setPref(FPref.PHASE_AI_EOT, String.valueOf(fvAi.getLabel(PhaseType.END_OF_TURN).getStopAtPhase())); prefs.setPref(FPref.PHASE_AI_CLEANUP, String.valueOf(fvAi.getLabel(PhaseType.CLEANUP).getStopAtPhase())); - VPhaseIndicator fvHuman = view.getBottomPlayerPanel().getPhaseIndicator(); + final VPhaseIndicator fvHuman = view.getBottomPlayerPanel().getPhaseIndicator(); prefs.setPref(FPref.PHASE_HUMAN_UPKEEP, String.valueOf(fvHuman.getLabel(PhaseType.UPKEEP).getStopAtPhase())); prefs.setPref(FPref.PHASE_HUMAN_DRAW, String.valueOf(fvHuman.getLabel(PhaseType.DRAW).getStopAtPhase())); prefs.setPref(FPref.PHASE_HUMAN_MAIN1, String.valueOf(fvHuman.getLabel(PhaseType.MAIN1).getStopAtPhase())); @@ -413,25 +414,28 @@ public class MatchController extends AbstractGuiGame { } @Override - public int showOptionDialog(final String message, final String title, final FSkinProp icon, final String[] options, final int defaultOption) { + public int showOptionDialog(final String message, final String title, final FSkinProp icon, final List options, final int defaultOption) { return SOptionPane.showOptionDialog(message, title, icon, options, defaultOption); } @Override - public String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final String[] inputOptions) { + public String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final List inputOptions) { return SOptionPane.showInputDialog(message, title, icon, initialInput, inputOptions); } @Override - public boolean confirm(final CardView c, final String question, final boolean defaultIsYes, String[] options) { + public boolean confirm(final CardView c, final String question, final boolean defaultIsYes, final List options) { + final List optionsToUse; if (options == null) { - options = new String[] { "Yes", "No" }; + optionsToUse = ImmutableList.of("Yes", "No"); + } else { + optionsToUse = options; } - return FOptionPane.showCardOptionDialog(c, question, "", SOptionPane.INFORMATION_ICON, options, defaultIsYes ? 0 : 1) == 0; + return FOptionPane.showCardOptionDialog(c, question, "", SOptionPane.INFORMATION_ICON, optionsToUse, defaultIsYes ? 0 : 1) == 0; } @Override - public List getChoices(final String message, final int min, final int max, final Collection choices, final T selected, final Function display) { + public List getChoices(final String message, final int min, final int max, final List choices, final T selected, final Function display) { return GuiBase.getInterface().getChoices(message, min, max, choices, selected, display); } @@ -445,14 +449,14 @@ public class MatchController extends AbstractGuiGame { return new WaitCallback>() { @Override public void run() { - FSideboardDialog sideboardDialog = new FSideboardDialog(sideboard, main, this); + final FSideboardDialog sideboardDialog = new FSideboardDialog(sideboard, main, this); sideboardDialog.show(); } }.invokeAndWait(); } @Override - public GameEntityView chooseSingleEntityForEffect(final String title, final Collection optionList, final DelayedReveal delayedReveal, final boolean isOptional) { + public GameEntityView chooseSingleEntityForEffect(final String title, final List optionList, final DelayedReveal delayedReveal, final boolean isOptional) { if (delayedReveal == null || Iterables.isEmpty(delayedReveal.getCards())) { if (isOptional) { return SGuiChoose.oneOrNone(title, optionList); @@ -464,11 +468,11 @@ public class MatchController extends AbstractGuiGame { final String revealListCaption = StringUtils.capitalize(MessageUtil.formatMessage("{player's} " + delayedReveal.getZone().name(), delayedReveal.getOwner(), delayedReveal.getOwner())); final FImage revealListImage = MatchController.getView().getPlayerPanels().values().iterator().next().getZoneTab(delayedReveal.getZone()).getIcon(); - //use special dialog for choosing card and offering ability to see all revealed cards at the same time + //use special dialog for choosing card and offering ability to see all revealed cards at the same time return new WaitCallback() { @Override public void run() { - GameEntityPicker picker = new GameEntityPicker(title, optionList, revealList, revealListCaption, revealListImage, isOptional, this); + final GameEntityPicker picker = new GameEntityPicker(title, optionList, revealList, revealListCaption, revealListImage, isOptional, this); picker.show(); } }.invokeAndWait(); diff --git a/forge-gui-mobile/src/forge/toolbox/FOptionPane.java b/forge-gui-mobile/src/forge/toolbox/FOptionPane.java index 9368ed10e9f..4610ba93b8b 100644 --- a/forge-gui-mobile/src/forge/toolbox/FOptionPane.java +++ b/forge-gui-mobile/src/forge/toolbox/FOptionPane.java @@ -1,10 +1,13 @@ package forge.toolbox; +import java.util.List; + import org.apache.commons.lang3.StringUtils; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment; import com.badlogic.gdx.math.Vector2; +import com.google.common.collect.ImmutableList; import forge.Forge; import forge.Graphics; @@ -52,11 +55,11 @@ public class FOptionPane extends FDialog { } public static void showMessageDialog(final String message, final String title, final FImage icon) { - showOptionDialog(message, title, icon, new String[] {"OK"}, 0, null); + showOptionDialog(message, title, icon, ImmutableList.of("OK"), 0, null); } public static void showMessageDialog(final String message, final String title, final FImage icon, final Callback callback) { - showOptionDialog(message, title, icon, new String[] {"OK"}, 0, callback); + showOptionDialog(message, title, icon, ImmutableList.of("OK"), 0, callback); } public static void showConfirmDialog(final String message, final Callback callback) { @@ -76,7 +79,7 @@ public class FOptionPane extends FDialog { } public static void showConfirmDialog(final String message, final String title, final String yesButtonText, final String noButtonText, final boolean defaultYes, final Callback callback) { - final String[] options = {yesButtonText, noButtonText}; + final List options = ImmutableList.of(yesButtonText, noButtonText); showOptionDialog(message, title, QUESTION_ICON, options, defaultYes ? 0 : 1, new Callback() { @Override public void run(final Integer result) { @@ -85,16 +88,16 @@ public class FOptionPane extends FDialog { }); } - public static void showOptionDialog(final String message, final String title, final FImage icon, final String[] options, final Callback callback) { + public static void showOptionDialog(final String message, final String title, final FImage icon, final List options, final Callback callback) { showOptionDialog(message, title, icon, options, 0, callback); } - public static void showOptionDialog(final String message, final String title, final FImage icon, final String[] options, final int defaultOption, final Callback callback) { + public static void showOptionDialog(final String message, final String title, final FImage icon, final List options, final int defaultOption, final Callback callback) { final FOptionPane optionPane = new FOptionPane(message, title, icon, null, options, defaultOption, callback); optionPane.show(); } - public static int showCardOptionDialog(final CardView card, final String message, final String title, final FSkinProp icon, final String[] options, final int defaultOption) { + public static int showCardOptionDialog(final CardView card, final String message, final String title, final FSkinProp icon, final List options, final int defaultOption) { return new WaitCallback() { @Override public void run() { @@ -103,7 +106,7 @@ public class FOptionPane extends FDialog { }.invokeAndWait(); } - public static void showCardOptionDialog(final CardView card, String message, String title, FImage icon, final String[] options, final int defaultOption, final Callback callback) { + public static void showCardOptionDialog(final CardView card, String message, String title, FImage icon, final List options, final int defaultOption, final Callback callback) { final FDisplayObject cardDisplay; if (card != null) { cardDisplay = new FDisplayObject() { @@ -150,7 +153,7 @@ public class FOptionPane extends FDialog { public static void showInputDialog(final String title, final T initialInput, final Callback callback) { showInputDialog(null, title, initialInput, null, callback); } - public static void showInputDialog(final String message, final String title, final T initialInput, final T[] inputOptions, final Callback callback) { + public static void showInputDialog(final String message, final String title, final T initialInput, final List inputOptions, final Callback callback) { final FDisplayObject inputField; final FTextField txtInput; final FComboBox cbInput; @@ -178,7 +181,7 @@ public class FOptionPane extends FDialog { container.add(inputField); container.setHeight(inputField.getHeight() + padTop + PADDING); - final FOptionPane optionPane = new FOptionPane(message, title, null, container, new String[] {"OK", "Cancel"}, 0, new Callback() { + final FOptionPane optionPane = new FOptionPane(message, title, null, container, ImmutableList.of("OK", "Cancel"), 0, new Callback() { @SuppressWarnings("unchecked") @Override public void run(final Integer result) { @@ -221,8 +224,8 @@ public class FOptionPane extends FDialog { private final int defaultOption; private final boolean centerIcon; - public FOptionPane(final String message, final String title, final FImage icon, final FDisplayObject displayObj0, final String[] options, final int defaultOption0, final Callback callback0) { - super(title, options.length); + public FOptionPane(final String message, final String title, final FImage icon, final FDisplayObject displayObj0, final List options, final int defaultOption0, final Callback callback0) { + super(title, options.size()); if (icon != null) { centerIcon = icon.getWidth() >= 100; //for large icon, center in dialog @@ -254,9 +257,10 @@ public class FOptionPane extends FDialog { callback = callback0; - for (int i = 0; i < options.length; i++) { + final int optionsSize = options.size(); + for (int i = 0; i < optionsSize; i++) { final int option = i; - initButton(i, options[i], new FEventHandler() { + initButton(i, options.get(i), new FEventHandler() { @Override public void handleEvent(final FEvent e) { setResult(option); diff --git a/forge-gui-mobile/src/forge/toolbox/GuiDialog.java b/forge-gui-mobile/src/forge/toolbox/GuiDialog.java index fdf1fb02134..ccd47516355 100644 --- a/forge-gui-mobile/src/forge/toolbox/GuiDialog.java +++ b/forge-gui-mobile/src/forge/toolbox/GuiDialog.java @@ -1,15 +1,19 @@ package forge.toolbox; +import java.util.List; + import forge.game.card.CardView; import forge.util.Callback; import org.apache.commons.lang3.StringUtils; +import com.google.common.collect.ImmutableList; + /** * Holds player interactions using standard windows */ public class GuiDialog { - private static final String[] defaultConfirmOptions = { "Yes", "No" }; + private static final ImmutableList defaultConfirmOptions = ImmutableList.of("Yes", "No"); public static void confirm(final CardView c, final String question, final Callback callback) { GuiDialog.confirm(c, question, true, null, callback); @@ -17,18 +21,17 @@ public class GuiDialog { public static void confirm(final CardView c, final String question, final boolean defaultChoice, final Callback callback) { GuiDialog.confirm(c, question, defaultChoice, null, callback); } - public static void confirm(final CardView c, final String question, String[] options, final Callback callback) { + public static void confirm(final CardView c, final String question, final List options, final Callback callback) { GuiDialog.confirm(c, question, true, options, callback); } - public static void confirm(final CardView c, final String question, final boolean defaultIsYes, final String[] options, final Callback callback) { + public static void confirm(final CardView c, final String question, final boolean defaultIsYes, final List options, final Callback callback) { final String title = c == null ? "Question" : c + " - Ability"; String questionToUse = StringUtils.isBlank(question) ? "Activate card's ability?" : question; - String[] opts = options == null ? defaultConfirmOptions : options; + final List opts = options == null ? defaultConfirmOptions : options; FOptionPane.showCardOptionDialog(c, questionToUse, title, FOptionPane.QUESTION_ICON, opts, defaultIsYes ? 0 : 1, new Callback() { - @Override - public void run(Integer result) { - callback.run(result == 0); + @Override public void run(final Integer result) { + callback.run(result.intValue() == 0); } }); } diff --git a/forge-gui-mobile/src/forge/toolbox/ListChooser.java b/forge-gui-mobile/src/forge/toolbox/ListChooser.java index 92a02a1ba98..477a43192a8 100644 --- a/forge-gui-mobile/src/forge/toolbox/ListChooser.java +++ b/forge-gui-mobile/src/forge/toolbox/ListChooser.java @@ -19,6 +19,7 @@ package forge.toolbox; import com.google.common.base.Function; +import com.google.common.collect.ImmutableList; import forge.FThreads; import forge.Graphics; @@ -108,12 +109,11 @@ public class ListChooser extends FContainer { }); } - String[] options; + final List options; if (minChoices == 0) { - options = new String[] {"OK", "Cancel"}; - } - else { - options = new String[] {"OK"}; + options = ImmutableList.of("OK", "Cancel"); + } else { + options = ImmutableList.of("OK"); } updateHeight(); diff --git a/forge-gui/src/main/java/forge/interfaces/IGuiBase.java b/forge-gui/src/main/java/forge/interfaces/IGuiBase.java index 70c774d717e..fa024a969d3 100644 --- a/forge-gui/src/main/java/forge/interfaces/IGuiBase.java +++ b/forge-gui/src/main/java/forge/interfaces/IGuiBase.java @@ -30,8 +30,8 @@ public interface IGuiBase { ISkinImage createLayeredImage(FSkinProp background, String overlayFilename, float opacity); void showBugReportDialog(String title, String text, boolean showExitAppBtn); void showImageDialog(ISkinImage image, String message, String title); - int showOptionDialog(String message, String title, FSkinProp icon, String[] options, int defaultOption); - String showInputDialog(String message, String title, FSkinProp icon, String initialInput, String[] inputOptions); + int showOptionDialog(String message, String title, FSkinProp icon, List options, int defaultOption); + String showInputDialog(String message, String title, FSkinProp icon, String initialInput, List inputOptions); List getChoices(String message, int min, int max, Collection choices, T selected, Function display); List order(String title, String top, int remainingObjectsMin, int remainingObjectsMax, List sourceChoices, List destChoices); String showFileDialog(String title, String defaultDir); diff --git a/forge-gui/src/main/java/forge/interfaces/IGuiGame.java b/forge-gui/src/main/java/forge/interfaces/IGuiGame.java index e8147498f56..493e96388a0 100644 --- a/forge-gui/src/main/java/forge/interfaces/IGuiGame.java +++ b/forge-gui/src/main/java/forge/interfaces/IGuiGame.java @@ -63,21 +63,19 @@ public interface IGuiGame { boolean showConfirmDialog(String message, String title, String yesButtonText, String noButtonText); boolean showConfirmDialog(String message, String title, String yesButtonText, String noButtonText, boolean defaultYes); - int showOptionDialog(String message, String title, FSkinProp icon, - String[] options, int defaultOption); + int showOptionDialog(String message, String title, FSkinProp icon, List options, int defaultOption); String showInputDialog(String message, String title); String showInputDialog(String message, String title, FSkinProp icon); String showInputDialog(String message, String title, FSkinProp icon, String initialInput); - String showInputDialog(String message, String title, FSkinProp icon, String initialInput, String[] inputOptions); + String showInputDialog(String message, String title, FSkinProp icon, String initialInput, List inputOptions); boolean confirm(CardView c, String question); - boolean confirm(CardView c, String question, String[] options); - boolean confirm(CardView c, String question, boolean defaultIsYes, String[] options); + boolean confirm(CardView c, String question, List options); + boolean confirm(CardView c, String question, boolean defaultIsYes, List options); - List getChoices(String message, int min, int max, T[] choices); - List getChoices(String message, int min, int max, Collection choices); - List getChoices(String message, int min, int max, Collection choices, T selected, Function display); + List getChoices(String message, int min, int max, List choices); + List getChoices(String message, int min, int max, List choices, T selected, Function display); // Get Integer in range Integer getInteger(String message, int min); @@ -99,8 +97,7 @@ public interface IGuiGame { * getChoices. * @see #getChoices(String, int, int, Object...) */ - T oneOrNone(String message, T[] choices); - T oneOrNone(String message, Collection choices); + T oneOrNone(String message, List choices); /** *

@@ -115,10 +112,9 @@ public interface IGuiGame { * a T object. * @return One of {@code choices}. Can only be {@code null} if {@code choices} is empty. */ - T one(String message, T[] choices); - T one(String message, Collection choices); + T one(String message, List choices); - void reveal(String message, Collection items); + void reveal(String message, List items); List many(String title, String topCaption, int cnt, List sourceChoices, CardView c); List many(String title, String topCaption, int min, int max, List sourceChoices, CardView c); @@ -142,7 +138,7 @@ public interface IGuiGame { List insertInList(String title, T newItem, List oldItems); List sideboard(CardPool sideboard, CardPool main); - GameEntityView chooseSingleEntityForEffect(String title, Collection optionList, DelayedReveal delayedReveal, boolean isOptional); void setCard(CardView card); + GameEntityView chooseSingleEntityForEffect(String title, List optionList, DelayedReveal delayedReveal, boolean isOptional); void setCard(CardView card); void setPlayerAvatar(LobbyPlayer player, IHasIcon ihi); boolean openZones(Collection zones, Map players); void restoreOldZones(Map playersToRestoreZonesFor); diff --git a/forge-gui/src/main/java/forge/match/AbstractGuiGame.java b/forge-gui/src/main/java/forge/match/AbstractGuiGame.java index b32726911b3..b01a4d4a686 100644 --- a/forge-gui/src/main/java/forge/match/AbstractGuiGame.java +++ b/forge-gui/src/main/java/forge/match/AbstractGuiGame.java @@ -1,7 +1,6 @@ package forge.match; -import java.util.ArrayList; -import java.util.Arrays; +import java.io.Serializable; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -12,6 +11,7 @@ import java.util.TimerTask; import org.apache.commons.lang3.StringUtils; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -114,17 +114,17 @@ public abstract class AbstractGuiGame implements IGuiGame, IMayViewCards { if (altState == null) { return false; } switch (altState.getState()) { - case Original: - final CardStateView currentState = cv.getCurrentState(); - if (currentState.getState() == CardStateName.FaceDown) { - return getCurrentPlayer() == null || cv.canFaceDownBeShownToAny(getLocalPlayers()); - } - return true; //original can always be shown if not a face down that can't be shown - case Flipped: - case Transformed: - return true; - default: - return false; + case Original: + final CardStateView currentState = cv.getCurrentState(); + if (currentState.getState() == CardStateName.FaceDown) { + return getCurrentPlayer() == null || cv.canFaceDownBeShownToAny(getLocalPlayers()); + } + return true; //original can always be shown if not a face down that can't be shown + case Flipped: + case Transformed: + return true; + default: + return false; } } @@ -339,17 +339,8 @@ public abstract class AbstractGuiGame implements IGuiGame, IMayViewCards { * @see #getChoices(String, int, int, Object...) */ @Override - public T oneOrNone(final String message, final T[] choices) { - if ((choices == null) || (choices.length == 0)) { - return null; - } - final List choice = getChoices(message, 0, 1, choices); - return choice.isEmpty() ? null : choice.get(0); - } - - @Override - public T oneOrNone(final String message, final Collection choices) { - if ((choices == null) || choices.isEmpty()) { + public T oneOrNone(final String message, final List choices) { + if (choices == null || choices.isEmpty()) { return null; } final List choice = getChoices(message, 0, 1, choices); @@ -371,14 +362,7 @@ public abstract class AbstractGuiGame implements IGuiGame, IMayViewCards { * @return a T object. */ @Override - public T one(final String message, final T[] choices) { - final List choice = getChoices(message, 1, 1, choices); - assert choice.size() == 1; - return choice.get(0); - } - - @Override - public T one(final String message, final Collection choices) { + public T one(final String message, final List choices) { if (choices == null || choices.isEmpty()) { return null; } @@ -393,7 +377,7 @@ public abstract class AbstractGuiGame implements IGuiGame, IMayViewCards { // Nothing to choose here. Code uses this to just reveal one or more items @Override - public void reveal(final String message, final Collection items) { + public void reveal(final String message, final List items) { getChoices(message, -1, -1, items); } @@ -424,14 +408,14 @@ public abstract class AbstractGuiGame implements IGuiGame, IMayViewCards { for (int i = 0; i < count; i++) { choices[count - i - 1] = Integer.valueOf(i + min); } - } - else { + } else { for (int i = 0; i < count; i++) { choices[i] = Integer.valueOf(i + min); } } - return oneOrNone(message, choices); + return oneOrNone(message, ImmutableList.copyOf(choices)); } + @Override public Integer getInteger(final String message, final int min, final int max, final int cutoff) { if (max <= min || cutoff < min) { @@ -442,15 +426,15 @@ public abstract class AbstractGuiGame implements IGuiGame, IMayViewCards { return getInteger(message, min, max); } - final List choices = new ArrayList(); + final ImmutableList.Builder choices = ImmutableList.builder(); for (int i = min; i <= cutoff; i++) { choices.add(Integer.valueOf(i)); } choices.add("..."); - final Object choice = oneOrNone(message, choices); + final Object choice = oneOrNone(message, choices.build()); if (choice instanceof Integer || choice == null) { - return (Integer)choice; + return (Integer) choice; } //if Other option picked, prompt for number input @@ -481,12 +465,7 @@ public abstract class AbstractGuiGame implements IGuiGame, IMayViewCards { // returned Object will never be null @Override - public List getChoices(final String message, final int min, final int max, final T[] choices) { - return getChoices(message, min, max, Arrays.asList(choices), null, null); - } - - @Override - public List getChoices(final String message, final int min, final int max, final Collection choices) { + public List getChoices(final String message, final int min, final int max, final List choices) { return getChoices(message, min, max, choices, null, null); } @@ -546,8 +525,9 @@ public abstract class AbstractGuiGame implements IGuiGame, IMayViewCards { public boolean confirm(final CardView c, final String question) { return confirm(c, question, true, null); } + @Override - public boolean confirm(final CardView c, final String question, final String[] options) { + public boolean confirm(final CardView c, final String question, final List options) { return confirm(c, question, true, options); } diff --git a/forge-gui/src/main/java/forge/match/input/InputSelectTargets.java b/forge-gui/src/main/java/forge/match/input/InputSelectTargets.java index e4138933147..85cb44fa201 100644 --- a/forge-gui/src/main/java/forge/match/input/InputSelectTargets.java +++ b/forge-gui/src/main/java/forge/match/input/InputSelectTargets.java @@ -6,6 +6,8 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import com.google.common.collect.ImmutableList; + import forge.game.GameEntity; import forge.game.GameObject; import forge.game.ability.ApiType; @@ -46,20 +48,22 @@ public final class InputSelectTargets extends InputSyncronizedBase { sb.append("Targeted:\n"); for (final Entry o : targetDepth.entrySet()) { sb.append(o.getKey()); - if( o.getValue() > 1 ) - sb.append(" (").append(o.getValue()).append(" times)"); - sb.append("\n"); + if (o.getValue() > 1) { + sb.append(String.format(" (%d times)", o.getValue())); + } + sb.append("\n"); } if (!sa.getUniqueTargets().isEmpty()) { sb.append("Parent Targeted:"); sb.append(sa.getUniqueTargets()).append("\n"); } sb.append(sa.getHostCard() + " - " + tgt.getVTSelection()); - - int maxTargets = tgt.getMaxTargets(sa.getHostCard(), sa); - int targeted = sa.getTargets().getNumTargeted(); - if(maxTargets > 1) - sb.append("\n(").append(maxTargets - targeted).append(" more can be targeted)"); + + final int maxTargets = tgt.getMaxTargets(sa.getHostCard(), sa); + final int targeted = sa.getTargets().getNumTargeted(); + if(maxTargets > 1) { + sb.append(String.format("\n(%d more can be targeted)", Integer.valueOf(maxTargets - targeted))); + } showMessage(sb.toString()); @@ -122,9 +126,9 @@ public final class InputSelectTargets extends InputSyncronizedBase { // If all cards must have different controllers if (tgt.isDifferentControllers()) { final List targetedControllers = new ArrayList(); - for (GameObject o : targetDepth.keySet()) { + for (final GameObject o : targetDepth.keySet()) { if (o instanceof Card) { - Player p = ((Card) o).getController(); + final Player p = ((Card) o).getController(); targetedControllers.add(p); } } @@ -150,9 +154,9 @@ public final class InputSelectTargets extends InputSyncronizedBase { // allow allocation only if the max targets isn't reached and there are more candidates if ((sa.getTargets().getNumTargeted() + 1 < tgt.getMaxTargets(sa.getHostCard(), sa)) && (tgt.getNumCandidates(sa, true) - 1 > 0) && stillToDivide > 1) { - final Integer[] choices = new Integer[stillToDivide]; + final ImmutableList.Builder choices = ImmutableList.builder(); for (int i = 1; i <= stillToDivide; i++) { - choices[i - 1] = i; + choices.add(Integer.valueOf(i)); } String apiBasedMessage = "Distribute how much to "; if (sa.getApi() == ApiType.DealDamage) { @@ -167,7 +171,7 @@ public final class InputSelectTargets extends InputSyncronizedBase { final StringBuilder sb = new StringBuilder(); sb.append(apiBasedMessage); sb.append(card.toString()); - final Integer chosen = getController().getGui().oneOrNone(sb.toString(), choices); + final Integer chosen = getController().getGui().oneOrNone(sb.toString(), choices.build()); if (chosen == null) { return true; //still return true since there was a valid choice } @@ -184,7 +188,7 @@ public final class InputSelectTargets extends InputSyncronizedBase { } @Override - public String getActivateAction(Card card) { + public String getActivateAction(final Card card) { if (!tgt.isUniqueTargets() && targetDepth.containsKey(card)) { return null; } @@ -195,7 +199,7 @@ public final class InputSelectTargets extends InputSyncronizedBase { } @Override - protected final void onPlayerSelected(Player player, final ITriggerEvent triggerEvent) { + protected final void onPlayerSelected(final Player player, final ITriggerEvent triggerEvent) { if (!tgt.isUniqueTargets() && targetDepth.containsKey(player)) { return; } @@ -204,15 +208,15 @@ public final class InputSelectTargets extends InputSyncronizedBase { showMessage(sa.getHostCard() + " - Cannot target this player (Hexproof? Protection? Restrictions?)."); return; } - + if (tgt.isDividedAsYouChoose()) { final int stillToDivide = tgt.getStillToDivide(); int allocatedPortion = 0; // allow allocation only if the max targets isn't reached and there are more candidates if ((sa.getTargets().getNumTargeted() + 1 < tgt.getMaxTargets(sa.getHostCard(), sa)) && (tgt.getNumCandidates(sa, true) - 1 > 0) && stillToDivide > 1) { - final Integer[] choices = new Integer[stillToDivide]; + final ImmutableList.Builder choices = ImmutableList.builder(); for (int i = 1; i <= stillToDivide; i++) { - choices[i - 1] = i; + choices.add(Integer.valueOf(i)); } String apiBasedMessage = "Distribute how much to "; if (sa.getApi() == ApiType.DealDamage) { @@ -223,7 +227,7 @@ public final class InputSelectTargets extends InputSyncronizedBase { final StringBuilder sb = new StringBuilder(); sb.append(apiBasedMessage); sb.append(player.getName()); - final Integer chosen = getController().getGui().oneOrNone(sb.toString(), choices); + final Integer chosen = getController().getGui().oneOrNone(sb.toString(), choices.build()); if (null == chosen) { return; } @@ -245,15 +249,15 @@ public final class InputSelectTargets extends InputSyncronizedBase { } final Integer val = targetDepth.get(ge); targetDepth.put(ge, val == null ? Integer.valueOf(1) : Integer.valueOf(val.intValue() + 1) ); - - if(hasAllTargets()) { + + if (hasAllTargets()) { bOk = true; this.done(); - } - else + } else { this.showMessage(); + } } - + private void done() { for (final GameEntity c : targetDepth.keySet()) { if (c instanceof Card) { @@ -263,7 +267,7 @@ public final class InputSelectTargets extends InputSyncronizedBase { this.stop(); } - + private boolean hasAllTargets() { return tgt.isMaxTargetsChosen(sa.getHostCard(), sa) || ( tgt.getStillToDivide() == 0 && tgt.isDividedAsYouChoose()); } diff --git a/forge-gui/src/main/java/forge/net/ProtocolMethod.java b/forge-gui/src/main/java/forge/net/ProtocolMethod.java index fed131286de..68667680516 100644 --- a/forge-gui/src/main/java/forge/net/ProtocolMethod.java +++ b/forge-gui/src/main/java/forge/net/ProtocolMethod.java @@ -1,6 +1,5 @@ package forge.net; -import java.lang.reflect.Array; import java.lang.reflect.Method; import java.util.Collection; import java.util.List; @@ -55,13 +54,13 @@ public enum ProtocolMethod { message (Mode.SERVER, Void.TYPE, String.class, String.class), showErrorDialog (Mode.SERVER, Void.TYPE, String.class, String.class), showConfirmDialog (Mode.SERVER, Boolean.TYPE, String.class, String.class, String.class, String.class, Boolean.TYPE), - showOptionDialog (Mode.SERVER, Integer.TYPE, String.class, String.class, FSkinProp.class, Array/*String*/.class, Integer.TYPE), - showInputDialog (Mode.SERVER, String.class, String.class, String.class, FSkinProp.class, String.class, Array/*String*/.class), - confirm (Mode.SERVER, Boolean.TYPE, CardView.class, String.class, Boolean.TYPE, Array/*String*/.class), - getChoices (Mode.SERVER, List.class, String.class, Integer.TYPE, Integer.TYPE, Collection.class, Object.class, Function.class), + showOptionDialog (Mode.SERVER, Integer.TYPE, String.class, String.class, FSkinProp.class, List/*String*/.class, Integer.TYPE), + showInputDialog (Mode.SERVER, String.class, String.class, String.class, FSkinProp.class, String.class, List/*String*/.class), + confirm (Mode.SERVER, Boolean.TYPE, CardView.class, String.class, Boolean.TYPE, List/*String*/.class), + getChoices (Mode.SERVER, List.class, String.class, Integer.TYPE, Integer.TYPE, List.class, Object.class, Function.class), order (Mode.SERVER, List.class, String.class, String.class, Integer.TYPE, Integer.TYPE, List.class, List.class, CardView.class, Boolean.TYPE), sideboard (Mode.SERVER, List.class, CardPool.class, CardPool.class), - chooseSingleEntityForEffect(Mode.SERVER, GameEntityView.class, String.class, TrackableCollection.class, DelayedReveal.class, Boolean.TYPE), + chooseSingleEntityForEffect(Mode.SERVER, GameEntityView.class, String.class, List.class, DelayedReveal.class, Boolean.TYPE), setCard (Mode.SERVER, Void.TYPE, CardView.class), // TODO case "setPlayerAvatar": openZones (Mode.SERVER, Boolean.TYPE, Collection/*ZoneType*/.class, Map/*PlayerView,Object*/.class), diff --git a/forge-gui/src/main/java/forge/net/server/NetGuiGame.java b/forge-gui/src/main/java/forge/net/server/NetGuiGame.java index 7d4c9dfac7d..9deeba6833e 100644 --- a/forge-gui/src/main/java/forge/net/server/NetGuiGame.java +++ b/forge-gui/src/main/java/forge/net/server/NetGuiGame.java @@ -189,22 +189,22 @@ public class NetGuiGame extends AbstractGuiGame { } @Override - public int showOptionDialog(final String message, final String title, final FSkinProp icon, final String[] options, final int defaultOption) { + public int showOptionDialog(final String message, final String title, final FSkinProp icon, final List options, final int defaultOption) { return sendAndWait(ProtocolMethod.showOptionDialog, message, title, icon, options, defaultOption); } @Override - public String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final String[] inputOptions) { + public String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final List inputOptions) { return sendAndWait(ProtocolMethod.showInputDialog, message, title, icon, initialInput, inputOptions); } @Override - public boolean confirm(final CardView c, final String question, final boolean defaultIsYes, final String[] options) { + public boolean confirm(final CardView c, final String question, final boolean defaultIsYes, final List options) { return sendAndWait(ProtocolMethod.confirm, c, question, defaultIsYes, options); } @Override - public List getChoices(final String message, final int min, final int max, final Collection choices, final T selected, final Function display) { + public List getChoices(final String message, final int min, final int max, final List choices, final T selected, final Function display) { return sendAndWait(ProtocolMethod.getChoices, message, min, max, choices, selected, display); } @@ -219,7 +219,7 @@ public class NetGuiGame extends AbstractGuiGame { } @Override - public GameEntityView chooseSingleEntityForEffect(final String title, final Collection optionList, final DelayedReveal delayedReveal, final boolean isOptional) { + public GameEntityView chooseSingleEntityForEffect(final String title, final List optionList, final DelayedReveal delayedReveal, final boolean isOptional) { return sendAndWait(ProtocolMethod.chooseSingleEntityForEffect, title, optionList, delayedReveal, isOptional); } diff --git a/forge-gui/src/main/java/forge/planarconquest/ConquestController.java b/forge-gui/src/main/java/forge/planarconquest/ConquestController.java index c658df63165..178cb069ec5 100644 --- a/forge-gui/src/main/java/forge/planarconquest/ConquestController.java +++ b/forge-gui/src/main/java/forge/planarconquest/ConquestController.java @@ -25,6 +25,7 @@ import java.util.Map.Entry; import java.util.Set; import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableList; import forge.FThreads; import forge.GuiBase; @@ -291,7 +292,7 @@ public class ConquestController { public void run() { awardWinStreakBonus(view); - String[] options = {"Booster", "Card"}; + final List options = ImmutableList.of("Booster", "Card"); if (SOptionPane.showOptionDialog("Choose one \u2014\n\n" + "\u2022 Open a random booster pack\n" + "\u2022 Choose a card from your opponent's deck", diff --git a/forge-gui/src/main/java/forge/player/PlayerControllerHuman.java b/forge-gui/src/main/java/forge/player/PlayerControllerHuman.java index 73b44ae2466..f378eef2801 100644 --- a/forge-gui/src/main/java/forge/player/PlayerControllerHuman.java +++ b/forge-gui/src/main/java/forge/player/PlayerControllerHuman.java @@ -24,6 +24,7 @@ import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -447,11 +448,11 @@ implements IGameController { if (min >= max) { return min; } - final Integer[] choices = new Integer[max + 1 - min]; + final ImmutableList.Builder choices = ImmutableList.builder(); for (int i = 0; i <= max - min; i++) { - choices[i] = Integer.valueOf(i + min); + choices.add(Integer.valueOf(i + min)); } - return getGui().one(title, choices).intValue(); + return getGui().one(title, choices.build()).intValue(); } @Override @@ -460,11 +461,14 @@ implements IGameController { } @Override - public SpellAbility chooseSingleSpellForEffect(final java.util.List spells, final SpellAbility sa, final String title) { + public SpellAbility chooseSingleSpellForEffect(final List spells, final SpellAbility sa, final String title) { if (spells.size() < 2) { - return spells.get(0); + return Iterables.getFirst(spells, null); } + // Show the card that asked for this choice + getGui().setCard(CardView.get(sa.getHostCard())); + // Human is supposed to read the message and understand from it what to choose return getGui().one(title, spells); } @@ -573,7 +577,7 @@ implements IGameController { } @Override - public void reveal(final Collection cards, final ZoneType zone, final PlayerView owner, String message) { + public void reveal(final List cards, final ZoneType zone, final PlayerView owner, String message) { if (StringUtils.isBlank(message)) { message = "Looking at cards in {player's} " + zone.name().toLowerCase(); } else { @@ -626,7 +630,7 @@ implements IGameController { final CardView view = CardView.get(c); tempShowCard(c); - final boolean result = getGui().confirm(view, "Put " + view + " on the top or bottom of your library?", new String[]{"Top", "Bottom"}); + final boolean result = getGui().confirm(view, String.format("Put %s on the top or bottom of your library?", view), ImmutableList.of("Top", "Bottom")); endTempShowCards(); return result; @@ -637,28 +641,28 @@ implements IGameController { List choices; tempShowCards(cards); switch (destinationZone) { - case Library: - choices = getGui().order("Choose order of cards to put into the library", "Closest to top", CardView.getCollection(cards), null); - break; - case Battlefield: - choices = getGui().order("Choose order of cards to put onto the battlefield", "Put first", CardView.getCollection(cards), null); - break; - case Graveyard: - choices = getGui().order("Choose order of cards to put into the graveyard", "Closest to bottom", CardView.getCollection(cards), null); - break; - case PlanarDeck: - choices = getGui().order("Choose order of cards to put into the planar deck", "Closest to top", CardView.getCollection(cards), null); - break; - case SchemeDeck: - choices = getGui().order("Choose order of cards to put into the scheme deck", "Closest to top", CardView.getCollection(cards), null); - break; - case Stack: - choices = getGui().order("Choose order of copies to cast", "Put first", CardView.getCollection(cards), null); - break; - default: - System.out.println("ZoneType " + destinationZone + " - Not Ordered"); - endTempShowCards(); - return cards; + case Library: + choices = getGui().order("Choose order of cards to put into the library", "Closest to top", CardView.getCollection(cards), null); + break; + case Battlefield: + choices = getGui().order("Choose order of cards to put onto the battlefield", "Put first", CardView.getCollection(cards), null); + break; + case Graveyard: + choices = getGui().order("Choose order of cards to put into the graveyard", "Closest to bottom", CardView.getCollection(cards), null); + break; + case PlanarDeck: + choices = getGui().order("Choose order of cards to put into the planar deck", "Closest to top", CardView.getCollection(cards), null); + break; + case SchemeDeck: + choices = getGui().order("Choose order of cards to put into the scheme deck", "Closest to top", CardView.getCollection(cards), null); + break; + case Stack: + choices = getGui().order("Choose order of copies to cast", "Put first", CardView.getCollection(cards), null); + break; + default: + System.out.println("ZoneType " + destinationZone + " - Not Ordered"); + endTempShowCards(); + return cards; } endTempShowCards(); return game.getCardList(choices); @@ -694,13 +698,13 @@ implements IGameController { if (cardsInGrave == 0) { return CardCollection.EMPTY; } - final CardCollection toExile = new CardCollection(); - final Integer[] cntChoice = new Integer[cardsInGrave + 1]; - for (int i = 0; i <= cardsInGrave; i++) { - cntChoice[i] = Integer.valueOf(i); - } - final Integer chosenAmount = getGui().one("Delve how many cards?", cntChoice); + final CardCollection toExile = new CardCollection(); + final ImmutableList.Builder cntChoice = ImmutableList.builder(); + for (int i = 0; i <= cardsInGrave; i++) { + cntChoice.add(Integer.valueOf(i)); + } + final int chosenAmount = getGui().one("Delve how many cards?", cntChoice.build()).intValue(); for (int i = 0; i < chosenAmount; i++) { final CardView nowChosen = getGui().oneOrNone("Exile which card?", CardView.getCollection(grave)); @@ -949,15 +953,15 @@ implements IGameController { @Override public boolean chooseBinary(final SpellAbility sa, final String question, final BinaryChoiceType kindOfChoice, final Boolean defaultVal) { - final String[] labels; + final List labels; switch (kindOfChoice) { - case HeadsOrTails: labels = new String[]{"Heads", "Tails"}; break; - case TapOrUntap: labels = new String[]{"Tap", "Untap"}; break; - case OddsOrEvens: labels = new String[]{"Odds", "Evens"}; break; - case UntapOrLeaveTapped: labels = new String[]{"Untap", "Leave tapped"}; break; - case UntapTimeVault: labels = new String[]{"Untap (and skip this turn)", "Leave tapped"}; break; - case PlayOrDraw: labels = new String[]{"Play", "Draw"}; break; - default: labels = kindOfChoice.toString().split("Or"); + case HeadsOrTails: labels = ImmutableList.of("Heads", "Tails"); break; + case TapOrUntap: labels = ImmutableList.of("Tap", "Untap"); break; + case OddsOrEvens: labels = ImmutableList.of("Odds", "Evens"); break; + case UntapOrLeaveTapped: labels = ImmutableList.of("Untap", "Leave tapped"); break; + case UntapTimeVault: labels = ImmutableList.of("Untap (and skip this turn)", "Leave tapped"); break; + case PlayOrDraw: labels = ImmutableList.of("Play", "Draw"); break; + default: labels = ImmutableList.copyOf(kindOfChoice.toString().split("Or")); } return getGui().confirm(CardView.get(sa.getHostCard()), question, defaultVal == null || defaultVal.booleanValue(), labels); } @@ -965,11 +969,11 @@ implements IGameController { @Override public boolean chooseFlipResult(final SpellAbility sa, final Player flipper, final boolean[] results, final boolean call) { final String[] labelsSrc = call ? new String[]{"heads", "tails"} : new String[]{"win the flip", "lose the flip"}; - final String[] strResults = new String[results.length]; + final ImmutableList.Builder strResults = ImmutableList.builder(); for (int i = 0; i < results.length; i++) { - strResults[i] = labelsSrc[results[i] ? 0 : 1]; + strResults.add(labelsSrc[results[i] ? 0 : 1]); } - return getGui().one(sa.getHostCard().getName() + " - Choose a result", strResults).equals(labelsSrc[0]); + return getGui().one(sa.getHostCard().getName() + " - Choose a result", strResults.build()).equals(labelsSrc[0]); } @Override @@ -986,12 +990,12 @@ implements IGameController { } final String counterChoiceTitle = "Choose a counter type on " + cardWithCounter; - final CounterType chosen = getGui().one(counterChoiceTitle, cardWithCounter.getCounters().keySet()); + final CounterType chosen = getGui().one(counterChoiceTitle, ImmutableList.copyOf(cardWithCounter.getCounters().keySet())); final String putOrRemoveTitle = "What to do with that '" + chosen.getName() + "' counter "; final String putString = "Put another " + chosen.getName() + " counter on " + cardWithCounter; final String removeString = "Remove a " + chosen.getName() + " counter from " + cardWithCounter; - final String addOrRemove = getGui().one(putOrRemoveTitle, new String[]{putString,removeString}); + final String addOrRemove = getGui().one(putOrRemoveTitle, ImmutableList.of(putString, removeString)); return new ImmutablePair(chosen,addOrRemove); } @@ -1061,9 +1065,9 @@ implements IGameController { public byte chooseColor(final String message, final SpellAbility sa, final ColorSet colors) { final int cntColors = colors.countColors(); switch (cntColors) { - case 0: return 0; - case 1: return colors.getColor(); - default: return chooseColorCommon(message, sa == null ? null : sa.getHostCard(), colors, false); + case 0: return 0; + case 1: return colors.getColor(); + default: return chooseColorCommon(message, sa == null ? null : sa.getHostCard(), colors, false); } } @@ -1071,29 +1075,25 @@ implements IGameController { public byte chooseColorAllowColorless(final String message, final Card c, final ColorSet colors) { final int cntColors = 1 + colors.countColors(); switch (cntColors) { - case 1: return 0; - default: return chooseColorCommon(message, c, colors, true); + case 1: return 0; + default: return chooseColorCommon(message, c, colors, true); } } private byte chooseColorCommon(final String message, final Card c, final ColorSet colors, final boolean withColorless) { - int cntColors = colors.countColors(); - if(withColorless) { - cntColors++; - } - final String[] colorNames = new String[cntColors]; - int i = 0; + final ImmutableList.Builder colorNamesBuilder = ImmutableList.builder(); if (withColorless) { - colorNames[i++] = MagicColor.toLongString((byte)0); + colorNamesBuilder.add(MagicColor.toLongString(MagicColor.COLORLESS)); } - for (final byte b : colors) { - colorNames[i++] = MagicColor.toLongString(b); + for (final Byte b : colors) { + colorNamesBuilder.add(MagicColor.toLongString(b.byteValue())); } - if (colorNames.length > 2) { + final ImmutableList colorNames = colorNamesBuilder.build(); + if (colorNames.size() > 2) { return MagicColor.fromName(getGui().one(message, colorNames)); } final int idxChosen = getGui().confirm(CardView.get(c), message, colorNames) ? 0 : 1; - return MagicColor.fromName(colorNames[idxChosen]); + return MagicColor.fromName(colorNames.get(idxChosen)); } @Override @@ -1105,7 +1105,7 @@ implements IGameController { } @Override - public CounterType chooseCounterType(final Collection options, final SpellAbility sa, final String prompt) { + public CounterType chooseCounterType(final List options, final SpellAbility sa, final String prompt) { if (options.size() <= 1) { return Iterables.getFirst(options, null); } @@ -1202,7 +1202,7 @@ implements IGameController { if (!faceUp) { final String p1Str = String.format("Pile 1 (%s cards)", pile1.size()); final String p2Str = String.format("Pile 2 (%s cards)", pile2.size()); - final String[] possibleValues = { p1Str , p2Str }; + final List possibleValues = ImmutableList.of(p1Str , p2Str); return getGui().confirm(CardView.get(sa.getHostCard()), "Choose a Pile", possibleValues); } @@ -1239,7 +1239,7 @@ implements IGameController { @Override public void revealAnte(final String message, final Multimap removedAnteCards) { for (final Player p : removedAnteCards.keySet()) { - getGui().reveal(message + " from " + Lang.getPossessedObject(MessageUtil.mayBeYou(player, p), "deck"), removedAnteCards.get(p)); + getGui().reveal(message + " from " + Lang.getPossessedObject(MessageUtil.mayBeYou(player, p), "deck"), ImmutableList.copyOf(removedAnteCards.get(p))); } } @@ -1568,7 +1568,7 @@ implements IGameController { final Card card = game.getCard(getGui().oneOrNone("Add counters to which card?", CardView.getCollection(cards))); if (card == null) { return; } - final CounterType counter = getGui().oneOrNone("Which type of counter?", CounterType.values()); + final CounterType counter = getGui().oneOrNone("Which type of counter?", CounterType.values); if (counter == null) { return; } final Integer count = getGui().getInteger("How many counters?", 1, Integer.MAX_VALUE, 10); @@ -1746,7 +1746,7 @@ implements IGameController { final Player player = game.getPlayer(getGui().oneOrNone("Which player should roll?", PlayerView.getCollection(game.getPlayers()))); if (player == null) { return; } - final PlanarDice res = getGui().oneOrNone("Choose result", PlanarDice.values()); + final PlanarDice res = getGui().oneOrNone("Choose result", PlanarDice.values); if (res == null) { return; } System.out.println("Rigging planar dice roll: " + res.toString()); diff --git a/forge-gui/src/main/java/forge/util/gui/SOptionPane.java b/forge-gui/src/main/java/forge/util/gui/SOptionPane.java index 99f8a869d22..248091dcdd7 100644 --- a/forge-gui/src/main/java/forge/util/gui/SOptionPane.java +++ b/forge-gui/src/main/java/forge/util/gui/SOptionPane.java @@ -1,5 +1,9 @@ package forge.util.gui; +import java.util.List; + +import com.google.common.collect.ImmutableList; + import forge.GuiBase; import forge.assets.FSkinProp; @@ -26,7 +30,7 @@ public class SOptionPane { } public static void showMessageDialog(final String message, final String title, final FSkinProp icon) { - showOptionDialog(message, title, icon, new String[] {"OK"}, 0); + showOptionDialog(message, title, icon, ImmutableList.of("OK"), 0); } public static boolean showConfirmDialog(final String message) { @@ -46,16 +50,16 @@ public class SOptionPane { } public static boolean showConfirmDialog(final String message, final String title, final String yesButtonText, final String noButtonText, final boolean defaultYes) { - final String[] options = {yesButtonText, noButtonText}; + final List options = ImmutableList.of(yesButtonText, noButtonText); final int reply = SOptionPane.showOptionDialog(message, title, QUESTION_ICON, options, defaultYes ? 0 : 1); return (reply == 0); } - public static int showOptionDialog(final String message, final String title, final FSkinProp icon, final String[] options) { + public static int showOptionDialog(final String message, final String title, final FSkinProp icon, final List options) { return showOptionDialog(message, title, icon, options, 0); } - public static int showOptionDialog(final String message, final String title, final FSkinProp icon, final String[] options, final int defaultOption) { + public static int showOptionDialog(final String message, final String title, final FSkinProp icon, final List options, final int defaultOption) { return GuiBase.getInterface().showOptionDialog(message, title, icon, options, defaultOption); } @@ -71,7 +75,7 @@ public class SOptionPane { return showInputDialog(message, title, icon, initialInput, null); } - public static String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final String[] inputOptions) { + public static String showInputDialog(final String message, final String title, final FSkinProp icon, final String initialInput, final List inputOptions) { return GuiBase.getInterface().showInputDialog(message, title, icon, initialInput, inputOptions); }