OSDN Git Service

UWP版に自動補完機能を付けた
[fooeditengine/FooEditEngine.git] / Core / CompleteCollection.cs
1 using System;
2 using System.ComponentModel;
3 using System.Collections.ObjectModel;
4 using System.Collections.Generic;
5 using System.Runtime.CompilerServices;
6
7 namespace FooEditEngine
8 {
9     public interface ICompleteItem : INotifyPropertyChanged
10     {
11         /// <summary>
12         /// 補完対象の単語を表す
13         /// </summary>
14         string word { get; }
15     }
16
17     public class CompleteWord : ICompleteItem
18     {
19         private string _word;
20         /// <summary>
21         /// コンストラクター
22         /// </summary>
23         public CompleteWord(string w)
24         {
25             this._word = w;
26             this.PropertyChanged += new PropertyChangedEventHandler((s,e)=>{});
27         }
28
29         /// <summary>
30         /// 補完候補となる単語を表す
31         /// </summary>
32         public string word
33         {
34             get { return this._word; }
35             set { this._word = value; this.OnPropertyChanged(); }
36         }
37
38         /// <summary>
39         /// プロパティが変更されたことを通知する
40         /// </summary>
41         public void OnPropertyChanged([CallerMemberName] string name = "")
42         {
43             if (this.PropertyChanged != null)
44                 this.PropertyChanged(this, new PropertyChangedEventArgs(name));
45         }
46
47         /// <summary>
48         /// プロパティが変更されたことを通知する
49         /// </summary>
50         public event PropertyChangedEventHandler PropertyChanged;
51     }
52
53     public sealed class CompleteCollection<T> : ObservableCollection<T> where T : ICompleteItem
54     {
55         public const string ShowMember = "word";
56
57         /// <summary>
58         /// 補完対象の単語を表す
59         /// </summary>
60         public CompleteCollection()
61         {
62             this.LongestItem = default(T);
63         }
64
65         /// <summary>
66         /// 最も長い単語を表す
67         /// </summary>
68         public T LongestItem
69         {
70             get;
71             private set;
72         }
73
74         public void AddRange(IEnumerable<T> collection)
75         {
76             foreach (T s in collection)
77                 this.Add(s);
78         }
79
80         public new void Add(T s)
81         {
82             if (this.LongestItem == null)
83                 this.LongestItem = s;
84             if (s.word.Length > this.LongestItem.word.Length)
85                 this.LongestItem = s;
86             base.Add(s);
87         }
88
89         public new void Insert(int index, T s)
90         {
91             if (this.LongestItem == null)
92                 this.LongestItem = s;
93             if (s.word.Length > this.LongestItem.word.Length)
94                 this.LongestItem = s;
95             base.Insert(index, s);
96         }
97
98         public new void Clear()
99         {
100             this.LongestItem = default(T);
101             base.Clear();
102         }
103     }
104 }