${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);
}
}
}