예제 #1
0
        protected override bool SetValueInternal(object value, ValueUpdateSource updateSource)
        {
            bool result = false;

#if _DOTNET2
            NullableConverter nc = TypeConverter as NullableConverter;
            if ((value == null) || ((value != null) && UnderlyingType.IsInstanceOfType(value)) ||
                ((nc != null) && ((value == null) || nc.UnderlyingType.IsInstanceOfType(value))))
#else
            if ((value != null) && UnderlyingType.IsInstanceOfType(value))
#endif
            {
                UnderlyingValue = value;
                result          = true;
            }

            if (updateSource != ValueUpdateSource.FromChild)
            {
                if ((mOwnerEnumerator != null) &&   // mOwnerEnumerator could be null for the boolean of Property._enabledVariable
                    !mOwnerEnumerator.Property.Value.NoLinkWithChildren)
                {
                    PropertyTypeDescriptorContext context = GetTypeDescriptorContext(OwnerEnumerator);
                    if (TypeConverter.GetPropertiesSupported(context) == false)
                    {
                        PropertyEnumerator childEnum = mOwnerEnumerator.Children;
                        if (childEnum.Count > 0)
                        {
                            string masterStr = mOwnerEnumerator.Property.Value.GetStringValue();

                            char separator = mOwnerEnumerator.Property.Value.GroupedValueSeparator;
                            while (childEnum != childEnum.RightBound)
                            {
                                masterStr.TrimStart(null);

                                int count = masterStr.IndexOf(separator);

                                if (count != -1)
                                {
                                    childEnum.Property.Value.SetValue(masterStr.Substring(0, count));
                                    if (count + 2 < masterStr.Length)
                                    {
                                        masterStr = masterStr.Substring(count + 2); // advance after the separator and the leading space
                                    }
                                }
                                else
                                {
                                    childEnum.Property.Value.SetValue(masterStr);
                                    break;
                                }

                                childEnum.MoveNext();
                            }
                        }
                    }
                }
            }

            return(result);
        }
예제 #2
0
        protected internal override bool ValidateSelfValueFromModifiedChild(PropertyEnumerator modifiedChildEnum, object value)
        {
            if (Validator != null)
            {
                if (modifiedChildEnum.Property.Value is PropertyValueSimulated)
                {
                    try
                    {
                        PropertyTypeDescriptorContext context    = GetTypeDescriptorContext(modifiedChildEnum);
                        PropertyDescriptorCollection  collection = TypeConverter.GetProperties(context, GetValue());
                        PropertyValue childValue = modifiedChildEnum.Property.Value;

                        if (collection != null)
                        {
                            IDictionary dictionary = new Hashtable(collection.Count);
                            foreach (PropertyDescriptor propertyDescriptor in collection)
                            {
                                if (modifiedChildEnum.Property.Name.Equals(propertyDescriptor.Name))
                                {
                                    if (value is string)
                                    {
                                        dictionary[propertyDescriptor.Name] = childValue.TypeConverter.ConvertFromString(
                                            context, childValue.CultureInfo, childValue.GetActualString(value as string));
                                    }
                                    else
                                    {
                                        dictionary[propertyDescriptor.Name] = value;
                                    }
                                }
                                else
                                {
                                    dictionary[propertyDescriptor.Name] = propertyDescriptor.GetValue(childValue.TargetInstance);
                                }
                            }

                            // Create the parent value from all the sibling values
                            object newParentValue = TypeConverter.CreateInstance(context, dictionary);

                            // Validate the parent value
                            PropertyEnumerator invalidPropEnum;
                            if (ValidateValue(newParentValue, out invalidPropEnum))
                            {
                                return(true);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        return(false);
                    }

                    return(true);
                }
            }

            return(true);
        }
예제 #3
0
        public override bool Check(object value, bool modify)
        {
            if (value == null)
            {
                return(true);
            }

            if (Enumerator == null)
            {
                throw new NullReferenceException("The constructor of the validator should have been passed an enumerator on the corresponding property.");
            }

            IComparable   valueToCheck;
            TypeConverter converter = TypeDescriptor.GetConverter(value.GetType());

            if (converter.CanConvertFrom(value.GetType()))
            {
                PropertyTypeDescriptorContext context = Enumerator.Property.Value.GetTypeDescriptorContext(Enumerator);
                valueToCheck = (IComparable)converter.ConvertFrom(context, Enumerator.Property.Value.CultureInfo, value);
            }
            else
            {
                valueToCheck = (IComparable)value;
            }

            bool result = true;

            if (valueToCheck.CompareTo(Min) == -1)
            {
                if (modify)
                {
                    Enumerator.Property.Value.SetValue(Min);
                }
                result = false;
            }
            else if (valueToCheck.CompareTo(Max) == 1)
            {
                if (modify)
                {
                    Enumerator.Property.Value.SetValue(Max);
                }
                result = false;
            }

            if (result == false)
            {
                Message = "" + valueToCheck + " is not between " + Min + " and " + Max + ".";
            }
            else
            {
                Message = "";
            }

            return(result);
        }
예제 #4
0
        protected override bool SetValueInternal(object value, ValueUpdateSource updateSource)
        {
            bool result;

            // Update the new value of this property
            if (value is string)
            {
                if (UnderlyingType.IsEnum)
                {
                    object enumValue = TypeConverter.ConvertFromString(GetTypeDescriptorContext(OwnerEnumerator),
                                                                       CultureInfo, GetActualString(value as string));

                    // If the enum is flagged, we don't test if the value to be assigned is correct
                    if (UnderlyingType.IsDefined(typeof(FlagsAttribute), false))
                    {
                        result = SetMultipleValues(enumValue);
                    }
                    else
                    {
                        if (Enum.IsDefined(UnderlyingType, enumValue))
                        {
                            result = SetMultipleValues(enumValue);
                        }
                        else
                        {
                            result = false;
                        }
                    }
                }
                else
                {
                    PropertyTypeDescriptorContext context = GetTypeDescriptorContext(OwnerEnumerator);
                    string str = GetActualString(value as string);
                    if (TypeConverter.CanConvertFrom(context, typeof(string)))
                    {
                        result = SetMultipleValues(TypeConverter.ConvertFromString(
                                                       context, CultureInfo, str));
                    }
                    else
                    {
                        result = SetMultipleValues(str);
                    }
                }
            }
            else
            {
                result = SetMultipleValues(value);
            }

            return(result);
        }
예제 #5
0
        protected internal override bool ValidateSelfValueFromModifiedChild(PropertyEnumerator modifiedChildEnum, object value)
        {
            if (NoLinkWithChildren)
            {
                return(true);
            }

            if (Validator != null)
            {
                string             str       = "";
                PropertyEnumerator selfEnum  = modifiedChildEnum.Parent;
                PropertyEnumerator childEnum = selfEnum.Children;
                while (childEnum != childEnum.RightBound)
                {
                    if (childEnum.Equals(modifiedChildEnum))
                    {
                        if (value is string)
                        {
                            str += (value as string);
                        }
                        else
                        {
                            PropertyTypeDescriptorContext context = childEnum.Property.Value.GetTypeDescriptorContext(childEnum);
                            str += (string)TypeDescriptor.GetConverter(value).ConvertTo(context,
                                                                                        childEnum.Property.Value.CultureInfo, value, typeof(string));
                        }
                    }
                    else
                    {
                        str += childEnum.Property.Value.GetStringValue();
                    }
                    childEnum.MoveNext();

                    if (childEnum != childEnum.RightBound)
                    {
                        str += GroupedValueSeparator;
                        str += " ";
                    }
                }

                if (Validator.Check(str, false) == false)
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #6
0
        protected override bool SetValueInternal(object value, ValueUpdateSource updateSource)
        {
            try
            {
                PropertyValue parentValue = mOwnerEnumerator.Parent.Property.Value;
                TypeConverter converter   = parentValue.TypeConverter;

                PropertyTypeDescriptorContext context    = GetTypeDescriptorContext(OwnerEnumerator);
                PropertyDescriptorCollection  collection = converter.GetProperties(context, parentValue.GetValue());

                if (collection != null)
                {
                    IDictionary dictionary = new Hashtable(collection.Count);
                    foreach (PropertyDescriptor propertyDescriptor in collection)
                    {
                        if (OwnerEnumerator.Property.Name.Equals(propertyDescriptor.Name))
                        {
                            if (value is string)
                            {
                                dictionary[propertyDescriptor.Name] = TypeConverter.ConvertFromString(
                                    context, CultureInfo, GetActualString(value as string));
                            }
                            else
                            {
                                dictionary[propertyDescriptor.Name] = value;
                            }
                        }
                        else
                        {
                            dictionary[propertyDescriptor.Name] = propertyDescriptor.GetValue(TargetInstance);
                        }
                    }

                    // Create the parent value from all the sibling values
                    PropertyDescriptor.SetValue(TargetInstance, value);
                    parentValue.SetValue(converter.CreateInstance(context, dictionary), ValueUpdateSource.FromChild);
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
예제 #7
0
        protected internal PropertyValue[] GetChildValues(object[] targetInstances, PropertyDescriptor[] propertyDescriptors)
        {
            PropertyValue[] valueArray;

            Hashtable propertyDescriptorDefs = new Hashtable(); // <PropertyDescriptor, List<PropertyDescriptor>>

            PropertyDescriptorCollection collection = null;
            bool firstTarget = true;
            bool toBeSorted = true;
            for(int i=0; i<targetInstances.Length; i++)
            {
                object targetInstance = targetInstances[i];

                PropertyDescriptorCollection pdCollection = OwnerEnumerator.Property.ParentGrid.
                    GetPropertyDescriptors(targetInstance, propertyDescriptors[i], out toBeSorted);
                if (collection == null)
                {
                    PropertyDescriptor[] array = new PropertyDescriptor[pdCollection.Count];
                    pdCollection.CopyTo(array, 0);
                    collection = new PropertyDescriptorCollection(array);
                }

                foreach (PropertyDescriptor propertyDescriptor in pdCollection)
                {
                    MergablePropertyAttribute mergeAttr = propertyDescriptor.Attributes[typeof(MergablePropertyAttribute)]
                        as MergablePropertyAttribute;
                    if ((mergeAttr != null) && (mergeAttr.AllowMerge == false))
                        continue;

                    if (firstTarget)
                    {
                        // For the first target instance, we store all the PropertyDescriptors
                        ArrayList pdList = new ArrayList();
                        pdList.Add(propertyDescriptor);

                        propertyDescriptorDefs.Add(propertyDescriptor, pdList);
                    }
                    else
                    {
                        // For all the remaining target instances, we compare the PropertyDescriptors
                        // to the first set of stored PropertyDescriptors. Their name and property type must
                        // correspond
                        ArrayList existPdList = (ArrayList)propertyDescriptorDefs[propertyDescriptor];
                        if (existPdList != null)
                        {
                            // If the propertyDesriptors match, we increment a count
                            if (OwnerEnumerator.Property.ParentGrid.MatchPropertyDescriptors(
                                (PropertyDescriptor)existPdList[0], propertyDescriptor))
                            {
                                existPdList.Add(propertyDescriptor);
                            }
                            // else the PropertyDescriptor is not eligible and is removed
                            else
                                propertyDescriptorDefs.Remove(propertyDescriptor);
                        }
                    }
                }

                firstTarget = false;
            }

            ArrayList validPropertyDescriptors = new ArrayList();

            // Ensure that we count only propertyDescriptors that are present in all target instances
            foreach (ArrayList pdList in propertyDescriptorDefs.Values)
            {
                if (pdList.Count != targetInstances.Length)
                    collection.Remove((PropertyDescriptor)pdList[0]);
            }

            // Sort the collection
            if (toBeSorted && (Grid.PropertyComparer != null))
                collection = collection.Sort(Grid.PropertyComparer);

            PropertyTypeDescriptorContext context = new PropertyTypeDescriptorContext(null, targetInstances[0], null,
                OwnerEnumerator.Property.ParentGrid);
            bool readOnlyProperties = TypeConverter.GetCreateInstanceSupported(context);
            valueArray = new PropertyValue[collection.Count];
            int index = 0;
            foreach (PropertyDescriptor descriptor in collection)
            {
                PropertyValue propValue;
                try
                {
                    object obj = UnderlyingValue;
                    if (obj is ICustomTypeDescriptor)
                        obj = ((ICustomTypeDescriptor)obj).GetPropertyOwner(descriptor);

                    // TODO : pourquoi cette ligne ?
                    descriptor.GetValue(obj);
                }
                catch (Exception)
                {
                    // Reason : activeXHide ?
                }

                if (readOnlyProperties)
                    propValue = new PropertyValueSimulated(Grid, this, descriptor);
                else
                    propValue = new PropertyValueIndirect(Grid, this, UnderlyingValue, descriptor, null);

                propValue.ChildValueCreatedBySPG = true;

                PropertyIdAttribute attr = descriptor.Attributes[typeof(PropertyIdAttribute)] as PropertyIdAttribute;
                propValue.Id = (attr != null ? attr.Id : 0);

                valueArray[index++] = propValue;
            }

            return valueArray;
        }
예제 #8
0
        public override void Recreate(bool sendPropertyCreatedEvent)
        {
            Grid.InsideValueRecreate = true;
            Grid.BeginUpdate();
            object states = Grid.SavePropertiesStates();

            // Set the grid in categorized mode
            PropertyGrid.DisplayModes dispMode = Grid.DisplayMode;
            if (dispMode != PropertyGrid.DisplayModes.Categorized)
            {
                Grid.Grid.DisplayMode = PropertyGrid.DisplayModes.Categorized;
            }

            RecreateChildProperties();

            PropertyValue parentValue = null;

            if (OwnerEnumerator.Parent.Property != null)
            {
                parentValue = OwnerEnumerator.Parent.Property.Value;
            }

            PropertyTypeDescriptorContext context = GetTypeDescriptorContext(OwnerEnumerator);

            if ((parentValue == null) || !parentValue.TypeConverter.GetPropertiesSupported(context) ||
                parentValue.TypeConverter.GetCreateInstanceSupported(context))
            {
                PropertyDescriptor pd = null;

                object target = TargetInstance;
                if (target != null)
                {
                    pd = TypeDescriptor.GetProperties(target,
                                                      new Attribute[1] {
                        BrowsableAttribute.Yes
                    })[((PropertyDescriptor)((Utilities.KeyValuePair)_targets[0]).Key).Name];
                }

                if (pd != null)
                {
                    _targets[0] = new Utilities.KeyValuePair(pd, ((Utilities.KeyValuePair)_targets[0]).Value);
                    ResetDisplayedValues(true);

                    Grid.ShowProperty(mOwnerEnumerator, true);
                }
                else
                {
                    // Hide properties that are suddenly not browsable
                    Grid.ShowProperty(mOwnerEnumerator, false);
                }
            }

            // Handle ReadOnly
            bool readOnly = IsReadOnly(mOwnerEnumerator);

            if (readOnly)
            {
                if (Grid.ReadOnlyVisual == PropertyGrid.ReadOnlyVisuals.Disabled)
                {
                    Grid.EnableProperty(mOwnerEnumerator, false);
                }
                else
                {
                    mOwnerEnumerator.Property.ForeColor = DisabledForeColor;
                }
            }
            else
            {
                Grid.EnableProperty(mOwnerEnumerator, true);
                mOwnerEnumerator.Property.ForeColor = Grid.ForeColor;
            }

            if (Feel == null)
            {
                if (!readOnly || (((Look == null) || !Look.NoFeelForReadOnly) && (Grid.ReadOnlyVisual == PropertyGrid.ReadOnlyVisuals.ReadOnlyFeel)))
                {
                    Feel = Grid.Grid.FindFeel(mOwnerEnumerator.Parent, mOwnerEnumerator);
                }
            }

            if (sendPropertyCreatedEvent)
            {
                Grid.OnPropertyCreated(new PropertyCreatedEventArgs(OwnerEnumerator));
            }

            // Set the grid in non categorized mode if it was as such before calling SelectedObject(s)
            if (dispMode != PropertyGrid.DisplayModes.Categorized)
            {
                Grid.Grid.DisplayMode = dispMode;
            }

            Grid.RestorePropertiesStates(states);
            Grid.EndUpdate();
            Grid.InsideValueRecreate = false;
        }
예제 #9
0
        private void HandleUpDownButton(ButtonID buttonId, bool rotate)
        {
            string strValue = Text;

            PropertyUpDownEventArgs args = new PropertyUpDownEventArgs(
                _ownerPropertyEnum, (buttonId == ButtonID.Up ? PropertyUpDownEventArgs.UpDownButtons.Up :
                                     PropertyUpDownEventArgs.UpDownButtons.Down));

            args.Value = Text;
            string oldStr = Text;

            _ownerPropertyEnum.Property.ParentGrid.OnPropertyUpDown(args);

            if (Text != args.Value)
            {
                strValue = args.Value;
            }
            else
            {
                string[] displayedStrings = _ownerPropertyEnum.Property.Value.GetDisplayedValues();

                if (displayedStrings.Length > 0)
                {
                    strValue = Text;

                    if (_ownerPropertyEnum.Property.Value.HasMultipleValues)
                    {
                        int index = -1;
                        if (buttonId == ButtonID.Down)
                        {
                            index = displayedStrings.Length - 1;
                        }
                        else if (buttonId == ButtonID.Up)
                        {
                            index = 0;
                        }

                        if (index != -1)
                        {
                            strValue = displayedStrings[index];
                        }
                    }
                    else
                    {
                        for (int i = 0; i < displayedStrings.Length; i++)
                        {
                            if (displayedStrings[i] == strValue)
                            {
                                int index = -1;
                                if ((buttonId == ButtonID.Down) && (i > 0))
                                {
                                    index = i - 1;
                                }
                                else if ((buttonId == ButtonID.Down) && rotate)
                                {
                                    index = displayedStrings.Length - 1;
                                }
                                else if ((buttonId == ButtonID.Up) && (i < displayedStrings.Length - 1))
                                {
                                    index = i + 1;
                                }
                                else if ((buttonId == ButtonID.Up) && rotate)
                                {
                                    index = 0;
                                }

                                if (index != -1)
                                {
                                    strValue = displayedStrings[index];
                                }

                                break;
                            }
                        }
                    }
                }
                else if (_ownerPropertyEnum.Property.Value.UnderlyingType != typeof(string))
                {
                    TypeConverter tc = _ownerPropertyEnum.Property.Value.TypeConverter;
                    PropertyTypeDescriptorContext context = _ownerPropertyEnum.Property.Value.GetTypeDescriptorContext(_ownerPropertyEnum);

                    decimal value;
                    object  originalValue;
                    try
                    {
                        if (_ownerPropertyEnum.Property.Value.HasMultipleValues)
                        {
                            DefaultValueAttribute attr = (DefaultValueAttribute)_ownerPropertyEnum.Property.Value.GetAttribute(typeof(DefaultValueAttribute));
                            if (attr != null)
                            {
                                originalValue = attr.Value;
                            }
                            else
                            {
                                originalValue = 0;
                            }
                            value = Convert.ToDecimal(originalValue);
                        }
                        else
                        {
                            originalValue = tc.ConvertFromString(context,
                                                                 _ownerPropertyEnum.Property.Value.CultureInfo, Text);
                            value = Convert.ToDecimal(originalValue);
                        }
                    }
                    catch (Exception e)
                    {
                        _currentInvalidPropertyEnum   = OwnerPropertyEnumerator;
                        _currentValueValidationResult = PropertyValue.ValueValidationResult.TypeConverterFailed;
                        _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                            new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum, Text,
                                                         PropertyValue.ValueValidationResult.TypeConverterFailed, e));
                        return;
                    }

                    if (buttonId == ButtonID.Up)
                    {
                        try
                        {
                            value += Increment;
                        }
                        catch (OverflowException)
                        {
                        }
                    }
                    else if (buttonId == ButtonID.Down)
                    {
                        try
                        {
                            value -= Increment;
                        }
                        catch (OverflowException)
                        {
                        }
                    }

                    CultureInfo culture = _ownerPropertyEnum.Property.Value.CultureInfo;
                    strValue = tc.ConvertToString(context, culture,
                                                  Convert.ChangeType(value, originalValue.GetType(), culture));
                    if (strValue == null)
                    {
                        strValue = "";
                    }
                }
            }

            if (Text != strValue)
            {
                _ownerPropertyEnum.Property.Value.PreviousValue = _ownerPropertyEnum.Property.Value.GetValue();

                PropertyValidatorBase validator = _ownerPropertyEnum.Property.Value.Validator;

                // Check current value
                try
                {
                    object valueToValidate = _ownerPropertyEnum.Property.Value.GetValueToValidate(Text);
                    if (validator != null)
                    {
                        if (!validator.Check(_ownerPropertyEnum.Property.Value.GetValueToValidate(Text), false))
                        {
                            if (_edit != null)
                            {
                                _edit.SelectAll();
                            }

                            _currentInvalidPropertyEnum   = OwnerPropertyEnumerator;
                            _currentValueValidationResult = PropertyValue.ValueValidationResult.ValidatorFailed;
                            _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                                new ValueValidationEventArgs(OwnerPropertyEnumerator, OwnerPropertyEnumerator,
                                                             Text, _currentValueValidationResult));
                            return;
                        }
                    }
                }
                catch (Exception e)
                {
                    if (_edit != null)
                    {
                        _edit.SelectAll();
                    }

                    _currentInvalidPropertyEnum   = OwnerPropertyEnumerator;
                    _currentValueValidationResult = PropertyValue.ValueValidationResult.TypeConverterFailed;
                    _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                        new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum, Text,
                                                     PropertyValue.ValueValidationResult.TypeConverterFailed, e));
                    return;
                }

                if (validator != null)
                {
                    if (!validator.Check(_ownerPropertyEnum.Property.Value.GetValueToValidate(strValue), false))
                    {
                        return;
                    }
                }

                if (_realtimeChange)
                {
                    CommitChanges(strValue, false);
                }
                else
                {
                    Text = strValue;
                }
            }

            if ((_edit != null) && _edit.Focused)
            {
                _edit.SelectAll();
            }
            else
            {
                Invalidate();
            }
        }