Add formatter and initial tests.
This commit is contained in:
127
zaaReloaded2/Formatter/ItemFormatter.cs
Executable file
127
zaaReloaded2/Formatter/ItemFormatter.cs
Executable file
@ -0,0 +1,127 @@
|
||||
/* 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;
|
||||
}
|
||||
|
||||
// Insert the formatted text into the document.
|
||||
document.Range().InsertAfter(
|
||||
String.Format(
|
||||
"{0} {1}{2}",
|
||||
LabItem.QualifiedName,
|
||||
LabItem.Value,
|
||||
reference
|
||||
));
|
||||
HasBeenUsed = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user