//===============================================================================
// Microsoft patterns & practices
// Web Service Software Factory
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using System.Runtime.Serialization;
using EnvDTE;
using Microsoft.Practices.Common;
using Microsoft.Practices.RecipeFramework.Library;
using System.Diagnostics;
using System.Security.Permissions;
using Microsoft.Practices.RecipeFramework.Extensions.References.VisualStudio;
using Microsoft.Practices.SoftwareFactories.ServiceFactory.References; 
#endregion

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.References
{
    /// <summary>
    /// UnBoundRecipeReference that allows to be executed only on a Vs Project item based on property
    /// </summary>
    [Serializable]
    public class ProjectItemPropertyReference : ProjectPropertyReference
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ProjectItemPropertyReference"/> class.
        /// </summary>
        /// <param name="recipe">The recipe.</param>
        public ProjectItemPropertyReference(string recipe)
            : base(recipe)
        {
        }

        /// <summary>
        /// Required constructor for deserialization.
        /// </summary>
        protected ProjectItemPropertyReference(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        #endregion

        #region Methods

        /// <summary>
        /// Performs the validation of the item passed as target
        /// Returns true if the reference is allowed to be executed in the target
        /// that is if the target is a project that has the mapping property.
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public override bool OnIsEnabledFor(object target)
        {
            ProjectItem item = target as ProjectItem;

            if(item != null)
            {
                if(item.FileCodeModel != null)
                {
                    return base.OnIsEnabledFor(item.ContainingProject);
                }
            }

            return false;
        }

        #endregion
    }
}
