internal bool SetupTemplate(IElementTemplate template)
        {
            bool success = template.SetupTemplate(treeElements.SelectedElementNodes);

            if (success)
            {
                IEnumerable <ElementNode> createdElements = template.GenerateElements(treeElements.SelectedElementNodes);
                if (createdElements == null || !createdElements.Any())
                {
                    var messageBox =
                        new MessageBoxForm("Could not create elements.  Ensure you use a valid name and try again.", "",
                                           MessageBoxButtons.OKCancel, SystemIcons.Error);
                    messageBox.ShowDialog();
                    return(false);
                }

                var question = new MessageBoxForm("Would you like to configure a dimming curve for this Prop?", "Dimming Curve Setup", MessageBoxButtons.YesNo, SystemIcons.Question);
                var response = question.ShowDialog(this);
                if (response == DialogResult.OK)
                {
                    DimmingCurveHelper dimmingHelper = new DimmingCurveHelper(true);
                    dimmingHelper.Perform(createdElements);
                }

                ColorSetupHelper helper = new ColorSetupHelper();
                helper.SetColorType(ElementColorType.FullColor);
                helper.Perform(createdElements);

                AddNodeToTree(createdElements.First());
            }

            return(success);
        }
        public async Task CreateAsync()
        {
            Task t = Task.Factory.StartNew(() =>
            {
                _elementModelMap = new Dictionary <Guid, ElementNode>();
                //Optimize the name check for performance. We know we are going to create a bunch of them and we can handle it ourselves more efficiently
                _elementNames = new HashSet <string>(VixenSystem.Nodes.Select(x => x.Name));

                var rootNode = _prop.RootNode;

                ElementNode rootElementNode = ElementNodeService.Instance.CreateSingle(null, NamingUtilities.Uniquify(_elementNames, TokenizeName(rootNode.Name)), true, false);
                PreviewCustomProp.Name      = rootElementNode.Name;
                _elementNames.Add(rootElementNode.Name);

                _elementModelMap.Add(rootNode.Id, rootElementNode);

                CreateElementsForChildren(rootElementNode, rootNode);

                if (_prop.PhysicalMetadata.ColorMode != ColorMode.Other)
                {
                    //Now lets setup the color handling.
                    ColorSetupHelper helper = new ColorSetupHelper();
                    switch (_prop.PhysicalMetadata.ColorMode)
                    {
                    case ColorMode.FullColor:
                        helper.SetColorType(ElementColorType.FullColor);
                        helper.SilentMode = true;
                        break;

                    case ColorMode.Multiple:
                        helper.SetColorType(ElementColorType.MultipleDiscreteColors);
                        break;

                    default:
                        helper.SetColorType(ElementColorType.SingleColor);
                        break;
                    }

                    helper.Perform(_leafNodes);
                }

                PreviewCustomProp.UpdateColorType();

                PreviewCustomProp.Layout();
            });

            await t;
        }
        private void buttonAddTemplate_Click(object sender, EventArgs e)
        {
            ComboBoxItem item = (comboBoxNewItemType.SelectedItem as ComboBoxItem);

            if (item != null)
            {
                IElementTemplate template = item.Value as IElementTemplate;
                bool             act      = template.SetupTemplate(elementTree.SelectedElementNodes);
                if (act)
                {
                    IEnumerable <ElementNode> createdElements = template.GenerateElements(elementTree.SelectedElementNodes);
                    if (createdElements == null || !createdElements.Any())
                    {
                        //messageBox Arguments are (Text, Title, No Button Visible, Cancel Button Visible)
                        MessageBoxForm.msgIcon = SystemIcons.Error;                         //this is used if you want to add a system icon to the message form.
                        var messageBox = new MessageBoxForm("Could not create elements.  Ensure you use a valid name and try again.", "", false, false);
                        messageBox.ShowDialog();
                        return;
                    }

                    var question = new MessageBoxForm("Would you like to configure a dimming curve for this Prop?", "Dimming Curve Setup", MessageBoxButtons.YesNo, SystemIcons.Question);
                    var response = question.ShowDialog(this);
                    if (response == DialogResult.OK)
                    {
                        DimmingCurveHelper dimmingHelper = new DimmingCurveHelper(true);
                        dimmingHelper.Perform(createdElements);
                    }

                    ColorSetupHelper helper = new ColorSetupHelper();
                    helper.SetColorType(ElementColorType.FullColor);
                    helper.Perform(createdElements);

                    elementTree.AddNodePathToTree(new [] { createdElements.First() });
                    OnElementsChanged(new ElementsChangedEventArgs(ElementsChangedEventArgs.ElementsChangedAction.Add));
                    UpdateFormWithNode();
                    UpdateScrollPosition();
                }
            }
        }
        public async Task <ElementNode> CreateAsync()
        {
            return(await Task.Factory.StartNew(() =>
            {
                _elementModelMap = new Dictionary <Guid, ElementNode>();
                //Optimize the name check for performance. We know we are going to create a bunch of them and we can handle it ourselves more efficiently
                _elementNames = new HashSet <string>(VixenSystem.Nodes.Select(x => x.Name));

                var rootNode = _prop.RootNode;

                ElementNode rootElementNode = ElementNodeService.Instance.CreateSingle(null, NamingUtilities.Uniquify(_elementNames, TokenizeName(rootNode.Name)), true, false);
                PreviewCustomProp.Name = rootElementNode.Name;
                _elementNames.Add(rootElementNode.Name);

                _elementModelMap.Add(rootNode.Id, rootElementNode);

                CreateElementsForChildren(rootElementNode, rootNode);

                var parent = Application.OpenForms["VixenPreviewSetup3"];
                if (parent != null)
                {
                    //Get on the UI thread
                    parent.Invoke((MethodInvoker) delegate
                    {
                        var question = new MessageBoxForm("Would you like to configure a dimming curve for this element?", "Dimming Curve Setup", MessageBoxButtons.YesNo, SystemIcons.Question);
                        var ans = question.ShowDialog(parent);

                        if (ans == DialogResult.OK)
                        {
                            DimmingCurveHelper dimmingHelper = new DimmingCurveHelper(true);
                            dimmingHelper.Owner = parent;
                            dimmingHelper.Perform(_leafNodes);
                        }

                        if (_prop.PhysicalMetadata.ColorMode != ColorMode.Other)
                        {
                            //Now lets setup the color handling.
                            ColorSetupHelper helper = new ColorSetupHelper();
                            helper.Owner = parent;
                            switch (_prop.PhysicalMetadata.ColorMode)
                            {
                            case ColorMode.FullColor:
                                helper.SetColorType(ElementColorType.FullColor);
                                helper.SilentMode = true;
                                break;

                            case ColorMode.Multiple:
                                helper.SetColorType(ElementColorType.MultipleDiscreteColors);
                                break;

                            default:
                                helper.SetColorType(ElementColorType.SingleColor);
                                break;
                            }

                            helper.Perform(_leafNodes);
                            if (helper.GetColorType() != ElementColorType.FullColor)
                            {
                                EnsureFaceMapColors(rootElementNode);
                            }
                        }
                    });
                }

                PreviewCustomProp.UpdateColorType();

                PreviewCustomProp.Layout();

                return rootElementNode;
            }));
        }