agc93
12/11/2016 - 2:47 PM

Nuget Link Drop Handler

Nuget Link Drop Handler

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cake.VisualStudio.Helpers;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Editor.DragDrop;
using Microsoft.VisualStudio.Utilities;

namespace Cake.VisualStudio.Editor
{
    [Export(typeof(IDropHandlerProvider))]
    [DropFormat("Rich Text Format")]
    [DropFormat("Text")]
    [Name("NuGetDropHandler")]
    [ContentType(Constants.CakeContentType)]
    [Order(Before = "CakeDropHandler")]
    class NuGetPackageDropHandlerProvider : IDropHandlerProvider
    {
        [Import]
        ITextDocumentFactoryService TextDocumentFactoryService { get; set; }

        public IDropHandler GetAssociatedDropHandler(IWpfTextView wpfTextView)
        {
            ITextDocument document;

            if (TextDocumentFactoryService.TryGetTextDocument(wpfTextView.TextBuffer, out document))
            {
                return
                    wpfTextView.Properties.GetOrCreateSingletonProperty(
                        () => new NuGetPackageDropHandler(wpfTextView, document));
            }

            return null;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Editor.DragDrop;

namespace Cake.VisualStudio.Editor
{
    class NuGetPackageDropHandler : CakeDropHandler
    {
        private ITextDocument document;
        private IWpfTextView wpfTextView;

        public NuGetPackageDropHandler(IWpfTextView wpfTextView, ITextDocument document)
        {
            this.wpfTextView = wpfTextView;
            this.document = document;
        }

        public override DragDropPointerEffects HandleDataDropped(DragDropInfo dragDropInfo)
        {
            throw new NotImplementedException();
        }

        public override bool IsDropEnabled(DragDropInfo dragDropInfo)
        {
            var target = GetDropTarget(dragDropInfo);
            return false;
        }

        private static string GetDropTarget(DragDropInfo dragDropInfo)
        {
            var data = new DataObject(dragDropInfo.Data);

            if (dragDropInfo.Data.GetDataPresent("FileDrop"))
            {
                var files = data.GetFileDropList();

                if (files.Count == 1)
                {
                    return files[0];
                }
            }
            else if (dragDropInfo.Data.GetDataPresent("CF_VSSTGPROJECTITEMS"))
            {
                return data.GetText();
            }
            return null;
        }
    }
}