#region Using Directives using System; using System.Collections.Generic; #endregion Using Directives namespace ScintillaNET { public class SnippetLinkCollection : IDictionary, IList { #region Fields private int _activeLinkIndex = -1; private SnippetLinkRange _activeRange = null; private SnippetLinkEnd _endPoint = null; private bool _isActive = false; List _snippetLinks = new List(); #endregion Fields #region Methods public void Add(SnippetLink item) { Add(item.Key, item); } public void Add(string key, SnippetLink value) { if (!key.Equals(value.Key, StringComparison.CurrentCultureIgnoreCase)) throw new ArgumentException("Key argument must == value.Key", "key"); else if (ContainsKey(key)) throw new ArgumentException("Key already exists", "key"); _snippetLinks.Add(value); } public void Add(KeyValuePair item) { Add(item.Key, item.Value); } public void Clear() { List rageList = new List(); foreach (SnippetLink sl in _snippetLinks) { foreach (Range r in sl.Ranges) { ManagedRange mr = r as ManagedRange; rageList.Add(mr); } } _snippetLinks.Clear(); foreach (ManagedRange mr in rageList) mr.Dispose(); } public bool Contains(SnippetLink item) { return _snippetLinks.Contains(item); } public bool Contains(KeyValuePair item) { return ContainsKey(item.Key); } public bool ContainsKey(string key) { foreach (SnippetLink sl in _snippetLinks) if (sl.Key.Equals(key, StringComparison.CurrentCultureIgnoreCase)) return true; return false; } public void CopyTo(SnippetLink[] array, int arrayIndex) { _snippetLinks.CopyTo(array, arrayIndex); } public void CopyTo(KeyValuePair[] array, int arrayIndex) { throw new Exception("The method or operation is not implemented."); } public IEnumerator> GetEnumerator() { throw new Exception("The method or operation is not implemented."); } IEnumerator IEnumerable.GetEnumerator() { return _snippetLinks.GetEnumerator(); } public int IndexOf(SnippetLink item) { return _snippetLinks.IndexOf(item); } public void Insert(int index, SnippetLink item) { _snippetLinks.Insert(index, item); } public bool Remove(string key) { for (int i = 0; i < _snippetLinks.Count; i++) { if (_snippetLinks[i].Key.Equals(key, StringComparison.CurrentCultureIgnoreCase)) { _snippetLinks.RemoveAt(i); return true; } } return false; } public bool Remove(SnippetLink item) { return _snippetLinks.Remove(item); } public bool Remove(KeyValuePair item) { return Remove(item.Key); } public void RemoveAt(int index) { _snippetLinks.RemoveAt(index); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return _snippetLinks.GetEnumerator(); } public bool TryGetValue(string key, out SnippetLink value) { value = null; for (int i = 0; i < _snippetLinks.Count; i++) { if (_snippetLinks[i].Key.Equals(key, StringComparison.CurrentCultureIgnoreCase)) { value = _snippetLinks[i]; return true; } } return false; } #endregion Methods #region Properties public SnippetLinkRange ActiveRange { get { return _activeRange; } set { _activeRange = value; } } public SnippetLink ActiveSnippetLink { get { if (_activeLinkIndex < 0 || _activeLinkIndex >= _snippetLinks.Count) return null; return _snippetLinks[_activeLinkIndex]; } set { if (value == null) { _activeLinkIndex = -1; return; } _activeLinkIndex = _snippetLinks.IndexOf(value); } } public int Count { get { return _snippetLinks.Count; } } public SnippetLinkEnd EndPoint { get { return _endPoint; } set { _endPoint = value; } } public bool IsActive { get { return _isActive; } set { _isActive = value; } } public bool IsReadOnly { get { return false; } } public ICollection Keys { get { string[] keys = new string[_snippetLinks.Count]; for (int i = 0; i < _snippetLinks.Count; i++) { keys[i] = _snippetLinks[i].Key; } return keys; } } public SnippetLink NextActiveSnippetLink { get { int newIndex = _activeLinkIndex; if (newIndex < 0) return null; else if (++newIndex >= _snippetLinks.Count) newIndex = 0; return _snippetLinks[newIndex]; } } public SnippetLink PreviousActiveSnippetLink { get { int newIndex = _activeLinkIndex; if (newIndex < 0) return null; else if (--newIndex < 0) newIndex = _snippetLinks.Count - 1; return _snippetLinks[newIndex]; } } public ICollection Values { get { SnippetLink[] values = new SnippetLink[_snippetLinks.Count]; for (int i = 0; i < _snippetLinks.Count; i++) { values[i] = _snippetLinks[i]; } return values; } } #endregion Properties #region Indexers public SnippetLink this[string key] { get { for (int i = 0; i < _snippetLinks.Count; i++) { if (_snippetLinks[i].Key.Equals(key, StringComparison.CurrentCultureIgnoreCase)) return _snippetLinks[i]; } throw new KeyNotFoundException(); } set { if (!key.Equals(value.Key, StringComparison.CurrentCultureIgnoreCase)) throw new ArgumentException("Key argument must == value.Key", "key"); for (int i = 0; i < _snippetLinks.Count; i++) { if (_snippetLinks[i].Key.Equals(key, StringComparison.CurrentCultureIgnoreCase)) { _snippetLinks[i] = value; return; } } _snippetLinks.Add(value); } } public SnippetLink this[int index] { get { return _snippetLinks[index]; } set { _snippetLinks[index] = value; } } #endregion Indexers } }