/* 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 { /// /// A pool of comments. /// class CommentPool { #region Singleton /// /// Gets the singleton instance of the CommentPool. /// public static CommentPool Default { get { return _instance; } } private static readonly CommentPool _instance = new CommentPool(); #endregion #region Properties /// /// Gets the number of ItemComments in the pool. /// public int Count { get { return _itemComments.Count; } } #endregion #region Event public event EventHandler FillInComment; #endregion #region Constructor /// /// Static constructor to support the singleton implementation. /// /// /// See http://csharpindepth.com/Articles/General/Singleton.aspx#cctor /// static CommentPool() { } private CommentPool() { _itemComments = new List(); } #endregion #region Methods /// /// Clear the pool of ItemComments and sets the event handler /// to null. /// public void Reset() { _itemComments.Clear(); FillInComment = null; } /// /// Retrieves the ItemComment for a given definitionString; /// creates a new ItemComment object if necessary. /// /// /// ItemComment derived from the /// 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 h = FillInComment; if (h != null) { h(this, args); } } #endregion #region Fields List _itemComments; #endregion } }