Skip to content

Commit

Permalink
Use ClientSettings enum to save/load preference instead of model objects
Browse files Browse the repository at this point in the history
  • Loading branch information
DanVanAtta committed Jul 27, 2017
1 parent c558981 commit 71bcf7a
Show file tree
Hide file tree
Showing 40 changed files with 342 additions and 665 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ public class ClientContextTest {

@Test
public void verifyClientContext() {
assertThat(ClientContext.aiSettings(), notNullValue());
assertThat(ClientContext.battleCalcSettings(), notNullValue());
assertThat(ClientContext.battleOptionsSettings(), notNullValue());
assertThat(ClientContext.downloadCoordinator(), notNullValue());
assertThat(ClientContext.engineVersion(), notNullValue());
assertThat(ClientContext.folderSettings(), notNullValue());
assertThat(ClientContext.gameEnginePropertyReader(), notNullValue());
assertThat(ClientContext.mapDownloadController(), notNullValue());
assertThat(ClientContext.scrollSettings(), notNullValue());

assertThat(ClientContext.getMapDownloadList(), notNullValue());
assertThat(ClientContext.getMapDownloadList().isEmpty(), is(false));
}

}
6 changes: 1 addition & 5 deletions src/main/java/games/strategy/engine/ClientContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
import games.strategy.internal.persistence.serializable.PropertyBagMementoProxy;
import games.strategy.internal.persistence.serializable.VersionProxy;
import games.strategy.persistence.serializable.ProxyRegistry;
import games.strategy.triplea.settings.ai.AiSettings;
import games.strategy.triplea.settings.battle.calc.BattleCalcSettings;
import games.strategy.triplea.settings.battle.options.BattleOptionsSettings;
import games.strategy.triplea.settings.folders.FolderSettings;
import games.strategy.triplea.settings.scrolling.ScrollSettings;
import games.strategy.triplea.settings.ClientSettings;
import games.strategy.util.Version;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import games.strategy.engine.config.client.GameEnginePropertyReader;
import games.strategy.engine.framework.GameRunner;
import games.strategy.engine.framework.system.SystemProperties;
import games.strategy.triplea.settings.ClientSettings;
import games.strategy.util.Version;

/**
Expand Down Expand Up @@ -156,7 +157,7 @@ public static File getUserRootFolder() {
* retained between engine installations. Users can override this location in settings.
*/
public static File getUserMapsFolder() {
final String path = ClientContext.clientSettings().getFolderSettings().getDownloadedMapPath();
final String path = ClientSettings.USER_MAPS_FOLDER_PATH.value();


final File mapsFolder = new File(path);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/games/strategy/engine/framework/ArgParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import java.util.Arrays;

import games.strategy.triplea.settings.SystemPreferenceKey;
import games.strategy.triplea.settings.SystemPreferences;
import games.strategy.triplea.settings.ClientSettings;

public class ArgParser {
/**
Expand All @@ -18,7 +17,7 @@ public static boolean handleCommandLineArgs(
}

for (final String arg : args) {
String key;
final String key;
final int indexOf = arg.indexOf('=');
if (indexOf > 0) {
key = arg.substring(0, indexOf);
Expand All @@ -30,14 +29,15 @@ public static boolean handleCommandLineArgs(
return false;
}
}
ClientSettings.flush();
return true;
}

private static boolean setSystemProperty(String key, String value, String[] availableProperties) {
private static boolean setSystemProperty(final String key, final String value, final String[] availableProperties) {
for (final String property : availableProperties) {
if (key.equals(property)) {
if (property.equals(GameRunner.MAP_FOLDER)) {
SystemPreferences.put(SystemPreferenceKey.MAP_FOLDER_OVERRIDE, value);
ClientSettings.MAP_FOLDER_OVERRIDE.save(value);
} else {
System.getProperties().setProperty(property, value);
}
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/games/strategy/engine/framework/GameRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import games.strategy.engine.lobby.server.GameDescription;
import games.strategy.net.Messengers;
import games.strategy.triplea.ai.proAI.ProAI;
import games.strategy.triplea.settings.SystemPreferenceKey;
import games.strategy.triplea.settings.ClientSettings;
import games.strategy.triplea.settings.SystemPreferences;
import games.strategy.ui.ProgressWindow;
import games.strategy.ui.SwingAction;
Expand Down Expand Up @@ -346,7 +346,9 @@ public static void setServerStartGameSyncWaitTime(final int seconds) {
if (wait == getServerStartGameSyncWaitTime()) {
return;
}
SystemPreferences.put(SystemPreferenceKey.TRIPLEA_SERVER_START_GAME_SYNC_WAIT_TIME, wait);

ClientSettings.TRIPLEA_SERVER_START_GAME_SYNC_WAIT_TIME.save(wait);
ClientSettings.flush();
}

public static int getServerObserverJoinWaitTime() {
Expand All @@ -363,7 +365,8 @@ public static void setServerObserverJoinWaitTime(final int seconds) {
if (wait == getServerObserverJoinWaitTime()) {
return;
}
SystemPreferences.put(SystemPreferenceKey.TRIPLEA_SERVER_OBSERVER_JOIN_WAIT_TIME, wait);
ClientSettings.TRIPLEA_SERVER_OBSERVER_JOIN_WAIT_TIME.save(wait);
ClientSettings.flush();
}

private static void checkForUpdates() {
Expand Down Expand Up @@ -404,22 +407,22 @@ private static void checkForUpdates() {
*/
private static boolean checkForLatestEngineVersionOut() {
try {
final boolean firstTimeThisVersion =
SystemPreferences.get(SystemPreferenceKey.TRIPLEA_FIRST_TIME_THIS_VERSION_PROPERTY, true);
final boolean firstTimeThisVersion = ClientSettings.TRIPLEA_FIRST_TIME_THIS_VERSION_PROPERTY.booleanValue();
// check at most once per 2 days (but still allow a 'first run message' for a new version of triplea)
final LocalDateTime localDateTime = LocalDateTime.now();
final int year = localDateTime.get(ChronoField.YEAR);
final int day = localDateTime.get(ChronoField.DAY_OF_YEAR);
// format year:day
final String lastCheckTime = SystemPreferences.get(SystemPreferenceKey.TRIPLEA_LAST_CHECK_FOR_ENGINE_UPDATE, "");
final String lastCheckTime = ClientSettings.TRIPLEA_LAST_CHECK_FOR_ENGINE_UPDATE.value();
if (!firstTimeThisVersion && lastCheckTime != null && lastCheckTime.trim().length() > 0) {
final String[] yearDay = lastCheckTime.split(":");
if (Integer.parseInt(yearDay[0]) >= year && Integer.parseInt(yearDay[1]) + 1 >= day) {
return false;
}
}

SystemPreferences.put(SystemPreferenceKey.TRIPLEA_LAST_CHECK_FOR_ENGINE_UPDATE, year + ":" + day);
ClientSettings.TRIPLEA_LAST_CHECK_FOR_ENGINE_UPDATE.save(year + ":" + day);
ClientSettings.flush();

final EngineVersionProperties latestEngineOut = EngineVersionProperties.contactServerForEngineVersionProperties();
if (latestEngineOut == null) {
Expand All @@ -433,7 +436,7 @@ private static boolean checkForLatestEngineVersionOut() {
return true;
}
} catch (final Exception e) {
System.out.println("Error while checking for engine updates: " + e.getMessage());
ClientLogger.logError("Error while checking for engine updates", e);
}
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/games/strategy/engine/framework/ServerGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import games.strategy.debug.ClientLogger;
import games.strategy.debug.ErrorConsole;
import games.strategy.engine.ClientContext;
import games.strategy.engine.GameOverException;
import games.strategy.engine.data.Change;
import games.strategy.engine.data.CompositeChange;
Expand Down Expand Up @@ -51,6 +50,7 @@
import games.strategy.net.INode;
import games.strategy.net.Messengers;
import games.strategy.triplea.TripleAPlayer;
import games.strategy.triplea.settings.ClientSettings;

/**
* Represents a running game.
Expand Down Expand Up @@ -347,8 +347,8 @@ public void stopGame() {
}

private void autoSave(final String fileName) {
SaveGameFileChooser.ensureMapsFolderExists();
final File autoSaveDir = new File(ClientContext.clientSettings().getFolderSettings().getSaveGamePath()
final File autoSaveDir = new File(
ClientSettings.SAVE_GAMES_FOLDER_PATH.value()
+ (SystemProperties.isWindows() ? "\\" : "/" + "autoSave"));
if (!autoSaveDir.exists()) {
autoSaveDir.mkdirs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import games.strategy.net.IServerMessenger;
import games.strategy.sound.ClipPlayer;
import games.strategy.triplea.Constants;
import games.strategy.triplea.settings.ClientSettings;
import games.strategy.util.MD5Crypt;
import games.strategy.util.ThreadUtil;
import games.strategy.util.TimeManager;
Expand Down Expand Up @@ -199,10 +200,9 @@ public String remoteStopGame(final String hashedPassword, final String salt) {
if (iGame != null) {
(new Thread(() -> {
System.out.println("Remote Stop Game Initiated.");
SaveGameFileChooser.ensureMapsFolderExists();
try {
iGame.saveGame(new File(
ClientContext.clientSettings().getFolderSettings().getSaveGamePath(),
ClientSettings.SAVE_GAMES_FOLDER_PATH.value(),
SaveGameFileChooser.getAutoSaveFileName()));
} catch (final Exception e) {
ClientLogger.logQuietly(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,76 @@
package games.strategy.engine.framework.lookandfeel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.pushingpixels.substance.api.skin.SubstanceAutumnLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceBusinessBlackSteelLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceBusinessBlueSteelLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceBusinessLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceCeruleanLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceChallengerDeepLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceCremeCoffeeLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceCremeLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceDustCoffeeLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceDustLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceEmeraldDuskLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceGeminiLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceGraphiteAquaLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceGraphiteGlassLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceGraphiteLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceMagellanLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceMarinerLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceMistAquaLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceMistSilverLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceModerateLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceNebulaBrickWallLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceNebulaLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceOfficeBlack2007LookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceOfficeSilver2007LookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceRavenLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceSaharaLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceTwilightLookAndFeel;

import games.strategy.debug.ClientLogger;
import games.strategy.engine.framework.system.SystemProperties;
import games.strategy.triplea.settings.SystemPreferenceKey;
import games.strategy.triplea.settings.ClientSettings;
import games.strategy.triplea.settings.SystemPreferences;
import games.strategy.triplea.ui.menubar.TripleAMenuBar;
import games.strategy.ui.SwingAction;

public class LookAndFeel {

public static List<String> getLookAndFeelAvailableList() {
final List<String> substanceLooks = new ArrayList<>();
for (final UIManager.LookAndFeelInfo look : UIManager.getInstalledLookAndFeels()) {
substanceLooks.add(look.getClassName());
}
substanceLooks.addAll(Arrays.asList(SubstanceAutumnLookAndFeel.class.getName(),
SubstanceBusinessBlackSteelLookAndFeel.class.getName(), SubstanceBusinessBlueSteelLookAndFeel.class.getName(),
SubstanceBusinessLookAndFeel.class.getName(), SubstanceCeruleanLookAndFeel.class.getName(),
SubstanceChallengerDeepLookAndFeel.class.getName(), SubstanceCremeCoffeeLookAndFeel.class.getName(),
SubstanceCremeLookAndFeel.class.getName(), SubstanceDustCoffeeLookAndFeel.class.getName(),
SubstanceDustLookAndFeel.class.getName(), SubstanceEmeraldDuskLookAndFeel.class.getName(),
SubstanceGeminiLookAndFeel.class.getName(), SubstanceGraphiteAquaLookAndFeel.class.getName(),
SubstanceGraphiteGlassLookAndFeel.class.getName(), SubstanceGraphiteLookAndFeel.class.getName(),
SubstanceMagellanLookAndFeel.class.getName(), SubstanceMarinerLookAndFeel.class.getName(),
SubstanceMistAquaLookAndFeel.class.getName(), SubstanceMistSilverLookAndFeel.class.getName(),
SubstanceModerateLookAndFeel.class.getName(), SubstanceNebulaBrickWallLookAndFeel.class.getName(),
SubstanceNebulaLookAndFeel.class.getName(), SubstanceOfficeBlack2007LookAndFeel.class.getName(),
SubstanceOfficeBlue2007LookAndFeel.class.getName(), SubstanceOfficeSilver2007LookAndFeel.class.getName(),
SubstanceRavenLookAndFeel.class.getName(), SubstanceSaharaLookAndFeel.class.getName(),
SubstanceTwilightLookAndFeel.class.getName()));
return substanceLooks;
}

public static void setupLookAndFeel() {
SwingAction.invokeAndWait(() -> {
try {
UIManager.setLookAndFeel(getDefaultLookAndFeel());
UIManager.setLookAndFeel(ClientSettings.LOOK_AND_FEEL_PREF.value());
// FYI if you are getting a null pointer exception in Substance, like this:
// org.pushingpixels.substance.internal.utils.SubstanceColorUtilities
// .getDefaultBackgroundColor(SubstanceColorUtilities.java:758)
Expand All @@ -38,26 +89,7 @@ public static void setupLookAndFeel() {
});
}

private static String getDefaultLookAndFeel() {
String defaultLookAndFeel = SubstanceGraphiteLookAndFeel.class.getName();

if (SystemProperties.isMac()) {
// stay consistent with mac look and feel if we are on a mac
defaultLookAndFeel = UIManager.getSystemLookAndFeelClassName();
}

String userDefault = SystemPreferences.get(SystemPreferenceKey.LOOK_AND_FEEL_PREF, defaultLookAndFeel);
final List<String> availableSkins = TripleAMenuBar.getLookAndFeelAvailableList();

if (availableSkins.contains(userDefault)) {
return userDefault;
}
if (availableSkins.contains(defaultLookAndFeel)) {
setDefaultLookAndFeel(defaultLookAndFeel);
return defaultLookAndFeel;
}
return UIManager.getSystemLookAndFeelClassName();
}

public static void setDefaultLookAndFeel(final String lookAndFeelClassName) {
try {
Expand All @@ -71,6 +103,6 @@ public static void setDefaultLookAndFeel(final String lookAndFeelClassName) {
+ Arrays.asList(UIManager.getInstalledLookAndFeels()) , e);
return;
}
SystemPreferences.put(SystemPreferenceKey.LOOK_AND_FEEL_PREF, lookAndFeelClassName);
ClientSettings.LOOK_AND_FEEL_PREF.save(lookAndFeelClassName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
import java.util.stream.Collectors;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;

import games.strategy.debug.ClientLogger;
import games.strategy.engine.ClientContext;
import games.strategy.engine.ClientFileSystemHelper;
import games.strategy.triplea.ResourceLoader;
import games.strategy.triplea.settings.SystemPreferenceKey;
import games.strategy.triplea.settings.ClientSettings;
import games.strategy.triplea.settings.SystemPreferences;
import games.strategy.ui.SwingComponents;
import games.strategy.util.Version;
Expand All @@ -37,15 +38,16 @@ public boolean checkDownloadedMapsAreLatest() {
final int year = locaDateTime.get(ChronoField.YEAR);
final int month = locaDateTime.get(ChronoField.MONTH_OF_YEAR);
// format year:month
final String lastCheckTime = SystemPreferences.get(SystemPreferenceKey.TRIPLEA_LAST_CHECK_FOR_MAP_UPDATES, "");
if (lastCheckTime != null && lastCheckTime.trim().length() > 0) {
final String lastCheckTime = ClientSettings.TRIPLEA_LAST_CHECK_FOR_MAP_UPDATES.value();
if (!Strings.nullToEmpty(lastCheckTime).trim().isEmpty()) {
final String[] yearMonth = lastCheckTime.split(":");
if (Integer.parseInt(yearMonth[0]) >= year && Integer.parseInt(yearMonth[1]) >= month) {
return false;
}
}

SystemPreferences.put(SystemPreferenceKey.TRIPLEA_LAST_CHECK_FOR_MAP_UPDATES, year + ":" + month);
ClientSettings.TRIPLEA_LAST_CHECK_FOR_MAP_UPDATES.save(year + ":" + month);
ClientSettings.flush();

final List<DownloadFileDescription> allDownloads = ClientContext.getMapDownloadList();
final Collection<String> outOfDateMapNames = getOutOfDateMapNames(allDownloads);
Expand Down Expand Up @@ -151,12 +153,13 @@ private static TutorialMapPreferences getTutorialMapPreferences() {
return new TutorialMapPreferences() {
@Override
public void preventPromptToDownload() {
SystemPreferences.put(SystemPreferenceKey.TRIPLEA_PROMPT_TO_DOWNLOAD_TUTORIAL_MAP, false);
ClientSettings.TRIPLEA_PROMPT_TO_DOWNLOAD_TUTORIAL_MAP.save(false);
ClientSettings.flush();
}

@Override
public boolean canPromptToDownload() {
return SystemPreferences.get(SystemPreferenceKey.TRIPLEA_PROMPT_TO_DOWNLOAD_TUTORIAL_MAP, true);
return ClientSettings.TRIPLEA_PROMPT_TO_DOWNLOAD_TUTORIAL_MAP.booleanValue();
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public class ChangeToAutosaveClientAction extends AbstractAction {
private static final long serialVersionUID = 1972868158345085949L;
private final Component m_parent;
private final IClientMessenger m_clientMessenger;
private final SaveGameFileChooser.AUTOSAVE_TYPE m_typeOfAutosave;
private final SaveGameFileChooser.AutoSaveType m_typeOfAutosave;

public ChangeToAutosaveClientAction(final Component parent, final IClientMessenger clientMessenger,
final SaveGameFileChooser.AUTOSAVE_TYPE typeOfAutosave) {
final SaveGameFileChooser.AutoSaveType typeOfAutosave) {
super("Change To " + typeOfAutosave.toString().toLowerCase());
m_parent = JOptionPane.getFrameForComponent(parent);
m_clientMessenger = clientMessenger;
Expand Down
Loading

0 comments on commit 71bcf7a

Please sign in to comment.