diff --git a/mRemoteV1/UI/Controls/FilteredPropertyGrid/FilteredPropertyGrid.cs b/mRemoteV1/UI/Controls/FilteredPropertyGrid/FilteredPropertyGrid.cs index 37c1829b..97b33a70 100644 --- a/mRemoteV1/UI/Controls/FilteredPropertyGrid/FilteredPropertyGrid.cs +++ b/mRemoteV1/UI/Controls/FilteredPropertyGrid/FilteredPropertyGrid.cs @@ -6,166 +6,238 @@ using mRemoteNG.App; namespace mRemoteNG.UI.Controls.FilteredPropertyGrid { - /// - /// This class overrides the standard PropertyGrid provided by Microsoft. - /// It also allows to hide (or filter) the properties of the SelectedObject displayed by the PropertyGrid. - /// - public partial class FilteredPropertyGrid : PropertyGrid + /// + /// This class overrides the standard PropertyGrid provided by Microsoft. + /// It also allows to hide (or filter) the properties of the SelectedObject displayed by the PropertyGrid. + /// + public partial class FilteredPropertyGrid : PropertyGrid { - /// Contain a reference to the collection of properties to show in the parent PropertyGrid. + /// + /// Contain a reference to the collection of properties to show in the parent PropertyGrid. + /// /// By default, m_PropertyDescriptors contain all the properties of the object. - readonly List m_PropertyDescriptors = new List(); - /// Contain a reference to the array of properties to display in the PropertyGrid. - private AttributeCollection m_HiddenAttributes, m_BrowsableAttributes; - /// Contain references to the arrays of properties or categories to hide. - private string[] m_BrowsableProperties, m_HiddenProperties; - /// Contain a reference to the wrapper that contains the object to be displayed into the PropertyGrid. - private ObjectWrapper m_Wrapper; + readonly List _propertyDescriptors = new List(); - /// Public constructor. - public FilteredPropertyGrid() { + /// + /// Contain a reference to the array of properties to display in the PropertyGrid. + /// + private AttributeCollection _hiddenAttributes; + private AttributeCollection _browsableAttributes; + + /// + /// Contain references to the arrays of properties or categories to hide. + /// + private string[] _mBrowsableProperties; + private string[] _mHiddenProperties; + /// + /// Contain a reference to the wrapper that contains the object to be displayed into the PropertyGrid. + /// + private ObjectWrapper _mWrapper; + + /// + /// Public constructor. + /// + public FilteredPropertyGrid() + { InitializeComponent(); - base.SelectedObject = m_Wrapper; + base.SelectedObject = _mWrapper; } - public new AttributeCollection BrowsableAttributes { - get { return m_BrowsableAttributes; } + public new AttributeCollection BrowsableAttributes { + get { return _browsableAttributes; } set { - if (m_BrowsableAttributes == value) return; - m_HiddenAttributes = null; - m_BrowsableAttributes = value; + if (_browsableAttributes == value) return; + _hiddenAttributes = null; + _browsableAttributes = value; RefreshProperties(); } } - /// Get or set the categories to hide. + /// + /// Get or set the categories to hide. + /// public AttributeCollection HiddenAttributes { - get { return m_HiddenAttributes; } + get { return _hiddenAttributes; } set { - if (value == m_HiddenAttributes) return; - m_HiddenAttributes = value; - m_BrowsableAttributes = null; + if (value == _hiddenAttributes) return; + _hiddenAttributes = value; + _browsableAttributes = null; RefreshProperties(); } } - /// Get or set the properties to show. + + /// + /// Get or set the properties to show. + /// /// if one or several properties don't exist. public string[] BrowsableProperties { - get { return m_BrowsableProperties; } + get { return _mBrowsableProperties; } set { - if (value == m_BrowsableProperties) return; - m_BrowsableProperties = value; - //m_HiddenProperties = null; + if (value == _mBrowsableProperties) return; + _mBrowsableProperties = value; RefreshProperties(); } } /// Get or set the properties to hide. public string[] HiddenProperties { - get { return m_HiddenProperties; } + get { return _mHiddenProperties; } set { - if (value == m_HiddenProperties) return; - //m_BrowsableProperties = null; - m_HiddenProperties = value; + if (value == _mHiddenProperties) return; + _mHiddenProperties = value; RefreshProperties(); } } - /// Overwrite the PropertyGrid.SelectedObject property. + /// + /// Overwrite the PropertyGrid.SelectedObject property. + /// /// The object passed to the base PropertyGrid is the wrapper. public new object SelectedObject { - get { return m_Wrapper != null ? ((ObjectWrapper)base.SelectedObject).SelectedObject : null; } + get + { + return _mWrapper != null + ? ((ObjectWrapper)base.SelectedObject).SelectedObject + : null; + } set { // Set the new object to the wrapper and create one if necessary. - if(m_Wrapper == null) { - m_Wrapper = new ObjectWrapper(value); + if(_mWrapper == null) + { + _mWrapper = new ObjectWrapper(value); RefreshProperties(); } - else if(m_Wrapper.SelectedObject != value) { - var needrefresh = value.GetType() != m_Wrapper.SelectedObject.GetType(); - m_Wrapper.SelectedObject = value; - if(needrefresh) RefreshProperties(); + else if(_mWrapper.SelectedObject != value) + { + var needrefresh = value.GetType() != _mWrapper.SelectedObject.GetType(); + _mWrapper.SelectedObject = value; + if(needrefresh) + RefreshProperties(); } // Set the list of properties to the wrapper. - m_Wrapper.PropertyDescriptors = m_PropertyDescriptors; + _mWrapper.PropertyDescriptors = _propertyDescriptors; // Link the wrapper to the parent PropertyGrid. - base.SelectedObject = m_Wrapper; + base.SelectedObject = _mWrapper; } } - /* - /// Called when the browsable properties have changed. - private void OnBrowsablePropertiesChanged() { - if(m_Wrapper == null) return; - } - */ + /// + /// Build the list of the properties to be displayed in the PropertyGrid, following the filters defined the Browsable and Hidden properties. + /// + private void RefreshProperties() + { + if(_mWrapper == null) + return; - /// Build the list of the properties to be displayed in the PropertyGrid, following the filters defined the Browsable and Hidden properties. - private void RefreshProperties() { - if(m_Wrapper == null) return; // Clear the list of properties to be displayed. - m_PropertyDescriptors.Clear(); + _propertyDescriptors.Clear(); // Check whether the list is filtered - if(m_BrowsableAttributes != null && m_BrowsableAttributes.Count > 0) { + if(_browsableAttributes != null && _browsableAttributes.Count > 0) + { // Add to the list the attributes that need to be displayed. - foreach(Attribute attribute in m_BrowsableAttributes) ShowAttribute(attribute); - } else { - // Fill the collection with all the properties. - var originalpropertydescriptors = TypeDescriptor.GetProperties(m_Wrapper.SelectedObject); - foreach(PropertyDescriptor propertydescriptor in originalpropertydescriptors) m_PropertyDescriptors.Add(propertydescriptor); - // Remove from the list the attributes that mustn't be displayed. - if(m_HiddenAttributes != null) foreach(Attribute attribute in m_HiddenAttributes) HideAttribute(attribute); + foreach(Attribute attribute in _browsableAttributes) + ShowAttribute(attribute); } + else + { + // Fill the collection with all the properties. + var originalpropertydescriptors = TypeDescriptor.GetProperties(_mWrapper.SelectedObject); + + foreach(PropertyDescriptor propertydescriptor in originalpropertydescriptors) + _propertyDescriptors.Add(propertydescriptor); + + // Remove from the list the attributes that mustn't be displayed. + if(_hiddenAttributes != null) + foreach (Attribute attribute in _hiddenAttributes) + HideAttribute(attribute); + } + // Get all the properties of the SelectedObject - var allproperties = TypeDescriptor.GetProperties(m_Wrapper.SelectedObject); - // Hide if necessary, some properties - if(m_HiddenProperties != null && m_HiddenProperties.Length > 0) { + var allproperties = TypeDescriptor.GetProperties(_mWrapper.SelectedObject); + + // Hide if necessary, some properties + if(_mHiddenProperties != null && _mHiddenProperties.Length > 0) + { // Remove from the list the properties that mustn't be displayed. - foreach(var propertyname in m_HiddenProperties) { - try { + foreach(var propertyname in _mHiddenProperties) + { + try + { var property = allproperties[propertyname]; // Remove from the list the property HideProperty(property); - } catch(Exception ex) { + } + catch (Exception ex) + { Runtime.MessageCollector.AddExceptionMessage("FilteredPropertyGrid: Could not hide Property.", ex); } } } + // Display if necessary, some properties - if(m_BrowsableProperties != null && m_BrowsableProperties.Length > 0) { - foreach(var propertyname in m_BrowsableProperties) { - try { + if(_mBrowsableProperties != null && _mBrowsableProperties.Length > 0) + { + foreach(var propertyname in _mBrowsableProperties) + { + try + { ShowProperty(allproperties[propertyname]); - } catch(Exception knfe) { - Runtime.MessageCollector.AddExceptionMessage("FilteredPropertyGrid: Property not found", knfe); + } + catch (Exception ex) + { + Runtime.MessageCollector.AddExceptionMessage("FilteredPropertyGrid: Property not found", ex); } } } } - /// Allows to hide a set of properties to the parent PropertyGrid. + + /// + /// Allows to hide a set of properties to the parent PropertyGrid. + /// /// A set of attributes that filter the original collection of properties. /// For better performance, include the BrowsableAttribute with true value. - private void HideAttribute(Attribute attribute) { - var filteredoriginalpropertydescriptors = TypeDescriptor.GetProperties(m_Wrapper.SelectedObject,new[] { attribute }); - if(filteredoriginalpropertydescriptors == null || filteredoriginalpropertydescriptors.Count == 0) throw new ArgumentException("Attribute not found",attribute.ToString()); - foreach(PropertyDescriptor propertydescriptor in filteredoriginalpropertydescriptors) HideProperty(propertydescriptor); + private void HideAttribute(Attribute attribute) + { + var filteredoriginalpropertydescriptors = TypeDescriptor.GetProperties(_mWrapper.SelectedObject,new[] { attribute }); + if(filteredoriginalpropertydescriptors == null || filteredoriginalpropertydescriptors.Count == 0) + throw new ArgumentException("Attribute not found", attribute.ToString()); + + foreach(PropertyDescriptor propertydescriptor in filteredoriginalpropertydescriptors) + HideProperty(propertydescriptor); } - /// Add all the properties that match an attribute to the list of properties to be displayed in the PropertyGrid. + + /// + /// Add all the properties that match an attribute to the list of properties to be displayed in the PropertyGrid. + /// /// The attribute to be added. - private void ShowAttribute(Attribute attribute) { - var filteredoriginalpropertydescriptors = TypeDescriptor.GetProperties(m_Wrapper.SelectedObject,new[] { attribute }); - if(filteredoriginalpropertydescriptors == null || filteredoriginalpropertydescriptors.Count == 0) throw new ArgumentException("Attribute not found",attribute.ToString()); - foreach(PropertyDescriptor propertydescriptor in filteredoriginalpropertydescriptors) ShowProperty(propertydescriptor); + private void ShowAttribute(Attribute attribute) + { + var filteredoriginalpropertydescriptors = TypeDescriptor.GetProperties(_mWrapper.SelectedObject,new[] { attribute }); + if(filteredoriginalpropertydescriptors == null || filteredoriginalpropertydescriptors.Count == 0) + throw new ArgumentException("Attribute not found",attribute.ToString()); + + foreach(PropertyDescriptor propertydescriptor in filteredoriginalpropertydescriptors) + ShowProperty(propertydescriptor); } - /// Add a property to the list of properties to be displayed in the PropertyGrid. + + /// + /// Add a property to the list of properties to be displayed in the PropertyGrid. + /// /// The property to be added. - private void ShowProperty(PropertyDescriptor property) { - if(!m_PropertyDescriptors.Contains(property)) m_PropertyDescriptors.Add(property); + private void ShowProperty(PropertyDescriptor property) + { + if(!_propertyDescriptors.Contains(property)) + _propertyDescriptors.Add(property); } - /// Allows to hide a property to the parent PropertyGrid. + + /// + /// Allows to hide a property to the parent PropertyGrid. + /// /// The name of the property to be hidden. - private void HideProperty(PropertyDescriptor property) { - if(m_PropertyDescriptors.Contains(property)) m_PropertyDescriptors.Remove(property); + private void HideProperty(PropertyDescriptor property) + { + if(_propertyDescriptors.Contains(property)) + _propertyDescriptors.Remove(property); } } } \ No newline at end of file diff --git a/mRemoteV1/UI/Controls/FilteredPropertyGrid/ObjectWrapper.cs b/mRemoteV1/UI/Controls/FilteredPropertyGrid/ObjectWrapper.cs index b9398552..a67abef4 100644 --- a/mRemoteV1/UI/Controls/FilteredPropertyGrid/ObjectWrapper.cs +++ b/mRemoteV1/UI/Controls/FilteredPropertyGrid/ObjectWrapper.cs @@ -4,96 +4,119 @@ using System.ComponentModel; namespace mRemoteNG.UI.Controls.FilteredPropertyGrid { - /// This class is a wrapper. It contains the object the propertyGrid has to display. - internal class ObjectWrapper : ICustomTypeDescriptor + /// + /// This class is a wrapper. It contains the object the PropertyGrid has to display. + /// + internal class ObjectWrapper : ICustomTypeDescriptor { - /// Contain a reference to the selected objet that will linked to the parent PropertyGrid. - private object m_SelectedObject; - /// Contain a reference to the collection of properties to show in the parent PropertyGrid. - /// By default, m_PropertyDescriptors contain all the properties of the object. - List m_PropertyDescriptors = new List(); - - /// Simple constructor. + /// + /// Creates a new instance of an with the given object to be wrapped. + /// /// A reference to the selected object that will linked to the parent PropertyGrid. - internal ObjectWrapper(object obj) { - m_SelectedObject = obj; + internal ObjectWrapper(object obj) + { + SelectedObject = obj; } - /// Get or set a reference to the selected objet that will linked to the parent PropertyGrid. - public object SelectedObject { - get { return m_SelectedObject; } - set { m_SelectedObject = value; } - } + /// + /// Get or set a reference to the selected objet that will linked to the parent PropertyGrid. + /// + public object SelectedObject { get; set; } - /// Get or set a reference to the collection of properties to show in the parent PropertyGrid. - public List PropertyDescriptors { - get { return m_PropertyDescriptors; } - set { m_PropertyDescriptors = value; } - } + /// + /// Get or set a reference to the collection of properties to show in the parent PropertyGrid + /// + public List PropertyDescriptors { get; set; } = new List(); - #region ICustomTypeDescriptor Members - public PropertyDescriptorCollection GetProperties(Attribute[] attributes) { + #region ICustomTypeDescriptor Members + public PropertyDescriptorCollection GetProperties(Attribute[] attributes) + { return GetProperties(); } - public PropertyDescriptorCollection GetProperties() { - return new PropertyDescriptorCollection(m_PropertyDescriptors.ToArray(),true); + public PropertyDescriptorCollection GetProperties() + { + return new PropertyDescriptorCollection(PropertyDescriptors.ToArray(), true); } - /// GetAttributes. + /// + /// GetAttributes + /// /// AttributeCollection - public AttributeCollection GetAttributes() { - return TypeDescriptor.GetAttributes(m_SelectedObject,true); - } - /// Get Class Name. - /// String - public String GetClassName() { - return TypeDescriptor.GetClassName(m_SelectedObject,true); - } - /// GetComponentName. - /// String - public String GetComponentName() { - return TypeDescriptor.GetComponentName(m_SelectedObject,true); + public AttributeCollection GetAttributes() + { + return TypeDescriptor.GetAttributes(SelectedObject, true); } - /// GetConverter. + /// + /// Get Class Name + /// + /// String + public string GetClassName() + { + return TypeDescriptor.GetClassName(SelectedObject, true); + } + + /// + /// GetComponentName + /// + /// String + public string GetComponentName() + { + return TypeDescriptor.GetComponentName(SelectedObject, true); + } + + /// + /// GetConverter + /// /// TypeConverter - public TypeConverter GetConverter() { - return TypeDescriptor.GetConverter(m_SelectedObject,true); + public TypeConverter GetConverter() + { + return TypeDescriptor.GetConverter(SelectedObject, true); } - /// GetDefaultEvent. + /// + /// GetDefaultEvent + /// /// EventDescriptor - public EventDescriptor GetDefaultEvent() { - return TypeDescriptor.GetDefaultEvent(m_SelectedObject,true); + public EventDescriptor GetDefaultEvent() + { + return TypeDescriptor.GetDefaultEvent(SelectedObject, true); } - /// GetDefaultProperty. + /// + /// GetDefaultProperty + /// /// PropertyDescriptor - public PropertyDescriptor GetDefaultProperty() { - return TypeDescriptor.GetDefaultProperty(m_SelectedObject,true); + public PropertyDescriptor GetDefaultProperty() + { + return TypeDescriptor.GetDefaultProperty(SelectedObject, true); } - /// GetEditor. + /// + /// GetEditor + /// /// editorBaseType /// object - public object GetEditor(Type editorBaseType) { - return TypeDescriptor.GetEditor(this,editorBaseType,true); + public object GetEditor(Type editorBaseType) + { + return TypeDescriptor.GetEditor(this,editorBaseType, true); } - public EventDescriptorCollection GetEvents(Attribute[] attributes) { - return TypeDescriptor.GetEvents(m_SelectedObject,attributes,true); + public EventDescriptorCollection GetEvents(Attribute[] attributes) + { + return TypeDescriptor.GetEvents(SelectedObject, attributes, true); } - public EventDescriptorCollection GetEvents() { - return TypeDescriptor.GetEvents(m_SelectedObject,true); + public EventDescriptorCollection GetEvents() + { + return TypeDescriptor.GetEvents(SelectedObject, true); } - public object GetPropertyOwner(PropertyDescriptor pd) { - return m_SelectedObject; + public object GetPropertyOwner(PropertyDescriptor pd) + { + return SelectedObject; } - #endregion - } }