minor style cleanup

This commit is contained in:
David Sparer
2018-07-26 08:55:55 -05:00
parent e31088fd2e
commit 88f0d00a15
2 changed files with 243 additions and 148 deletions

View File

@@ -6,166 +6,238 @@ using mRemoteNG.App;
namespace mRemoteNG.UI.Controls.FilteredPropertyGrid
{
/// <summary>
/// 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.
/// </summary>
public partial class FilteredPropertyGrid : PropertyGrid
/// <summary>
/// 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.
/// </summary>
public partial class FilteredPropertyGrid : PropertyGrid
{
/// <summary>Contain a reference to the collection of properties to show in the parent PropertyGrid.</summary>
/// <summary>
/// Contain a reference to the collection of properties to show in the parent PropertyGrid.
/// </summary>
/// <remarks>By default, m_PropertyDescriptors contain all the properties of the object. </remarks>
readonly List<PropertyDescriptor> m_PropertyDescriptors = new List<PropertyDescriptor>();
/// <summary>Contain a reference to the array of properties to display in the PropertyGrid.</summary>
private AttributeCollection m_HiddenAttributes, m_BrowsableAttributes;
/// <summary>Contain references to the arrays of properties or categories to hide.</summary>
private string[] m_BrowsableProperties, m_HiddenProperties;
/// <summary>Contain a reference to the wrapper that contains the object to be displayed into the PropertyGrid.</summary>
private ObjectWrapper m_Wrapper;
readonly List<PropertyDescriptor> _propertyDescriptors = new List<PropertyDescriptor>();
/// <summary>Public constructor.</summary>
public FilteredPropertyGrid() {
/// <summary>
/// Contain a reference to the array of properties to display in the PropertyGrid.
/// </summary>
private AttributeCollection _hiddenAttributes;
private AttributeCollection _browsableAttributes;
/// <summary>
/// Contain references to the arrays of properties or categories to hide.
/// </summary>
private string[] _mBrowsableProperties;
private string[] _mHiddenProperties;
/// <summary>
/// Contain a reference to the wrapper that contains the object to be displayed into the PropertyGrid.
/// </summary>
private ObjectWrapper _mWrapper;
/// <summary>
/// Public constructor.
/// </summary>
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();
}
}
/// <summary>Get or set the categories to hide.</summary>
/// <summary>
/// Get or set the categories to hide.
/// </summary>
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();
}
}
/// <summary>Get or set the properties to show.</summary>
/// <summary>
/// Get or set the properties to show.
/// </summary>
/// <exception cref="ArgumentException">if one or several properties don't exist.</exception>
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();
}
}
/// <summary>Get or set the properties to hide.</summary>
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();
}
}
/// <summary>Overwrite the PropertyGrid.SelectedObject property.</summary>
/// <summary>
/// Overwrite the PropertyGrid.SelectedObject property.
/// </summary>
/// <remarks>The object passed to the base PropertyGrid is the wrapper.</remarks>
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;
}
}
/*
/// <summary>Called when the browsable properties have changed.</summary>
private void OnBrowsablePropertiesChanged() {
if(m_Wrapper == null) return;
}
*/
/// <summary>
/// Build the list of the properties to be displayed in the PropertyGrid, following the filters defined the Browsable and Hidden properties.
/// </summary>
private void RefreshProperties()
{
if(_mWrapper == null)
return;
/// <summary>Build the list of the properties to be displayed in the PropertyGrid, following the filters defined the Browsable and Hidden properties.</summary>
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);
}
}
}
}
/// <summary>Allows to hide a set of properties to the parent PropertyGrid.</summary>
/// <summary>
/// Allows to hide a set of properties to the parent PropertyGrid.
/// </summary>
/// <param name="attribute">A set of attributes that filter the original collection of properties.</param>
/// <remarks>For better performance, include the BrowsableAttribute with true value.</remarks>
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);
}
/// <summary>Add all the properties that match an attribute to the list of properties to be displayed in the PropertyGrid.</summary>
/// <summary>
/// Add all the properties that match an attribute to the list of properties to be displayed in the PropertyGrid.
/// </summary>
/// <param name="attribute">The attribute to be added.</param>
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);
}
/// <summary>Add a property to the list of properties to be displayed in the PropertyGrid.</summary>
/// <summary>
/// Add a property to the list of properties to be displayed in the PropertyGrid.
/// </summary>
/// <param name="property">The property to be added.</param>
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);
}
/// <summary>Allows to hide a property to the parent PropertyGrid.</summary>
/// <summary>
/// Allows to hide a property to the parent PropertyGrid.
/// </summary>
/// <param name="property">The name of the property to be hidden.</param>
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);
}
}
}

View File

@@ -4,96 +4,119 @@ using System.ComponentModel;
namespace mRemoteNG.UI.Controls.FilteredPropertyGrid
{
/// <summary>This class is a wrapper. It contains the object the propertyGrid has to display.</summary>
internal class ObjectWrapper : ICustomTypeDescriptor
/// <summary>
/// This class is a wrapper. It contains the object the PropertyGrid has to display.
/// </summary>
internal class ObjectWrapper : ICustomTypeDescriptor
{
/// <summary>Contain a reference to the selected objet that will linked to the parent PropertyGrid.</summary>
private object m_SelectedObject;
/// <summary>Contain a reference to the collection of properties to show in the parent PropertyGrid.</summary>
/// <remarks>By default, m_PropertyDescriptors contain all the properties of the object. </remarks>
List<PropertyDescriptor> m_PropertyDescriptors = new List<PropertyDescriptor>();
/// <summary>Simple constructor.</summary>
/// <summary>
/// Creates a new instance of an <see cref="ObjectWrapper"/> with the given object to be wrapped.
/// </summary>
/// <param name="obj">A reference to the selected object that will linked to the parent PropertyGrid.</param>
internal ObjectWrapper(object obj) {
m_SelectedObject = obj;
internal ObjectWrapper(object obj)
{
SelectedObject = obj;
}
/// <summary>Get or set a reference to the selected objet that will linked to the parent PropertyGrid.</summary>
public object SelectedObject {
get { return m_SelectedObject; }
set { m_SelectedObject = value; }
}
/// <summary>
/// Get or set a reference to the selected objet that will linked to the parent PropertyGrid.
/// </summary>
public object SelectedObject { get; set; }
/// <summary>Get or set a reference to the collection of properties to show in the parent PropertyGrid.</summary>
public List<PropertyDescriptor> PropertyDescriptors {
get { return m_PropertyDescriptors; }
set { m_PropertyDescriptors = value; }
}
/// <summary>
/// Get or set a reference to the collection of properties to show in the parent PropertyGrid
/// </summary>
public List<PropertyDescriptor> PropertyDescriptors { get; set; } = new List<PropertyDescriptor>();
#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);
}
/// <summary>GetAttributes.</summary>
/// <summary>
/// GetAttributes
/// </summary>
/// <returns>AttributeCollection</returns>
public AttributeCollection GetAttributes() {
return TypeDescriptor.GetAttributes(m_SelectedObject,true);
}
/// <summary>Get Class Name.</summary>
/// <returns>String</returns>
public String GetClassName() {
return TypeDescriptor.GetClassName(m_SelectedObject,true);
}
/// <summary>GetComponentName.</summary>
/// <returns>String</returns>
public String GetComponentName() {
return TypeDescriptor.GetComponentName(m_SelectedObject,true);
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(SelectedObject, true);
}
/// <summary>GetConverter.</summary>
/// <summary>
/// Get Class Name
/// </summary>
/// <returns>String</returns>
public string GetClassName()
{
return TypeDescriptor.GetClassName(SelectedObject, true);
}
/// <summary>
/// GetComponentName
/// </summary>
/// <returns>String</returns>
public string GetComponentName()
{
return TypeDescriptor.GetComponentName(SelectedObject, true);
}
/// <summary>
/// GetConverter
/// </summary>
/// <returns>TypeConverter</returns>
public TypeConverter GetConverter() {
return TypeDescriptor.GetConverter(m_SelectedObject,true);
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(SelectedObject, true);
}
/// <summary>GetDefaultEvent.</summary>
/// <summary>
/// GetDefaultEvent
/// </summary>
/// <returns>EventDescriptor</returns>
public EventDescriptor GetDefaultEvent() {
return TypeDescriptor.GetDefaultEvent(m_SelectedObject,true);
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(SelectedObject, true);
}
/// <summary>GetDefaultProperty.</summary>
/// <summary>
/// GetDefaultProperty
/// </summary>
/// <returns>PropertyDescriptor</returns>
public PropertyDescriptor GetDefaultProperty() {
return TypeDescriptor.GetDefaultProperty(m_SelectedObject,true);
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(SelectedObject, true);
}
/// <summary>GetEditor.</summary>
/// <summary>
/// GetEditor
/// </summary>
/// <param name="editorBaseType">editorBaseType</param>
/// <returns>object</returns>
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
}
}