using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using GZTW.AyaNova.BLL;
namespace AylXML
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog dlgOpen;
private System.Windows.Forms.MainMenu mnu;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem mnuOpen;
private System.Windows.Forms.DataGrid grid;
private System.Windows.Forms.MenuItem mnuSaveAsXML;
private System.Windows.Forms.SaveFileDialog dlgSave;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.dlgOpen = new System.Windows.Forms.OpenFileDialog();
this.mnu = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.mnuOpen = new System.Windows.Forms.MenuItem();
this.mnuSaveAsXML = new System.Windows.Forms.MenuItem();
this.grid = new System.Windows.Forms.DataGrid();
this.dlgSave = new System.Windows.Forms.SaveFileDialog();
((System.ComponentModel.ISupportInitialize)(this.grid)).BeginInit();
this.SuspendLayout();
//
// mnu
//
this.mnu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuOpen,
this.mnuSaveAsXML});
this.menuItem1.Text = "&File";
//
// mnuOpen
//
this.mnuOpen.Index = 0;
this.mnuOpen.Text = "&Open";
this.mnuOpen.Click += new System.EventHandler(this.mnuOpen_Click);
//
// mnuSaveAsXML
//
this.mnuSaveAsXML.Enabled = false;
this.mnuSaveAsXML.Index = 1;
this.mnuSaveAsXML.Text = "Save to XML";
this.mnuSaveAsXML.Click += new System.EventHandler(this.mnuSaveAsXML_Click);
//
// grid
//
this.grid.DataMember = "";
this.grid.Dock = System.Windows.Forms.DockStyle.Fill;
this.grid.FlatMode = true;
this.grid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.grid.Location = new System.Drawing.Point(0, 0);
this.grid.Name = "grid";
this.grid.PreferredColumnWidth = 100;
this.grid.ReadOnly = true;
this.grid.Size = new System.Drawing.Size(608, 409);
this.grid.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(608, 409);
this.Controls.Add(this.grid);
this.Menu = this.mnu;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.grid)).EndInit();
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
// __ _ _ ___ ____ __ __ __ __ __ ___ ___
// / _)( )( )/ __)(_ _)/ \( \/ ) / _)/ \( \( _)
// ( (_ )()( \__ \ )( ( () )) ( ( (_( () )) ) )) _)
// \__) \__/ (___/ (__) \__/(_/\/\_) \__)\__/(___/(___)
//
private DataTable dt=null;
private void Form1_Load(object sender, System.EventArgs e)
{
dt=new DataTable("LT");
//setup the columns
dt.Columns.Add("Locale",typeof(string));
dt.Columns.Add("Key",typeof(string));
dt.Columns.Add("DisplayText",typeof(string));
dt.PrimaryKey=new DataColumn[]{dt.Columns[0],dt.Columns[1]};
grid.DataSource=dt;
}
private void mnuOpen_Click(object sender, System.EventArgs e)
{
dlgOpen.Filter=
"Localized text file|*.ayl";
if(dlgOpen.ShowDialog()==DialogResult.OK)
{
Import(dlgOpen.FileName);
}
this.mnuSaveAsXML.Enabled=true;
}
private void Import(string path)
{
LocalePortable lp =null;
FileStream fs = new FileStream(path, FileMode.Open);
try
{
// Construct a BinaryFormatter and use it to de-serialize the data from the stream.
BinaryFormatter formatter = new BinaryFormatter();
formatter.AssemblyFormat=System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
lp=(LocalePortable)formatter.Deserialize(fs);
}
catch (SerializationException e)
{
MessageBox.Show("Failed to deserialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
dt.Clear();
this.Refresh();
Cursor.Current=Cursors.WaitCursor;
foreach(LocalizedTextPortable o in lp.Items)
{
dt.Rows.Add(new object[] {lp.Locale,o.Key,o.DisplayText});
}
}
private void Export(string path)
{
DataSet ds=new DataSet();
ds.Tables.Add(dt);
ds.WriteXml(path,XmlWriteMode.WriteSchema);
}
private void mnuSaveAsXML_Click(object sender, System.EventArgs e)
{
dlgSave.FileName=dt.Rows[0]["Locale"].ToString();
dlgSave.Filter=
"XML file|*.xml";
dlgSave.RestoreDirectory=true;
dlgSave.FilterIndex=1;
if(dlgSave.ShowDialog()==DialogResult.OK)
{
Cursor.Current=Cursors.WaitCursor;
Export(dlgSave.FileName);
}
}
// ///
// /// Contains a portable Locale
// ///
// ///
// /// Used for serializing and de-serializing a locale
// /// (i.e. importing and exporting locales outside the program)
// ///
// [Serializable]
// public class LocalePortable
// {
//
// private ArrayList mItems;
// public ArrayList Items{get{return mItems;}set{mItems=value;}}
//
// private string mLocale;
// public string Locale{get{return mLocale;}set{mLocale=value;}}
//
//
// }
//
//
//
// ///
// /// Contains a portable Localized text object data
// ///
// ///
// /// Used for serializing and de-serializing a locale
// /// (i.e. importing and exporting locales outside the program)
// ///
// [Serializable]
// public class LocalizedTextPortable
// {
//
// private string mKey;
// public string Key{get{return mKey;}set{mKey=value;}}
//
// private string mDisplayText;
// public string DisplayText{get{return mDisplayText;}set{mDisplayText=value;}}
//
//
// }
//------------------------
}
}