Click here to Skip to main content
15,895,799 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   510   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.VariableDecl"/>.
    /// </summary>
    public abstract class VariableDeclVM : StatementVM
    {
        #region /* FIELDS */

        protected ExpressionVM _typeVM;
        protected ExpressionVM _initializationVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.VariableDecl"/>.
        /// </summary>
        protected VariableDeclVM(VariableDecl variableDecl, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(variableDecl, dictionary)
        {
            if (variableDecl.Type != null)
                _typeVM = (ExpressionVM)CreateVM(variableDecl.Type, false, dictionary);
            if (variableDecl.Initialization != null)
                _initializationVM = (ExpressionVM)CreateVM(variableDecl.Initialization, false, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

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

        /// <summary>
        /// The type of the variable declaration.
        /// </summary>
        public virtual ExpressionVM Type
        {
            get { return _typeVM; }
        }

        /// <summary>
        /// An optional initialization <see cref="ExpressionVM"/>.
        /// </summary>
        public ExpressionVM Initialization
        {
            get { return _initializationVM; }
        }

        /// <summary>
        /// True if the variable has an initialization <see cref="ExpressionVM"/>.
        /// </summary>
        public bool HasInitialization
        {
            get { return (_initializationVM != null); }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public static readonly Brush StaticBorderBrush = DarkPalerGreen;
        public static readonly Brush StaticBackgroundBrush = PalerGreen;

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

        public override Brush BackgroundBrush
        {
            get { return StaticBackgroundBrush; }
        }

        public virtual void RenderType(CodeRenderer renderer, RenderFlags flags)
        {
            ExpressionVM type = Type;
            if (type != null)
            {
                RenderFlags passFlags = (flags & RenderFlags.PassMask);
                type.Render(renderer, passFlags | RenderFlags.IsPrefix | RenderFlags.Declaration);
            }
        }

        protected override void RenderStatement(CodeRenderer renderer, RenderFlags flags)
        {
            RenderType(renderer, flags);
            renderer.RenderText(VariableDecl.Name, IDENTIFIER_BRUSH, this);
        }

        protected void RenderConstantValue(CodeRenderer renderer, RenderFlags flags)
        {
            LiteralVM.RenderConstantValue(renderer, flags, VariableDecl.GetValue(), flags.HasFlag(RenderFlags.FormatAsHex), this);
        }

        protected void RenderInitialization(CodeRenderer renderer, RenderFlags flags)
        {
            renderer.RenderText(" ", PUNC_BRUSH, this);
            if (OperatorVM.UseAlternativeDisplay)
                AssignmentVM.AltRenderOperator(renderer, OPERATOR_BRUSH, this);
            else
                AssignmentVM.RenderOperator(renderer, OPERATOR_BRUSH, this);

           Initialization.Render(renderer, flags | RenderFlags.ForceBorder | RenderFlags.PrefixSpace);
        }

        public override void RenderVisible(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderVisible(renderer, flags);
            if (_initializationVM != null)
                _initializationVM.RenderVisible(renderer, flags);
        }

        public override void UnRender()
        {
            if (_typeVM != null)
                _typeVM.UnRender();
            if (_initializationVM != null)
                _initializationVM.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