/// <summary> /// /// </summary> void IEditableObject.CancelEdit() { // make sure that the object is actually being edited. if (!isBeingEdited) { return; } // invoke RemoveObject(), this should remove the item of the array if (isNew && RemoveObject != null) { RemoveObject(this, new EventArgs()); } // restore the old values back since we are not a new object int i = 0; foreach (PropertyInfo propertyInfo in properties) { bool restore = propertyInfo.CanWrite; object[] attributes = propertyInfo.GetCustomAttributes(typeof(VisibleAttribute), true); if (attributes.Length == 1) { VisibleAttribute visible = (VisibleAttribute)attributes[0]; restore &= visible.Value; } if (restore) { Reflector.SetValue(propertyInfo, parentInstance, oldValues[i++]); } } // set the dirty flag to false isDirty = false; }
/// <summary> /// /// </summary> void IEditableObject.BeginEdit() { if (isBeingEdited) { return; } isBeingEdited = true; // iterate over each of the properties, and save the old values that are visible foreach (PropertyInfo propertyInfo in properties) { object[] attributes = propertyInfo.GetCustomAttributes(typeof(VisibleAttribute), true); if (attributes.Length == 1) { VisibleAttribute visible = (VisibleAttribute)attributes[0]; if (visible.Value) { oldValues.Add(Reflector.GetValue(propertyInfo, parentInstance)); } } else { oldValues.Add(Reflector.GetValue(propertyInfo, parentInstance)); } } }
/// <summary> /// Implementation of <see cref="System.ComponentModel.ITypedList.GetItemProperties"/>. /// </summary> /// <remarks> /// For each property method of the type contained in the list, a <c>PropertyDescriptor</c> /// will be created. If a property is flagged <i>not visible</i>, it will be omitted. /// The <c>TypedArrayList</c> uses a custom implementation of the PropertyDescriptor, /// <see cref="CaptionPropertyDescriptor"/>, to support interpreting attributes. /// </remarks> public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors) { ArrayList descriptors = new ArrayList(); foreach (PropertyInfo propertyInfo in properties) { object[] attributes = propertyInfo.GetCustomAttributes(typeof(VisibleAttribute), true); CaptionPropertyDescriptor cpd = null; if (attributes.Length == 1) { VisibleAttribute visible = (VisibleAttribute)attributes[0]; if (visible.Value) { cpd = new CaptionPropertyDescriptor(propertyInfo.Name, propertyInfo); } } else { cpd = new CaptionPropertyDescriptor(propertyInfo.Name, propertyInfo); } if (cpd != null && cpd.OriginalDescriptor != null) { descriptors.Add(cpd); } } PropertyDescriptor[] propertyDescriptors = new PropertyDescriptor[descriptors.Count]; descriptors.CopyTo(propertyDescriptors, 0); return(new PropertyDescriptorCollection(propertyDescriptors)); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { VisibleAttribute attr = attribute as VisibleAttribute; if (attr == null || attr.Result(DrawerUtil.GetTarget(property))) { DrawerUtil.OnGUI(position, property, label); } }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { VisibleAttribute attr = attribute as VisibleAttribute; if (attr == null || attr.Result(DrawerUtil.GetTarget(property))) { return(DrawerUtil.GetPropertyHeight(property, label)); } else { return(0f); } }
private void InitAttrs() { if (Field != null) { VisibleAttribute visibleAttribute = Field.GetCustomAttribute <VisibleAttribute>(); if (visibleAttribute != null) { visibleAttrDrawer = (VisibleAttrDrawer)DrawerUtility.GetAttrDrawerInstance(visibleAttribute); visibleAttrDrawer.Attr = visibleAttribute; visibleAttrDrawer.ItemDrawer = this; } decoratorAttrDrawers.Clear(); DecoratorAttribute[] decoratorAttributes = (DecoratorAttribute[])Field.GetCustomAttributes(typeof(DecoratorAttribute), false); if (decoratorAttributes != null && decoratorAttributes.Length > 0) { foreach (var attr in decoratorAttributes) { DecoratorAttrDrawer drawer = (DecoratorAttrDrawer)DrawerUtility.GetAttrDrawerInstance(attr); drawer.ItemDrawer = this; decoratorAttrDrawers.Add(drawer); } } layoutAttrDrawers.Clear(); LayoutAttribute[] layoutAttributes = (LayoutAttribute[])Field.GetCustomAttributes(typeof(LayoutAttribute), false); if (layoutAttributes != null && layoutAttributes.Length > 0) { foreach (var attr in layoutAttributes) { LayoutAttrDrawer drawer = (LayoutAttrDrawer)DrawerUtility.GetAttrDrawerInstance(attr); drawer.Occasion = attr.Occasion; layoutAttrDrawers.Add(drawer); } } ContentAttribute contentAttribute = Field.GetCustomAttribute <ContentAttribute>(); if (contentAttribute != null) { ContentAttrDrawer contentAttrDrawer = (ContentAttrDrawer)DrawerUtility.GetAttrDrawerInstance(contentAttribute); contentAttrDrawer.ItemDrawer = this; innerDrawer = contentAttrDrawer; } } }
void Analyze(object com, PropertyInfo pr) { var propertyName = pr.Name; var visible = pr.GetCustomAttribute <VisibleAttribute>(); if (visible == null) //visible by default { visible = new VisibleAttribute(); } var visibility = visible.Mode; var display = pr.GetCustomAttribute <DisplayAttribute>(); var title = display?.GetName() ?? propertyName; var readOnly = pr.GetCustomAttribute <ReadOnlyAttribute>(); var isReadOnly = readOnly?.IsReadOnly ?? !pr.CanWrite; var val = pr.GetValue(com); var type = pr.PropertyType; if (type.IsPrimitive || Type.GetTypeCode(type) == TypeCode.String) { var converter = GetConverter(type, currentUICulture); var toString = GetToStringConverter(type, currentUICulture); Action <string?> change = x => { if (string.IsNullOrWhiteSpace(x)) { pr.SetValue(com, default); } else { pr.SetValue(com, converter(x)); } }; switch (Type.GetTypeCode(type)) { case TypeCode.Boolean: AddSequence(propertyName, title, val?.ToString() ?? string.Empty, change, new[] { bool.TrueString, bool.FalseString }); break; default: AddPrimitive(propertyName, title, toString(val), change, isReadOnly); break; } } else if (type.IsValueType && type.IsGenericType && Type.GetTypeCode(type.GenericTypeArguments[0]) == TypeCode.Boolean) { Action <string?> update = _value => { if (_value == null) { pr.SetValue(com, null); } else { pr.SetValue(com, (bool?)bool.Parse(_value)); } }; Properties.Add(new CheckBoxViewModelProperty(propertyName, (bool?)val, update, this) { Title = title, }); } else if (type.IsEnum) { var fields = type.GetFields().ToList(); fields.RemoveAt(0);//remove first because it default field of enum type void change(string?x) => pr.SetValue(com, Enum.Parse(type, x)); var values = fields.Select(x => x.GetValue(com)?.ToString()).ToArray(); AddSequence(propertyName, title, val?.ToString() ?? string.Empty, change, fields); } else if (type.IsClass && val is IViewModelProxy proxy) { //inner classes must implement IViewModelProxy AddGroup(propertyName, title, proxy); } }