//
// FileSelectionPanel.cs
//
// Copyright(c) 2011 Actia (China) Co., Ltd. All Rights Reserved.
//
// Owner: Vincent BRIARD
// Created on: 2011/4/28
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AppCore.UI.Interface;
using EnvCore;
using Utils.App;
using Utils.Resource;
using Utils.Sys;
using System.Xml;
using Utils.Model;
using AppCore;
using System.Collections;
namespace AppCore.UI.Settings
{
/// <summary>
/// Enum to define what kind of FileSelection is needed
/// </summary>
public enum FileSelType
{
Import,
Export,
}
public partial class FileSelectionPanel : UserControl, ILocalizable, ISettingControl
{
#region SetPtr
private IntPtr _SettingsPtr = IntPtr.Zero;
public void SetPtr(IntPtr Ptr)
{
_SettingsPtr = Ptr;
}
#endregion
#region attributes
/// <summary>
/// The type of the component
/// </summary>
private FileSelType fsl;
/// <summary>
/// The file selected
/// </summary>
private FileInfo selectedFile;
#endregion attributes
public FileSelectionPanel(FileSelType pType)
{
InitializeComponent();
this.Margin = new Padding(0, 0, 0, 0);
this.fsl = pType;
this.selectedFile = null;
}
#region ILocalizable Members
/// <summary>
/// Load localized content
/// </summary>
public void LoadLocalizedContent()
{
String strLblTitle, strLblTextB, strLblBtn;
//according to the type of the component, get the labels
switch (this.fsl)
{
case FileSelType.Export:
// set label title
strLblTitle = TranslationManager.Instance().GetString(55, "UI");
this.lblTitle.Text = strLblTitle;
// set label file txtbox
strLblTextB = TranslationManager.Instance().GetString(53, "UI");
this.lblTextBox.Text = strLblTextB;
// set label for export button
strLblBtn = TranslationManager.Instance().GetString(54, "UI");
this.btnAction.Text = strLblBtn;
break;
case FileSelType.Import:
// set label title
strLblTitle = TranslationManager.Instance().GetString(50, "UI");
this.lblTitle.Text = strLblTitle;
// set label file txtbox
strLblTextB = TranslationManager.Instance().GetString(51, "UI");
this.lblTextBox.Text = strLblTextB;
// set label for import button
strLblBtn = TranslationManager.Instance().GetString(52, "UI");
this.btnAction.Text = strLblBtn;
break;
}
// set label for browse button
String strLblBtnBrowse = TranslationManager.Instance().GetString(13, "UI");
this.btnBrowse.Text = strLblBtnBrowse;
}
#endregion
#region ISettingControl Members
public bool CanBeValidated()
{
if (this.txtbFileDir.Text != "")
{
return true;
}
else
{
return false;
}
}
#endregion
/// <summary>
/// set file path
/// </summary>
/// <param name="pPath"></param>
public void SetFilePath(String pPath)
{
this.txtbFileDir.Text = pPath;
}
///get file path
public String GetFilePath()
{
return this.txtbFileDir.Text;
}
/// <summary>
/// Method used to resize the content of a TabControl which font changed
/// </summary>
public void InitialFontSize()
{
this.Font = new Font(this.Font.FontFamily, 8.25f);
}
/// <summary>
/// Method called when the user click the browse button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBrowse_Click(object sender, EventArgs e)
{
try
{
FileDialog fileDialog = null;
FolderBrowserDialog folderDialog = null;
switch (this.fsl)
{
case FileSelType.Import:
fileDialog = new OpenFileDialog();
fileDialog.InitialDirectory = ApplicationManager.Instance().GetExecutionDirectory();
//TMP use the real extension for imported/exported settings files
fileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
fileDialog.FilterIndex = 2;
fileDialog.RestoreDirectory = true;
// display the dialog
if (fileDialog.ShowDialog() == DialogResult.OK)
{
try
{
String selectedFileName = fileDialog.FileName;
this.selectedFile = FileManager.Instance().GetFileInfoFromPath(selectedFileName);
}
catch (Exception ex)
{
String errorLoc = TranslationManager.Instance().GetString(10, "LOG");
LoggerManager.Instance().AppendString(errorLoc + ex.Message, LogLevel.LvlMax);
}
}
break;
case FileSelType.Export:
try
{
folderDialog = new FolderBrowserDialog();
folderDialog.Description = "請選擇您要導出的配置文件保存的文件夾";
folderDialog.ShowNewFolderButton = true;
// display the dialog
if (folderDialog.ShowDialog() == DialogResult.OK)
{
try
{
String selectedPath = folderDialog.SelectedPath;
FileStream fs = File.Create(selectedPath + "\\ApplicationSettings.xml");
fs.Close();
this.selectedFile = FileManager.Instance().GetFileInfoFromPath(selectedPath + "\\ApplicationSettings.xml");
}
catch (Exception ex)
{
String errorLoc = TranslationManager.Instance().GetString(10, "LOG");
LoggerManager.Instance().AppendString(errorLoc + ex.Message, LogLevel.LvlMax);
}
}
}
catch (Exception ex)
{
LoggerManager.Instance().AppendString(ex.Message, LogLevel.LvlMin);
Application.Instance().ShowCustomDiag(ex.Message);
}
break;
default:
break;
}
// update the textbox file name
this.UpdateSelectedFile();
}
catch (Exception ex)
{
LoggerManager.Instance().AppendString(ex.Message, LogLevel.LvlMin);
Application.Instance().ShowCustomDiag(ex.Message);
}
}
/// <summary>
/// Update the text filed after the file has been changed or chosen
/// </summary>
private void UpdateSelectedFile()
{
if (this.selectedFile != null)
{
this.txtbFileDir.Text = this.selectedFile.FullName;
}
}
protected string strXmlFile;
protected XmlDocument objXmlDoc = new XmlDocument();
//獲得所有子節點
string strReturn = string.Empty;
public string XmlFileAllNodes(string xmlpath, String XmlData)
{
string filename = this.txtbFileDir.Text;
XmlDataDocument MyXml = new XmlDataDocument();
if (XmlData == "")
{
MyXml.Load(xmlpath);//得到XML數據
}
else
{
MyXml.LoadXml(XmlData);
}
for (int i = 0; i < MyXml.DocumentElement.ChildNodes.Count; i++)
{
XmlNode cuxnl = MyXml.DocumentElement.ChildNodes[i];
if (cuxnl.NodeType == XmlNodeType.Element)
{
strReturn += cuxnl.Name + "{";
if (cuxnl.Attributes != null && cuxnl.Attributes.Count > 0)
{
foreach (XmlAttribute atr in cuxnl.Attributes)
{
strReturn += atr.LocalName + ",";
}
}
XmlFileAllNodes(xmlpath, MyXml.DocumentElement.ChildNodes[i].OuterXml);//得到這個XML文件下的所有子節點
strReturn += "}";
}
}
return strReturn;
}
public const int USER = 0x400;
public const int WM_REFRESH = USER + 101;
[System.Runtime.InteropServices.DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
IntPtr hWnd, // 窗體句柄
uint Msg, // 消息的標識符
uint wParam, // 具體取決於消息
uint lParam // 具體取決於消息
);
public SettingValueList GetSettingsManagement(SettingValueList psetList)
{
SettingsMapper settMapper = new SettingsMapper("Settings/UserSettings");
SettingValueList settingsValuesFromFile = settMapper.GetSettingsValuesFor(psetList);
return settingsValuesFromFile;
}
/// <summary>
/// Method called when the import/export button is clicked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAction_Click(object sender, EventArgs e)
{
try
{
// according to the type of selection
string fileName = this.txtbFileDir.Text;
XmlDocument xdoc = new XmlDocument();
String strPathSettFile = ApplicationStructureManager.Instance().GetResourceForModule("setting-file", "EnvCore");
string filepath1 = strPathSettFile;
string filepath = fileName;
SettingsPanel setpanel = new SettingsPanel();
switch (this.fsl)
{
// the file must exist
case FileSelType.Import:
// 打開xml,判斷節點數是否跟配置文件一致,更新xml
xdoc.Load(fileName);
XmlDocument xdoc1 = new XmlDocument();
xdoc1.Load(filepath1);
string str = XmlFileAllNodes(fileName, "");
strReturn = string.Empty;
string str1 = XmlFileAllNodes(filepath1, "");
strReturn = string.Empty;
if (str.Equals(str1))
{
xdoc1.InnerXml = xdoc.InnerXml;
xdoc1.Save(filepath1);
Application.Instance().GetMainWindows().GetSettingsPanel().LoadSettings();
MessageBox.Show("It's OK");
}
else
{
MessageBox.Show("Configuration is Error!");
}
break;
// the file can exist or be created
case FileSelType.Export:
//判斷要存儲的位置是否存在該文件 add by wlm
if (!File.Exists(this.txtbFileDir.Text))
{
FileStream lFileStream = File.Create(this.txtbFileDir.Text);
lFileStream.Close();
}
StreamReader strReader = new StreamReader(filepath1);
FileStream fs = new FileStream(this.txtbFileDir.Text, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("utf-8"));
sw.Write(strReader.ReadToEnd().ToString());
sw.Close();
fs.Close();
strReader.Close();
MessageBox.Show("Export is OK!");
break;
default:
break;
}
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}