/// <summary> /// Adds a file to the recent file list. If the file already exists in the list then it only changes the position in the list. /// </summary> /// <param name="fileName">The path of the recent file.</param> /// <exception cref="ArgumentException">The argument fileName must not be null or empty.</exception> public void AddFile(string fileName) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentException("The argument fileName must not be null or empty.", nameof(fileName)); } RecentFile recentFile = recentFiles.FirstOrDefault(r => r.Path == fileName); if (recentFile != null) { int oldIndex = recentFiles.IndexOf(recentFile); int newIndex = recentFile.IsPinned ? 0 : PinCount; if (oldIndex != newIndex) { recentFiles.Move(oldIndex, newIndex); } } else { if (PinCount < maxFilesNumber) { if (recentFiles.Count >= maxFilesNumber) { RemoveAt(recentFiles.Count - 1); } Insert(PinCount, new RecentFile(fileName)); } } }
/// <summary> /// Removes the specified recent file. /// </summary> /// <param name="recentFile">The recent file to remove.</param> /// <exception cref="ArgumentNullException">The argument recentFile must not be null.</exception> /// <exception cref="ArgumentException">The argument recentFile was not found in the recent files list.</exception> public void Remove(RecentFile recentFile) { if (recentFile == null) { throw new ArgumentNullException("recentFile"); } if (!recentFiles.Remove(recentFile)) { throw new ArgumentException("The passed recentFile was not found in the recent files list."); } }
/// <summary> /// Removes the specified recent file. /// </summary> /// <param name="recentFile">The recent file to remove.</param> /// <exception cref="ArgumentNullException">The argument recentFile must not be null.</exception> /// <exception cref="ArgumentException">The argument recentFile was not found in the recent files list.</exception> public void Remove(RecentFile recentFile) { if (recentFile == null) { throw new ArgumentNullException(nameof(recentFile)); } if (recentFiles.Remove(recentFile)) { recentFile.PropertyChanged -= RecentFilePropertyChanged; } else { throw new ArgumentException("The passed recentFile was not found in the recent files list.", nameof(recentFile)); } }
public void ArgumentsTest() { AssertHelper.ExpectedException<ArgumentException>(() => new RecentFile(null)); RecentFile recentFile = new RecentFile("Doc1"); Assert.AreEqual("Doc1", recentFile.Path); AssertHelper.PropertyChangedEvent(recentFile, x => x.IsPinned, () => recentFile.IsPinned = true); Assert.IsTrue(recentFile.IsPinned); IXmlSerializable serializable = recentFile; Assert.IsNull(serializable.GetSchema()); AssertHelper.ExpectedException<ArgumentNullException>(() => serializable.ReadXml(null)); AssertHelper.ExpectedException<ArgumentNullException>(() => serializable.WriteXml(null)); }
void IXmlSerializable.ReadXml(XmlReader reader) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } reader.ReadToDescendant("RecentFile"); while (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "RecentFile") { RecentFile recentFile = new RecentFile(); ((IXmlSerializable)recentFile).ReadXml(reader); Add(recentFile); } if (!reader.IsEmptyElement) { reader.ReadEndElement(); } }
private void RecentFilePropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(RecentFile.IsPinned)) { RecentFile recentFile = (RecentFile)sender; int oldIndex = recentFiles.IndexOf(recentFile); if (recentFile.IsPinned) { recentFiles.Move(oldIndex, 0); } else { int newIndex = PinCount; if (oldIndex != newIndex) { recentFiles.Move(oldIndex, newIndex); } } } }
private void Insert(int index, RecentFile recentFile) { recentFile.PropertyChanged += RecentFilePropertyChanged; recentFiles.Insert(index, recentFile); }
private void Add(RecentFile recentFile) { recentFile.PropertyChanged += RecentFilePropertyChanged; recentFiles.Add(recentFile); }
/// <summary> /// Removes the specified recent file. /// </summary> /// <param name="recentFile">The recent file to remove.</param> /// <exception cref="ArgumentNullException">The argument recentFile must not be null.</exception> /// <exception cref="ArgumentException">The argument recentFile was not found in the recent files list.</exception> public void Remove(RecentFile recentFile) { if (recentFile == null) { throw new ArgumentNullException("recentFile"); } if (recentFiles.Remove(recentFile)) { recentFile.PropertyChanged -= RecentFilePropertyChanged; } else { throw new ArgumentException("The passed recentFile was not found in the recent files list."); } }
void IXmlSerializable.ReadXml(XmlReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } reader.ReadToDescendant("RecentFile"); while (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "RecentFile") { RecentFile recentFile = new RecentFile(); ((IXmlSerializable)recentFile).ReadXml(reader); Add(recentFile); } if (!reader.IsEmptyElement) { reader.ReadEndElement(); } }