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

Resolving Symbolic References in a CodeDOM (Part 7)

Rate me:
Please Sign up or sign in to vote.
4.75/5 (6 votes)
2 Dec 2012CDDL12 min read 19.4K   509   14  
Resolving symbolic references in 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.Goto"/>.
    /// </summary>
    public class GotoVM : StatementVM
    {
        #region /* STATICS */

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

        #endregion

        #region /* FIELDS */

        protected SymbolicRefVM _targetVM;
        protected ExpressionVM _constantExpressionVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.Goto"/>.
        /// </summary>
        public GotoVM(Goto @goto, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(@goto, dictionary)
        {
            _targetVM = (SymbolicRefVM)CreateVM(@goto.Target, false, dictionary);
            _constantExpressionVM = (ExpressionVM)CreateVM(@goto.ConstantExpression, false, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

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

        /// <summary>
        /// The target <see cref="GotoTargetRef"/> (<see cref="LabelRef"/> or <see cref="SwitchItemRef"/>) or <see cref="UnresolvedRef"/>.
        /// </summary>
        public SymbolicRefVM Target
        {
            get { return _targetVM; }
        }

        /// <summary>
        /// The constant expression if this is a "goto case ...", otherwise null.
        /// </summary>
        public ExpressionVM ConstantExpression
        {
            get { return _constantExpressionVM; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public override Brush BorderBrush
        {
            get { return DarkPink; }
        }

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

        protected override void RenderArgument(CodeRenderer renderer, RenderFlags flags)
        {
            // If we have a constant expression ("goto case ..."), always render it instead of
            // the target reference.
            if (_constantExpressionVM != null)
            {
                renderer.RenderText(Case.ParseToken + " ", _targetVM.GetMessageBrush(KEYWORD_BRUSH, flags), this);
                _constantExpressionVM.Render(renderer, flags);
            }
            else
                _targetVM.Render(renderer, flags);
        }

        public override void UnRender()
        {
            if (_constantExpressionVM != null)
                _constantExpressionVM.UnRender();
            else
                _targetVM.UnRender();
            base.UnRender();
        }

        public override void RenderToolTip(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderToolTip(renderer, flags);

            // If we have a "goto case ...", show the hidden target reference in addition to the constant expression
            if (_constantExpressionVM != null && _targetVM != null)
            {
                renderer.NewLine();
                CreateVM(Goto.Target).RenderToolTip(renderer, RenderFlags.SuppressLineCol);
            }
        }

        #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