Click here to Skip to main content
15,894,740 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.4K   834   69  
A WPF hybrid smart client that synchronises your goals with the Toodledo online To-do service.
//===============================================================================
// Goal Book.
// Copyright © 2009 Mark Brownsword. 
//===============================================================================

#region Using Statements
using System;
using System.Reflection;
using System.Windows.Input;
using GoalBook.Goals.Views;
using GoalBook.Infrastructure;
using GoalBook.Infrastructure.Constants;
using GoalBook.Infrastructure.Enums;
using GoalBook.Infrastructure.Interfaces;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Presentation.Commands;
#endregion

namespace GoalBook.Goals
{
    [Module(ModuleName = "Goals")]
    public class GoalsModule : IModule
    {        
        private readonly INavigationService _navigation;
        private readonly ILoggerService _logger;
        private readonly IDialogService _dialogService;
        private readonly IPersistenceService _persistenceService;
        private readonly IEventAggregator _eventAggregator;
        private readonly IPrintService _printService;
        private readonly ISettingsService _settingsService;
        private GoalsViewPresenter _goalsViewPresenter;        
        private string _moduleName;        
                                          
        /// <summary>
        /// Constructor.
        /// </summary>
        public GoalsModule(INavigationService navigation, ILoggerService logger, IDialogService dialogService,
            IPersistenceService persistenceService, IEventAggregator eventAggregator, IPrintService printService, ISettingsService settingsService)
        {
            _navigation = navigation;            
            _logger = logger;
            _dialogService = dialogService;
            _persistenceService = persistenceService;
            _eventAggregator = eventAggregator;
            _printService = printService;
            _settingsService = settingsService;
        }

        /// <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]);

            _moduleName = moduleAttribute.ModuleName;                        
            MenuInfo menuInfo = new MenuInfo();

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

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

            //Initialise GoalsViewPresenter.
            _goalsViewPresenter = new GoalsViewPresenter(_navigation, _dialogService, _logger, _persistenceService, _eventAggregator, _printService);
            _goalsViewPresenter.View = new GoalsView();
            _goalsViewPresenter.ActionView = new GoalsActionView();
            _goalsViewPresenter.InitViews(); 

            //Add Navigation MenuItem to the Shell's Go menu.
            _navigation.SetNavigationMetadata(menuInfo, _goalsViewPresenter.GetNewMenuInfo());
        }

        /// <summary>
        /// Handle ShowModuleExecuteHandler event. Load the default view each
        /// time the navigation menu item is executed.
        /// </summary>
        private void ShowModuleExecute_EventHandler(CommandInfo parameter)
        {
            if (parameter.Title != _moduleName) { return; }

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

        /// <summary>
        /// Handle CanShowModuleExecuteHandler event. Disable navigation
        /// when default view is currently active.
        /// </summary>
        private bool CanShowModuleExecute_EventHandler(CommandInfo parameter)
        {
            if (_goalsViewPresenter == null) 
            {
                return this._settingsService.StartUpModule != this._moduleName; 
            }
            return !_goalsViewPresenter.View.IsActive;
        }
    }
}

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