/* 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 { /// /// This formatting element is concerned with writing s /// to a Word document. /// 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 items = new List(); 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 /// /// 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. /// 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 /// /// Parses the Line and splits it into an optional caption and individual /// items. /// void ParseLine() { _items = null; _caption = null; Regex r = new Regex(@"((?[^:]+):\s*)?((?[^,]+),\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() .Select(c => c.Value).ToList(); } } #endregion #region Fields string _line; string _caption; List _items; #endregion } }