Code to save to an XML file and fixing localization issues.

This commit is contained in:
Tim Young 2017-06-16 13:03:38 -05:00
parent 16f5c54ee9
commit b62155c76e
3 changed files with 145 additions and 61 deletions

View File

@ -322,11 +322,15 @@ namespace EduNetworkBuilder
/// Find the global random number generator.
/// </summary>
/// <returns>A valid random number generator</returns>
public static CultureInfo GetCulture()
public static CultureInfo GetCulture(NBSettings theSettings = null)
{
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
CultureInfo CI=null;
NBSettings oursettings = NB.GetSettings();
NBSettings oursettings;
if (theSettings == null) oursettings = NB.GetSettings();
else oursettings = theSettings;
if (myWin != null)
{
CI = myWin.GetCulture();
@ -419,13 +423,14 @@ namespace EduNetworkBuilder
/// </summary>
/// <param name="key">The key for the item we are translating</param>
/// <returns>A string of the translated information</returns>
public static string Translate(string key)
public static string Translate(string key, NBSettings theSettings=null)
{
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin == null)
{
ResourceManager RM = GetResource();
CultureInfo CI = GetCulture();
CultureInfo CI = GetCulture(theSettings);
string answer="";
answer = RM.GetString(key, CI);
if (answer == null) return "";
@ -467,11 +472,15 @@ namespace EduNetworkBuilder
return mystring;
}
public static void ChangeLanguage()
public static void ChangeLanguage(NBSettings theSettings=null)
{
//Find the window. If it exists, use /set the language setting there. If not, use / set the default.
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
NBSettings oursettings = NB.GetSettings();
NBSettings oursettings;
if (theSettings == null) oursettings = NB.GetSettings();
else oursettings = theSettings;
string lang = oursettings.ChosenLanguage;
if (lang == "") lang = "en";

View File

@ -3,6 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Reflection;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;
namespace EduNetworkBuilder
{
@ -26,9 +31,56 @@ namespace EduNetworkBuilder
public NBSettings()
{
if(NB.IsRunningOnMono())
//We need this for reflection
}
public NBSettings(NBSettings fromSettings)
{
if (fromSettings == null) return; //Break if we are going to error out.
Duplicate(fromSettings, this);
}
static void Duplicate(NBSettings FromClass, NBSettings ToClass)
{
if (FromClass == null) return; //Break if we are going to error out.
if (ToClass == null) return; //Break if we are going to error out.
//
ToClass.LastPath = FromClass.LastPath;
ToClass.AutoStartPuzzles = FromClass.AutoStartPuzzles;
ToClass.ChosenLanguage = FromClass.ChosenLanguage;
ToClass.LanguageHasBeenChosen = FromClass.LanguageHasBeenChosen;
ToClass.MainWindowX = FromClass.MainWindowX;
ToClass.MainWindowY = FromClass.MainWindowY;
ToClass.MainWindowHeight = FromClass.MainWindowHeight;
ToClass.MainWindowWidth = FromClass.MainWindowWidth;
foreach (string one in FromClass.ScoreList)
{
if (!ToClass.ScoreList.Contains(one))
ToClass.ScoreList.Add(one);
}
}
public NBSettings(bool UsingMono)
{
if(UsingMono)
{
try
{
//Try loading from xml file
string filename = GetFilename();
if (File.Exists(filename))
{
XmlSerializer sr = new XmlSerializer(this.GetType());
TextReader Reader = new StreamReader(filename);
NBSettings settings = (NBSettings)sr.Deserialize(Reader);
Duplicate(settings, this);
Reader.Close();
}
}
catch (Exception e)
{
MessageBox.Show("Error Loading Settings:\n" + e.ToString(),"Error Loading Settings");
}
}
else
{
@ -49,11 +101,34 @@ namespace EduNetworkBuilder
}
}
public void Save()
public static string GetFilename()
{
if(NB.IsRunningOnMono())
string BaseDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string FileName = "EduNetworkBuilder_config.xml";
return Path.Combine(BaseDir, FileName);
}
public void Save(bool UsingMono)
{
if(UsingMono)
{
//Save it to an XML file.
try
{
string filename = GetFilename();
if(!Directory.Exists(Path.GetDirectoryName(filename)))
{
Directory.CreateDirectory(Path.GetDirectoryName(filename));
}
XmlSerializer sr = new XmlSerializer(this.GetType());
TextWriter writer = new StreamWriter(filename);
sr.Serialize(writer, this);
writer.Close();
}
catch(Exception e)
{
MessageBox.Show("ERROR Saving Settings:\n" + e.ToString(),"Error saving settings.");
}
}
else
{

View File

@ -19,7 +19,7 @@ namespace EduNetworkBuilder
public partial class BuilderWindow : Form
{
public Random GameRandomGen = new Random();
public NBSettings OurSettings = new NBSettings(); //This will auto-load the settings
public NBSettings OurSettings = new NBSettings(NB.IsRunningOnMono()); //This will auto-load the settings
private int LastPacketID=1;
public DebugPausePoint DebugSetting = DebugPausePoint.none;
@ -58,7 +58,7 @@ namespace EduNetworkBuilder
LastPath = OurSettings.LastPath;
if(!OurSettings.LanguageHasBeenChosen)
NB.ChangeLanguage();
NB.ChangeLanguage(OurSettings);
LanguagifyComponents();
//I never implimented cut/copy/paste/undo. So we will remove them since they do nothing anyway
cutToolStripMenuItem.Visible = false;
@ -72,12 +72,12 @@ namespace EduNetworkBuilder
// Force the ToolTip text to be displayed whether or not the form is active.
myTooltip.ShowAlways = true;
myTooltip.SetToolTip(rbHelp1, NB.Translate("NB_BuildWindNone"));
myTooltip.SetToolTip(rbHelp2, NB.Translate("NB_BuildWindColor"));
myTooltip.SetToolTip(rbHelp3, NB.Translate("NB_BuildWindMinor"));
myTooltip.SetToolTip(rbHelp4, NB.Translate("NB_BuildWindDecent"));
myTooltip.SetToolTip(btnHelp, NB.Translate("NB_BuildWindMsg"));
myTooltip.SetToolTip(cbViewTitles, NB.Translate("NB_cbViewTitles"));
myTooltip.SetToolTip(rbHelp1, NB.Translate("NB_BuildWindNone", OurSettings));
myTooltip.SetToolTip(rbHelp2, NB.Translate("NB_BuildWindColor", OurSettings));
myTooltip.SetToolTip(rbHelp3, NB.Translate("NB_BuildWindMinor", OurSettings));
myTooltip.SetToolTip(rbHelp4, NB.Translate("NB_BuildWindDecent", OurSettings));
myTooltip.SetToolTip(btnHelp, NB.Translate("NB_BuildWindMsg", OurSettings));
myTooltip.SetToolTip(cbViewTitles, NB.Translate("NB_cbViewTitles", OurSettings));
myTooltip.Popup += myTooltip_Popup;
lblStatus.Text = "";
@ -111,47 +111,47 @@ namespace EduNetworkBuilder
private void LanguagifyComponents()
{
msMainMenuStrip.Text = NB.Translate("NB_msMainMenuStrip");
fileToolStripMenuItem.Text = NB.Translate("NB_fileToolStripMenuItem");
newToolStripMenuItem.Text = NB.Translate("LBW_btnAdd");
loadToolStripMenuItem.Text = NB.Translate("_Load");
reloadToolStripMenuItem.Text = NB.Translate("NB_reloadToolStripMenuItem");
saveToolStripMenuItem.Text = NB.Translate("NB_saveToolStripMenuItem");
exitToolStripMenuItem.Text = NB.Translate("NB_exitToolStripMenuItem");
editToolStripMenuItem.Text = NB.Translate("_Edit");
cutToolStripMenuItem.Text = NB.Translate("NB_cutToolStripMenuItem");
copyToolStripMenuItem.Text = NB.Translate("NB_copyToolStripMenuItem");
pasteToolStripMenuItem.Text = NB.Translate("NB_pasteToolStripMenuItem");
undoToolStripMenuItem.Text = NB.Translate("NB_undoToolStripMenuItem");
optionsToolStripMenuItem.Text = NB.Translate("NB_optionsToolStripMenuItem");
allToolStripMenuItem.Text = NB.Translate("_All");
dHCPRequestToolStripMenuItem.Text = NB.Translate("NB_NetViewDHCP");
clearArpTableToolStripMenuItem.Text = NB.Translate("NB_NetViewClr");
clearIPsToolStripMenuItem.Text = NB.Translate("NB_clearIPsToolStripMenuItem");
pingToolStripMenuItem.Text = NB.Translate("_Ping");
helpToolStripMenuItem.Text = NB.Translate("_Help");
helpToolStripMenuItem1.Text = NB.Translate("_Help");
aboutToolStripMenuItem.Text = NB.Translate("NB_aboutToolStripMenuItem");
releaseNotesToolStripMenuItem.Text = NB.Translate("NB_releaseNotesToolStripMenuItem");
checkForUpdatesToolStripMenuItem.Text = NB.Translate("NB_checkForUpdatesToolStripMenuItem");
samplesToolStripMenuItem.Text = NB.Translate("NB_samplesToolStripMenuItem");
puzzlesToolStripMenuItem.Text = NB.Translate("NB_puzzlesToolStripMenuItem");
solvedToolStripMenuItem.Text = NB.Translate("_Solved");
dHCPToolStripMenuItem.Text = NB.Translate("_DHCP");
oneNetworkToolStripMenuItem.Text = NB.Translate("NB_OneNetwork");
twoNetworksToolStripMenuItem.Text = NB.Translate("NB_TwoNetworks");
threeNetworksToolStripMenuItem.Text = NB.Translate("NB_ThreeNetworks");
firewallsToolStripMenuItem.Text = NB.Translate("NB_Firewalls");
toSolveToolStripMenuItem.Text = NB.Translate("NB_toSolveToolStripMenuItem");
solvedDHCPToolStripMenuItem.Text = NB.Translate("_DHCP");
solvedOneNetworkToolStripMenuItem.Text = NB.Translate("NB_OneNetwork");
solvedTwoNetworksToolStripMenuItem.Text = NB.Translate("NB_TwoNetworks");
SolvedThreeNetworksToolStripMenuItem.Text = NB.Translate("NB_ThreeNetworks");
firewallsToolStripMenuItem1.Text = NB.Translate("NB_Firewalls");
lblStatus.Text = NB.Translate("NB_lblStatus");
btnHelp.Text = NB.Translate("NB_btnHelp");
changeLanguageToolStripMenuItem.Text = NB.Translate("NB_changeLanguageToolStripMenuItem");
Text = NB.Translate("NB_Form");
msMainMenuStrip.Text = NB.Translate("NB_msMainMenuStrip", OurSettings);
fileToolStripMenuItem.Text = NB.Translate("NB_fileToolStripMenuItem", OurSettings);
newToolStripMenuItem.Text = NB.Translate("LBW_btnAdd", OurSettings);
loadToolStripMenuItem.Text = NB.Translate("_Load", OurSettings);
reloadToolStripMenuItem.Text = NB.Translate("NB_reloadToolStripMenuItem", OurSettings);
saveToolStripMenuItem.Text = NB.Translate("NB_saveToolStripMenuItem", OurSettings);
exitToolStripMenuItem.Text = NB.Translate("NB_exitToolStripMenuItem", OurSettings);
editToolStripMenuItem.Text = NB.Translate("_Edit", OurSettings);
cutToolStripMenuItem.Text = NB.Translate("NB_cutToolStripMenuItem", OurSettings);
copyToolStripMenuItem.Text = NB.Translate("NB_copyToolStripMenuItem", OurSettings);
pasteToolStripMenuItem.Text = NB.Translate("NB_pasteToolStripMenuItem", OurSettings);
undoToolStripMenuItem.Text = NB.Translate("NB_undoToolStripMenuItem", OurSettings);
optionsToolStripMenuItem.Text = NB.Translate("NB_optionsToolStripMenuItem", OurSettings);
allToolStripMenuItem.Text = NB.Translate("_All", OurSettings);
dHCPRequestToolStripMenuItem.Text = NB.Translate("NB_NetViewDHCP", OurSettings);
clearArpTableToolStripMenuItem.Text = NB.Translate("NB_NetViewClr", OurSettings);
clearIPsToolStripMenuItem.Text = NB.Translate("NB_clearIPsToolStripMenuItem", OurSettings);
pingToolStripMenuItem.Text = NB.Translate("_Ping", OurSettings);
helpToolStripMenuItem.Text = NB.Translate("_Help", OurSettings);
helpToolStripMenuItem1.Text = NB.Translate("_Help", OurSettings);
aboutToolStripMenuItem.Text = NB.Translate("NB_aboutToolStripMenuItem", OurSettings);
releaseNotesToolStripMenuItem.Text = NB.Translate("NB_releaseNotesToolStripMenuItem", OurSettings);
checkForUpdatesToolStripMenuItem.Text = NB.Translate("NB_checkForUpdatesToolStripMenuItem", OurSettings);
samplesToolStripMenuItem.Text = NB.Translate("NB_samplesToolStripMenuItem", OurSettings);
puzzlesToolStripMenuItem.Text = NB.Translate("NB_puzzlesToolStripMenuItem", OurSettings);
solvedToolStripMenuItem.Text = NB.Translate("_Solved", OurSettings);
dHCPToolStripMenuItem.Text = NB.Translate("_DHCP", OurSettings);
oneNetworkToolStripMenuItem.Text = NB.Translate("NB_OneNetwork", OurSettings);
twoNetworksToolStripMenuItem.Text = NB.Translate("NB_TwoNetworks", OurSettings);
threeNetworksToolStripMenuItem.Text = NB.Translate("NB_ThreeNetworks", OurSettings);
firewallsToolStripMenuItem.Text = NB.Translate("NB_Firewalls", OurSettings);
toSolveToolStripMenuItem.Text = NB.Translate("NB_toSolveToolStripMenuItem", OurSettings);
solvedDHCPToolStripMenuItem.Text = NB.Translate("_DHCP", OurSettings);
solvedOneNetworkToolStripMenuItem.Text = NB.Translate("NB_OneNetwork", OurSettings);
solvedTwoNetworksToolStripMenuItem.Text = NB.Translate("NB_TwoNetworks", OurSettings);
SolvedThreeNetworksToolStripMenuItem.Text = NB.Translate("NB_ThreeNetworks", OurSettings);
firewallsToolStripMenuItem1.Text = NB.Translate("NB_Firewalls", OurSettings);
lblStatus.Text = NB.Translate("NB_lblStatus", OurSettings);
btnHelp.Text = NB.Translate("NB_btnHelp", OurSettings);
changeLanguageToolStripMenuItem.Text = NB.Translate("NB_changeLanguageToolStripMenuItem", OurSettings);
Text = NB.Translate("NB_Form", OurSettings);
}
public ResourceManager GetResource()
@ -401,7 +401,7 @@ namespace EduNetworkBuilder
UpdateVisuals();
processing = true;
cbViewTitles.Checked = myNetwork.ShowLabelsHere;
Text = NB.Translate("NB_UpdteFrmName");
Text = NB.Translate("NB_UpdteFrmName", OurSettings);
if (myNetwork.NetTitle.GetText() != "")
Text += ": " + myNetwork.NetTitle.GetText();
if (myNetwork.NetMessage.GetText() != "")
@ -1434,7 +1434,7 @@ namespace EduNetworkBuilder
private void BuilderWindow_FormClosing(object sender, FormClosingEventArgs e)
{
//The values in the settings are all up-to-date. Just tell them to save.
OurSettings.Save();
OurSettings.Save(NB.IsRunningOnMono());
}
private void dHCPRequestToolStripMenuItem_Click(object sender, EventArgs e)