zaaReloaded2/zaaReloaded2/Formatter/Elements/Items.cs

161 lines
4.5 KiB
C#
Executable File

/* Items.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 System.Text.RegularExpressions;
using Microsoft.Office.Interop.Word;
using zaaReloaded2.LabModel;
using System.Diagnostics;
namespace zaaReloaded2.Formatter.Elements
{
/// <summary>
/// This formatting element is concerned with writing <see cref="LabItem"/>s
/// to a Word document.
/// </summary>
class Items : ElementBase
{
#region ElementBase implementation
public override string Label
{
get { return Line; }
}
public override void Run(Formatter formatter)
{
bool _needComma = false;
// Find out if we have any items that we can write
// to the document
List<ItemFormatter> items = new List<ItemFormatter>();
if (_items != null && _items.Count > 0)
{
foreach (string itemName in _items)
{
TimePointFormatter tpf = formatter.WorkingTimePoints
.FirstOrDefault(tp => tp.Value.ContainsItem(itemName))
.Value;
if (tpf != null)
{
// If tpf is not null, this means that it contains an
// item with itemName.
items.Add(tpf.ItemFormatters[itemName]);
}
}
}
// If there are items, write the caption (if any), then the items
if (items.Count > 0)
{
if (!String.IsNullOrEmpty(_caption))
{
formatter.Document.Range().InsertAfter(
String.Format("{0}: ", _caption)
);
};
foreach (ItemFormatter i in items)
{
if (_needComma)
{
formatter.Document.Range().InsertAfter(", ");
}
else
{
_needComma = true;
}
i.WriteToDocument(formatter.Document);
}
}
}
#endregion
#region Properties
/// <summary>
/// Gets or sets a text line that contains a comma-separated list of
/// parsable laboratory item names. The list may optionally be preceded
/// with a caption followed by a colon.
/// </summary>
public string Line
{
[DebuggerStepThrough]
get
{
return _line;
}
set
{
_line = value;
ParseLine();
}
}
#endregion
#region Constructors
public Items() : base() { }
public Items(string line)
: this()
{
Line = line;
}
#endregion
#region Private methods
/// <summary>
/// Parses the Line and splits it into an optional caption and individual
/// items.
/// </summary>
void ParseLine()
{
_items = null;
_caption = null;
Regex r = new Regex(@"((?<caption>[^:]+):\s*)?((?<items>[^,]+),\s*)+");
Match m = r.Match(Line);
if (m.Success)
{
if (m.Groups["caption"].Success)
{
_caption = m.Groups["caption"].Value;
}
_items = m.Groups["items"].Captures
.Cast<Capture>()
.Select<Capture, string>(c => c.Value).ToList<string>();
}
}
#endregion
#region Fields
string _line;
string _caption;
List<string> _items;
#endregion
}
}