- Issue 106: Refactor Gui_NewGame. Moved preferences to FModel.

* Updated former references to preferences to use FModel in forge.GuiDisplay4 and in forge.view.swing.OldGuiNewGame.
* Added simple unit test.

Bug: 106
This commit is contained in:
Braids
2011-08-13 02:38:01 +00:00
parent e6032d1825
commit 5a3f6d312c
6 changed files with 67 additions and 25 deletions

View File

@@ -1175,7 +1175,7 @@ public class GuiDisplay4 extends JFrame implements CardContainer, Display, NewCo
* @return a boolean.
*/
public boolean loadPrefs() {
ForgePreferences fp = OldGuiNewGame.preferences;
ForgePreferences fp = Singletons.getModel().getPreferences();
cbAIUpkeep.setSelected(fp.bAIUpkeep);
cbAIDraw.setSelected(fp.bAIDraw);
@@ -1201,7 +1201,7 @@ public class GuiDisplay4 extends JFrame implements CardContainer, Display, NewCo
*/
public boolean savePrefs() {
Constant.Runtime.Mill[0] = canLoseByDecking.isSelected();
ForgePreferences fp = OldGuiNewGame.preferences;
ForgePreferences fp = Singletons.getModel().getPreferences();
fp.bAIUpkeep = cbAIUpkeep.isSelected();
fp.bAIDraw = cbAIDraw.isSelected();

View File

@@ -2,8 +2,12 @@ package forge;
/**
* Contains a main delegate; this class used to be Forge's entry point.
*
* The class that used to be here is presently at
* {@link forge.view.swing.OldGuiNewGame}, which is slowly being refactored out
* of existence.
*/
public class Gui_NewGame {
public final class Gui_NewGame {
/**
* Do not instantiate.
@@ -15,11 +19,9 @@ public class Gui_NewGame {
/**
* This is a delegate.
*
* @deprecated use forge.view.swing.Main.main
*
* @see forge.view.swing.Main.main
*
* @param args from the OS or command line
*
* @deprecated use {@link forge.view.swing.Main.main}
*/
public static void main(final String[] args) {
forge.view.swing.Main.main(args);

View File

@@ -7,6 +7,8 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import forge.properties.ForgePreferences;
//import net.slightlymagic.braids.util.progress_monitor.BaseProgressMonitor;
import net.slightlymagic.braids.util.progress_monitor.BraidsProgressMonitor;
import arcane.util.MultiplexOutputStream;
@@ -27,6 +29,7 @@ public class FModel {
private final transient PrintStream oldSystemOut;
private final transient PrintStream oldSystemErr;
private BuildInfo buildInfo;
private ForgePreferences preferences;
/**
* Constructor.
@@ -61,6 +64,12 @@ public class FModel {
oldSystemErr = System.err;
System.setErr(new PrintStream(new MultiplexOutputStream(System.err, logFileStream), true));
try {
setPreferences(new ForgePreferences("forge.preferences"));
} catch (Exception exn) {
throw new RuntimeException(exn);
}
setBuildInfo(new BuildInfo());
}
@@ -106,4 +115,19 @@ public class FModel {
this.buildInfo = neoBuildInfo;
}
/**
* @return the preferences
*/
public final ForgePreferences getPreferences() {
return preferences;
}
/**
* @param neoPreferences the preferences to set
*/
public final void setPreferences(final ForgePreferences neoPreferences) {
this.preferences = neoPreferences;
}
}

View File

@@ -50,8 +50,8 @@ public final class Main {
ForgePreferences preferences = null;
try {
OldGuiNewGame.preferences = new ForgePreferences("forge.preferences");
preferences = OldGuiNewGame.preferences;
preferences = model.getPreferences();
OldGuiNewGame.useLAFFonts.setSelected(preferences.lafFonts);
// newGuiCheckBox.setSelected(preferences.newGui);
@@ -77,8 +77,8 @@ public final class Main {
Constant.Runtime.UpldDrft[0] = false;
}
} catch (Exception e) {
Log.error("Error loading preferences");
} catch (Exception exn) {
Log.error("Error loading preferences: " + exn);
}
OldGuiNewGame.loadDynamicGamedata();

View File

@@ -65,6 +65,7 @@ import forge.ImageCache;
import forge.MyRandom;
import forge.PlayerType;
import forge.SealedDeck;
import forge.Singletons;
import forge.deck.Deck;
import forge.deck.DeckManager;
import forge.deck.generate.Generate2ColorDeck;
@@ -168,9 +169,6 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
private final Action DNLD_PRICES_ACTION = new DownloadPriceAction();
private final Action BUGZ_REPORTER_ACTION = new BugzReporterAction();
/** Constant <code>preferences</code>. */
public static ForgePreferences preferences;
/**
* <p>
* Constructor for OldGuiNewGame.
@@ -528,7 +526,7 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
devModeCheckBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(final ActionEvent e) {
Constant.Runtime.DevMode[0] = devModeCheckBox.isSelected();
preferences.developerMode = Constant.Runtime.DevMode[0];
Singletons.getModel().getPreferences().developerMode = Constant.Runtime.DevMode[0];
}
});
@@ -540,7 +538,7 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
upldDrftCheckBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(final ActionEvent e) {
Constant.Runtime.UpldDrft[0] = upldDrftCheckBox.isSelected();
preferences.uploadDraftAI = Constant.Runtime.UpldDrft[0];
Singletons.getModel().getPreferences().uploadDraftAI = Constant.Runtime.UpldDrft[0];
}
});
@@ -549,7 +547,7 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
foilRandomCheckBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(final ActionEvent e) {
Constant.Runtime.RndCFoil[0] = foilRandomCheckBox.isSelected();
preferences.randCFoil = Constant.Runtime.RndCFoil[0];
Singletons.getModel().getPreferences().randCFoil = Constant.Runtime.RndCFoil[0];
}
});
@@ -1321,7 +1319,7 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
return;
}
// UIManager.setLookAndFeel(info[index].getClassName());
preferences.laf = LAFMap.get(name);
Singletons.getModel().getPreferences().laf = LAFMap.get(name);
UIManager.setLookAndFeel(LAFMap.get(name));
SwingUtilities.updateComponentTreeUI(c);
@@ -1512,7 +1510,7 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
* @param index
*/
public static void set(final int index) {
preferences.cardSize = CardSizeType.valueOf(keys[index].toLowerCase());
Singletons.getModel().getPreferences().cardSize = CardSizeType.valueOf(keys[index].toLowerCase());
Constant.Runtime.width[0] = widths[index];
Constant.Runtime.height[0] = heights[index];
}
@@ -1522,7 +1520,7 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
* @param s
*/
public static void set(final CardSizeType s) {
preferences.cardSize = s;
Singletons.getModel().getPreferences().cardSize = s;
int index = 0;
for (String str : keys) {
if (str.toLowerCase().equals(s.toString())) {
@@ -1580,7 +1578,7 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
* @param index
*/
public static void set(final int index) {
preferences.maxStackSize = values[index];
Singletons.getModel().getPreferences().maxStackSize = values[index];
Constant.Runtime.stackSize[0] = values[index];
}
@@ -1589,7 +1587,7 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
* @param val
*/
public static void setVal(final int val) {
preferences.maxStackSize = val;
Singletons.getModel().getPreferences().maxStackSize = val;
Constant.Runtime.stackSize[0] = val;
}
}
@@ -1636,7 +1634,7 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
* @param index
*/
public static void set(final int index) {
preferences.stackOffset = StackOffsetType.valueOf(keys[index].toLowerCase());
Singletons.getModel().getPreferences().stackOffset = StackOffsetType.valueOf(keys[index].toLowerCase());
Constant.Runtime.stackOffset[0] = offsets[index];
}
@@ -1645,7 +1643,7 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
* @param s
*/
public static void set(final StackOffsetType s) {
preferences.stackOffset = s;
Singletons.getModel().getPreferences().stackOffset = s;
int index = 0;
for (String str : keys) {
if (str.toLowerCase().equals(s.toString())) {
@@ -1742,6 +1740,7 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
*/
public final boolean exit() {
try {
ForgePreferences preferences = Singletons.getModel().getPreferences();
preferences.laf = UIManager.getLookAndFeel().getClass().getName();
preferences.lafFonts = useLAFFonts.isSelected();
// preferences.newGui = newGuiCheckBox.isSelected();

View File

@@ -7,6 +7,8 @@ import java.io.FileNotFoundException;
import org.testng.annotations.Test;
import forge.properties.ForgePreferences;
/**
* Tests FModel.
*/
@@ -55,4 +57,19 @@ public class FModelTest {
model.close();
}
/**
* Test getPreferences.
* @throws FileNotFoundException indirectly
*/
@Test
public final void test_getPreferences() throws FileNotFoundException {
final FModel model = new FModel(null);
try {
ForgePreferences prefs = model.getPreferences();
assertNotNull(prefs, "prefs instance is not null");
} finally {
model.close();
}
}
}