/// <summary>
 /// Initializes a new instance of the SaveElementState_Component class.
 /// </summary>
 public SaveElementState_Component()
     : base("Save Element States", "SaveStates",
            "This component lets you save the states of selected elements for later retrieval",
            "Human UI", "UI Main")
 {
     savedStates = new StateSet_Goo();
 }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            bool         restore = false;
            string       setName = "";
            StateSet_Goo states  = null;

            if (!DA.GetData <StateSet_Goo>("Saved States", ref states))
            {
                return;
            }
            if (!DA.GetData <string>("State Name to restore", ref setName))
            {
                return;
            }
            DA.GetData <bool>("Restore", ref restore);

            //if there's an output param, pass out the value of restore.
            if (Params.Output.Count > 0)
            {
                DA.SetData(0, restore);
            }

            if (!restore)
            {
                return;
            }

            //retrieve the appropriate state from the state set dictionary
            State stateToRestore = states.states[setName];

            //restore state
            foreach (KeyValuePair <UIElement_Goo, object> elementState in stateToRestore.stateDict)
            {
                UIElement element = elementState.Key.element;

                //if it has a parent, it's a real element that exists - if the parent is null we are in a new document session and the original element
                //may not really exist any longer.
                var parent = System.Windows.Media.VisualTreeHelper.GetParent(element);

                //This is the situation when opening a definition fresh - we have to retrtieve the element based on the saved state's
                //component instance guid and output data index. Because there's no way to serialize a  UIElement we have to retrieve it
                // from the component actively creating it (or more precisely a new but identical instance of it)

                if (parent == null)
                {
                    Guid id    = elementState.Key.instanceGuid;
                    int  index = elementState.Key.index;

                    UIElement_Goo newGoo = SaveElementState_Component.getElementGoo(this.OnPingDocument(), id, index);
                    element = newGoo.element;
                }
                //Once we've got the element, try to set its value.
                HUI_Util.TrySetElementValue(HUI_Util.extractBaseElement(element), elementState.Value);
            }
        }