Create new namespace zaaReloaded2.Formatter, move settings there.
This commit is contained in:
48
zaaReloaded2/Controller/Elements/ElementBase.cs
Executable file
48
zaaReloaded2/Controller/Elements/ElementBase.cs
Executable file
@ -0,0 +1,48 @@
|
||||
/* ElementBase.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.Controller.Elements
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for formatting elements.
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public abstract class ElementBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the label for this formatting element.
|
||||
/// </summary>
|
||||
abstract public string Label { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Executes the action described by this formatting element.
|
||||
/// For example, an Items element Writes the laboratory items
|
||||
/// listed in its Line property to a Word document.
|
||||
/// </summary>
|
||||
/// <param name="formatter">Formatter object that this
|
||||
/// Element belongs to. The Formatter object provides access
|
||||
/// to the current Word document etc.</param>
|
||||
abstract public void Run(zaaReloaded2.Formatter.Formatter formatter);
|
||||
}
|
||||
}
|
162
zaaReloaded2/Controller/Elements/Items.cs
Executable file
162
zaaReloaded2/Controller/Elements/Items.cs
Executable file
@ -0,0 +1,162 @@
|
||||
/* 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 System.Diagnostics;
|
||||
using Microsoft.Office.Interop.Word;
|
||||
using zaaReloaded2.LabModel;
|
||||
using zaaReloaded2.Formatter;
|
||||
|
||||
namespace zaaReloaded2.Controller.Elements
|
||||
{
|
||||
/// <summary>
|
||||
/// This formatting element is concerned with writing <see cref="LabItem"/>s
|
||||
/// to a Word document.
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
class Items : ElementBase
|
||||
{
|
||||
#region ElementBase implementation
|
||||
|
||||
public override string Label
|
||||
{
|
||||
get { return Line; }
|
||||
}
|
||||
|
||||
public override void Run(zaaReloaded2.Formatter.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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user