private UndoableDelegateCommand CreateRemoveConnectionCommand(IConnection connection)
        {
            var compositeRemoveConnectionCommand = new CompositeCommand(CommandNames.RemoveConnections);

            UndoableDelegateCommand removeCommand = new UndoableDelegateCommand(CommandNames.RemoveConnection, s => this.Diagram.RemoveConnection(connection), s => this.Diagram.AddConnection(connection));

            compositeRemoveConnectionCommand.AddCommand(removeCommand);

            return(compositeRemoveConnectionCommand);
        }
 protected override void OnAdd()
 {
     var model = this.ContextItem as TableModel;
     if (model != null)
     {
         var itemToAdd = new RowModel() { ColumnName = "NewRow", DataType = DataType.String };
         var addCommand = new UndoableDelegateCommand("Add new row", new Action<object>((o) => this.AddNewRow(model, itemToAdd)), new Action<object>((o) => this.RemoveRow(model, itemToAdd)));
         this.Diagram.UndoRedoService.ExecuteCommand(addCommand);
     }
 }
 protected override void OnRemove()
 {
     var model = this.ContextItem as TableModel;
     if (model != null && model.InternalItems.Count > 0)
     {
         var itemToRemove = model.InternalItems.LastOrDefault();
         var removeCommand = new UndoableDelegateCommand("Add new row", new Action<object>((o) => this.RemoveRow(model, itemToRemove)), new Action<object>((o) => this.AddNewRow(model, itemToRemove)));
         this.Diagram.UndoRedoService.ExecuteCommand(removeCommand);
     }
 }
        private CompositeCommand CreateRemoveShapeCommand(RadDiagramShapeBase shape)
        {
            if (shape == null)
            {
                return(null);
            }
            var compositeRemoveShapeCommand = new CompositeCommand(CommandNames.RemoveShapes);
            var removeCommand = new UndoableDelegateCommand(CommandNames.RemoveShape, s => this.Diagram.RemoveShape(shape), s => this.Diagram.AddShape(shape));

            var parentContainer = shape.ParentContainer;

            if (parentContainer != null)
            {
                var execute = new Action <object>((o) => parentContainer.RemoveItem(shape));
                var undo    = new Action <object>((o) =>
                {
                    if (!parentContainer.Items.Contains(shape))
                    {
                        parentContainer.AddItem(shape);
                    }
                });
                compositeRemoveShapeCommand.AddCommand(new UndoableDelegateCommand(CommandNames.RemoveItemFromContainer, execute, undo));
            }

            foreach (var changeSourceCommand in shape.OutgoingLinks.Union(shape.IncomingLinks).ToList().Select(connection => this.CreateRemoveConnectionCommand(connection)))
            {
                compositeRemoveShapeCommand.AddCommand(changeSourceCommand);
            }

            compositeRemoveShapeCommand.AddCommand(removeCommand);

            var container = shape as RadDiagramContainerShape;

            if (container != null)
            {
                for (int i = container.Items.Count - 1; i >= 0; i--)
                {
                    var shapeToRemove = container.Items[i] as RadDiagramShapeBase;
                    if (shapeToRemove != null)
                    {
                        compositeRemoveShapeCommand.AddCommand(this.CreateRemoveShapeCommand(shapeToRemove));
                    }
                    else
                    {
                        var connection = container.Items[i] as IConnection;
                        if (connection != null && connection.Source == null && connection.Target == null)
                        {
                            compositeRemoveShapeCommand.AddCommand(this.CreateRemoveConnectionCommand(container.Items[i] as IConnection));
                        }
                    }
                }
            }

            return(compositeRemoveShapeCommand);
        }
        protected override void OnRemove()
        {
            var model = this.ContextItem as TableModel;

            if (model != null && model.InternalItems.Count > 0)
            {
                var itemToRemove  = model.InternalItems.LastOrDefault();
                var removeCommand = new UndoableDelegateCommand("Add new row", new Action <object>((o) => this.RemoveRow(model, itemToRemove)), new Action <object>((o) => this.AddNewRow(model, itemToRemove)));
                this.Diagram.UndoRedoService.ExecuteCommand(removeCommand);
            }
        }
        protected override void OnAdd()
        {
            var model = this.ContextItem as TableModel;

            if (model != null)
            {
                var itemToAdd = new RowModel()
                {
                    ColumnName = "NewRow", DataType = DataType.String
                };
                var addCommand = new UndoableDelegateCommand("Add new row", new Action <object>((o) => this.AddNewRow(model, itemToAdd)), new Action <object>((o) => this.RemoveRow(model, itemToAdd)));
                this.Diagram.UndoRedoService.ExecuteCommand(addCommand);
            }
        }
        public void MoveTo(SwimlaneShapeBase dragObject, int endPosition, bool isUndoable = true)
        {
            bool shouldAdd = !this.Items.Contains(dragObject);
            bool addToDiagram = !this.Diagram.Items.Contains(dragObject);

            var diagramPosition = dragObject.Position;
            var startPosition = shouldAdd ? this.Items.Count : dragObject.ContainerPosition;
            endPosition = startPosition <= endPosition ? endPosition : endPosition + 1;

            var doAction = new Action<object>((o) =>
            {
                this.MoveContainer(startPosition, endPosition, dragObject);
                if (shouldAdd)
                    this.AddItem(dragObject);
                this.UpdateChildContainers();
            });

            if (isUndoable)
            {
                var command = new UndoableDelegateCommand("Move container",
                        doAction,
                         new Action<object>((o) =>
                         {
                             this.MoveContainer(endPosition, startPosition, dragObject);
                             if (shouldAdd)
                             {
                                 this.RemoveItem(dragObject);
                                 dragObject.Position = diagramPosition;
                                 if (addToDiagram)
                                     this.Diagram.Items.Remove(dragObject);
                             }
                             this.UpdateChildContainers();
                         }));
                this.Diagram.ServiceLocator.GetService<IUndoRedoService>().ExecuteCommand(command);
            }
            else
            {
                doAction.Invoke(null);
            }
        }
		internal void CommitItemColorStyle(ColorStyle oldColorStyle, ColorStyle newColorStyle)
		{
			if (this.owner == null)
			{
				return;
			}

			var compositeCommand = new CompositeCommand("Change Items Color Style");

			foreach (var item in this.SelectedItems)
			{
				var ensureDifferentItem = item;
				var command = new UndoableDelegateCommand("Change Item Color Style",
					p =>
						{
							if (newColorStyle != null)
							{
								ensureDifferentItem.Background = newColorStyle.Fill;
								ensureDifferentItem.BorderBrush = newColorStyle.Stroke;
								ensureDifferentItem.Stroke = newColorStyle.Stroke;
							}
						},
					p =>
						{
							if (oldColorStyle != null)
							{
								ensureDifferentItem.Background = oldColorStyle.Fill;
								ensureDifferentItem.BorderBrush = oldColorStyle.Stroke;
								ensureDifferentItem.Stroke = oldColorStyle.Stroke;
							}
						});
				compositeCommand.AddCommand(command);
			}

			this.owner.UndoRedoService.ExecuteCommand(compositeCommand);
		}
		internal void CommitItemStrokeDashArray(DoubleCollection oldStrokeDashArray, DoubleCollection newStrokeDashArray)
		{
			if (this.owner == null)
			{
				return;
			}

			var compositeCommand = new CompositeCommand("Change Items Border Dash Style");

			foreach (var item in this.SelectedItems)
			{
				var ensureDifferentItem = item;
				var command = new UndoableDelegateCommand("Change Item Border Dash Style",
					p => ensureDifferentItem.StrokeDashArray = newStrokeDashArray,
					p => ensureDifferentItem.StrokeDashArray = oldStrokeDashArray);
				compositeCommand.AddCommand(command);
			}

			this.owner.UndoRedoService.ExecuteCommand(compositeCommand);
		}
		internal void CommitItemStrokeThickness(double oldStrokeThickness, double newStrokeThickness)
		{
			if (this.owner == null)
			{
				return;
			}

			var compositeCommand = new CompositeCommand("Change Items Border Thickness");

			foreach (var item in this.SelectedItems)
			{
				var ensureDifferentItem = item;
				var command = new UndoableDelegateCommand("Change Item Border Thikness",
					p => ensureDifferentItem.StrokeThickness = newStrokeThickness,
					p => ensureDifferentItem.StrokeThickness = oldStrokeThickness);
				compositeCommand.AddCommand(command);
			}

			this.owner.UndoRedoService.ExecuteCommand(compositeCommand);
		}
		internal void CommitConnectionTargetCapType(CapType? oldTargetCapType, CapType? newTargetCapType)
		{
			if (this.owner == null)
			{
				return;
			}

			var compositeCommand = new CompositeCommand("Change Connections Target Cap Type");

			foreach (var conn in this.SelectedConnections)
			{
				var ensureDifferentConnection = conn;
				var command = new UndoableDelegateCommand("Change Connection Target Cap Type",
					p => ensureDifferentConnection.TargetCapType = newTargetCapType.HasValue ? newTargetCapType.Value : CapType.None,
					p => ensureDifferentConnection.TargetCapType = oldTargetCapType.HasValue ? oldTargetCapType.Value : CapType.None);
				compositeCommand.AddCommand(command);
			}

			this.owner.UndoRedoService.ExecuteCommand(compositeCommand);
		}
		internal void CommitShapeRotaionAngle(double newRotaionAngle)
		{
			if (this.owner == null)
			{
				return;
			}

			var compositeCommand = new CompositeCommand(CommandNames.RotateItems);

			foreach (var shape in this.SelectedShapes)
			{
				var ensureDifferentShape = shape;
				var ensureOldValue = ensureDifferentShape.RotationAngle;
				var command = new UndoableDelegateCommand(CommandNames.RotateItem,
					p => ensureDifferentShape.RotationAngle = newRotaionAngle,
					p => ensureDifferentShape.RotationAngle = ensureOldValue);
				compositeCommand.AddCommand(command);
			}

			this.owner.UndoRedoService.ExecuteCommand(compositeCommand);

			this.OnBoundsChanged();
		}
		internal void CommitShapePositionY(double newPositionY)
		{
			if (this.owner == null)
			{
				return;
			}

			var compositeCommand = new CompositeCommand(CommandNames.MoveItems);

			foreach (var shape in this.SelectedShapes)
			{
				var ensureDifferentShape = shape;
				var ensureOldValue = ensureDifferentShape.Position.Y;
				var command = new UndoableDelegateCommand(CommandNames.MoveItem,
					p => ensureDifferentShape.Position = new Point(ensureDifferentShape.Position.X, newPositionY),
					p => ensureDifferentShape.Position = new Point(ensureDifferentShape.Position.X, ensureOldValue));
				compositeCommand.AddCommand(command);
			}

			this.owner.UndoRedoService.ExecuteCommand(compositeCommand);

			this.OnBoundsChanged();
		}
        private UndoableDelegateCommand CreateRemoveConnectionCommand(IConnection connection)
        {
            var compositeRemoveConnectionCommand = new CompositeCommand(CommandNames.RemoveConnections);

            UndoableDelegateCommand removeCommand = new UndoableDelegateCommand(CommandNames.RemoveConnection, s => this.Diagram.RemoveConnection(connection), s => this.Diagram.AddConnection(connection));

            compositeRemoveConnectionCommand.AddCommand(removeCommand);

            return compositeRemoveConnectionCommand;
        }
		internal void CommitItemFontSize(double? oldFontSize, double? newFontSize)
		{
			if (this.owner == null)
			{
				return;
			}

			var compositeCommand = new CompositeCommand("Change Items Font Size");

			foreach (var item in this.SelectedItems)
			{
				var ensureDifferentItem = item;
				var command = new UndoableDelegateCommand("Change Item Font Size",
					p =>
						{
							if (newFontSize.HasValue)
							{
								ensureDifferentItem.FontSize = newFontSize.Value;
							}
						},
					p =>
						{
							if (oldFontSize.HasValue)
							{
								ensureDifferentItem.FontSize = oldFontSize.Value;
							}
						});
				compositeCommand.AddCommand(command);
			}

			this.owner.UndoRedoService.ExecuteCommand(compositeCommand);
		}
        private void OnDrop(object sender, DragDrop.DragEventArgs e)
        {
#if SILVERLIGHT
            var swimlane = e.Data as SwimlaneShapeBase;
#else
            var swimlane = (e.Data as DataObject).GetData(typeof(SwimlaneShapeBase)) as SwimlaneShapeBase;
#endif
            if (swimlane != null)
            {
                var transformedPoint = this.GetTransformedPoint(e.GetPosition(this));
                var newPosition = transformedPoint.Substract(swimlane.DragStartOffset);
                var oldPosition = swimlane.Position;
                var mainParent = swimlane.ParentContainer as MainContainerShapeBase;
                if (mainParent != null)
                {
                    var command = new UndoableDelegateCommand("Remove container from main",
                        new Action<object>((o) =>
                        {
                            swimlane.Position = newPosition;
                            mainParent.Items.Remove(swimlane);
                            mainParent.UpdateChildContainers();
                        }),
                           new Action<object>((o) =>
                           {
                               swimlane.Position = oldPosition;
                               mainParent.Items.Add(swimlane);
                               mainParent.UpdateChildContainers();
                           }));

                    this.UndoRedoService.ExecuteCommand(command);
                }
                else
                {
                    var command = new UndoableDelegateCommand("Remove container from main",
                                          new Action<object>((o) =>
                                          {
                                              swimlane.Position = newPosition;
                                          }),
                                          new Action<object>((o) =>
                                          {
                                              swimlane.Position = oldPosition;
                                          }));

                    this.UndoRedoService.ExecuteCommand(command);
                }
            }
        }
        internal void SwapChildren(SwimlaneShapeBase first, SwimlaneShapeBase second)
        {
            int firstPosition = first.ContainerPosition;
            int secondPosition = second.ContainerPosition;
            var command = new UndoableDelegateCommand("Swap children",
                       new Action<object>((o) =>
                       {
                           first.ContainerPosition = secondPosition;
                           second.ContainerPosition = firstPosition;
                           this.UpdateChildContainers();
                       }),
                          new Action<object>((o) =>
                          {
                              first.ContainerPosition = firstPosition;
                              second.ContainerPosition = secondPosition;
                              this.UpdateChildContainers();
                          }));

            this.Diagram.ServiceLocator.GetService<IUndoRedoService>().ExecuteCommand(command);
        }
		internal void CommitItemLabel(string oldLabel, string newLabel)
		{
			if (this.owner == null)
			{
				return;
			}

			var compositeCommand = new CompositeCommand("Change Items Content");

			foreach (var item in this.SelectedItems)
			{
				var ensureDifferentItem = item;
				var command = new UndoableDelegateCommand("Change Item Content",
					p => ensureDifferentItem.Content = newLabel,
					p => ensureDifferentItem.Content = oldLabel);
				compositeCommand.AddCommand(command);
			}

			this.owner.UndoRedoService.ExecuteCommand(compositeCommand);
		}
		internal void CommitItemFontFamily(FontFamily oldFontFamily, FontFamily newFontFamily)
		{
			if (this.owner == null)
			{
				return;
			}

			var compositeCommand = new CompositeCommand("Change Items Font Family");

			foreach (var item in this.SelectedItems)
			{
				var ensureDifferentItem = item;
				var command = new UndoableDelegateCommand("Change Item Font Family",
					p => ensureDifferentItem.FontFamily = newFontFamily,
					p => ensureDifferentItem.FontFamily = oldFontFamily);
				compositeCommand.AddCommand(command);
			}

			this.owner.UndoRedoService.ExecuteCommand(compositeCommand);
		}
        private CompositeCommand CreateRemoveShapeCommand(RadDiagramShapeBase shape)
        {
            if (shape == null) return null;
            var compositeRemoveShapeCommand = new CompositeCommand(CommandNames.RemoveShapes);
            var removeCommand = new UndoableDelegateCommand(CommandNames.RemoveShape, s => this.Diagram.RemoveShape(shape), s => this.Diagram.AddShape(shape));

            var parentContainer = shape.ParentContainer;
            if (parentContainer != null)
            {
                var execute = new Action<object>((o) => parentContainer.RemoveItem(shape));
                var undo = new Action<object>((o) =>
                {
                    if (!parentContainer.Items.Contains(shape))
                        parentContainer.AddItem(shape);
                });
                compositeRemoveShapeCommand.AddCommand(new UndoableDelegateCommand(CommandNames.RemoveItemFromContainer, execute, undo));
            }

            foreach (var changeSourceCommand in shape.OutgoingLinks.Union(shape.IncomingLinks).ToList().Select(connection => this.CreateRemoveConnectionCommand(connection)))
            {
                compositeRemoveShapeCommand.AddCommand(changeSourceCommand);
            }

            compositeRemoveShapeCommand.AddCommand(removeCommand);

            var container = shape as RadDiagramContainerShape;
            if (container != null)
            {
                for (int i = container.Items.Count - 1; i >= 0; i--)
                {
                    var shapeToRemove = container.Items[i] as RadDiagramShapeBase;
                    if (shapeToRemove != null)
                        compositeRemoveShapeCommand.AddCommand(this.CreateRemoveShapeCommand(shapeToRemove));
                    else
                    {
                        var connection = container.Items[i] as IConnection;
                        if (connection != null && connection.Source == null && connection.Target == null)
                            compositeRemoveShapeCommand.AddCommand(this.CreateRemoveConnectionCommand(container.Items[i] as IConnection));
                    }
                }
            }

            return compositeRemoveShapeCommand;
        }
		internal void CommitItemFontColor(Brush oldFontColor, Brush newFontColor)
		{
			if (this.owner == null)
			{
				return;
			}

			var compositeCommand = new CompositeCommand("Change Items Font Color");

			foreach (var item in this.SelectedItems)
			{
				var ensureDifferentItem = item;
				var command = new UndoableDelegateCommand("Change Item Font Color",
					p =>
						{
							if (newFontColor != null)
							{
								ensureDifferentItem.Foreground = newFontColor;
							}
						},
					p =>
						{
							if (oldFontColor != null)
							{
								ensureDifferentItem.Foreground = oldFontColor;
							}
						});
				compositeCommand.AddCommand(command);
			}

			this.owner.UndoRedoService.ExecuteCommand(compositeCommand);
		}
		internal void CommitShapeHeight(double newHeight)
		{
			if (this.owner == null)
			{
				return;
			}

			var compositeCommand = new CompositeCommand(CommandNames.ResizeShapes);

			foreach (var shape in this.SelectedShapes)
			{
				var ensureDifferentShape = shape;
				var ensureOldValue = ensureDifferentShape.Height;
				var command = new UndoableDelegateCommand(CommandNames.ResizeShape,
					p => ensureDifferentShape.Height = newHeight,
					p => ensureDifferentShape.Height = ensureOldValue);
				compositeCommand.AddCommand(command);
			}

			this.owner.UndoRedoService.ExecuteCommand(compositeCommand);

			this.OnBoundsChanged();
		}