zaaReloaded2/zaaReloaded2/Controller/Comments/CommentPool.cs

145 lines
3.7 KiB
C#
Executable File

/* CommentPool.cs
* part of zaaReloaded2
*
* Copyright 2015-2017 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;
namespace zaaReloaded2.Controller.Comments
{
/// <summary>
/// A pool of comments.
/// </summary>
class CommentPool
{
#region Singleton
/// <summary>
/// Gets the singleton instance of the CommentPool.
/// </summary>
public static CommentPool Default
{
get
{
return _instance;
}
}
private static readonly CommentPool _instance = new CommentPool();
#endregion
#region Properties
/// <summary>
/// Gets the number of ItemComments in the pool.
/// </summary>
public int Count { get { return _itemComments.Count; } }
#endregion
#region Event
public event EventHandler<ItemCommentEventArgs> FillInComment;
#endregion
#region Constructor
/// <summary>
/// Static constructor to support the singleton implementation.
/// </summary>
/// <remarks>
/// See http://csharpindepth.com/Articles/General/Singleton.aspx#cctor
/// </remarks>
static CommentPool() { }
private CommentPool()
{
_itemComments = new List<ItemComment>();
}
#endregion
#region Methods
/// <summary>
/// Clear the pool of ItemComments and sets the event handler
/// to null.
/// </summary>
public void Reset()
{
_itemComments.Clear();
FillInComment = null;
}
/// <summary>
/// Retrieves the ItemComment for a given definitionString;
/// creates a new ItemComment object if necessary.
/// </summary>
/// <param name="definitionString"></param>
/// <returns>ItemComment derived from the
/// <paramref name="definitionString"/></returns>
public ItemComment GetCommentFor(string definitionString)
{
ItemComment itemComment = _itemComments.FirstOrDefault(
i => i.Definition == definitionString);
if (itemComment == null)
{
itemComment = ItemComment.FromDefinitionString(definitionString);
if (itemComment != null)
{
_itemComments.Add(itemComment);
itemComment.FillInComment += itemComment_FillInComment;
}
}
return itemComment;
}
#endregion
#region Private methods
protected virtual void itemComment_FillInComment(object sender, ItemCommentEventArgs e)
{
OnFillInComment(e);
}
#endregion
#region Protected methods
protected virtual void OnFillInComment(ItemCommentEventArgs args)
{
EventHandler<ItemCommentEventArgs> h = FillInComment;
if (h != null)
{
h(this, args);
}
}
#endregion
#region Fields
List<ItemComment> _itemComments;
#endregion
}
}