/// <summary>
        /// Keyからタグを取得
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public TagInformation GetTagValue(int key)
        {
            TagInformation tag;

            lock (this.gate)
            {
                if (this.registeredTags.TryGetValue(key, out tag))
                {
                    return(tag);
                }

                tag = new TagInformation()
                {
                    Name     = key.ToString(),
                    Shortcut = "",
                    Id       = key,
                };

                this.registeredTags.Add(key, tag);
            }

            this.AddedSubject.OnNext(tag);

            this.IsEdited = true;
            return(tag);
        }
        /// <summary>
        /// キーボードショートカットを設定
        /// </summary>
        /// <param name="tag"></param>
        public void SetShortcut(TagInformation tag)
        {
            if (!tag.Shortcut.IsNullOrWhiteSpace())
            {
                TagInformation[] tags;

                lock (this.gate)
                {
                    tags = this.registeredTags
                           .Where(x =>
                    {
                        return(x.Value != tag &&
                               x.Value.Shortcut != null &&
                               x.Value.Shortcut.Length > 0 &&
                               x.Value.Shortcut.Equals(tag.Shortcut,
                                                       StringComparison.OrdinalIgnoreCase));
                    })
                           .Select(x => x.Value)
                           .ToArray();
                }

                foreach (var x in tags)
                {
                    x.Shortcut = "_" + x.Shortcut;
#if DEBUG
                    System.Windows.MessageBox.Show(x.Shortcut);
#endif
                }
            }
        }
 public void CopyFrom(TagInformation other)
 {
     this.Name      = other.Name;
     this.Shortcut  = other.Shortcut;
     this.IsIgnored = other.IsIgnored;
     this.LastUsed  = other.LastUsed;
 }
        private int AddOrReplace(TagInformation value)
        {
            var key = 1;

            var added = false;

            lock (this.gate)
            {
                TagInformation tag = null;
                while (this.registeredTags.TryGetValue(key, out tag))
                {
                    if (tag != null && tag.IsIgnored)
                    {
                        break;
                    }
                    tag = null;
                    key++;
                }
                if (tag != null)
                {
                    tag.CopyFrom(value);
                    tag.Id        = key;
                    tag.IsIgnored = false;
                }
                else
                {
                    this.registeredTags.Add(key, value);
                    value.Id = key;
                    added    = true;
                    //this.AddedSubject.OnNext(value);
                }
            }
            if (added)
            {
                this.AddedSubject.OnNext(value);
            }

            this.IsEdited = true;
            return(key);
        }
        /// <summary>
        /// 新しいタグを登録
        /// </summary>
        /// <param name="newTag"></param>
        /// <returns></returns>
        public int SetTag(TagInformation newTag)
        {
            this.SetShortcut(newTag);

            KeyValuePair <int, TagInformation> existingTag;

            lock (this.gate)
            {
                existingTag = this.registeredTags
                              .FirstOrDefault(x => x.Value.Name.Equals(newTag.Name));
            }

            if (existingTag.Value != null)
            {
                if (!newTag.Shortcut.IsNullOrWhiteSpace())
                {
                    existingTag.Value.Shortcut = newTag.Shortcut;
                }
                return(existingTag.Key);
            }

            return(this.AddOrReplace(newTag));
        }