Click here to Skip to main content
15,881,139 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 79K   834   69  
A WPF hybrid smart client that synchronises your goals with the Toodledo online To-do service.
// <copyright file="NotesModule.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
{
    #region Using Statements
    using System;
    using System.Reflection;
    using System.Windows.Input;
    using GoalBook.Infrastructure;
    using GoalBook.Infrastructure.Constants;
    using GoalBook.Infrastructure.Enums;
    using GoalBook.Infrastructure.Interfaces;
    using GoalBook.Notes.Views;
    using Microsoft.Practices.Composite.Modularity;
    using Microsoft.Practices.Composite.Presentation.Commands;
    using Microsoft.Practices.Unity;
    #endregion

    /// <summary>
    /// Notes Module.
    /// </summary>
    [Module(ModuleName = "Notes")]
    public class NotesModule : IModule
    {
        #region Constants and Enums
        #endregion

        #region Inner Classes and Structures
        #endregion

        #region Delegates and Events
        #endregion

        #region Instance and Shared Fields
        /// <summary>
        /// Declaration for IUnityContainer.
        /// </summary>
        private readonly IUnityContainer container;

        /// <summary>
        /// Declaration for INavigationService.
        /// </summary>
        private readonly INavigationService navigation;

        /// <summary>
        /// Declaration for ISettingsService.
        /// </summary>
        private readonly ISettingsService settingsService;
        
        /// <summary>
        /// Declaration for moduleName.
        /// </summary>
        private string moduleName;
        
        /// <summary>
        /// Declaration for NotesViewPresenter.
        /// </summary>
        private NotesViewPresenter notesViewPresenter;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the NotesModule class.
        /// </summary>
        /// <param name="navigation">navigation parameter</param>
        /// <param name="container">container parameter</param>
        /// <param name="logger">logger parameter</param>
        /// <param name="settingsService">settingsService parameter</param>
        public NotesModule(INavigationService navigation, IUnityContainer container, ILoggerService logger, ISettingsService settingsService)
        {
            this.container = container;
            this.navigation = navigation;
            this.settingsService = settingsService;
        }
        #endregion

        #region Properties
        #endregion

        #region Public and internal Methods
        /// <summary>
        /// Initialize the module.
        /// </summary>
        public void Initialize()
        {
            // Get the ModuleName, as set in the ModuleAttribute for this class.
            ModuleAttribute moduleAttribute = (ModuleAttribute)this.GetType().GetCustomAttributes(
                typeof(ModuleAttribute), false)[0];
            
            this.moduleName = moduleAttribute.ModuleName;            
            MenuInfo menuInfo = new MenuInfo();

            // Initialise the DelegateCommand.
            menuInfo.MenuDelegateCommand = new DelegateCommand<CommandInfo>(
                this.ShowModuleExecute_EventHandler, this.CanShowModuleExecute_EventHandler);

            // Initialise the MenuCommandInfo.
            menuInfo.MenuCommandInfo = new CommandInfo(
                this.moduleName, 
                0, 
                ParentMenuType.Go,
                new Uri(MenuConstants.MENU_NEW_NOTE_IMAGE_URI, UriKind.Absolute), 
                null);

            // Initialise NotesViewPresenter.
            this.notesViewPresenter = this.container.Resolve<NotesViewPresenter>();
            this.notesViewPresenter.View = this.container.Resolve<NotesView>();
            this.notesViewPresenter.ActionView = this.container.Resolve<NotesActionView>();
            this.notesViewPresenter.InitViews();

            // Add Navigation MenuItem to the Shell's Go menu.
            this.navigation.SetNavigationMetadata(menuInfo, this.notesViewPresenter.GetNewMenuInfo());
        }
        #endregion

        #region Base Class Overrides
        #endregion

        #region Private and Protected Methods
        /// <summary>
        /// Handle ShowModuleExecuteHandler event. Load the default view each
        /// time the navigation menu item is executed.
        /// </summary>
        /// <param name="parameter">CommandInfo parameter</param>
        private void ShowModuleExecute_EventHandler(CommandInfo parameter)
        {
            if (parameter.Title != this.moduleName)
            {
                return;
            }

            Mouse.SetCursor(Cursors.Wait);
            try
            {                
                // Load and Activate the default view.
                if (this.navigation.LoadModuleView(
                        this.moduleName, 
                        parameter.IconUri, 
                        this.notesViewPresenter.View,
                        this.notesViewPresenter.ActionView,
                        this.notesViewPresenter.ViewMenuInfo))
                {
                    this.notesViewPresenter.View.Activate();
                    this.notesViewPresenter.ActionView.Activate();
                }
            }
            finally 
            { 
                Mouse.UpdateCursor(); 
            }
        }

        /// <summary>
        /// Handle CanShowModuleExecuteHandler event. Disable navigation
        /// when default view is currently active.
        /// </summary>
        /// <param name="parameter">CommandInfo parameter</param>
        /// <returns>Active status of module</returns>
        private bool CanShowModuleExecute_EventHandler(CommandInfo parameter)
        {
            if (this.notesViewPresenter == null)
            {
                return this.settingsService.StartUpModule != this.moduleName;
            }

            return !this.notesViewPresenter.View.IsActive;
        }
        #endregion
        
        #region Event Handlers
        #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