public DictionaryPropertyEditor(IPropertyEditorParams editorParams, Func <Type, PropertyEditorParams, Widget, object, IEnumerable <IPropertyEditor> > populateEditors) : base(editorParams)
        {
            if (EditorParams.Objects.Skip(1).Any())
            {
                EditorContainer.AddNode(new Widget()
                {
                    Layout = new HBoxLayout(),
                    Nodes  = { new ThemedSimpleText {
                                   Text = "Edit of dictionary properties isn't supported for multiple selection.", ForceUncutText = false
                               } },
                    Presenter = new WidgetFlatFillPresenter(Theme.Colors.WarningBackground)
                });
                return;
            }
            this.populateEditors = populateEditors;
            dictionary           = PropertyValue(EditorParams.Objects.First()).GetValue();
            var addButton = new ThemedAddButton {
                Clicked = () => {
                    if (dictionary == null)
                    {
                        var pi = EditorParams.PropertyInfo;
                        var o  = EditorParams.Objects.First();
                        pi.SetValue(o, dictionary = Activator.CreateInstance <TDictionary>());
                    }
                    if (dictionary.ContainsKey(keyValueToAdd.Key))
                    {
                        AddWarning($"Key \"{keyValueToAdd.Key}\" already exists.", ValidationResult.Warning);
                        return;
                    }
                    using (Document.Current.History.BeginTransaction()) {
                        InsertIntoDictionary <TDictionary, string, TValue> .Perform(dictionary, keyValueToAdd.Key,
                                                                                    keyValueToAdd.Value);

                        ExpandableContent.Nodes.Add(CreateDefaultKeyValueEditor(keyValueToAdd.Key,
                                                                                keyValueToAdd.Value));
                        Document.Current.History.CommitTransaction();
                    }
                    keyValueToAdd.Value = DefaultValue;
                    keyValueToAdd.Key   = string.Empty;
                },
                LayoutCell = new LayoutCell(Alignment.LeftCenter),
            };
            var keyEditorContainer = CreateKeyEditor(editorParams, keyValueToAdd, s => keyValueToAdd.Key = s, addButton);

            ExpandableContent.Nodes.Add(keyEditorContainer);
            ExpandableContent.Nodes.Add(CreateValueEditor(editorParams, keyValueToAdd, populateEditors));
            Rebuild();
            EditorContainer.Tasks.AddLoop(() => {
                if (dictionary != null && ((ICollection)dictionary).Count != pairs.Count)
                {
                    Rebuild();
                }
            });
            var current = PropertyValue(EditorParams.Objects.First());

            ContainerWidget.AddChangeWatcher(() => current.GetValue(), d => {
                dictionary = d;
                Rebuild();
            });
        }
示例#2
0
 public BlendingCell(object obj, string propName)
 {
     Layout       = new HBoxLayout();
     MinMaxHeight = 20;
     Anchors      = Anchors.LeftRightTopBottom;
     property     = new Property <BlendingOption>(obj, propName);
     AddButton    = new ThemedAddButton {
         Anchors = Anchors.Center,
         Clicked = () =>
                   history.DoTransaction(
             () => Core.Operations.SetProperty.Perform(obj, propName, new BlendingOption())),
         LayoutCell = new LayoutCell {
             Alignment = Alignment.Center
         }
     };
     RemoveButton = new ThemedTabCloseButton {
         Clicked = () =>
                   history.DoTransaction(() => Core.Operations.SetProperty.Perform(obj, propName, null))
     };
     Nodes.Add(AddButton);
     AddChangeWatcher(() => property.Value, (v) => {
         Nodes.Clear();
         if (v == null)
         {
             Nodes.Add(AddButton);
         }
         else
         {
             new BlendingPropertyEditor(new PropertyEditorParams(this, obj, propName)
             {
                 ShowLabel      = false,
                 History        = history,
                 PropertySetter = SetProperty
             });
             Nodes.Add(RemoveButton);
         }
     });
 }