/// <summary> /// Sets the position of the control, according to the X & Y provided /// </summary> /// <param name="box"></param> /// <param name="x"></param> /// <param name="y"></param> private void SetBoxPosition(BoxControl <TEnclosing, TBoxModel, TArrowModel> box, int x, int y) { int posX = (x / GridSize) * GridSize; int posY = (y / GridSize) * GridSize; box.Location = new Point(posX, posY); }
/// <summary> /// Refreshes the control according to the model /// </summary> public void RefreshControl() { try { SuspendLayout(); // Clear all _boxes.Clear(); _arrows.Clear(); // Consider all boxes in this panel foreach (TBoxModel model in GetBoxes()) { // Ensure that the box is always visible if (model.X < 0) { model.X = 0; } if (model.Y < 0) { model.Y = 0; } BoxControl <TEnclosing, TBoxModel, TArrowModel> boxControl = CreateBox(model); _boxes[model] = boxControl; } // Consider all arrows in this panel List <TArrowModel> theArrows = GetArrows(); foreach (TArrowModel model in theArrows) { bool showArrow = true; if (model.Source != null) { // ReSharper disable once ConditionIsAlwaysTrueOrFalse showArrow = showArrow && !model.Source.Hidden; } if (model.Target != null) { showArrow = showArrow && !model.Target.Hidden; } if (showArrow) { ArrowControl <TEnclosing, TBoxModel, TArrowModel> arrowControl = CreateArrow(model); _arrows[model] = arrowControl; } } UpdatePositions(); } finally { ResumeLayout(true); } }
/// <summary> /// Handles a mouse up event /// </summary> /// <param name="sender"></param> /// <param name="mouseEventArgs"></param> private void HandleMouseUp(object sender, MouseEventArgs mouseEventArgs) { GraphicElement element = ElementForLocation(mouseEventArgs.Location, _movingBox); if (element != null) { element.HandleMouseUp(sender, mouseEventArgs); } if (_changingArrow != null) { _changingArrow = null; _chaningArrowAction = ChangeAction.None; RefreshControl(); } if (_movingBox != null) { if (_movingBoxHasMoved) { if (element != null) { BaseTreeNode targetNode = CorrespondingNode(element.Model as IModelElement); BaseTreeNode sourceNode = CorrespondingNode(_movingBox.Model as IModelElement); if (targetNode != null && sourceNode != null && sourceNode != targetNode) { targetNode.AcceptDrop(sourceNode); _movingBox.Location = new Point(0, 0); if (Settings.Default.AllowRefactor) { RefactorAndRelocateOperation refactorAndRelocate = new RefactorAndRelocateOperation(sourceNode.Model as ModelElement); refactorAndRelocate.ExecuteUsingProgressDialog(GuiUtils.MdiWindow, "Refactoring", false); } } } // Register the fact that the element has moved // because if (_movingBox.TypedModel.X != _positionBeforeMove.X || _movingBox.TypedModel.Y != _positionBeforeMove.Y) { EfsSystem.Instance.Context.HandleChangeEvent(_movingBox.Model as BaseModelElement, Context.ChangeKind.ModelChange); } } _movingBox = null; _movingBoxHasMoved = false; } }
/// <summary> /// Provides the box control which corresponds to the model provided /// </summary> /// <param name="model"></param> /// <returns></returns> public BoxControl <TEnclosing, TBoxModel, TArrowModel> GetBoxControl(TBoxModel model) { BoxControl <TEnclosing, TBoxModel, TArrowModel> retVal = null; if (model != null) { if (_boxes.ContainsKey(model)) { retVal = _boxes[model]; } } return(retVal); }
/// <summary> /// Provides the box at a given location /// </summary> /// <param name="location"></param> /// <param name="excludedElement">This element should not be considered during search</param> /// <returns></returns> protected BoxControl <TEnclosing, TBoxModel, TArrowModel> BoxForLocation(Point location, GraphicElement excludedElement) { BoxControl <TEnclosing, TBoxModel, TArrowModel> retVal = null; foreach (BoxControl <TEnclosing, TBoxModel, TArrowModel> box in _boxes.Values) { if ((location.X > box.Location.X && location.X < box.Location.X + box.Width) && (location.Y > box.Location.Y && location.Y < box.Location.Y + box.Height)) { if (box != excludedElement) { retVal = box; } } } return(retVal); }
/// <summary> /// Called when the drop operation is performed on a node /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DragDropHandler(object sender, DragEventArgs e) { if (e.Data.GetDataPresent("WindowsForms10PersistentObject", false)) { object data = e.Data.GetData("WindowsForms10PersistentObject"); BaseTreeNode sourceNode = data as BaseTreeNode; if (sourceNode != null) { // The location where the element has been dropped Point location = PointToClient(new Point(e.X, e.Y)); BoxControl <TEnclosing, TBoxModel, TArrowModel> target = BoxForLocation(location, null); if (target != null) { target.AcceptDrop(sourceNode.Model as ModelElement); } } } }
/// <summary> /// Handles a mouse down event /// </summary> /// <param name="sender"></param> /// <param name="mouseEventArgs"></param> public void HandleMouseDown(object sender, MouseEventArgs mouseEventArgs) { GraphicElement element = ElementForLocation(mouseEventArgs.Location, null); if (element != null) { element.HandleMouseDown(sender, mouseEventArgs); } if (mouseEventArgs.Button == MouseButtons.Left) { Point clickPoint = new Point(mouseEventArgs.X, mouseEventArgs.Y); foreach (ArrowControl <TEnclosing, TBoxModel, TArrowModel> arrow in _arrows.Values) { if (Around(arrow.StartLocation, clickPoint)) { _changingArrow = arrow; _chaningArrowAction = ChangeAction.InitialBox; Selected = arrow; break; } if (Around(arrow.TargetLocation, clickPoint)) { _changingArrow = arrow; _chaningArrowAction = ChangeAction.TargetBox; Selected = arrow; break; } } if (_changingArrow == null) { BoxControl <TEnclosing, TBoxModel, TArrowModel> box = BoxForLocation(clickPoint, null); if (box != null) { _movingBox = box; _movingBoxHasMoved = false; _moveStartLocation = mouseEventArgs.Location; _positionBeforeMove = box.Location; } } } }
/// <summary> /// Creates the editor for the selected object /// </summary> /// <param name="model"></param> /// <returns></returns> public override object CreateEditor(IModelElement model) { object retVal = null; BoxControl <TEnclosing, TBoxModel, TArrowModel> boxControl = GetBoxControl(model as TBoxModel); if (boxControl != null) { retVal = CreateBoxEditor(boxControl); } ArrowControl <TEnclosing, TBoxModel, TArrowModel> arrowControl = GetArrowControl(model as TArrowModel); if (arrowControl != null) { retVal = CreateArrowEditor(arrowControl); } return(retVal); }
/// <summary> /// Handles the move event, which, in case of an arrow is selected to be modified, /// </summary> /// <param name="sender"></param> /// <param name="mouseEventArgs"></param> private void HandleMouseMove(object sender, MouseEventArgs mouseEventArgs) { GraphicElement element = ElementForLocation(mouseEventArgs.Location, null); if (element != null) { element.HandleMouseMove(sender, mouseEventArgs); } if (_changingArrow != null && _chaningArrowAction != ChangeAction.None) { BoxControl <TEnclosing, TBoxModel, TArrowModel> box = BoxForLocation(mouseEventArgs.Location, null); if (box != null) { switch (_chaningArrowAction) { case ChangeAction.InitialBox: if (_changingArrow.TypedModel.Source != box.Model) { _changingArrow.SetInitialBox(box.TypedModel); } break; case ChangeAction.TargetBox: if (_changingArrow.TypedModel.Target != box.Model) { if (_changingArrow.TypedModel.Source != null) { _changingArrow.SetTargetBox(box.TypedModel); } } break; } } } if (_movingBox != null) { Point mouseMoveLocation = mouseEventArgs.Location; int deltaX = mouseMoveLocation.X - _moveStartLocation.X; int deltaY = mouseMoveLocation.Y - _moveStartLocation.Y; if (Math.Abs(deltaX) > 5 || Math.Abs(deltaY) > 5) { IModelElement model = _movingBox.TypedModel; if (model != null && !_movingBoxHasMoved) { Context.SelectionCriteria criteria = GuiUtils.SelectionCriteriaBasedOnMouseEvent(mouseEventArgs); EfsSystem.Instance.Context.SelectElement(model, this, criteria); _movingBoxHasMoved = true; } Util.DontNotify(() => { int newX = _positionBeforeMove.X + deltaX; int newY = _positionBeforeMove.Y + deltaY; SetBoxPosition(_movingBox, newX, newY); UpdatePositions(); }); } } }
/// <summary> /// Factory for BoxEditor /// </summary> /// <param name="control"></param> /// <returns></returns> protected virtual BoxEditor <TEnclosing, TBoxModel, TArrowModel> CreateBoxEditor( BoxControl <TEnclosing, TBoxModel, TArrowModel> control) { return(new BoxEditor <TEnclosing, TBoxModel, TArrowModel>(control)); }
/// <summary> /// Constructor /// </summary> /// <param name="control"></param> public BoxEditor(BoxControl <TEnclosing, TBoxModel, TArrowModel> control) { Control = control; }