Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Presentation / System / Activities / Presentation / CutCopyPasteHelper.cs / 1477467 / CutCopyPasteHelper.cs
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------
namespace System.Activities.Presentation
{
using System;
using System.Activities.Debugger;
using System.Activities.Presentation.Model;
using System.Activities.Presentation.View;
using System.Activities.Statements;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime;
using System.Runtime.InteropServices;
using System.ServiceModel;
using System.ServiceModel.Activities;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Xaml;
public static class CutCopyPasteHelper
{
internal static readonly DependencyProperty ChildContainersProperty =
DependencyProperty.RegisterAttached("ChildContainers", typeof(HashSet), typeof(CutCopyPasteHelper), new UIPropertyMetadata(null));
static object workflowCallbackContext = null;
const string WorkflowClipboardFormat = "WorkflowXamlFormat";
//define a workflow callback clipboard format - make it unique across all processes
static readonly string WorkflowCallbackClipboardFormat = string.Format(CultureInfo.InvariantCulture, "WorkflowCallbackFormat{0}", Guid.NewGuid());
const string versionInfo = "1.0";
static IList disallowedTypesForCopy;
static IEnumerable DisallowedTypesForCopy
{
get
{
if (null == disallowedTypesForCopy)
{
disallowedTypesForCopy = new List();
disallowedTypesForCopy.Add(typeof(ActivityBuilder));
disallowedTypesForCopy.Add(typeof(ModelItemKeyValuePair<,>));
disallowedTypesForCopy.Add(typeof(WorkflowService));
disallowedTypesForCopy.Add(typeof(Catch));
}
return disallowedTypesForCopy;
}
}
internal static void AddDisallowedTypeForCopy(Type type)
{
if (!DisallowedTypesForCopy.Any(p => type == p))
{
disallowedTypesForCopy.Add(type);
}
}
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
static void AddChildContainer(WorkflowViewElement viewElement, ICompositeView sourceContainer)
{
if (viewElement == null)
{
throw FxTrace.Exception.AsError(new ArgumentNullException("viewElement"));
}
if (sourceContainer == null)
{
throw FxTrace.Exception.AsError(new ArgumentNullException("sourceContainer"));
}
HashSet containers = (HashSet)viewElement.GetValue(CutCopyPasteHelper.ChildContainersProperty);
if (containers == null)
{
containers = new HashSet();
viewElement.SetValue(CutCopyPasteHelper.ChildContainersProperty, containers);
}
containers.Add(sourceContainer);
}
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
static HashSet GetChildContainers(WorkflowViewElement workflowViewElement)
{
HashSet childContainers = null;
if (workflowViewElement != null && workflowViewElement.ShowExpanded)
{
childContainers = (HashSet)workflowViewElement.GetValue(CutCopyPasteHelper.ChildContainersProperty);
}
return childContainers;
}
//This enables us to get children ICompositeViews from WorkflowViewElements.
//Eg. get the WorkflowItemsPresenter from SequenceDesigner.
//This is useful for Cut-Copy-Paste, Delete handling, etc.
internal static void RegisterWithParentViewElement(ICompositeView container)
{
WorkflowViewElement parent = GetParentViewElement(container);
if (parent != null)
{
CutCopyPasteHelper.AddChildContainer(parent, container);
}
}
//Returns the first WorkflowViewElement in the parent chain.
//If ICompositeView is a WorkflowViewElement this method returns the same object.
static WorkflowViewElement GetParentViewElement(ICompositeView container)
{
DependencyObject parent = container as DependencyObject;
return GetParentViewElement(parent);
}
//Returns the first WorkflowViewElement in the parent chain.
//Move this to a helper class.
internal static WorkflowViewElement GetParentViewElement(DependencyObject obj)
{
while (obj != null && !(obj is WorkflowViewElement))
{
obj = VisualTreeHelper.GetParent(obj);
}
return obj as WorkflowViewElement;
}
public static void DoCut(EditingContext context)
{
if (context == null)
{
throw FxTrace.Exception.AsError(new ArgumentNullException("context"));
}
Selection currentSelection = context.Items.GetValue();
List modelItemsToCut = new List(currentSelection.SelectedObjects);
CutCopyPasteHelper.DoCut(modelItemsToCut, context);
}
internal static void DoCut(List modelItemsToCut, EditingContext context)
{
if (modelItemsToCut == null)
{
throw FxTrace.Exception.AsError(new ArgumentNullException("modelItemsToCut"));
}
if (context == null)
{
throw FxTrace.Exception.AsError(new ArgumentNullException("context"));
}
modelItemsToCut.RemoveAll((modelItem) => { return modelItem == null; });
if (modelItemsToCut.Count > 0)
{
using (EditingScope es = (EditingScope)modelItemsToCut[0].BeginEdit(SR.CutOperationEditingScopeDescription))
{
try
{
CutCopyOperation(modelItemsToCut, context, true);
}
catch (ExternalException e)
{
es.Revert();
ErrorReporting.ShowErrorMessage(e.Message);
return;
}
DesignerView view = context.Services.GetService();
//Setting the selection to Breadcrumb root.
Fx.Assert(view != null, "DesignerView Cannot be null during cut");
WorkflowViewElement rootView = view.RootDesigner as WorkflowViewElement;
if (rootView != null)
{
Selection.SelectOnly(context, rootView.ModelItem);
}
es.Complete();
}
}
}
public static void DoCopy(EditingContext context)
{
if (context == null)
{
throw FxTrace.Exception.AsError(new ArgumentNullException("context"));
}
Selection currentSelection = context.Items.GetValue();
List modelItemsToCopy = new List(currentSelection.SelectedObjects);
CutCopyPasteHelper.DoCopy(modelItemsToCopy, context);
}
internal static void DoCopy(List modelItemsToCopy, EditingContext context)
{
if (modelItemsToCopy == null)
{
throw FxTrace.Exception.AsError(new ArgumentNullException("modelItemsToCopy"));
}
if (context == null)
{
throw FxTrace.Exception.AsError(new ArgumentNullException("context"));
}
// copy only works if we have DesignerView up and running so check and throw here
if (context.Services.GetService() == null)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.CutCopyRequiresDesignerView));
}
modelItemsToCopy.RemoveAll((modelItem) => { return modelItem == null; });
try
{
CutCopyOperation(modelItemsToCopy, context, false);
}
catch (ExternalException e)
{
ErrorReporting.ShowErrorMessage(e.Message);
}
}
static void CutCopyOperation(List modelItemsToCutCopy, EditingContext context, bool isCutOperation)
{
List