From 43578a95b0709d1771a3ec2c80a057a4fc880aa0 Mon Sep 17 00:00:00 2001 From: Justin Dearing Date: Sat, 20 Jan 2007 11:18:25 +0000 Subject: [PATCH] A pattern for implementing a ConfigurationCollection for storing recently opened files in your application. Its a good starting point. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2308 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../CSharp/CSharp.RecentFilesCollection.xft | 25 ++ .../file/CSharp/RecentFileElement.cs | 33 +++ .../file/CSharp/RecentFilesCollection.cs | 213 ++++++++++++++++++ 3 files changed, 271 insertions(+) create mode 100644 data/templates/file/CSharp/CSharp.RecentFilesCollection.xft create mode 100644 data/templates/file/CSharp/RecentFileElement.cs create mode 100644 data/templates/file/CSharp/RecentFilesCollection.cs diff --git a/data/templates/file/CSharp/CSharp.RecentFilesCollection.xft b/data/templates/file/CSharp/CSharp.RecentFilesCollection.xft new file mode 100644 index 0000000000..c1c1bd9446 --- /dev/null +++ b/data/templates/file/CSharp/CSharp.RecentFilesCollection.xft @@ -0,0 +1,25 @@ + + diff --git a/data/templates/file/CSharp/RecentFileElement.cs b/data/templates/file/CSharp/RecentFileElement.cs new file mode 100644 index 0000000000..585fb5357c --- /dev/null +++ b/data/templates/file/CSharp/RecentFileElement.cs @@ -0,0 +1,33 @@ +${StandardHeader.C#} + +using System; +using System.Configuration; + +namespace ${StandardNamespace} +{ + /// + /// An xml representation of a recent element. + /// + public sealed class RecentFileElement : ConfigurationElement + { + /// + /// The full path of the recently opened file. + /// + [ConfigurationProperty("fileName", IsKey = true, IsRequired = true)] + public string Name + { + get { return (string)this["fileName"]; } + set { this["fileName"] = value; } + } + + internal RecentFileElement() : base() + {} + + internal RecentFileElement(string FileName) : base() + { + this.Name = FileName; + } + } + +} + diff --git a/data/templates/file/CSharp/RecentFilesCollection.cs b/data/templates/file/CSharp/RecentFilesCollection.cs new file mode 100644 index 0000000000..3077017c5a --- /dev/null +++ b/data/templates/file/CSharp/RecentFilesCollection.cs @@ -0,0 +1,213 @@ +${StandardHeader.C#} + +using System; +using System.Configuration; +using System.IO; +using System.Windows.Forms; + + +namespace ${StandardNamespace} +{ + /// + /// ConfigurationSection with PlaneDisaster settings. + /// + public sealed class RecentFilesCollection : ConfigurationElementCollection + { + #region Properties + + /// + /// Gets the CollectionType of the ConfigurationElementCollection. + /// + public override ConfigurationElementCollectionType CollectionType + { + get { return ConfigurationElementCollectionType.BasicMap; } + } + + + /// + /// Gets the Name of Elements of the collection. + /// + protected override string ElementName + { + get { return "PlaneDisaster"; } + } + + + /// + /// Retrieve and item in the collection by index. + /// + public RecentFileElement this[int index] + { + get { return (RecentFileElement)BaseGet(index); } + set + { + if (BaseGet(index) != null) + { + BaseRemoveAt(index); + } + BaseAdd(index, value); + } + } + + + /// + /// The maximum number of RecentFileElements to store + /// + [ConfigurationProperty("maxCount", IsKey=true, DefaultValue=(short)5)] + public short MaxCount { + get { return (short)this["maxCount"]; } + set { this["maxCount"] = value; } + } + + + #endregion + + /// + /// Adds a PlaneDisasterElement to the configuration file. + /// + public void Add(RecentFileElement element) + { + RecentFileElement [] NewFiles = new RecentFileElement [this.Count]; + short FileCount = this.MaxCount; + + this.CopyTo(NewFiles, 0); + this.Clear(); + int i = 1; + this.BaseAdd(element); + foreach (RecentFileElement curFile in NewFiles) { + if (curFile.Name != element.Name) { + this.BaseAdd(curFile); + i++; + if (i >= FileCount) break; + } + } + } + + + /// + /// Adds a PlaneDisasterElement to the configuration file. + /// + public void Add(string FileName) + { + Add (new RecentFileElement(Path.GetFullPath(FileName))); + } + + + private void AddRecentFileToMenu + (string FileName, + ToolStripDropDownItem oToolStripItem, + EventHandler menu_Click) + { + oToolStripItem.Enabled = true; + ToolStripMenuItem RecentFileMenu = new ToolStripMenuItem(FileName); + RecentFileMenu.Click += menu_Click; + oToolStripItem.DropDownItems.Add + (RecentFileMenu); + } + + /// + /// Adds a group of to the configuration file. + /// + public void AddRange(string [] Files) { + foreach (string File in Files) { + RecentFileElement RecentFile = + new RecentFileElement(File); + this.Add(RecentFile); + } + } + + + /// + /// Clears all PlaneDisasterElements to the collection. + /// + public void Clear() + { + BaseClear(); + } + + + /// + /// Creates a new PlaneDisasterElement. + /// + /// A new PlaneDisasterElement + protected override ConfigurationElement CreateNewElement() + { + return new RecentFileElement(); + } + + + /// + /// Checks for the existance of a given file in the + /// RecentFilesCollection. + /// + /// The name of the file. + /// True if the file exists. False otherwise. + public bool FileExists(string FileName) + { + FileName = Path.GetFullPath(FileName); + foreach (RecentFileElement File in this) { + if (File.Name == FileName) return true; + } return false; + + } + + /// + /// Generates a group of drop ToolStipItem(s) + /// under the given ToolStripDropDownItem + /// The EventHandler menu_Click is assign + /// to each ToolStripDropDownItem.Click event. + /// + /// + /// + /// The developer can assume that ToolStripDropDownItem.Text + /// is the name of the file. It is assumed that the developer would do + /// something similar to the code below. + /// + /// ToolStripDropDownItem menuItem = (ToolStripDropDownItem) sender; + /// string FileName = menuItem.Text; + /// + /// //Open the file + /// + /// + /// This function will be marked [Obsolete] in the event that + /// changes to it are made. I might assign the file name to + /// ToolStripDropDownItem.Tag + /// + /// + /// Parent ToolStripDropDownItem + /// + /// + /// EventHandler for Click Events + /// + public void GenerateOpenRecentMenu + (ToolStripDropDownItem menuParent, EventHandler menu_Click) { + menuParent.DropDownItems.Clear(); + foreach (RecentFileElement RecentFile in this) { + AddRecentFileToMenu + (RecentFile.Name, menuParent, menu_Click); + } + } + + + /// + /// Gets the key of an element based on it's Id. + /// + /// Element to get the key of. + /// The key of element. + protected override object GetElementKey(ConfigurationElement element) + { + return ((RecentFileElement)element).Name; + } + + + /// + /// Removes a PlaneDisasterElement with the given name. + /// + /// The name of the PlaneDisasterElement to remove. + public void Remove (string name) { + base.BaseRemove(name); + } + + } +} +