private void OnMenuPaste(object sender, EventArgs e) { object primarySelection = this.selectionService.PrimarySelection; CompositeActivityDesigner parentDesigner = ActivityDesigner.GetDesigner(primarySelection as Activity) as CompositeActivityDesigner; if (parentDesigner == null) { parentDesigner = ActivityDesigner.GetParentDesigner(primarySelection); } if ((parentDesigner != null) && parentDesigner.IsEditable) { IDataObject dataObject = Clipboard.GetDataObject(); ICollection activities = null; try { activities = CompositeActivityDesigner.DeserializeActivitiesFromDataObject(this.serviceProvider, dataObject, true); } catch (Exception exception) { if (exception != CheckoutException.Canceled) { throw new Exception(DR.GetString("ActivityInsertError", new object[0]) + "\n" + exception.Message, exception); } } if (activities == null) { throw new InvalidOperationException(DR.GetString("InvalidOperationBadClipboardFormat", new object[0])); } System.Workflow.ComponentModel.Design.HitTestInfo insertLocation = null; if (primarySelection is System.Workflow.ComponentModel.Design.HitTestInfo) { insertLocation = (System.Workflow.ComponentModel.Design.HitTestInfo)primarySelection; } else if (primarySelection is CompositeActivity) { insertLocation = new System.Workflow.ComponentModel.Design.HitTestInfo(parentDesigner, HitTestLocations.Designer); } else if (primarySelection is Activity) { Activity item = primarySelection as Activity; CompositeActivity parent = item.Parent; CompositeActivityDesigner designer = ActivityDesigner.GetDesigner(parent) as CompositeActivityDesigner; if (designer != null) { insertLocation = new ConnectorHitTestInfo(designer, HitTestLocations.Designer, parent.Activities.IndexOf(item) + 1); } } List <Activity> list = new List <Activity>(Helpers.GetTopLevelActivities(activities)); if ((insertLocation == null) || !parentDesigner.CanInsertActivities(insertLocation, list.AsReadOnly())) { throw new Exception(SR.GetString("Error_NoPasteSupport")); } IExtendedUIService service = this.serviceProvider.GetService(typeof(IExtendedUIService)) as IExtendedUIService; if (service != null) { foreach (Activity activity3 in activities) { service.AddAssemblyReference(activity3.GetType().Assembly.GetName()); } } CompositeActivityDesigner.InsertActivities(parentDesigner, insertLocation, list.AsReadOnly(), SR.GetString("PastingActivities")); Stream data = dataObject.GetData("CF_WINOEDESIGNERCOMPONENTSSTATE") as Stream; if (data != null) { Helpers.DeserializeDesignersFromStream(activities, data); } this.selectionService.SetSelectedComponents(list.ToArray(), SelectionTypes.Replace); this.workflowView.EnsureVisible(this.selectionService.PrimarySelection); } }
protected override bool OnDragDrop(DragEventArgs eventArgs) { //Invalidate the entire rectangle so that we draw active placement glyphs on connectors WorkflowView parentView = ParentView; parentView.InvalidateClientRectangle(Rectangle.Empty); //By default we do not allow any drag drop operation eventArgs.Effect = DragDropEffects.None; DestroyDragFeedbackImages(); //Get the coordinates Point clientPoint = parentView.PointToClient(new Point(eventArgs.X, eventArgs.Y)); Point logicalPoint = parentView.ScreenPointToLogical(new Point(eventArgs.X, eventArgs.Y)); //Now we check if the drag drop was in any valid area, if not then do not proceed further if (!parentView.IsClientPointInActiveLayout(clientPoint)) { if (this.dropTargetDesigner != null) { ((IWorkflowDesignerMessageSink)this.dropTargetDesigner).OnDragLeave(); } this.wasCtrlKeyPressed = false; this.dropTargetDesigner = null; this.draggedActivities.Clear(); return(false); } //Now we have a potential for successful drag drop, so construct drag event arguments with logical coordinates this.wasCtrlKeyPressed = ((eventArgs.KeyState & 8) == 8); ActivityDragEventArgs dragdropEventArgs = new ActivityDragEventArgs(eventArgs, this.dragInitiationPoint, logicalPoint, this.draggedActivities); //Now check which designer is under the cursor, if we have the same designer as the old one //If not then we set the new one as drop target and pump in messages HitTestInfo hitTestInfo = MessageHitTestContext; if (this.dropTargetDesigner != hitTestInfo.AssociatedDesigner) { if (this.dropTargetDesigner != null) { ((IWorkflowDesignerMessageSink)this.dropTargetDesigner).OnDragLeave(); this.dropTargetDesigner = null; } if (hitTestInfo.AssociatedDesigner != null) { this.dropTargetDesigner = hitTestInfo.AssociatedDesigner; if (this.dropTargetDesigner != null) { ((IWorkflowDesignerMessageSink)this.dropTargetDesigner).OnDragEnter(dragdropEventArgs); } } } //We now have appropriate droptarget designer try { if (this.dropTargetDesigner != null) { //We do not allow recursive drag and drop if (!this.wasCtrlKeyPressed && IsRecursiveDropOperation(this.dropTargetDesigner) || (this.dropTargetDesigner is CompositeActivityDesigner && !((CompositeActivityDesigner)this.dropTargetDesigner).IsEditable)) { ((IWorkflowDesignerMessageSink)this.dropTargetDesigner).OnDragLeave(); dragdropEventArgs.Effect = DragDropEffects.None; } else { // IMPORTANT: Don't use draggedActivities variable, because components which are // there may not be created using the assembly references added to ITypeResultionService // this.workflowView.time the components will be created using the assembly references got added to the project List <Activity> droppedActivities = new List <Activity>(); string transactionDescription = SR.GetString(SR.DragDropActivities); //This means that we are trying to move activities so we use the same activities for drop if (!this.wasCtrlKeyPressed && this.existingDraggedActivities.Count > 0) { droppedActivities.AddRange(this.existingDraggedActivities); if (droppedActivities.Count > 1) { transactionDescription = SR.GetString(SR.MoveMultipleActivities, droppedActivities.Count); } else if (droppedActivities.Count == 1) { transactionDescription = SR.GetString(SR.MoveSingleActivity, droppedActivities[0].GetType()); } } else { droppedActivities.AddRange(CompositeActivityDesigner.DeserializeActivitiesFromDataObject(ParentView, eventArgs.Data, true)); if (droppedActivities.Count > 0) { transactionDescription = SR.GetString(SR.CreateActivityFromToolbox, droppedActivities[0].GetType()); } } //Now that we have what needs to be dropped, we start the actual drag and drop IDesignerHost designerHost = GetService(typeof(IDesignerHost)) as IDesignerHost; DesignerTransaction transaction = null; if (droppedActivities.Count > 0) { transaction = designerHost.CreateTransaction(transactionDescription); } dragdropEventArgs = new ActivityDragEventArgs(eventArgs, this.dragInitiationPoint, logicalPoint, droppedActivities); try { ((IWorkflowDesignerMessageSink)this.dropTargetDesigner).OnDragDrop(dragdropEventArgs); if (dragdropEventArgs.Effect == DragDropEffects.Move) { this.existingDraggedActivities.Clear(); } if (transaction != null) { transaction.Commit(); } } catch (Exception e) { if (transaction != null) { transaction.Cancel(); } throw e; } //We deserialize the designers and try to store the designer states if (droppedActivities.Count > 0) { Stream componentStateStream = eventArgs.Data.GetData(DragDropManager.CF_DESIGNERSTATE) as Stream; if (componentStateStream != null) { Helpers.DeserializeDesignersFromStream(droppedActivities, componentStateStream); } //Set the current selection ISelectionService selectionService = (ISelectionService)GetService(typeof(ISelectionService)); if (selectionService != null) { selectionService.SetSelectedComponents(droppedActivities, SelectionTypes.Replace); } } //Active the design surface if (designerHost != null) { designerHost.Activate(); } } } } catch (Exception ex) { //We purposely consume application thrown exception which are result of user cancelling the action //during dragdrop where we popup UI Wizards during drag drop. Ref: InvokeWebService ((IWorkflowDesignerMessageSink)this.dropTargetDesigner).OnDragLeave(); dragdropEventArgs.Effect = DragDropEffects.None; string dragDropException = ex.Message; if (ex.InnerException != null && !String.IsNullOrEmpty(ex.InnerException.Message)) { dragDropException = ex.InnerException.Message; } string errorMessage = DR.GetString(DR.Error_FailedToDeserializeComponents); errorMessage += "\r\n" + DR.GetString(DR.Error_Reason, dragDropException); DesignerHelpers.ShowError(ParentView, errorMessage); if (ex != CheckoutException.Canceled) { throw new Exception(errorMessage, ex); } } finally { //Make sure that mouse over designer is set to null this.wasCtrlKeyPressed = false; this.draggedActivities.Clear(); this.dropTargetDesigner = null; this.exceptionInDragDrop = false; eventArgs.Effect = dragdropEventArgs.Effect; } return(true); }
protected override bool OnDragDrop(DragEventArgs eventArgs) { WorkflowView parentView = base.ParentView; parentView.InvalidateClientRectangle(Rectangle.Empty); eventArgs.Effect = DragDropEffects.None; this.DestroyDragFeedbackImages(); Point clientPoint = parentView.PointToClient(new Point(eventArgs.X, eventArgs.Y)); Point point = parentView.ScreenPointToLogical(new Point(eventArgs.X, eventArgs.Y)); if (!parentView.IsClientPointInActiveLayout(clientPoint)) { if (this.dropTargetDesigner != null) { ((IWorkflowDesignerMessageSink)this.dropTargetDesigner).OnDragLeave(); } this.wasCtrlKeyPressed = false; this.dropTargetDesigner = null; this.draggedActivities.Clear(); return(false); } this.wasCtrlKeyPressed = (eventArgs.KeyState & 8) == 8; ActivityDragEventArgs e = new ActivityDragEventArgs(eventArgs, this.dragInitiationPoint, point, this.draggedActivities); System.Workflow.ComponentModel.Design.HitTestInfo messageHitTestContext = base.MessageHitTestContext; if (this.dropTargetDesigner != messageHitTestContext.AssociatedDesigner) { if (this.dropTargetDesigner != null) { ((IWorkflowDesignerMessageSink)this.dropTargetDesigner).OnDragLeave(); this.dropTargetDesigner = null; } if (messageHitTestContext.AssociatedDesigner != null) { this.dropTargetDesigner = messageHitTestContext.AssociatedDesigner; if (this.dropTargetDesigner != null) { ((IWorkflowDesignerMessageSink)this.dropTargetDesigner).OnDragEnter(e); } } } try { if (this.dropTargetDesigner != null) { if ((!this.wasCtrlKeyPressed && this.IsRecursiveDropOperation(this.dropTargetDesigner)) || ((this.dropTargetDesigner is CompositeActivityDesigner) && !((CompositeActivityDesigner)this.dropTargetDesigner).IsEditable)) { ((IWorkflowDesignerMessageSink)this.dropTargetDesigner).OnDragLeave(); e.Effect = DragDropEffects.None; } else { List <Activity> draggedActivities = new List <Activity>(); string description = SR.GetString("DragDropActivities"); if (!this.wasCtrlKeyPressed && (this.existingDraggedActivities.Count > 0)) { draggedActivities.AddRange(this.existingDraggedActivities); if (draggedActivities.Count > 1) { description = SR.GetString("MoveMultipleActivities", new object[] { draggedActivities.Count }); } else if (draggedActivities.Count == 1) { description = SR.GetString("MoveSingleActivity", new object[] { draggedActivities[0].GetType() }); } } else { draggedActivities.AddRange(CompositeActivityDesigner.DeserializeActivitiesFromDataObject(base.ParentView, eventArgs.Data, true)); if (draggedActivities.Count > 0) { description = SR.GetString("CreateActivityFromToolbox", new object[] { draggedActivities[0].GetType() }); } } IDesignerHost host = base.GetService(typeof(IDesignerHost)) as IDesignerHost; DesignerTransaction transaction = null; if (draggedActivities.Count > 0) { transaction = host.CreateTransaction(description); } e = new ActivityDragEventArgs(eventArgs, this.dragInitiationPoint, point, draggedActivities); try { ((IWorkflowDesignerMessageSink)this.dropTargetDesigner).OnDragDrop(e); if (e.Effect == DragDropEffects.Move) { this.existingDraggedActivities.Clear(); } if (transaction != null) { transaction.Commit(); } } catch (Exception exception) { if (transaction != null) { transaction.Cancel(); } throw exception; } if (draggedActivities.Count > 0) { Stream data = eventArgs.Data.GetData("CF_WINOEDESIGNERCOMPONENTSSTATE") as Stream; if (data != null) { Helpers.DeserializeDesignersFromStream(draggedActivities, data); } ISelectionService service = (ISelectionService)base.GetService(typeof(ISelectionService)); if (service != null) { service.SetSelectedComponents(draggedActivities, SelectionTypes.Replace); } } if (host != null) { host.Activate(); } } } } catch (Exception exception2) { ((IWorkflowDesignerMessageSink)this.dropTargetDesigner).OnDragLeave(); e.Effect = DragDropEffects.None; string message = exception2.Message; if ((exception2.InnerException != null) && !string.IsNullOrEmpty(exception2.InnerException.Message)) { message = exception2.InnerException.Message; } string str3 = DR.GetString("Error_FailedToDeserializeComponents", new object[0]) + "\r\n" + DR.GetString("Error_Reason", new object[] { message }); DesignerHelpers.ShowError(base.ParentView, str3); if (exception2 != CheckoutException.Canceled) { throw new Exception(str3, exception2); } } finally { this.wasCtrlKeyPressed = false; this.draggedActivities.Clear(); this.dropTargetDesigner = null; this.exceptionInDragDrop = false; eventArgs.Effect = e.Effect; } return(true); }