Click here to Skip to main content
15,886,110 members
Articles / Desktop Programming / WPF

GoalBook - A Hybrid Smart Client

Rate me:
Please Sign up or sign in to vote.
4.86/5 (24 votes)
25 Sep 2009CPOL10 min read 79.1K   834   69  
A WPF hybrid smart client that synchronises your goals with the Toodledo online To-do service.
// <copyright file="NotesDialogViewPresenter.cs" company="GoalBook"> 
//    Copyright © 2009 Mark Brownsword. All rights reserved.
//    This source code and supporting files are licensed under The Code Project  
//    Open License (CPOL) as detailed at http://www.codeproject.com/info/cpol10.aspx. 
// </copyright>
namespace GoalBook.Notes.Views
{
    #region Using Statements
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Documents;
    using System.Windows.Input;
    using GoalBook.Infrastructure;
    using GoalBook.Infrastructure.Converters;
    using GoalBook.Infrastructure.Enums;
    using GoalBook.Infrastructure.Helpers;
    using GoalBook.Infrastructure.Interfaces;
    using GoalBook.Infrastructure.ObjectModel;
    using GoalBook.Notes.Properties;
    using Microsoft.Practices.Composite.Presentation.Commands;
    #endregion

    /// <summary>
    /// Notes Dialog View Presenter class.
    /// </summary>
    public class NotesDialogViewPresenter
    {
        #region Constants and Enums
        /// <summary>
        /// Declaration for IPersistenceService.
        /// </summary>
        private readonly IPersistenceService persistenceService;

        /// <summary>
        /// Declaration for IDialogService.
        /// </summary>
        private readonly IDialogService dialogService;

        /// <summary>
        /// Declaration for XamlToFlowDocumentConverter.
        /// </summary>
        private XamlToFlowDocumentConverter converter;
        #endregion

        #region Instance and Shared Fields
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the NotesDialogViewPresenter class.
        /// </summary>
        /// <param name="editNote">Note parameter</param>
        /// <param name="persistenceService">IPersistenceService parameter</param>
        /// <param name="dialogService">IDialogService parameter</param>
        public NotesDialogViewPresenter(Note editNote, IPersistenceService persistenceService, IDialogService dialogService)
        {
            // Reference to PersistenceService.
            this.persistenceService = persistenceService;

            // Reference to DialogService
            this.dialogService = dialogService;

            // Initialise the XamlToFlowDocumentConverter.
            this.converter = new XamlToFlowDocumentConverter();
            
            // Initialise Model.
            this.Model = new NotesDialogViewPresentationModel(new ModuleCommands());
            this.Model.EditNote = editNote;            
            this.Model.EditNote.PropertyChanged += new PropertyChangedEventHandler(this.AddNote_PropertyChanged);
            this.Model.CutCommand = new DelegateCommand<CommandInfo>(this.CommandExecute_EventHandler, this.CanCommandExecute_EventHandler);
            this.Model.CopyCommand = new DelegateCommand<CommandInfo>(this.CommandExecute_EventHandler, this.CanCommandExecute_EventHandler);
            this.Model.PasteCommand = new DelegateCommand<CommandInfo>(this.CommandExecute_EventHandler, this.CanCommandExecute_EventHandler);
            this.Model.UndoCommand = new DelegateCommand<CommandInfo>(this.CommandExecute_EventHandler, this.CanCommandExecute_EventHandler);
            this.Model.RedoCommand = new DelegateCommand<CommandInfo>(this.CommandExecute_EventHandler, this.CanCommandExecute_EventHandler);
            this.Model.BoldCommand = new DelegateCommand<CommandInfo>(this.CommandExecute_EventHandler, this.CanCommandExecute_EventHandler);
            this.Model.ItalicCommand = new DelegateCommand<CommandInfo>(this.CommandExecute_EventHandler, this.CanCommandExecute_EventHandler);
            this.Model.LinkCommand = new DelegateCommand<CommandInfo>(this.CommandExecute_EventHandler, this.CanCommandExecute_EventHandler);
            this.Model.OrderedListCommand = new DelegateCommand<CommandInfo>(this.CommandExecute_EventHandler, this.CanCommandExecute_EventHandler);
            this.Model.UnorderedListCommand = new DelegateCommand<CommandInfo>(this.CommandExecute_EventHandler, this.CanCommandExecute_EventHandler);
            this.Model.IndentCommand = new DelegateCommand<CommandInfo>(this.CommandExecute_EventHandler, this.CanCommandExecute_EventHandler);
            this.Model.OutdentCommand = new DelegateCommand<CommandInfo>(this.CommandExecute_EventHandler, this.CanCommandExecute_EventHandler);

            // Initialise ReferenceData            
            this.Model.FolderList = this.persistenceService.FetchFolderList();
        }
        #endregion

        #region Delegates and Events
        #endregion

        #region Properties
        /// <summary>
        /// Gets Model.
        /// </summary>
        public NotesDialogViewPresentationModel Model { get; private set; }

        /// <summary>
        /// Gets or sets View.
        /// </summary>
        public NotesDialogView View { get; set; }
        #endregion

        #region Public and internal Methods
        /// <summary>
        /// Initialise the View.
        /// </summary>
        public void InitView()
        {
            this.View.DataContext = this.Model;
            this.View.buttonLink.ToolTip = string.Format(Properties.Resources.ToolBarLinkFormatText, this.Model.LinkCommandInfo.KeyInputGesture.DisplayString); 
            this.View.NoteEditorInsertHyperlink += this.View_NoteEditorInsertHyperlink;
            this.View.NoteEditorSelectionChanged += this.View_NoteEditorSelectionChanged;
            this.View.NoteEditorFocusChanged += this.View_NoteEditorFocusChanged;
            this.View.NoteEditorTextChanged += this.View_NoteEditorTextChanged;
            this.View.NoteEditorPaste += this.View_NoteEditorPaste;
                        
            // Initialise Editor Document.
            this.LoadDocument();

            // Flag document loaded so text changed event can start firing.
            this.View.IsDocumentLoaded = true;           
        }                       
        #endregion

        #region Base Class Overrides
        #endregion

        #region Private and Protected Methods
        /// <summary>
        /// Raise ErrorInfo Changed. Notify Dialog that text has changed (so can set OK button enabled).
        /// </summary>
        private void RaiseErrorInfoChanged()
        {
            this.View.RaiseErrorInfoChanged(this.Model.EditNote as IDataErrorInfo);
        }
        
        /// <summary>
        /// Initialise Editor Document.
        /// </summary>
        private void LoadDocument()
        {            
            // Initialise the FlowDocument.
            FlowDocument flowDocument = this.converter.Convert(this.Model.EditNote.Text, null, null, null) as FlowDocument;
                                                                            
            // Locate all hyperlinks in the document and hook them
            // up to the Hyperlink_Click event handler.
            this.LocateAndHookHyperlinks(flowDocument);

            // Set IsDocumentLoaded False so text changed event doesn't fire when document is set.
            this.View.IsDocumentLoaded = false;

            // Initialise Editor Document.
            this.View.noteEditor.Document = flowDocument;
        }

        /// <summary>
        /// Locate And Hook Hyperlinks.
        /// </summary>
        /// <param name="flowDocument">FlowDocument parameter</param>
        private void LocateAndHookHyperlinks(FlowDocument flowDocument)
        {            
            FlowDocumentHelper helper = new FlowDocumentHelper(flowDocument);
            foreach (Hyperlink hyperlink in helper.GetHyperlinks())
            {
                this.HookHyperlink(hyperlink);
            }
        }

        /// <summary>
        /// Hook Hyperlink to event handler.
        /// </summary>
        /// <param name="hyperlink">Hyperlink parameter</param>
        private void HookHyperlink(Hyperlink hyperlink)
        {
            hyperlink.Click += this.Hyperlink_Click;
            hyperlink.Focusable = false;
            hyperlink.ToolTip = string.Format(
                "Ctrl + click to follow link: {0}", 
                hyperlink.NavigateUri);
        }

        /// <summary>
        /// Initialise Hyperlink.
        /// </summary>
        /// <param name="parameter">CommandInfo parameter</param>
        /// <returns>True when hyperlink initialised</returns>
        private bool InitialiseHyperlink(CommandInfo parameter)
        {
            TextSelection selection = this.View.noteEditor.Selection;            
            Mouse.SetCursor(Cursors.Wait);

            HyperlinkInfo info = new HyperlinkInfo("http://", selection.Text);

            HyperlinkDialogViewPresenter presenter = new HyperlinkDialogViewPresenter(info);
            presenter.View = new HyperlinkDialogView();
            presenter.InitView();

            DialogOptions options = new DialogOptions();
            options.AllowResize = false;
            options.DialogTitle = parameter.Title;
            options.IconUri = new Uri(Infrastructure.Constants.MenuConstants.MENU_INTERNET_LINK_IMAGE_URI);
            options.Buttons = ButtonType.OKCancel;

            // Display the dialog.
            bool? result = this.dialogService.ShowView(
                presenter.View, 
                options,
                presenter.Model.HyperlinkInfo as IDataErrorInfo);

            if (result == true)
            {
                Hyperlink hyperlink = new Hyperlink(selection.Start, selection.End);
                hyperlink.NavigateUri = new Uri(info.Uri);

                this.HookHyperlink(hyperlink);                
            }

            return result.Value;
        }
        
        /// <summary>
        /// Perform Paste operation
        /// </summary>
        /// <returns>True when paste occurred</returns>
        private bool PerformPaste()
        {
            Mouse.SetCursor(Cursors.Wait);

            try
            {
                if (Clipboard.ContainsText())
                {
                    // Ensure Clipboard contains only text.
                    Clipboard.SetText(Clipboard.GetText());                    
                    ApplicationCommands.Paste.Execute(null, this.View.noteEditor);

                    return true;
                }                
            }
            finally
            {
                Mouse.UpdateCursor();
            }

            return false;
        }
        #endregion

        #region Event Handlers
        /// <summary>
        /// Handle CommandExecute_EventHandler event.
        /// http://msdn.microsoft.com/en-us/library/system.windows.documents.editingcommands.aspx
        /// </summary> 
        /// <param name="parameter">CommandInfo parameter</param>
        private void CommandExecute_EventHandler(CommandInfo parameter)
        {
            bool updateRequired = false;
            if (parameter.Title == Resources.ToolBarCutText)
            {
                ApplicationCommands.Cut.Execute(null, this.View.noteEditor);
                updateRequired = true;
            }
            else if (parameter.Title == Resources.ToolBarCopyText)
            {
                ApplicationCommands.Copy.Execute(null, this.View.noteEditor);                
            }
            else if (parameter.Title == Resources.ToolBarPasteText)
            {
                if (this.PerformPaste())
                {
                    updateRequired = true;
                }
            }
            else if (parameter.Title == Resources.ToolBarUndoText)
            {
                ApplicationCommands.Undo.Execute(null, this.View.noteEditor);                
            }
            else if (parameter.Title == Resources.ToolBarRedoText)
            {
                ApplicationCommands.Redo.Execute(null, this.View.noteEditor);                
            }
            else if (parameter.Title == Resources.ToolBarBoldText)
            {
                EditingCommands.ToggleBold.Execute(null, this.View.noteEditor);
                updateRequired = true;
            }
            else if (parameter.Title == Resources.ToolBarItalicText)
            {
                EditingCommands.ToggleItalic.Execute(null, this.View.noteEditor);
                updateRequired = true;
            }
            else if (parameter.Title == Resources.ToolBarLinkText)
            {
                if (this.InitialiseHyperlink(parameter))
                {
                    updateRequired = true;
                }
            }
            else if (parameter.Title == Resources.ToolBarOrderedListText)
            {
                EditingCommands.ToggleNumbering.Execute(null, this.View.noteEditor);
                updateRequired = true;
            }
            else if (parameter.Title == Resources.ToolBarUnorderedListText)
            {
                EditingCommands.ToggleBullets.Execute(null, this.View.noteEditor);
                updateRequired = true;
            }
            else if (parameter.Title == Resources.ToolBarIndentText)
            {
                EditingCommands.IncreaseIndentation.Execute(null, this.View.noteEditor);
                updateRequired = true;
            }
            else if (parameter.Title == Resources.ToolBarOutdentText)
            {
                EditingCommands.DecreaseIndentation.Execute(null, this.View.noteEditor);
                updateRequired = true;
            }

            if (updateRequired)
            {
                // Set focus back to the editor.
                this.View.noteEditor.Focus();

                // Notify listeners that the note text has changed.
                this.RaiseErrorInfoChanged();              
            }
        }
                        
        /// <summary>
        /// Handle CanCommandExecute_EventHandler event.
        /// </summary>        
        /// <param name="parameter">CommandInfo parameter</param>
        /// <returns>True when command can execute</returns>
        private bool CanCommandExecute_EventHandler(CommandInfo parameter)
        {
            if (parameter == null)
            {
                return false;
            }

            if (!this.View.noteEditor.IsFocused)
            {
                return false;
            }

            if (parameter.Title == Resources.ToolBarLinkText)
            {
                if (this.View.noteEditor.Selection.IsEmpty || this.View.noteEditor.Selection.Text.ToString().Contains(Environment.NewLine.ToString()))
                {
                    return false;
                }                
            }

            return true;
        }

        /// <summary>
        /// Handle AddGoal_PropertyChanged event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">PropertyChangedEventArgs parameter</param>        
        private void AddNote_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.RaiseErrorInfoChanged();
        }

        /// <summary>
        /// Handle Hyperlink_Click event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">RoutedEventArgs parameter</param>
        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Hyperlink)
            {
                Process p = new Process();
                p.StartInfo.FileName = (sender as Hyperlink).NavigateUri.ToString();
                p.StartInfo.UseShellExecute = true;
                p.StartInfo.RedirectStandardOutput = false;
                p.StartInfo.Arguments = string.Empty;

                p.Start();
            }
        }

        /// <summary>
        /// Handle View_NoteEditorTextChanged event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">EventArgs parameter</param>
        private void View_NoteEditorTextChanged(object sender, EventArgs e)
        {
            // Notify listeners that the note text has changed.
            this.RaiseErrorInfoChanged();
        }
        
        /// <summary>
        /// Handle View_NoteEditorPaste event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">EventArgs parameter</param>
        private void View_NoteEditorPaste(object sender, EventArgs e)
        {
            this.PerformPaste();
        }
        
        /// <summary>
        /// Handle View_NoteEditorSelectionChanged event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">EventArgs parameter</param>
        private void View_NoteEditorSelectionChanged(object sender, EventArgs e)
        {            
            this.Model.LinkCommand.RaiseCanExecuteChanged();            
        }

        /// <summary>
        /// Handle View_NoteEditorFocusChanged event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">EventArgs parameter</param>
        private void View_NoteEditorFocusChanged(object sender, EventArgs e)
        {
            this.Model.CutCommand.RaiseCanExecuteChanged();
            this.Model.CopyCommand.RaiseCanExecuteChanged();
            this.Model.PasteCommand.RaiseCanExecuteChanged();
            this.Model.UndoCommand.RaiseCanExecuteChanged();
            this.Model.RedoCommand.RaiseCanExecuteChanged();
            this.Model.BoldCommand.RaiseCanExecuteChanged();
            this.Model.ItalicCommand.RaiseCanExecuteChanged();            
            this.Model.OrderedListCommand.RaiseCanExecuteChanged();
            this.Model.UnorderedListCommand.RaiseCanExecuteChanged();
            this.Model.IndentCommand.RaiseCanExecuteChanged();
            this.Model.OutdentCommand.RaiseCanExecuteChanged();
        }  

        /// <summary>
        /// Handle View_NoteEditorInsertHyperlink event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">EventArgs parameter</param>
        private void View_NoteEditorInsertHyperlink(object sender, EventArgs e)
        {
            if (this.Model.LinkCommand.CanExecute(this.Model.LinkCommandInfo))
            {
                this.Model.LinkCommand.Execute(this.Model.LinkCommandInfo);
            }
        }   
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
I've been working as a software developer since 2000 and hold a Bachelor of Business degree from The Open Polytechnic of New Zealand. Computers are for people and I aim to build applications for people that they would want to use.

Comments and Discussions