//****************************************************************************************************** // CategorizedSettingsElementCollection.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/eclipse-1.0.php // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 04/11/2006 - Pinal C. Patel // Generated original version of source code. // 05/25/2006 - Pinal C. Patel // Modified the Item(name) property to add a configuration element if it does not exist. // 08/17/2007 - Darrell Zuercher // Edited code comments. // 09/17/2008 - Pinal C. Patel // Converted code to C#. // 09/29/2008 - Pinal C. Patel // Reviewed code comments. // 09/14/2009 - Stephen C. Wills // Added new header and license agreement. // 04/20/2010 - Pinal C. Patel // Added new Name and Section properties for the purpose of managing user scope setting. // 04/21/2010 - Pinal C. Patel // Removed unnecessary overloads of Add() for manageability. // 12/14/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.Configuration; namespace GSF.Configuration { /// /// Represents a collection of objects. /// public class CategorizedSettingsElementCollection : ConfigurationElementCollection { #region [ Members ] // Fields private string m_name; private string m_cryptoKey; private CategorizedSettingsSection m_section; #endregion #region [ Properties ] /// /// Gets or sets the name of the . /// public string Name { get { return m_name; } internal set { m_name = value; } } /// /// Gets or sets the to which this belongs. /// public CategorizedSettingsSection Section { get { return m_section; } internal set { m_section = value; } } /// /// Gets or sets the object at the specified index. /// /// Zero-based index for the object to retrieve. /// The object at the specified index if it exists; otherwise null. public CategorizedSettingsElement this[int index] { get { if (index >= base.Count) throw new IndexOutOfRangeException(); CategorizedSettingsElement setting = (CategorizedSettingsElement)base.BaseGet(index); setting.SetCryptoKey(m_cryptoKey); return setting; } set { if ((object)base.BaseGet(index) != null) base.BaseRemoveAt(index); base.BaseAdd(index, value); } } /// /// Gets the object with the specified name. /// /// Name of the object to retrieve. /// The object with the specified name if it exists; otherwise null. public new CategorizedSettingsElement this[string name] { get { return this[name, false]; } } /// /// Gets the object with the specified name. /// /// Name of the object to retrieve. /// true if the object is to be created if it does not exist; otherwise false. /// The object with the specified name if it exists; otherwise null. public CategorizedSettingsElement this[string name, bool ensureExistance] { get { if (ensureExistance && (object)base.BaseGet(name) == null) { // Add setting since it's not there. Add(name, string.Empty); } CategorizedSettingsElement setting = (CategorizedSettingsElement)base.BaseGet(name); if ((object)setting != null) { // Set the crypto key for the setting. setting.SetCryptoKey(m_cryptoKey); } return setting; } } #endregion #region [ Methods ] /// /// Sets the key to be used for encrypting and decrypting setting values. /// /// New crypto key. public void SetCryptoKey(string cryptoKey) { m_cryptoKey = cryptoKey; } /// /// Gets the index of the specified object. /// /// The object whose index is to be retrieved. /// Index of the specified object if found; otherwise -1. public int IndexOf(CategorizedSettingsElement setting) { return base.BaseIndexOf(setting); } /// /// Adds a new object if one does not exist. /// /// Name of the object. /// Value of the object. public void Add(string name, object value) { Add(name, value, CategorizedSettingsElement.DefaultDescription, CategorizedSettingsElement.DefaultEncrypted, CategorizedSettingsElement.DefaultScope); } /// /// Adds a new object if one does not exist. /// /// Name of the object. /// Value of the object. /// Description of the object. public void Add(string name, object value, string description) { Add(name, value, description, CategorizedSettingsElement.DefaultEncrypted, CategorizedSettingsElement.DefaultScope); } /// /// Adds a new object if one does not exist. /// /// Name of the object. /// Value of the object. /// Description of the object. /// true if the Value of object is to be encrypted; otherwise false. public void Add(string name, object value, string description, bool encryptValue) { Add(name, value, description, encryptValue, CategorizedSettingsElement.DefaultScope); } /// /// Adds a new object if one does not exist. /// /// Name of the object. /// Value of the object. /// Description of the object. /// true if the Value of object is to be encrypted; otherwise false. /// One of the values. public void Add(string name, object value, string description, bool encryptValue, SettingScope scope) { if ((object)base.BaseGet(name) == null) { // Add the element only if it does not exist. CategorizedSettingsElement setting = new CategorizedSettingsElement(this, name); setting.Update(value, description, encryptValue, scope); Add(setting); } } /// /// Adds a new object if one does not exist. /// /// The object to add. public void Add(CategorizedSettingsElement setting) { if ((object)base.BaseGet(setting.Name) == null) { // Add the element only if it does not exist. setting.Category = this; setting.SetCryptoKey(m_cryptoKey); base.BaseAdd(setting); } } /// /// Removes a object if it exists. /// /// Name of the object to remove. public void Remove(string name) { base.BaseRemove(name); } /// /// Removes a object if it exists. /// /// The object to remove. public void Remove(CategorizedSettingsElement setting) { if (base.BaseIndexOf(setting) >= 0) { Remove(setting.Name); } } /// /// Removes the object at the specified index location. /// /// Index location of the object to remove. public void RemoveAt(int index) { base.BaseRemoveAt(index); } /// /// Removes all existing objects. /// public void Clear() { base.BaseClear(); } /// /// Creates a new object. /// /// Instance of . protected override ConfigurationElement CreateNewElement() { return new CategorizedSettingsElement(this); } /// /// Creates a new object. /// /// Name identifying the object. /// Instance of . protected override ConfigurationElement CreateNewElement(string elementName) { return new CategorizedSettingsElement(this, elementName); } /// /// Gets the key for a object. /// /// object whose key is to be retrieved. /// String key value for a object. protected override object GetElementKey(ConfigurationElement element) { return ((CategorizedSettingsElement)element).Name; } #endregion } }