Click here to Skip to main content
15,886,016 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.
//===============================================================================
// Goal Book.
// Copyright © 2009 Mark Brownsword. 
//===============================================================================

#region Using Statements
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using GoalBook.Infrastructure;
using GoalBook.Infrastructure.Enums;
using GoalBook.Shell.Misc;
using GoalBook.Shell.Services;
using System.Windows.Media;
#endregion

namespace GoalBook.Shell.Windows
{
    /// <summary>
    /// Main Window for the GoalBook Application. Uses the Microsoft 
    /// Patterns & Practices Composite Application Library for WPF to host modules.
    /// </summary>
    public partial class Main : Window, IShellView
    {
        #region Constants and Enums
        #endregion

        #region Inner Classes and Structures
        #endregion

        #region Delegates and Events

        #region CanClose Event
        /// <summary>
        /// CanClose EventHandler.
        /// </summary>
        public event CancelEventHandler CanClose;
        /// <summary>
        /// Raise the CanClose event.
        /// </summary>        
        private bool OnCanClose()
        {
            CancelEventHandler canClose = CanClose;
            if (canClose != null)
            {
                CancelEventArgs args = new CancelEventArgs();
                canClose(this, args);

                return !args.Cancel;
            }

            return true;
        }
        #endregion

        #region SaveChanges
        /// <summary>
        /// SaveChanges EventHandler.
        /// </summary>
        public event EventHandler SaveChanges;
        /// <summary>
        /// Raise the SaveChanges event.
        /// </summary>        
        private void OnSaveChanges()
        {
            EventHandler saveChanges = SaveChanges;
            if (saveChanges != null)
            {
                EventArgs args = new EventArgs();
                saveChanges(this, args);                
            }
        }
        #endregion

        #endregion

        #region Instance and Shared Fields
        private ApplicationSettings _settings;        
        #endregion

        #region Constructors
        /// <summary>
        /// Constructor.
        /// </summary>
        public Main()
        {
            InitializeComponent();

            //Set the image for synchronisation statusbar item.
            statusBarSyncImage.Source = new BitmapImage(new Uri(Infrastructure.Constants.MenuConstants.MENU_SYNCHRONISE_IMAGE_URI));
        }        
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets ActiveModule.
        /// </summary>
        public string ActiveModule { get; set; }
        #endregion

        #region Private and Protected Methods
        /// <summary>
        /// Get Parent Menu.
        /// </summary>        
        private MenuItem GetParentMenu(ParentMenuType parentMenuType)
        {
            switch (parentMenuType)
            {
                case ParentMenuType.Module:
                    return menuModule;
                case ParentMenuType.New:
                    return menuNew;
                case ParentMenuType.Edit:
                    break;
                case ParentMenuType.View:
                    break;
                case ParentMenuType.Go:
                    return menuGo;
                case ParentMenuType.Tools:
                    break;
                case ParentMenuType.Help:
                    break;
                default:
                    break;
            }
            return null;
        }
        /// <summary>
        /// Get Menu Index.
        /// </summary>        
        private int GetMenuIndex(int? index)
        {
            return index ?? 0;
        }
        /// <summary>
        /// Get Input Gesture Text.
        /// </summary>        
        private string GetInputGestureText(KeyGesture keyGesture)
        {
            if (keyGesture == null) { return string.Empty; }

            return keyGesture.DisplayString;
        }
        /// <summary>
        /// Get Image.
        /// </summary>        
        private Image GetImage(Uri uri)
        {
            Image image = new Image();
            image.Height = image.Width = 16;
            image.Source = new BitmapImage(uri);

            return image;
        }
        /// <summary>
        /// Check Children. Determine if the specified menuitem is associated with 
        /// the current keystroke.
        /// </summary>        
        private MenuItem CheckChildren(MenuItem menuItem, ModifierKeys modifiers, Key key)
        {
            foreach (object itemObject in menuItem.Items)
            {
                MenuItem item = null;
                if ((item = itemObject as MenuItem) != null)
                {                    
                    if (item.Items.Count > 0)
                    {
                        //Drill down a level (i.e. execute recursively) when the 
                        //menu item has children, so only bottom level menu items
                        //will be have their CommandInfo.KeyInputGesture data checked.
                        MenuItem childItem = null;
                        if ((childItem = CheckChildren(item, modifiers, key)) != null)
                        {
                            return childItem;
                        }
                    }
                    else
                    {
                        CommandInfo info = null;
                        if ((info = (item.CommandParameter as CommandInfo)) != null)
                        {
                            if (info.KeyInputGesture == null) { continue; }
                            if (info.KeyInputGesture.Modifiers == modifiers &&
                                info.KeyInputGesture.Key == key)
                            {
                                return item;
                            }
                        }
                    }
                }
            }

            return null;
        }
        /// <summary>
        /// Add item to Menu.
        /// </summary>        
        private MenuItem AddMenuItem<T>(T menu, int index, MenuItem item) where T : MenuItem
        {
            if (!menu.Items.Contains(item)) 
            { 
                menu.Items.Insert(index, item);

                if (item.Header.ToString() == this.ActiveModule)
                {
                    item.Command.Execute(item.CommandParameter);
                }

                return item;
            }
            
            return null;            
        }
        /// <summary>
        /// Remove item from Menu.
        /// </summary>        
        private void RemoveMenuItem<T>(T menu, MenuItem item) where T : MenuItem
        {
            if (menu.Items.Contains(item)) { menu.Items.Remove(item); }
        }
        #endregion

        #region Public and internal Methods
        #endregion

        #region Event Handlers
        /// <summary>
        /// Handle Window_KeyUp event. Inspect the menu items to determine
        /// if their shortcut key has been pressed.
        /// </summary>        
        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            MenuItem foundItem;
            foreach (MenuItem item in mainMenu.Items)
            {
                if (item.Visibility != Visibility.Visible) { continue; }
                if ((foundItem = CheckChildren(item, e.KeyboardDevice.Modifiers, e.Key)) != null)
                {
                    if (foundItem.Command.CanExecute(foundItem.CommandParameter))
                    {
                        foundItem.Command.Execute(foundItem.CommandParameter);
                        e.Handled = true;
                    }
                    break; //Exit foreach.
                }
            }
        }

        /// <summary>
        /// Handle Window_Closing event.
        /// </summary>        
        private void Window_Closing(object sender, CancelEventArgs e)
        {
            if (OnCanClose())
            {
                _settings.Top = this.RestoreBounds.Top;
                _settings.Left = this.RestoreBounds.Left;
                _settings.Height = this.RestoreBounds.Height;
                _settings.Width = this.RestoreBounds.Width;
                _settings.WindowState = this.WindowState;
                _settings.StartUpModule = this.ActiveModule;
                _settings.ActionPaneExpanded = this.actionPaneExpander.IsExpanded;
            }
            else
            {
                e.Cancel = true;
            }            
        }

        /// <summary>
        /// Handle ToolBar_Loaded event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolBar_Loaded(object sender, RoutedEventArgs e) 
        { 
            ToolBar toolBar = sender as ToolBar; var overflowGrid = toolBar.Template.FindName("OverflowGrid", toolBar) as FrameworkElement; 
            if (overflowGrid != null) { overflowGrid.Visibility = Visibility.Collapsed; } 
        }

        /// <summary>
        /// Handle Expander_Changed event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">RoutedEventArgs parameter</param>
        private void Expander_Changed(object sender, RoutedEventArgs e)
        {
            actionPaneColumn.Width = new GridLength(actionPaneExpander.IsExpanded ? 200 : 30, GridUnitType.Pixel);

            if (actionPaneGridWrapper != null)
            {
                actionPaneGridWrapper.Background = actionPaneExpander.IsExpanded ? Brushes.Transparent : Brushes.White;                
            }
            
            if (actionPaneGrid != null)
            {
                actionPaneGrid.Visibility = Visibility.Hidden;
                actionPaneGrid.Visibility = actionPaneExpander.IsExpanded ? Visibility.Visible : Visibility.Hidden;
                actionPaneExpander.ToolTip = actionPaneExpander.IsExpanded ? Properties.Resources.HideExpanderToolTip : Properties.Resources.ShowExpanderToolTip;
            }
        }
        #endregion

        #region Base Class Overrides
        #endregion

        #region IShellView Members
        /// <summary>
        /// ExitView.
        /// </summary>
        public void ExitView()
        {
            Application.Current.Shutdown();
        }
        /// <summary>
        /// ShowView.
        /// </summary>
        public void ShowView()
        {
            if (menuGo.Items.Count > 1) { menuGo.Visibility = Visibility.Visible; }
            this.Show();
        }        
        /// <summary>
        /// DisplayModuleInfo.
        /// </summary>        
        public void DisplayModuleInfo(string name, Uri imageUri)
        {
            // ShowStatusMessage(string.Format("Module '{0}' loaded.", name));
            viewTitle.Text = name;
            viewImage.Source = GetImage(imageUri).Source;
        }
        /// <summary>
        /// InitialiseSettings.
        /// </summary>        
        public void InitialiseSettings(ApplicationSettings settings)
        {
            _settings = settings;
            
            this.Top = _settings.Top;
            this.Left = _settings.Left;
            this.Height = _settings.Height;
            this.Width = _settings.Width;
            this.WindowState = _settings.WindowState;
            this.ActiveModule = _settings.StartUpModule;
            this.actionPaneExpander.IsExpanded = _settings.ActionPaneExpanded;
        }        
        /// <summary>
        /// Add Module MenuItem.
        /// </summary>        
        public MenuItem AddMenuItem(MenuInfo info)
        {
            MenuItem parent = GetParentMenu(info.MenuCommandInfo.ParentMenu);
            MenuItem menuItem = new MenuItem();

            menuItem.CommandParameter = info.MenuCommandInfo;
            menuItem.Command = info.MenuDelegateCommand;                    
            menuItem.Header = info.MenuCommandInfo.Title;
            menuItem.Icon = GetImage(info.MenuCommandInfo.IconUri);
            menuItem.InputGestureText = GetInputGestureText(info.MenuCommandInfo.KeyInputGesture);

            return AddMenuItem(parent, GetMenuIndex(info.MenuCommandInfo.Index), menuItem);            
        }        
        /// <summary>
        /// Remove Module MenuItem.
        /// </summary>        
        public void RemoveMenuItem(MenuItem menuItem, ParentMenuType parentMenu)
        {
            MenuItem parent = GetParentMenu(parentMenu);
            RemoveMenuItem(parent, menuItem);
        }
        /// <summary>
        /// Show Status Messages.
        /// </summary>        
        public void ShowStatusMessage(string message)
        {            
            statusBarItemMessage.Content = message;            
        }
        /// <summary>
        /// Show StatusSyncImage when synchronisationis pending.
        /// </summary>
        /// <param name="visible"></param>
        public void ShowStatusSyncImage(bool visible)
        {
            statusBarSyncImage.Visibility = visible ? Visibility.Visible : Visibility.Hidden;
        }        
        #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