private static IBasicInfo ShowDialog(IWin32Window owner, ICloneable targetItem, Type[] validTypes)
        {
            using (var window = new ElementEditor())
            {
                if (targetItem != null)
                {
                    window.CurrentItem = (IBasicInfo)targetItem.Clone();
                }
                else
                {
                    window.CurrentItem         = (IBasicInfo)Activator.CreateInstance(validTypes.First());
                    window.CurrentItem.Enabled = true;
                }

                window.ValidTypes = validTypes.Select(t => new ComboBoxWrapper <Type>(t,
                                                                                      type => BasicInfoBase.TrimName(type.Name))).OrderBy(w => w.ToString()).ToArray();

                window.comboBox1.Items.AddRange(window.ValidTypes.Cast <object>().ToArray());
                window.comboBox1.SelectedItem = window.comboBox1.Items.Cast <ComboBoxWrapper <Type> >()
                                                .First(x => x.WrappedObject == window.CurrentItem?.GetType());

                if (window.ShowDialog(owner) == DialogResult.OK)
                {
                    return(window.CurrentItem);
                }
            }

            return(null);
        }
        private void SetupEditor(Behaviour newBehaviour, IEnumerable <string> groups)
        {
            CurrentBehaviour = newBehaviour;

            var triggers = newBehaviour.Triggers;

            elementListTriggers.SetupList(triggers,
                                          (window, info) => ElementEditor.ShowDialog(window, (ITrigger)info),
                                          info => triggers.Add((ITrigger)info),
                                          info => triggers.Remove((ITrigger)info),
                                          null,
                                          () => triggers.Clear());

            var conditions = newBehaviour.Conditions;

            elementListConditions.SetupList(conditions,
                                            (window, info) => ElementEditor.ShowDialog(window, (ITrigger)info),
                                            info => conditions.Add((ITrigger)info),
                                            info => conditions.Remove((ITrigger)info),
                                            null,
                                            () => conditions.Clear());

            var actions = newBehaviour.Actions;

            elementListActions.SetupList(actions,
                                         (window, info) => ElementEditor.ShowDialog(window, (IAction)info),
                                         info => actions.Add((IAction)info),
                                         info => actions.Remove((IAction)info),
                                         (index, info) => actions[index] = (IAction)info,
                                         () => actions.Clear(),
                                         info => actions.MoveUp(actions.IndexOf((IAction)info)),
                                         info => actions.MoveDown(actions.IndexOf((IAction)info)));

            textBoxName.DataBindings.Add(nameof(textBoxName.Text),
                                         CurrentBehaviour, nameof(CurrentBehaviour.Name),
                                         false, DataSourceUpdateMode.OnValidation);

            checkBoxEnabled.DataBindings.Add(nameof(checkBoxEnabled.Checked),
                                             CurrentBehaviour, nameof(CurrentBehaviour.Enabled),
                                             false, DataSourceUpdateMode.OnValidation);

            _triggeringKindControls[Array.IndexOf(TriggeringKindResults, newBehaviour.TriggeringKind)].Checked = true;

            comboBoxGroup.Text = newBehaviour.Group ?? string.Empty;
            comboBoxGroup.Items.AddRange(groups.Cast <object>().ToArray());
            numericUpDown1.Value = Math.Min(
                Math.Max((decimal)newBehaviour.MinimalTimedTriggerDelay, numericUpDown1.Minimum),
                numericUpDown1.Maximum);
            numericUpDown2.Value = Math.Min(
                Math.Max((decimal)newBehaviour.CooldownPeriod, numericUpDown2.Minimum),
                numericUpDown2.Maximum);
        }