Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

GoalBook - A Hybrid Smart Client

, 25 Sep 2009
A WPF hybrid smart client that synchronises your goals with the Toodledo online To-do service.
Prize winner in Competition "Smart Client Article Contest"
GoalBook.zip
GoalBook
Dependencies
CAG WPF 2
Microsoft.Practices.Composite.dll
Microsoft.Practices.Composite.Presentation.dll
Microsoft.Practices.Composite.UnityExtensions.dll
Microsoft.Practices.ObjectBuilder2.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
vssver2.scc
CSLA
Csla.dll
Csla.XmlSerializers.dll
vssver2.scc
Infragistics
Infragistics3.Wpf.DataPresenter.v9.1.Express.dll
Infragistics3.Wpf.Editors.v9.1.Express.dll
Infragistics3.Wpf.v9.1.Express.dll
vssver2.scc
WPFToolkit
vssver2.scc
WPFToolkit.dll
GoalBook.Controls
CheckedList
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Goals
GoalBook.Goals.csproj.user
Properties
vssver2.scc
Views
vssver2.scc
vssver2.scc
GoalBook.Infrastructure
Comparers
vssver2.scc
Constants
vssver2.scc
Controls
Converters
vssver2.scc
Enums
vssver2.scc
Events
vssver2.scc
GoalBook.Infrastructure.csproj.user
Helpers
vssver2.scc
Interfaces
vssver2.scc
ObjectModel
vssver2.scc
Printing
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Notes
GoalBook.Notes.csproj.user
Properties
vssver2.scc
Resources
arrow_redo.png
arrow_undo.png
cut.png
page_copy.png
paste_plain.png
text_bold.png
text_indent.png
text_indent_remove.png
text_italic.png
text_list_bullets.png
text_list_numbers.png
vssver2.scc
world_link.png
Views
vssver2.scc
vssver2.scc
GoalBook.Public
Encryption
vssver2.scc
HtmlConverter
vssver2.scc
HtmlParser
vssver2.scc
Misc
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Shell
App.ico
Commands
vssver2.scc
GoalBook.Shell.csproj.user
Journal.ico
Journal48.ico
Misc
vssver2.scc
Modules
vssver2.scc
Properties
licenses.licx
vssver2.scc
Resources
arrow_refresh.png
arrow_undo.png
checked.gif
cross.png
disk.png
email.png
erase.png
error.png
exclamation.png
flag_red.png
group.png
help.png
information.png
magnifier.png
page_red.png
pencil.png
printer.png
user.png
vssver2.scc
Wave.jpg
world.png
world_link.png
Services
vssver2.scc
Splash.png
Views
vssver2.scc
vssver2.scc
Windows
vssver2.scc
GoalBook.snk
GoalBook.Synchronisation
Events
vssver2.scc
Properties
vssver2.scc
ToodleDo
vssver2.scc
vssver2.scc
GoalBook.Tasks
Controls
vssver2.scc
Properties
vssver2.scc
Views
vssver2.scc
vssver2.scc
vssver2.scc
//===============================================================================
// 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)

About the Author

Mark Brownsword
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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 25 Sep 2009
Article Copyright 2009 by Mark Brownsword
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid