zaaReloaded2/zaaReloaded2/Controller/Settings.cs
Daniel Kraus cde32f0a7e Implement styling of abnormal values.
- NEU: Auszeichnung pathologischer Werte kann eingestellt werden.
2015-08-24 23:17:49 +02:00

243 lines
8.1 KiB
C#
Executable File

/* Settings.cs
* part of zaaReloaded2
*
* Copyright 2015 Daniel Kraus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using zaaReloaded2.Formatter;
using zaaReloaded2.Controller.Elements;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
namespace zaaReloaded2.Controller
{
/// <summary>
/// Holds settings related to controlling laboratory output.
/// </summary>
[Serializable]
public class Settings : ICloneable, ISerializable
{
#region Persistence
/// <summary>
/// Deserializes a Settings object from SOAP XML that is read from a file.
/// </summary>
/// <param name="fileName">File to read.</param>
/// <returns>Deserialized Settings object with new unique ID.</returns>
/// <exception cref="InvalidDataException">if the file does not contain
/// SOAP XML that can be deserialized to a Settings object.</exception>
public static Settings LoadFromFile(string fileName)
{
StreamReader reader = new StreamReader(fileName);
SoapFormatter formatter = new SoapFormatter();
Settings settings = formatter.Deserialize(reader.BaseStream) as Settings;
if (settings == null)
{
throw new InvalidDataException("Datei enthält keine Stil-Daten oder ist beschädigt.");
}
settings.Uid = Guid.NewGuid();
return settings;
}
/// <summary>
/// Serializes a Settings object to SOAP XML that is written to a file.
/// </summary>
/// <param name="fileName">File to write to.</param>
public void SaveToFile(string fileName)
{
StreamWriter writer = new StreamWriter(fileName);
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(writer.BaseStream, this);
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the name of these settings.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the reference style.
/// </summary>
public ReferenceStyle ReferenceStyle { get; set; }
/// <summary>
/// Gets or sets the style in which abnormal values
/// are formatted.
/// </summary>
public AbnormalStyle AbnormalStyle { get; set; }
/// <summary>
/// Gets the list of controlling elements.
/// </summary>
public IList<ElementBase> Elements { get; private set; }
/// <summary>
/// Gets the unique ID of this Settings object. The unique
/// ID is not included in deep-copying (cloning).
/// </summary>
public Guid Uid { get; internal set; }
#endregion
#region Constructors
public Settings()
: this(string.Empty, null)
{ }
public Settings(string name)
: this(name, null)
{ }
/// <summary>
/// Creates a new Settings object with an initial
/// set of elements.
/// </summary>
/// <param name="initialElements">Set of ElementBase
/// object (or derived ones).</param>
public Settings(IList<ElementBase> initialElements)
: this(String.Empty, initialElements)
{ }
/// <summary>
/// Creates a new Settings object with an initial
/// set of elements and a name.
/// </summary>
/// <param name="initialElements">Set of ElementBase
/// object (or derived ones).</param>
/// <param name="name">Name of these settings.</param>
public Settings(string name, IList<ElementBase> initialElements)
{
Uid = Guid.NewGuid();
Name = name;
Elements = initialElements;
ReferenceStyle = Properties.Settings.Default.ReferenceStyle;
if (Elements == null)
{
Elements = new List<ElementBase>();
}
}
/// <summary>
/// Creates a new Settings object with an initial
/// set of elements, a name, and a unique ID.
/// </summary>
/// <param name="initialElements">Set of ElementBase
/// object (or derived ones).</param>
/// <param name="name">Name of these settings.</param>
/// <param name="Uid">Unique ID for this Settings object.</param>
public Settings(string name, IList<ElementBase> initialElements, Guid uid)
: this(name, initialElements)
{
Uid = uid;
}
/// <summary>
/// Deserialization constructor.
/// </summary>
protected Settings(SerializationInfo info, StreamingContext context)
{
int version = info.GetInt32("Version");
Uid = (Guid)info.GetValue("Uid", typeof(Guid));
Name = info.GetString("Name");
ReferenceStyle = (ReferenceStyle)info.GetValue("ReferenceStyle", typeof(ReferenceStyle));
if (version >= 2)
{
AbnormalStyle = (AbnormalStyle)info.GetValue("AbnormalStyle", typeof(AbnormalStyle));
}
int elementsCount = info.GetInt32("ElementsCount");
Elements = new List<ElementBase>();
for (int i = 0; i < elementsCount; i++)
{
Type type = info.GetValue(SerializationElementName(i, "Type"), typeof(Type)) as Type;
ElementBase element = info.GetValue(SerializationElementName(i, "Object"), type) as ElementBase;
Elements.Add(element);
}
}
#endregion
#region Public methods
/// <summary>
/// Adds an element to the list of Elements.
/// </summary>
/// <param name="element">Element to add.</param>
public void AddElement(ElementBase element)
{
Elements.Add(element);
}
#endregion
#region Implementation of ICloneable
public object Clone()
{
Settings clone = new Settings(
String.Format("Kopie von {0}", Name),
Elements.Select(e => e.Clone() as ElementBase).ToList()
);
clone.ReferenceStyle = ReferenceStyle;
clone.AbnormalStyle = AbnormalStyle;
return clone;
}
#endregion
#region Implementation of ISerializable
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Version", Properties.Settings.Default.SerializationVersion);
info.AddValue("Uid", Uid);
info.AddValue("Name", Name);
info.AddValue("ReferenceStyle", ReferenceStyle);
info.AddValue("AbnormalStyle", AbnormalStyle);
info.AddValue("ElementsCount", Elements.Count);
int i = 0;
foreach (ElementBase e in Elements)
{
info.AddValue(SerializationElementName(i, "Type"), e.GetType());
info.AddValue(SerializationElementName(i, "Object"), e);
i++;
}
}
string SerializationElementName(int index, string info)
{
return String.Format("Element{0}{1}", index, info);
}
#endregion
#region Fields
// Defines header lines for the persistence file: "[My name]" and so on.
static readonly Regex _persistenceHeaderRegex = new Regex(@"^\[(?<name>.+?)]$");
#endregion
}
}