Click here to Skip to main content
15,897,187 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.5K   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.Collections.Generic;
using System.Windows.Controls;
using GoalBook.Infrastructure;
using GoalBook.Infrastructure.Enums;
using GoalBook.Infrastructure.Interfaces;
using GoalBook.Shell.Misc;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Composite.Regions;
#endregion

namespace GoalBook.Shell.Services
{
    public class NavigationService : INavigationService
    {
        private readonly IRegionManager _regionManager;
        private readonly IShellView _shellView;
        private readonly IGlobalCommandsProxy _commandProxy;
        private IView _activeView;
        private IView _activeActionView;
        private List<MenuItem> _moduleMenuItems;
        private List<DelegateCommand<CommandInfo>> _moduleCommands = new List<DelegateCommand<CommandInfo>>();

        /// <summary>
        /// Constructor.
        /// </summary>        
        public NavigationService(IRegionManager regionManager, IShellView shellView, IGlobalCommandsProxy commandProxy)
        {            
            _regionManager = regionManager;            
            _shellView = shellView;
            _commandProxy = commandProxy;

            _moduleMenuItems = new List<MenuItem>();
        }
        
        /// <summary>
        /// Set Navigation Metadata.
        /// </summary>        
        public void SetNavigationMetadata(params MenuInfo[] menuItems)
        {
            foreach (MenuInfo info in menuItems)
            {
                _shellView.AddMenuItem(info);
                _moduleCommands.Add(info.MenuDelegateCommand);
                info.MenuDelegateCommand.RaiseCanExecuteChanged();
            }
        }

        /// <summary>
        /// Load Search View.
        /// </summary>
        /// <param name="view">View parameter</param>
        /// <returns>True after loading</returns>
        public bool LoadSearchView(object view)
        {
            _regionManager.Regions[Constants.REGION_SEARCH].Add(view, view.GetType().Name);

            return true;
        }
                                       
        /// <summary>
        /// LoadModuleView. Loads the specified view into MainRegion.
        /// </summary>        
        public bool LoadModuleView(string name, Uri imageUri, IView view, IView actionView, params MenuInfo[] menuItems)
        {
            if (view == _activeView) { return false; }
                         
            //Tear down existing view. 
            _shellView.ShowStatusMessage(string.Empty);
            RemoveMenuItems();
            RemoveViewFromMainRegion();
            RemoveViewFromActionRegion();
            
            //Add the new view.
            AddViewToMainRegion(view);
            AddViewToActionRegion(actionView);
            AddMenuItems(menuItems);

            //Raise CanExecute Changed for each module command. This allows the Go
            //menu items to be enabled/disabled after modules are loaded.
            _moduleCommands.ForEach(delegate(DelegateCommand<CommandInfo> command)
            {
                command.RaiseCanExecuteChanged();
            });
                        
            //Update status message.
            _shellView.DisplayModuleInfo(name, imageUri);
            _shellView.ActiveModule = name;
                                                           
            return true;
        }

        /// <summary>
        /// Register Info Command.
        /// </summary>
        /// <param name="type">GlobalCommand Type</param>
        /// <param name="command">Command to register</param>
        public void RegisterInfoCommand(GlobalCommandType type, DelegateCommand<CommandInfo> command)
        {
            switch (type)
            {
                case GlobalCommandType.Print:
                    _commandProxy.PrintCommand.RegisterCommand(command);
                    break;
                case GlobalCommandType.Undo:
                    _commandProxy.UndoCommand.RegisterCommand(command);
                    break;
                case GlobalCommandType.Delete:
                    _commandProxy.DeleteCommand.RegisterCommand(command);
                    break;                
            }
        }

        /// <summary>
        /// Register String Command.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="command"></param>
        public void RegisterStringCommand(GlobalCommandType type, DelegateCommand<string> command)
        {
            switch (type)
            {
                case GlobalCommandType.Search:
                    _commandProxy.SearchCommand.RegisterCommand(command);
                    break;
                case GlobalCommandType.Clear:
                    _commandProxy.ClearCommand.RegisterCommand(command);
                    break;
            }
        }

        /// <summary>
        /// Show Status Message.
        /// </summary>
        /// <param name="message">message parameter</param>
        public void ShowStatusMessage(string message)
        {
            _shellView.ShowStatusMessage(message);
        }

        /// <summary>
        /// Remove existing modules MenuItems from Shell's main menu each 
        /// time a new module is loaded.
        /// </summary>
        private void RemoveMenuItems()
        {            
            _moduleMenuItems.ForEach(delegate(MenuItem item) 
            { 
                _shellView.RemoveMenuItem(item, ParentMenuType.Module); 
            });
            
            _moduleMenuItems.Clear();
        }

        /// <summary>
        /// Add ModuleMenu Items. Adds specified items to the Shell's main 
        /// menu each time the module is activated.
        /// </summary>
        private void AddMenuItems(MenuInfo[] menuItems)
        {           
            foreach (MenuInfo info in menuItems)
            {                
                switch (info.MenuCommandInfo.ParentMenu)
                {
                    case ParentMenuType.None:
                        break;
                    case ParentMenuType.Module:
                        _moduleMenuItems.Add(_shellView.AddMenuItem(info)); break;
                    case ParentMenuType.Edit:
                        break;
                    case ParentMenuType.View:
                        break;
                    case ParentMenuType.Go:
                        break;
                    case ParentMenuType.Tools:
                        break;
                    case ParentMenuType.Help:
                        break;
                    default:
                        break;
                }                
            }
        }

        /// <summary>
        /// Remove View From ActionRegion.
        /// </summary>        
        private void RemoveViewFromActionRegion()
        {
            if (_activeActionView == null) { return; }

            if (_regionManager.Regions[Constants.REGION_ACTION].GetView(_activeActionView.GetType().Name) != null)
            {
                //Remove currently active action view.
                _regionManager.Regions[Constants.REGION_ACTION].Remove(_activeActionView);
                _activeActionView = null;
            }
        }

        /// <summary>
        /// Add View to ActionRegion.
        /// </summary>        
        private void AddViewToActionRegion(object view)
        {
            if (view == null) { return; }

            //Add the new view.
            _regionManager.Regions[Constants.REGION_ACTION].Add(view, view.GetType().Name);

            //Store the new view as active action view.
            _activeActionView = this._regionManager.Regions[Constants.REGION_ACTION].GetView(view.GetType().Name) as IView;
        }
                        
        /// <summary>
        /// Remove View From MainRegion.
        /// </summary>        
        private void RemoveViewFromMainRegion()
        {
            if (_activeView == null) { return; }

            if (_regionManager.Regions[Constants.REGION_MAIN].GetView(_activeView.GetType().Name) != null)
            {
                //Remove currently active view.
                _regionManager.Regions[Constants.REGION_MAIN].Remove(_activeView);
                _activeView = null;
            }
        }

        /// <summary>
        /// Add View to MainRegion. Also sets the ActiveView.
        /// </summary>        
        private void AddViewToMainRegion(object view)
        {
            //Add the new view.
            _regionManager.Regions[Constants.REGION_MAIN].Add(view, view.GetType().Name);

            //Store the new view as active view.
            _activeView = this._regionManager.Regions[Constants.REGION_MAIN].GetView(view.GetType().Name) as IView;            
        }
    }
}

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