From 6e1e22d6cb73c1bd19f196d69a1d9f6424b9cd12 Mon Sep 17 00:00:00 2001 From: spr Date: Tue, 8 Oct 2013 10:04:32 +0000 Subject: [PATCH] - Refactor GamePlayerUtil. --- .../gui/home/settings/GamePlayerUtil.java | 83 ++++++++++--------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/src/main/java/forge/gui/home/settings/GamePlayerUtil.java b/src/main/java/forge/gui/home/settings/GamePlayerUtil.java index ae5cc027489..7dfce65dcee 100644 --- a/src/main/java/forge/gui/home/settings/GamePlayerUtil.java +++ b/src/main/java/forge/gui/home/settings/GamePlayerUtil.java @@ -15,56 +15,59 @@ public final class GamePlayerUtil { public static void setPlayerName() { - String playerName = prefs.getPref(FPref.PLAYER_NAME); - String newName = null; + String oldPlayerName = prefs.getPref(FPref.PLAYER_NAME); + String newPlayerName = null; - if (StringUtils.isBlank(playerName)) { - newName = (String)JOptionPane.showInputDialog( - JOptionPane.getRootFrame(), - "By default, Forge will refer to you as the \"Human\" during gameplay.\n" + - "If you would prefer a different name please enter it now.\n", - "Personalize Forge Gameplay", - JOptionPane.QUESTION_MESSAGE, - null, null, null); + if (StringUtils.isBlank(oldPlayerName)) { + newPlayerName = getVerifiedPlayerName(getPlayerNameUsingFirstTimePrompt(), oldPlayerName); } else { - newName = getNewPlayerNameFromInputDialog(playerName); + newPlayerName = getVerifiedPlayerName(getPlayerNameUsingStandardPrompt(oldPlayerName), oldPlayerName); } + prefs.setPref(FPref.PLAYER_NAME, newPlayerName); + prefs.save(); + + if (StringUtils.isBlank(oldPlayerName) && newPlayerName != "Human") { + showThankYouPrompt(newPlayerName); + } + + } + + private static void showThankYouPrompt(String playerName) { + JOptionPane.showMessageDialog( + JOptionPane.getRootFrame(), + "Thank you, " + playerName + ". " + + "You will not be prompted again but you can change\nyour name at any time using the \"Player Name\" setting in Preferences.\n\n"); + } + + private static String getPlayerNameUsingFirstTimePrompt() { + return (String)JOptionPane.showInputDialog( + JOptionPane.getRootFrame(), + "By default, Forge will refer to you as the \"Human\" during gameplay.\n" + + "If you would prefer a different name please enter it now.\n", + "Personalize Forge Gameplay", + JOptionPane.QUESTION_MESSAGE, + null, null, null); + } + + private static String getPlayerNameUsingStandardPrompt(String playerName) { + return (String)JOptionPane.showInputDialog( + JOptionPane.getRootFrame(), + "Please enter a new name (alpha-numeric only)\n", + "Personalize Forge Gameplay", + JOptionPane.PLAIN_MESSAGE, + null, null, playerName); + } + + private static String getVerifiedPlayerName(String newName, String oldName) { if (newName == null || !StringUtils.isAlphanumericSpace(newName)) { - newName = (StringUtils.isBlank(playerName) ? "Human" : playerName); + newName = (StringUtils.isBlank(oldName) ? "Human" : oldName); } else if (StringUtils.isWhitespace(newName)) { newName = "Human"; } else { newName = newName.trim(); } - - prefs.setPref(FPref.PLAYER_NAME, newName); - prefs.save(); - - if (StringUtils.isBlank(playerName) && newName != "Human") { - JOptionPane.showMessageDialog( - JOptionPane.getRootFrame(), - "Thank you, " + newName + ". " + - "You will not be prompted again but you can change\nyour name at any time using the \"Player Name\" setting in Preferences.\n\n"); - } - - } - - private static String getNewPlayerNameFromInputDialog(String playerName) { - String newName = - (String)JOptionPane.showInputDialog( - JOptionPane.getRootFrame(), - "Please enter a new name (alpha-numeric only)\n", - "Personalize Forge Gameplay", - JOptionPane.PLAIN_MESSAGE, - null, null, playerName); - if (newName == null || !StringUtils.isAlphanumericSpace(newName)) { - return playerName; - } else if (StringUtils.isWhitespace(newName)) { - return "Human"; - } else { - return newName.trim(); - } + return newName; } }