private void SetKey(KeyValuePair keyValue, string key)
        {
            if (dictionary.ContainsKey(key))
            {
                AddWarning($"Key \"{key}\" already exists.", ValidationResult.Warning);
                return;
            }
            using (Document.Current.History.BeginTransaction()) {
                RemoveFromDictionary <TDictionary, string, TValue> .Perform(dictionary, keyValue.Key);

                keyValue.Key = key;
                InsertIntoDictionary <TDictionary, string, TValue> .Perform(dictionary, key, keyValue.Value);

                Document.Current.History.CommitTransaction();
            }
        }
        private Widget CreateDefaultKeyValueEditor(string key, TValue value)
        {
            var keyValue = new KeyValuePair {
                Key = key, Value = value,
            };

            pairs.Add(keyValue);
            Widget keyEditorContainer;
            var    deleteButton = new ThemedDeleteButton();
            var    container    = new Widget {
                Layout = new VBoxLayout(),
                Nodes  =
                {
                    (keyEditorContainer = CreateKeyEditor(EditorParams, keyValue, s => SetKey(keyValue, s), deleteButton)),
                    (CreateValueEditor(EditorParams, keyValue, populateEditors, (o, name, v) => SetValue(keyValue, (TValue)v))),
                }
            };

            keyEditorContainer.Tasks.AddLoop(() => {
                if (dictionary.TryGetValue(keyValue.Key, out value))
                {
                    keyValue.Value = value;
                }
                else
                {
                    Rebuild();
                }
            });
            deleteButton.Clicked += () => {
                pairs.Remove(keyValue);
                using (Document.Current.History.BeginTransaction()) {
                    RemoveFromDictionary <TDictionary, string, TValue> .Perform(dictionary, keyValue.Key);

                    Document.Current.History.CommitTransaction();
                }
                container.UnlinkAndDispose();
            };
            container.CompoundPresenter.Add(new WidgetFlatFillPresenter(
                                                pairs.Count % 2 == 0 ?
                                                ColorTheme.Current.Inspector.StripeBackground1 :
                                                ColorTheme.Current.Inspector.StripeBackground2
                                                )
            {
                IgnorePadding = true
            });
            return(container);
        }
 public void RemoveFromNullDictionaryShouldThrow()
 {
     var activity = new RemoveFromDictionary<string, string>();
     var host = new WorkflowInvokerTest(activity);
     dynamic input = new WorkflowArguments();
     input.Dictionary = null;
     input.Key = "key";
     try
     {
         AssertHelper.Throws<InvalidOperationException>(() => host.TestActivity(input));
     }
     finally
     {
         host.Tracking.Trace();
     }
 }
 public void RemoveFromDictionaryReturnTrueWhenRemoved()
 {
     const string ExpectedKey = "key";
     const string ExpectedValue = "value";
     var dictionary = new Dictionary<string, string> { { ExpectedKey, ExpectedValue } };
     var activity = new RemoveFromDictionary<string, string>();
     var host = new WorkflowInvokerTest(activity);
     dynamic input = new WorkflowArguments();
     input.Dictionary = dictionary;
     input.Key = ExpectedKey;
     try
     {
         host.TestActivity(input);
         host.AssertOutArgument.IsTrue("Result");
     }
     finally
     {
         host.Tracking.Trace();
     }
 }