Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / C#

Calculating Metrics and Searching with a CodeDOM (Part 8)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
6 Mar 2013CDDL7 min read 22.1K   684   10  
Calculating metrics on and searching a CodeDOM.
// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.Try"/>.
    /// </summary>
    public class TryVM : BlockStatementVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(Try),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new TryVM((Try)codeObject, isDescription, dictionary); });
        }

        #endregion

        #region /* FIELDS */

        protected ChildListVM<CatchVM> _catchVMs;
        protected FinallyVM _finallyVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.Try"/>.
        /// </summary>
        public TryVM(Try @try, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(@try, isDescription, dictionary)
        {
            _catchVMs = CreateListVM<Catch, CatchVM>(@try.Catches, dictionary);
            _finallyVM = (FinallyVM)CreateVM(@try.Finally, isDescription, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.Try"/> model.
        /// </summary>
        public Try Try
        {
            get { return (Try)CodeObject; }
        }

        /// <summary>
        /// A collection of <see cref="CatchVM"/>s.
        /// </summary>
        public ChildListVM<CatchVM> CatchVMs
        {
            get { return _catchVMs; }
        }

        /// <summary>
        /// True if there are any <see cref="CatchVM"/>s.
        /// </summary>
        public bool HasCatchVMs
        {
            get { return (_catchVMs != null && _catchVMs.Count > 0); }
        }

        /// <summary>
        /// An optional <see cref="FinallyVM"/>.
        /// </summary>
        public FinallyVM FinallyVM
        {
            get { return _finallyVM; }
        }

        /// <summary>
        /// True if there is a <see cref="FinallyVM"/>.
        /// </summary>
        public bool HasFinallyVM
        {
            get { return (_finallyVM != null); }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public override Brush BorderBrush
        {
            get { return Brushes.Pink; }
        }

        public override Brush BackgroundBrush
        {
            get { return Brushes.MistyRose; }
        }

        protected override void RenderAfter(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderAfter(renderer, flags);
            if (HasCatchVMs && !flags.HasFlag(RenderFlags.Description))
            {
                foreach (CatchVM catchVM in _catchVMs)
                    catchVM.Render(renderer, flags | RenderFlags.PrefixSpace);
            }
            if (HasFinallyVM && !flags.HasFlag(RenderFlags.Description))
                _finallyVM.Render(renderer, flags | RenderFlags.PrefixSpace);
        }

        public override void RenderVisible(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderVisible(renderer, flags);
            renderer.RenderVisibleList(_catchVMs, flags | RenderFlags.PrefixSpace);
            if (HasFinallyVM)
                _finallyVM.RenderVisible(renderer, flags | RenderFlags.PrefixSpace);
        }

        public override void UnRender()
        {
            ChildListHelpers.UnRender(_catchVMs);
            if (_finallyVM != null)
                _finallyVM.UnRender();
            base.UnRender();
        }

        #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 Common Development and Distribution License (CDDL)


Written By
Software Developer (Senior)
United States United States
I've been writing software since the late 70's, currently focusing mainly on C#.NET. I also like to travel around the world, and I own a Chocolate Factory (sadly, none of my employees are oompa loompas).

Comments and Discussions