コード例 #1
0
        private void SaveCustomAction()
        {
            // TODO: Move this somewhere else
            using (var form = new SaveCustomActionForm())
            {
                form.txtName.Text    = CurrentCustomAction?.Name;
                form.txtTooltip.Text = CurrentCustomAction?.Tooltip;
                form.Context         = CurrentCustomAction?.ValidContexts ?? UI.Selection.Context;

                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;

                    ScriptEngine.CompileCustomActions(new CustomActionsJson()
                    {
                        Actions = new[] { act }
                    });
                    if (ScriptEngine.CustomActionError)
                    {
                        MessageBox.Show("Compile failed, custom action contains errors and cannot be saved.", "Validation failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    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);
                    CurrentCustomAction = act;

                    ScriptEngine.AddCustomActions(UI.Actions);
                    PopulateCustomActionsDropDown();
                }
            }
        }
コード例 #2
0
ファイル: FormMain.cs プロジェクト: yzwbrian/TabularEditor
        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();
            }
        }