// Eventhandlers

        void BtnFolderBrowserDialog_Click(object sender, EventArgs e)
        {
            using (var folderBrowseDialog = new FolderBrowserDialog {
                Description = Properties.Resources.FolderBrowseDialogDescription
            })
            {
                var root = Path2.GetFullPathFromPathWithVariables(txtPath.Text);
                if (System.IO.Directory.Exists(root))
                {
                    folderBrowseDialog.SelectedPath = root;
                }

                if (folderBrowseDialog.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }

                var entry = GetPathWithVariablesFromFullPath(folderBrowseDialog.SelectedPath);

                if (!string.IsNullOrEmpty(entry) && !folderBrowseDialog.SelectedPath.Equals(entry, StringComparison.OrdinalIgnoreCase) && MessageBox.Show(this, Properties.Resources.PathAsVariableMessage.FormatString(folderBrowseDialog.SelectedPath, entry), Owner.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    txtPath.Text = entry;
                    lblEnvironmentFullPath.Text = Path2.GetFullPathFromPathWithVariables(entry);
                }
                else
                {
                    txtPath.Text = folderBrowseDialog.SelectedPath;
                    lblEnvironmentFullPath.Text = folderBrowseDialog.SelectedPath;
                }
            }
        }
        void RunTravers(MacroContainer container, bool supressMessage = false)
        {
            if (IsValidDirectory(out string message))
            {
                var menu = GetMacroMenu(container.Form);

                if (menu != null)
                {
                    var button = menu.InsertCommandbarButton(1, Key_Button_OpenInExplorer, MacroManagerResources.OpenInExplorerCommand);
                    container.Tools.Add(button.Tool, "OpenInExplorerCommand");
                }

                var macrosDirectory = Path2.GetFullPathFromPathWithVariables(Settings[Key_MacrosDirectory]);


                int folderCounter = 1;
                int fileCounter   = 1;

                if (Directory.Exists(macrosDirectory))
                {
                    DirectoryConverter.Travers(menu, 3, ref folderCounter, ref fileCounter, macrosDirectory, container, true);
                }
            }
            else
            {
                if (!supressMessage)
                {
                    MessageBox.Show(container.Form, message, container.Form.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        // Methods

        void InitializeDirectory(string directory)
        {
            if (!string.IsNullOrEmpty(directory))
            {
                txtPath.Text = directory;
                lblEnvironmentFullPath.Text = Path2.GetFullPathFromPathWithVariables(directory);
            }
        }
 void ToolStripItem_ItemClick(object sender, EventArgs e)
 {
     if (sender is ToolStripItem item)
     {
         txtPath.Text = item.Text;
         lblEnvironmentFullPath.Text = Path2.GetFullPathFromPathWithVariables(item.Text);
     }
 }
        bool IsValidDirectory(out string message)
        {
            message = null;

            if (!Settings.ContainsKey(Key_MacrosDirectory))
            {
                message = MacroManagerResources.ConfigurateAddonMessage;
                return(false);
            }

            if (!Directory.Exists(Path2.GetFullPathFromPathWithVariables(Settings[Key_MacrosDirectory])))
            {
                message = MacroManagerResources.DirectoryNotFoundMessage;
                return(false);
            }

            return(true);
        }
        bool ExistDirectory(string directory)
        {
            if (string.IsNullOrEmpty(directory))
            {
                MessageBox.Show(this, Properties.Resources.FolderBrowseDialogDescription, Owner.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }

            var path = Path2.GetFullPathFromPathWithVariables(directory);

            if (System.IO.Directory.Exists(path))
            {
                return(true);
            }

            MessageBox.Show(this, Properties.Resources.DirectoryNotResolveMessage.FormatString(path), Owner.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            return(false);
        }
        protected override void OnBeforePerformingCommand(BeforePerformingCommandEventArgs e)
        {
            e.Handled = true;

            var container = _containers.FirstOrDefault(c => c.Form.Equals(e.Form));

            switch (e.Key)
            {
            case (Key_Button_ShowMacroEditor):
            {
                CurrentEditor(e.Form, false, out bool hidden, out bool isNew).Activate();
            }
            break;

            case (Key_Button_Refresh):
            {
                UpdateTools(container);
            }
            break;

            case (Key_Button_Config):
            {
                using (var directoryDialog = new DirectoryDialog(Settings.TryGetStringValue(Key_MacrosDirectory))
                    {
                        Owner = e.Form
                    })
                {
                    if (directoryDialog.ShowDialog() == DialogResult.OK)
                    {
                        Settings[Key_MacrosDirectory] = directoryDialog.Directory;
                        Program.Settings.InitialDirectories.SetInitialDirectoryContext(Citavi.Settings.InitialDirectoryContext.Macros, Path2.GetFullPathFromPathWithVariables(directoryDialog.Directory));
                        UpdateTools(container);
                    }
                }
            }
            break;

            case (Key_Button_OpenInExplorer):
            {
                if (IsValidDirectory(out string message))
                {
                    var path = Path2.GetFullPathFromPathWithVariables(Settings[Key_MacrosDirectory]);
                    Process.Start("explorer.exe", path);
                }
                else
                {
                    MessageBox.Show(e.Form, message, e.Form.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            break;

            default:

                if (e.Key.StartsWith(Key_Button_Directory, StringComparison.OrdinalIgnoreCase))
                {
                    if (container.Macros.ContainsKey(e.Key))
                    {
                        var macro = container.Macros[e.Key];

                        if (File.Exists(macro.Path))
                        {
                            var hide = macro.Action == MacroAction.Run;

                            _editor = CurrentEditor(e.Form, hide, out bool hidden, out bool isNew);

                            if (!isNew && _editor.IsDirty())
                            {
                                if (MessageBox.Show(e.Form, MacroManagerResources.UserWarningSaveMessage, e.Form.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                                {
                                    _editor.Save();
                                }
                            }

                            _editor.MacroCode = File.ReadAllText(macro.Path);
                            _editor.SetFilePath(macro.Path);
                            _editor.Activate();

                            if (macro.Action == MacroAction.Run)
                            {
                                _editor.Run();
                            }

                            if (hidden)
                            {
                                _editor.Close();
                            }
                        }
                        else
                        {
                            MessageBox.Show(e.Form, MacroManagerResources.PathNotFoundMessage.FormatString(macro.Path), e.Form.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            UpdateTools(container, true);
                        }
                    }
                    else
                    {
                        e.Handled = false;
                    }
                }
                else
                {
                    e.Handled = false;
                }
                break;
            }

            base.OnBeforePerformingCommand(e);
        }
 void TxtPath_TextChanged(object sender, EventArgs e) => lblEnvironmentFullPath.Text = Path2.GetFullPathFromPathWithVariables(txtPath.Text);
Beispiel #9
0
        // Methods

        public override void OnBeforePerformingCommand(MainForm mainForm, BeforePerformingCommandEventArgs e)
        {
            e.Handled = true;

            var container = _containers.FirstOrDefault(c => c.MainForm.Equals(mainForm));

            switch (e.Key)
            {
            case ButtonKey_ShowMacroEditor:
            {
                GetOrCreateMacroEditorForm(false, out bool _, out bool _).Activate();
            }
            break;

            case ButtonKey_Refresh:
            {
                UpdateTools(container);
            }
            break;

            case ButtonKey_Configurate:
            {
                using (var directoryDialog = new DirectoryForm(mainForm, Settings.TryGetStringValue(SettingsKey)))
                {
                    if (directoryDialog.ShowDialog() == DialogResult.OK)
                    {
                        if (!directoryDialog.Directory.Equals(Settings.TryGetStringValue(SettingsKey), StringComparison.OrdinalIgnoreCase))
                        {
                            Settings[SettingsKey] = directoryDialog.Directory;
                            Program.Settings.InitialDirectories.SetInitialDirectoryContext(Citavi.Settings.InitialDirectoryContext.Macros, Path2.GetFullPathFromPathWithVariables(directoryDialog.Directory));
                            UpdateTools(container);
                        }
                    }
                }
            }
            break;

            case ButtonKey_OpenInExplorer:
            {
                if (IsValidDirectory(out string message))
                {
                    var path = Path2.GetFullPathFromPathWithVariables(Settings[SettingsKey]);
                    Process.Start("explorer.exe", path);
                }
                else
                {
                    MessageBox.Show(mainForm, message, mainForm.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            break;

            default:

                if (e.Key.StartsWith(ButtonKey, StringComparison.OrdinalIgnoreCase))
                {
                    if (container.Macros.ContainsKey(e.Key))
                    {
                        var macro = container.Macros[e.Key];

                        if (File.Exists(macro.Path))
                        {
                            var hide = macro.Action == MacroAction.Run;

                            var editor = GetOrCreateMacroEditorForm(hide, out bool hidden, out bool isNew);

                            if (!isNew && editor.IsDirty())
                            {
                                if (MessageBox.Show(mainForm, Properties.Resources.UserWarningSaveMessage, mainForm.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                                {
                                    editor.Save();
                                }
                            }

                            editor.MacroCode = File.ReadAllText(macro.Path);
                            editor.SetFilePath(macro.Path);
                            editor.Activate();

                            if (macro.Action == MacroAction.Run)
                            {
                                editor.Run();
                            }

                            if (hidden)
                            {
                                editor.Close();
                            }
                        }
                        else
                        {
                            MessageBox.Show(mainForm, Properties.Resources.PathNotFoundMessage.FormatString(macro.Path), mainForm.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            UpdateTools(container, true);
                        }
                    }
                    else
                    {
                        e.Handled = false;
                    }
                }
                else
                {
                    e.Handled = false;
                }
                break;
            }
        }