Ejemplo n.º 1
0
        private void SaveCustomAction()
        {
            // TODO: Move this somewhere else
            var form = new SaveCustomActionForm();

            form.Context      = UI.Selection.Context;
            form.txtName.Text = CurrentCustomAction;

            var res = form.ShowDialog();

            if (res == DialogResult.OK)
            {
                var act = new CustomActionJson();
                act.Name          = form.txtName.Text;
                act.Tooltip       = form.txtTooltip.Text;
                act.Execute       = txtAdvanced.Text;
                act.Enabled       = "true";
                act.ValidContexts = form.Context;

                if (custActions == null)
                {
                    custActions = new CustomActionsJson()
                    {
                        Actions = new CustomActionJson[0]
                    }
                }
                ;

                // Remove any existing actions with the same name:
                custActions.Actions = custActions.Actions.Where(a => !a.Name.Equals(act.Name, StringComparison.InvariantCultureIgnoreCase)).ToArray();
                var toRemove = UI.Actions.OfType <CustomAction>().FirstOrDefault(a => a.BaseName.Equals(act.Name, StringComparison.InvariantCultureIgnoreCase));
                if (toRemove != null)
                {
                    UI.Actions.Remove(toRemove);
                }

                var list = custActions.Actions.ToList();

                list.Add(act);

                custActions.Actions = list.ToArray();
                custActions.SaveToJson(ScriptEngine.CustomActionsJsonPath);

                // Compile and add the newly created action:
                ScriptEngine.CompileCustomActions(new CustomActionsJson()
                {
                    Actions = new [] { act }
                });
                if (!ScriptEngine.CustomActionError)
                {
                    ScriptEngine.AddCustomActions(UI.Actions);
                }
                PopulateCustomActionsDropDown();
            }
        }