Click here to Skip to main content
15,881,757 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.BinaryOperator"/>.
    /// </summary>
    public abstract class BinaryOperatorVM : OperatorVM
    {
        #region /* FIELDS */

        protected ExpressionVM _leftVM;
        protected ExpressionVM _rightVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.BinaryOperator"/>.
        /// </summary>
        protected BinaryOperatorVM(BinaryOperator binaryOperator, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(binaryOperator, dictionary)
        {
            _leftVM = (ExpressionVM)CreateVM(binaryOperator.Left, false, dictionary);
            _rightVM = (ExpressionVM)CreateVM(binaryOperator.Right, false, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

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

        /// <summary>
        /// The left-side <see cref="ExpressionVM"/>.
        /// </summary>
        public ExpressionVM Left
        {
            get { return _leftVM; }
        }

        /// <summary>
        /// The right-side <see cref="ExpressionVM"/>.
        /// </summary>
        public ExpressionVM Right
        {
            get { return _rightVM; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        protected override Brush RenderBrush(RenderFlags flags)
        {
            // If there is an unresolved operator overload, render it as an error (or warning if in a doc comment)
            return (BinaryOperator.HiddenRef is UnresolvedRef ? (flags.HasFlag(RenderFlags.InDocComment) ? WARNING_BRUSH : ERROR_BRUSH) : OPERATOR_BRUSH);
        }

        public override void Render(CodeRenderer renderer, RenderFlags flags)
        {
            // Increase the indent level for any binary operator expressions that wrap, unless the parent
            // is the same operator (required for right-associative operators, such as Assignment - left
            // associative operators would only indent once anyway, due to the drawing order).
            base.Render(renderer, flags | (ParentVM == null || ParentVM.GetType() != GetType() ? RenderFlags.IncreaseIndent : 0));
        }

        public override void RenderExpression(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            if (_leftVM != null)
                _leftVM.Render(renderer, passFlags | RenderFlags.IsPrefix);
            if (UseAlternativeDisplay)
                AltRenderOperator(renderer, flags);
            else
                RenderOperator(renderer, flags);
            RenderInfixComments(renderer, 0, flags | RenderFlags.PrefixSpace);
            if (_rightVM != null)
                _rightVM.Render(renderer, passFlags | RenderFlags.PrefixSpace);
        }

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

        public override void UnRender()
        {
            if (_leftVM != null)
                _leftVM.UnRender();
            if (_rightVM != null)
                _rightVM.UnRender();
            base.UnRender();
        }

        public override void RenderToolTip(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderToolTip(renderer, flags);
            if (BinaryOperator.HiddenRef != null)
            {
                renderer.NewLine();
                CreateVM(BinaryOperator.HiddenRef).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