zaaReloaded2/zaaReloaded2/Formatter/ItemFormatter.cs

151 lines
4.7 KiB
C#
Executable File

/* ItemFormatter.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 Microsoft.Office.Interop.Word;
using zaaReloaded2.LabModel;
namespace zaaReloaded2.Formatter
{
/// <summary>
/// Wraps a <see cref="LabItem"/> and provides methods to format it.
/// </summary>
public class ItemFormatter
{
#region Properties
/// <summary>
/// Gets or sets the LabItem wrapped by this ItemFormatter.
/// </summary>
public LabItem LabItem { get; set; }
/// <summary>
/// Gets or sets the ReferenceStyle to use in formatting.
/// </summary>
public ReferenceStyle ReferenceStyle { get; set; }
/// <summary>
/// Gets or sets a flag that indicates whether this ItemFormatter
/// has been used, i.e. whether the LabItem was written to a
/// document.
/// </summary>
public bool HasBeenUsed { get; set; }
#endregion
#region Constructor
/// <summary>
/// Creates a new ItemFormatter that wraps a <paramref name="labItem"/>.
/// </summary>
/// <param name="labItem">LabItem to wrap in this ItemFormatter.</param>
public ItemFormatter(LabItem labItem, ReferenceStyle referenceStyle)
{
LabItem = labItem;
ReferenceStyle = referenceStyle;
}
#endregion
#region Methods
/// <summary>
/// Formats and writes the LabItem details to a word <paramref name="document"/>.
/// </summary>
/// <param name="document">Word document to write to.</param>
public void WriteToDocument(Document document)
{
string reference;
if (
LabItem.HasLimitsOrNormal &&
(
ReferenceStyle == ReferenceStyle.Always ||
(ReferenceStyle == ReferenceStyle.IfAbnormal && !LabItem.IsNormal) ||
(ReferenceStyle == ReferenceStyle.IfSpecialItem && LabItem.AlwaysPrintLimits) ||
(ReferenceStyle == ReferenceStyle.IfSpecialOrAbnormal &&
(!LabItem.IsNormal || LabItem.AlwaysPrintLimits))
)
)
{
string normal;
if (LabItem.HasLowerLimit && LabItem.HasUpperLimit)
{
normal = String.Format("{0}-{1}", LabItem.LowerLimit, LabItem.UpperLimit);
}
else
{
if (LabItem.HasLowerLimit)
{
normal = String.Format("> {0}", LabItem.LowerLimit);
}
else if (LabItem.HasUpperLimit)
{
normal = String.Format("< {0}", LabItem.UpperLimit);
}
else
{
normal = LabItem.Normal;
}
}
reference = String.Format(" ({0})", normal);
}
else
{
reference = String.Empty;
}
string unit;
if (LabItem.HasUnit)
{
unit = String.Format(" {0}", LabItem.Unit);
}
else
{
unit = String.Empty;
}
string value;
if (LabItem.IsNumerical)
{
// Format the numerical value; this will convert
// decimal points to commas as needed.
value = String.Format("{0}", LabItem.NumericalValue);
}
else
{
value = LabItem.Value;
}
// Insert the formatted text into the document.
document.Range().InsertAfter(
String.Format(
"{0} {1}{2}{3}",
LabItem.QualifiedName,
value,
unit,
reference
));
HasBeenUsed = true;
}
#endregion
}
}