/// <summary>
        /// Bind a window to some commands to be executed by the viewmodel.
        /// </summary>
        /// <param name="win"></param>
        public void InitCommandBinding(Window win)
        {
            InitEditCommandBinding(win);

            win.CommandBindings.Add(new CommandBinding(AppCommand.Exit,
                                                       (s, e) =>
            {
                AppExit_CommandExecuted();
                e.Handled = true;
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.About,
                                                       (s, e) =>
            {
                AppAbout_CommandExecuted();
                e.Handled = true;
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.ProgramSettings,
                                                       (s, e) =>
            {
                AppProgramSettings_CommandExecuted();
                e.Handled = true;
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.ShowToolWindow,
                                                       (s, e) =>
            {
                if (!(e?.Parameter is IToolWindow toolwindowviewmodel))
                {
                    return;
                }


                if (toolwindowviewmodel is IRegisterableToolWindow)
                {
                    IRegisterableToolWindow registerTw = toolwindowviewmodel as IRegisterableToolWindow;

                    registerTw.SetToolWindowVisibility(this, !toolwindowviewmodel.IsVisible);
                }
                else
                {
                    toolwindowviewmodel.SetToolWindowVisibility(!toolwindowviewmodel.IsVisible);
                }

                e.Handled = true;
            }));
Example #2
0
        /// <summary>
        /// Bind a window to some commands to be executed by the viewmodel.
        /// </summary>
        /// <param name="win"></param>
        public void InitCommandBinding(Window win)
        {
            this.InitEditCommandBinding(win);

            win.CommandBindings.Add(new CommandBinding(AppCommand.Exit,
                                                       (s, e) =>
            {
                this.AppExit_CommandExecuted();
                e.Handled = true;
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.About,
                                                       (s, e) =>
            {
                this.AppAbout_CommandExecuted();
                e.Handled = true;
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.ProgramSettings,
                                                       (s, e) =>
            {
                this.AppProgramSettings_CommandExecuted();
                e.Handled = true;
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.ShowToolWindow,
                                                       (s, e) =>
            {
                if (e == null)
                {
                    return;
                }

                var toolwindowviewmodel = e.Parameter as IToolWindow;

                if (toolwindowviewmodel == null)
                {
                    return;
                }


                if (toolwindowviewmodel is IRegisterableToolWindow)
                {
                    IRegisterableToolWindow registerTW = toolwindowviewmodel as IRegisterableToolWindow;

                    registerTW.SetToolWindowVisibility(this, !toolwindowviewmodel.IsVisible);
                }
                else
                {
                    toolwindowviewmodel.SetToolWindowVisibility(!toolwindowviewmodel.IsVisible);
                }

                e.Handled = true;
            }));

            // Standard File New command binding via ApplicationCommands enumeration
            win.CommandBindings.Add(new CommandBinding(ApplicationCommands.New,
                                                       (s, e) =>
            {
                TypeOfDocument t = TypeOfDocument.EdiTextEditor;

                if (e != null)
                {
                    e.Handled = true;

                    if (e.Parameter != null)
                    {
                        if (e.Parameter is TypeOfDocument)
                        {
                            t = (TypeOfDocument)e.Parameter;
                        }
                    }
                }

                this.OnNew(t);
            }
                                                       ));

            // Standard File Open command binding via ApplicationCommands enumeration
            win.CommandBindings.Add(new CommandBinding(ApplicationCommands.Open,
                                                       (s, e) =>
            {
                string t = string.Empty;

                if (e != null)
                {
                    if (e.Parameter != null)
                    {
                        if (e.Parameter is string)
                        {
                            t = (string)e.Parameter;
                        }
                    }
                }

                this.OnOpen(t);
                e.Handled = true;
            }
                                                       ));

            // Close Document command
            // Closes the FileViewModel document supplied in e.parameter
            // or the Active document
            win.CommandBindings.Add(new CommandBinding(AppCommand.CloseFile,
                                                       (s, e) =>
            {
                try
                {
                    FileBaseViewModel f = null;

                    if (e != null)
                    {
                        e.Handled = true;
                        f         = e.Parameter as FileBaseViewModel;
                    }

                    if (f != null)
                    {
                        this.Close(f);
                    }
                    else
                    {
                        if (this.ActiveDocument != null)
                        {
                            this.Close(this.ActiveDocument);
                        }
                    }
                }
                catch (Exception exp)
                {
                    logger.Error(exp.Message, exp);
                    _MsgBox.Show(exp, Edi.Util.Local.Strings.STR_MSG_IssueTrackerTitle, MsgBoxButtons.OK, MsgBoxImage.Error, MsgBoxResult.NoDefaultButton,
                                 this.mAppCore.IssueTrackerLink,
                                 this.mAppCore.IssueTrackerLink,
                                 Edi.Util.Local.Strings.STR_MSG_IssueTrackerText, null, true);
                }
            },
                                                       (s, e) =>
            {
                try
                {
                    if (e != null)
                    {
                        e.Handled    = true;
                        e.CanExecute = false;

                        EdiViewModel f = null;

                        if (e != null)
                        {
                            e.Handled = true;
                            f         = e.Parameter as EdiViewModel;
                        }

                        if (f != null)
                        {
                            e.CanExecute = f.CanClose();
                        }
                        else
                        {
                            if (this.ActiveDocument != null)
                            {
                                e.CanExecute = this.ActiveDocument.CanClose();
                            }
                        }
                    }
                }
                catch (Exception exp)
                {
                    logger.Error(exp.Message, exp);
                    _MsgBox.Show(exp, Edi.Util.Local.Strings.STR_MSG_IssueTrackerTitle, MsgBoxButtons.OK, MsgBoxImage.Error, MsgBoxResult.NoDefaultButton,
                                 this.mAppCore.IssueTrackerLink,
                                 this.mAppCore.IssueTrackerLink,
                                 Util.Local.Strings.STR_MSG_IssueTrackerText, null, true);
                }
            }));

            // Change the WPF/TextEditor highlighting theme currently used in the application
            win.CommandBindings.Add(new CommandBinding(AppCommand.ViewTheme,
                                                       (s, e) => this.ChangeThemeCmd_Executed(s, e, win.Dispatcher)));

            win.CommandBindings.Add(new CommandBinding(AppCommand.BrowseURL,
                                                       (s, e) =>
            {
                Process.Start(new ProcessStartInfo("https://github.com/Dirkster99/Edi"));
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.ShowStartPage,
                                                       (s, e) =>
            {
                StartPageViewModel spage = this.GetStartPage(true);

                if (spage != null)
                {
                    logger.InfoFormat("TRACE Before setting startpage as ActiveDocument");
                    this.ActiveDocument = spage;
                    logger.InfoFormat("TRACE After setting startpage as ActiveDocument");
                }
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.ToggleOptimizeWorkspace,
                                                       (s, e) =>
            {
                logger.InfoFormat("TRACE AppCommand.ToggleOptimizeWorkspace parameter is {0}.", (e == null ? "(null)" : e.ToString()));

                try
                {
                    var newViewSetting            = !this.IsWorkspaceAreaOptimized;
                    this.IsWorkspaceAreaOptimized = newViewSetting;
                }
                catch (Exception exp)
                {
                    logger.Error(exp.Message, exp);
                    _MsgBox.Show(exp, Edi.Util.Local.Strings.STR_MSG_IssueTrackerTitle, MsgBoxButtons.OK, MsgBoxImage.Error, MsgBoxResult.NoDefaultButton,
                                 this.mAppCore.IssueTrackerLink, this.mAppCore.IssueTrackerLink,
                                 Util.Local.Strings.STR_MSG_IssueTrackerText, null, true);
                }
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.LoadFile,
                                                       (s, e) =>
            {
                try
                {
                    logger.InfoFormat("TRACE AppCommand.LoadFile parameter is {0}.", (e == null ? "(null)" : e.ToString()));

                    if (e == null)
                    {
                        return;
                    }

                    string filename = e.Parameter as string;

                    if (filename == null)
                    {
                        return;
                    }

                    logger.InfoFormat("TRACE AppCommand.LoadFile with: '{0}'", filename);

                    this.Open(filename);
                }
                catch (Exception exp)
                {
                    logger.Error(exp.Message, exp);
                    _MsgBox.Show(exp, Edi.Util.Local.Strings.STR_MSG_IssueTrackerTitle, MsgBoxButtons.OK, MsgBoxImage.Error, MsgBoxResult.NoDefaultButton,
                                 this.mAppCore.IssueTrackerLink, this.mAppCore.IssueTrackerLink,
                                 Util.Local.Strings.STR_MSG_IssueTrackerText, null, true);
                }
            }));

            win.CommandBindings.Add(new CommandBinding(ApplicationCommands.Save,
                                                       (s, e) =>
            {
                try
                {
                    if (e != null)
                    {
                        e.Handled = true;
                    }

                    if (this.ActiveDocument != null)
                    {
                        this.OnSave(this.ActiveDocument, false);
                    }
                }
                catch (Exception exp)
                {
                    logger.Error(exp.Message, exp);
                    _MsgBox.Show(exp, Edi.Util.Local.Strings.STR_MSG_UnknownError_Caption,
                                 MsgBoxButtons.OK, MsgBoxImage.Error, MsgBoxResult.NoDefaultButton,
                                 this.mAppCore.IssueTrackerLink, this.mAppCore.IssueTrackerLink,
                                 Edi.Util.Local.Strings.STR_MSG_IssueTrackerText, null, true);
                }
            },
                                                       (s, e) =>
            {
                if (e != null)
                {
                    e.Handled = true;

                    if (this.ActiveDocument != null)
                    {
                        e.CanExecute = this.ActiveDocument.CanSave();
                    }
                }
            }));

            win.CommandBindings.Add(new CommandBinding(ApplicationCommands.SaveAs,
                                                       (s, e) =>
            {
                try
                {
                    if (e != null)
                    {
                        e.Handled = true;
                    }

                    if (this.ActiveDocument != null)
                    {
                        if (this.OnSave(this.ActiveDocument, true))
                        {
                            var mruList = ServiceLocator.Current.GetInstance <IMRUListViewModel>();
                            mruList.UpdateEntry(this.ActiveDocument.FilePath);
                            this.mSettingsManager.SessionData.LastActiveFile = this.ActiveDocument.FilePath;
                        }
                    }
                }
                catch (Exception exp)
                {
                    logger.Error(exp.Message, exp);
                    _MsgBox.Show(exp, Edi.Util.Local.Strings.STR_MSG_IssueTrackerTitle, MsgBoxButtons.OK, MsgBoxImage.Error, MsgBoxResult.NoDefaultButton,
                                 this.mAppCore.IssueTrackerLink,
                                 this.mAppCore.IssueTrackerLink,
                                 Edi.Util.Local.Strings.STR_MSG_IssueTrackerText, null, true);
                }
            },
                                                       (s, e) =>
            {
                try
                {
                    if (e != null)
                    {
                        e.Handled    = true;
                        e.CanExecute = false;

                        if (this.ActiveDocument != null)
                        {
                            e.CanExecute = this.ActiveDocument.CanSaveAs();
                        }
                    }
                }
                catch (Exception exp)
                {
                    logger.Error(exp.Message, exp);
                    _MsgBox.Show(exp, Edi.Util.Local.Strings.STR_MSG_IssueTrackerTitle, MsgBoxButtons.OK, MsgBoxImage.Error, MsgBoxResult.NoDefaultButton,
                                 this.mAppCore.IssueTrackerLink,
                                 this.mAppCore.IssueTrackerLink,
                                 Edi.Util.Local.Strings.STR_MSG_IssueTrackerText, null, true);
                }
            }
                                                       ));

            // Execute a command to save all edited files and current program settings
            win.CommandBindings.Add(new CommandBinding(AppCommand.SaveAll,
                                                       (s, e) =>
            {
                try
                {
                    // Save all edited documents
                    if (this.mFiles != null)              // Close all open files and make sure there are no unsaved edits
                    {                                     // If there are any: Ask user if edits should be saved
                        IFileBaseViewModel activeDoc = this.ActiveDocument;

                        try
                        {
                            for (int i = 0; i < this.Files.Count; i++)
                            {
                                IFileBaseViewModel f = this.Files[i];

                                if (f != null)
                                {
                                    if (f.IsDirty == true && f.CanSaveData == true)
                                    {
                                        this.ActiveDocument = f;
                                        this.OnSave(f);
                                    }
                                }
                            }
                        }
                        catch (Exception exp)
                        {
                            _MsgBox.Show(exp.ToString(), Edi.Util.Local.Strings.STR_MSG_UnknownError_Caption, MsgBoxButtons.OK);
                        }
                        finally
                        {
                            if (activeDoc != null)
                            {
                                this.ActiveDocument = activeDoc;
                            }
                        }
                    }

                    // Save program settings
                    this.SaveConfigOnAppClosed();
                }
                catch (Exception exp)
                {
                    logger.Error(exp.Message, exp);
                    _MsgBox.Show(exp, Edi.Util.Local.Strings.STR_MSG_IssueTrackerTitle, MsgBoxButtons.OK, MsgBoxImage.Error, MsgBoxResult.NoDefaultButton,
                                 this.mAppCore.IssueTrackerLink,
                                 this.mAppCore.IssueTrackerLink,
                                 Edi.Util.Local.Strings.STR_MSG_IssueTrackerText, null, true);
                }
            }));

            // Execute a command to export UML editor content as image
            win.CommandBindings.Add(new CommandBinding(AppCommand.ExportUMLToImage,
                                                       (s, e) =>
            {
                try
                {
                    if (this.vm_DocumentViewModel != null)
                    {
                        if ((this.vm_DocumentViewModel.dm_DocumentDataModel.State == DataModel.ModelState.Ready) == true)
                        {
                            this.vm_DocumentViewModel.ExecuteExport(s, e, this.ActiveDocument.FileName + ".png");
                        }
                    }
                }
                catch (Exception exp)
                {
                    logger.Error(exp.Message, exp);
                    _MsgBox.Show(exp, Edi.Util.Local.Strings.STR_MSG_IssueTrackerTitle, MsgBoxButtons.OK, MsgBoxImage.Error, MsgBoxResult.NoDefaultButton,
                                 this.mAppCore.IssueTrackerLink,
                                 this.mAppCore.IssueTrackerLink,
                                 Edi.Util.Local.Strings.STR_MSG_IssueTrackerText, null, true);
                }
            },
                                                       (s, e) => // Execute this command only if an UML document is currently active
            {
                if (this.vm_DocumentViewModel != null)
                {
                    e.CanExecute = (this.vm_DocumentViewModel.dm_DocumentDataModel.State == DataModel.ModelState.Ready);
                }
                else
                {
                    e.CanExecute = false;
                }
            }
                                                       ));

            // Execute a command to export Text editor content as highlighted image content
            win.CommandBindings.Add(new CommandBinding(AppCommand.ExportTextToHTML,
                                                       (s, e) =>
            {
                try
                {
                    if (this.ActiveEdiDocument != null)
                    {
                        this.ActiveEdiDocument.ExportToHTML(this.ActiveDocument.FileName + ".html",
                                                            this.mSettingsManager.SettingData.TextToHTML_ShowLineNumbers,
                                                            this.mSettingsManager.SettingData.TextToHTML_AlternateLineBackground);
                    }
                }
                catch (Exception exp)
                {
                    logger.Error(exp.Message, exp);
                    _MsgBox.Show(exp, Edi.Util.Local.Strings.STR_MSG_IssueTrackerTitle, MsgBoxButtons.OK, MsgBoxImage.Error, MsgBoxResult.NoDefaultButton,
                                 this.mAppCore.IssueTrackerLink,
                                 this.mAppCore.IssueTrackerLink,
                                 Edi.Util.Local.Strings.STR_MSG_IssueTrackerText, null, true);
                }
            },
                                                       (s, e) => // Execute this command only if a Text document is currently active
            {
                if (this.ActiveEdiDocument != null)
                {
                    e.CanExecute = true;
                }
                else
                {
                    e.CanExecute = false;
                }
            }
                                                       ));


            /// <summary>
            /// Removes ALL MRU entries (even pinned entries) from the current list of entries.
            /// </summary>
            win.CommandBindings.Add(new CommandBinding(AppCommand.ClearAllMruItemsCommand,
                                                       (s, e) =>
            {
                this.GetToolWindowVM <RecentFilesViewModel>().MruList.Clear();
            }));

            /// <summary>
            /// Gets a command that removes all items that are older
            /// than a given <see cref="GroupType"/>.
            /// Eg.: Remove all MRU entries older than yesterday.
            /// </summary>
            win.CommandBindings.Add(new CommandBinding(AppCommand.RemoveItemsOlderThanThisCommand,
                                                       (s, e) =>
            {
                if (e.Parameter is GroupType == false)
                {
                    return;
                }

                var param = (GroupType)e.Parameter;

                this.GetToolWindowVM <RecentFilesViewModel>().MruList.RemoveEntryOlderThanThis(param);
            },
                                                       (s, e) =>
            {
                if (e.Parameter is GroupType == false)
                {
                    e.CanExecute = false;
                    return;
                }

                e.CanExecute = true;
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.MovePinnedMruItemUPCommand,
                                                       (s, e) =>
            {
                if (e.Parameter is IMRUEntryViewModel == false)
                {
                    return;
                }

                var param = e.Parameter as IMRUEntryViewModel;

                this.GetToolWindowVM <RecentFilesViewModel>().MruList.MovePinnedEntry(MoveMRUItem.Up, param);
            },
                                                       (s, e) =>
            {
                if (e.Parameter is IMRUEntryViewModel == false)
                {
                    e.CanExecute = false;
                    return;
                }

                if ((e.Parameter as IMRUEntryViewModel).IsPinned == 0)  //Make sure it is pinned
                {
                    e.CanExecute = false;
                    return;
                }

                e.CanExecute = true;
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.MovePinnedMruItemDownCommand,
                                                       (s, e) =>
            {
                if (e.Parameter is IMRUEntryViewModel == false)
                {
                    return;
                }

                var param = e.Parameter as IMRUEntryViewModel;

                this.GetToolWindowVM <RecentFilesViewModel>().MruList.MovePinnedEntry(MoveMRUItem.Down, param);
            },
                                                       (s, e) =>
            {
                if (e.Parameter is IMRUEntryViewModel == false)
                {
                    e.CanExecute = false;
                    return;
                }

                if ((e.Parameter as IMRUEntryViewModel).IsPinned == 0)  //Make sure it is pinned
                {
                    e.CanExecute = false;
                    return;
                }

                e.CanExecute = true;
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.PinItemCommand,
                                                       (s, e) =>
            {
                this.GetToolWindowVM <RecentFilesViewModel>().MruList.PinUnpinEntry(true, e.Parameter as IMRUEntryViewModel);
            },
                                                       (s, e) =>
            {
                if (e.Parameter is IMRUEntryViewModel == false)
                {
                    e.CanExecute = false;
                    return;
                }

                if ((e.Parameter as IMRUEntryViewModel).IsPinned == 0)  //Make sure it is pinned
                {
                    e.CanExecute = true;
                    return;
                }

                e.CanExecute = false;
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.UnPinItemCommand,
                                                       (s, e) =>
            {
                if (e.Parameter is IMRUEntryViewModel == false)
                {
                    return;
                }

                var param = e.Parameter as IMRUEntryViewModel;

                this.GetToolWindowVM <RecentFilesViewModel>().MruList.PinUnpinEntry(false, e.Parameter as IMRUEntryViewModel);
            },
                                                       (s, e) =>
            {
                if (e.Parameter is IMRUEntryViewModel == false)
                {
                    e.CanExecute = false;
                    return;
                }

                if ((e.Parameter as IMRUEntryViewModel).IsPinned == 0)  //Make sure it is pinned
                {
                    e.CanExecute = false;
                    return;
                }

                e.CanExecute = true;
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.PinUnpin,
                                                       (s, e) =>
            {
                this.PinCommand_Executed(e.Parameter, e);
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.RemoveMruEntry,
                                                       (s, e) =>
            {
                this.RemoveMRUEntry_Executed(e.Parameter, e);
            }));

            win.CommandBindings.Add(new CommandBinding(AppCommand.AddMruEntry,
                                                       (s, e) =>
            {
                this.AddMRUEntry_Executed(e.Parameter, e);
            }));
        }