126 lines
3.6 KiB
C#
126 lines
3.6 KiB
C#
///
|
|
/// Copyright (c) 2016 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Xml.Serialization;
|
|
using System.Windows.Forms;
|
|
using ResultsBrowser.Resources;
|
|
|
|
namespace ResultsBrowser
|
|
{
|
|
/// <summary>
|
|
/// Class to be serialized to an XML file...
|
|
/// Public members are filters / parts of a query.
|
|
/// </summary>
|
|
[XmlRootAttribute("Query")]
|
|
public class FiltersConfig
|
|
{
|
|
public string[] FilterID;
|
|
public string[] FilterData;
|
|
|
|
[XmlIgnore]
|
|
public int FiltersCount { get { return Math.Min((FilterID != null) ? FilterID.Length : 0,
|
|
(FilterData != null) ? FilterData.Length : 0); } }
|
|
|
|
public string[] ResultItems;
|
|
|
|
public string PrinterClass;
|
|
public string PrinterCfg;
|
|
|
|
[XmlArrayAttribute("ResultsClmnWidths")]
|
|
public int[] ResultsClmnWidths;
|
|
[XmlIgnore]
|
|
public int ResultsClmnCount { get { return (ResultsClmnWidths != null) ? ResultsClmnWidths.Length : 0; } }
|
|
|
|
|
|
public FiltersConfig()
|
|
{
|
|
}
|
|
|
|
public FiltersConfig(int count)
|
|
{
|
|
if (count > 0)
|
|
{
|
|
FilterID = new string[count];
|
|
FilterData = new string[count];
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load public fields of this class from the XML file.
|
|
/// </summary>
|
|
public static FiltersConfig Load(string fileName)
|
|
{
|
|
try
|
|
{
|
|
using (TextReader reader = new StreamReader(fileName))
|
|
{
|
|
FiltersConfig ls = (new XmlSerializer(typeof(FiltersConfig))).Deserialize(reader) as FiltersConfig;
|
|
// Copy the settings just in case De-serialize() completes OK
|
|
return ls;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
string msg = e.Message;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates public arrays FilterID[] and FilterData[] of this serializable class.
|
|
/// </summary>
|
|
/// <param name="filters"></param>
|
|
/// <returns></returns>
|
|
public bool UpdateFromFilters(IList<Filters.IFilter> filters)
|
|
{
|
|
try
|
|
{
|
|
int cnt = filters.Count;
|
|
FilterID = new string[cnt];
|
|
FilterData = new string[cnt];
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
for (int i = 0; i < cnt; i++)
|
|
{
|
|
FilterID[i] = filters[i].GetThisFilterID();
|
|
filters[i].UI2Data();
|
|
sb.Clear();
|
|
filters[i].Save(sb);
|
|
FilterData[i] = sb.ToString();
|
|
}
|
|
|
|
return true; /// All OK
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
MessageBox.Show(exc.Message, Strings.Error_serializing_filters, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Save public fields of this class to the XML file.
|
|
/// </summary>
|
|
public bool Save(string fileName)
|
|
{
|
|
try
|
|
{
|
|
using (TextWriter writer = new StreamWriter(fileName))
|
|
{
|
|
(new XmlSerializer(typeof(FiltersConfig))).Serialize(writer, this);
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
MessageBox.Show(exc.Message, Strings.Error_saving_file, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|