[2768] | 1 | using System;
|
---|
| 2 | using System.Diagnostics;
|
---|
| 3 | using System.Collections;
|
---|
| 4 | using System.Drawing;
|
---|
| 5 | using System.Windows.Forms;
|
---|
| 6 |
|
---|
[2861] | 7 | namespace Netron.Diagramming.Core {
|
---|
[2768] | 8 |
|
---|
[2861] | 9 | class ConnectionTool : AbstractTool, IMouseListener {
|
---|
[2768] | 10 |
|
---|
[2861] | 11 | #region Fields
|
---|
| 12 | /// <summary>
|
---|
| 13 | /// the location of the mouse when the motion starts
|
---|
| 14 | /// </summary>
|
---|
| 15 | private Point initialPoint;
|
---|
| 16 | private bool doDraw;
|
---|
| 17 | #endregion
|
---|
[2768] | 18 |
|
---|
[2861] | 19 | #region Constructor
|
---|
| 20 | /// <summary>
|
---|
| 21 | /// Initializes a new instance of the <see cref="T:ConnectionTool"/> class.
|
---|
| 22 | /// </summary>
|
---|
| 23 | /// <param name="name">The name of the tool.</param>
|
---|
| 24 | public ConnectionTool(string name)
|
---|
| 25 | : base(name) {
|
---|
| 26 | }
|
---|
| 27 | #endregion
|
---|
[2768] | 28 |
|
---|
[2861] | 29 | #region Methods
|
---|
[2768] | 30 |
|
---|
[2861] | 31 | /// <summary>
|
---|
| 32 | /// Called when the tool is activated.
|
---|
| 33 | /// </summary>
|
---|
| 34 | protected override void OnActivateTool() {
|
---|
| 35 | Controller.View.CurrentCursor = CursorPalette.Grip;
|
---|
| 36 | this.SuspendOtherTools();
|
---|
| 37 | doDraw = false;
|
---|
| 38 | }
|
---|
[2768] | 39 |
|
---|
[2861] | 40 | /// <summary>
|
---|
| 41 | /// Handles the mouse down event
|
---|
| 42 | /// </summary>
|
---|
| 43 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
| 44 | /// <returns>Returns 'true' if the event was handled, otherwise 'false'.</returns>
|
---|
| 45 | public bool MouseDown(MouseEventArgs e) {
|
---|
| 46 | if (e == null)
|
---|
| 47 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
| 48 | if (e.Button == MouseButtons.Left && Enabled && !IsSuspended) {
|
---|
[2768] | 49 |
|
---|
[2861] | 50 | initialPoint = e.Location;
|
---|
| 51 | doDraw = true;
|
---|
| 52 | return true;
|
---|
| 53 | }
|
---|
| 54 | return false;
|
---|
| 55 | }
|
---|
[2768] | 56 |
|
---|
[2861] | 57 | /// <summary>
|
---|
| 58 | /// Handles the mouse move event
|
---|
| 59 | /// </summary>
|
---|
| 60 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
| 61 | public void MouseMove(MouseEventArgs e) {
|
---|
| 62 | if (e == null)
|
---|
| 63 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
| 64 | Point point = e.Location;
|
---|
| 65 | if (IsActive) {
|
---|
| 66 | if (foundConnector != null)
|
---|
| 67 | foundConnector.Hovered = false;
|
---|
[2868] | 68 | foundConnector = this.Controller.Model.Selection.FindConnectorAt(e.Location);
|
---|
[2861] | 69 | if (foundConnector != null)
|
---|
| 70 | foundConnector.Hovered = true;
|
---|
| 71 | }
|
---|
| 72 | if (IsActive && doDraw) {
|
---|
| 73 | Controller.View.PaintGhostLine(initialPoint, point);
|
---|
| 74 | Controller.View.Invalidate(System.Drawing.Rectangle.Inflate(Controller.View.Ghost.Rectangle, 20, 20));
|
---|
[2768] | 75 |
|
---|
| 76 |
|
---|
[2861] | 77 | }
|
---|
| 78 | }
|
---|
| 79 | private IConnector foundConnector;
|
---|
| 80 | /// <summary>
|
---|
| 81 | ///
|
---|
| 82 | /// </summary>
|
---|
| 83 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
| 84 | public void MouseUp(MouseEventArgs e) {
|
---|
| 85 | if (IsActive) {
|
---|
| 86 | // First, make sure the initial point is far enough away from
|
---|
| 87 | // the final point to make a connection.
|
---|
| 88 | int maxX = Math.Abs(Math.Max(initialPoint.X, e.Location.X));
|
---|
| 89 | int maxY = Math.Abs(Math.Max(initialPoint.Y, e.Location.Y));
|
---|
[2768] | 90 |
|
---|
[2861] | 91 | if (!(maxX > ConnectionBase.MinLength) ||
|
---|
| 92 | !(maxY > ConnectionBase.MinLength)) {
|
---|
| 93 | return;
|
---|
| 94 | }
|
---|
[2768] | 95 |
|
---|
[2861] | 96 | //whatever comes hereafter, a compund command is the most economic approach
|
---|
| 97 | CompoundCommand package = new CompoundCommand(this.Controller);
|
---|
| 98 |
|
---|
| 99 | //let's see if the connection endpoints hit other connectors
|
---|
| 100 | //note that the following can be done because the actual connection has not been created yet
|
---|
| 101 | //otherwise the search would find the endpoints of the newly created connection, which
|
---|
| 102 | //would create a loop and a stack overflow!
|
---|
[2868] | 103 | IConnector startConnector = this.Controller.Model.Selection.FindConnectorAt(initialPoint);
|
---|
| 104 | IConnector endConnector = this.Controller.Model.Selection.FindConnectorAt(e.Location);
|
---|
[2861] | 105 |
|
---|
| 106 | #region Create the new connection
|
---|
| 107 | Connection cn = new Connection(this.initialPoint, e.Location, this.Controller.Model);
|
---|
| 108 | AddConnectionCommand newcon = new AddConnectionCommand(this.Controller, cn);
|
---|
| 109 |
|
---|
| 110 | #endregion
|
---|
| 111 |
|
---|
| 112 | #region Initial attachment?
|
---|
| 113 | if (startConnector != null) {
|
---|
| 114 | BindConnectorsCommand bindStart = new BindConnectorsCommand(this.Controller, startConnector, cn.From);
|
---|
| 115 | package.Commands.Add(bindStart);
|
---|
[2768] | 116 | }
|
---|
| 117 | #endregion
|
---|
[2861] | 118 |
|
---|
| 119 | #region Final attachment?
|
---|
| 120 | if (endConnector != null) {
|
---|
| 121 | BindConnectorsCommand bindEnd = new BindConnectorsCommand(this.Controller, endConnector, cn.To);
|
---|
| 122 | package.Commands.Add(bindEnd);
|
---|
| 123 | }
|
---|
| 124 | #endregion
|
---|
| 125 | package.Text = "New connection";
|
---|
| 126 | package.Commands.Add(newcon);
|
---|
| 127 | this.Controller.UndoManager.AddUndoCommand(package);
|
---|
| 128 |
|
---|
| 129 | //do it all
|
---|
| 130 | package.Redo();
|
---|
| 131 |
|
---|
| 132 | //reset highlight of the found connector
|
---|
| 133 | if (foundConnector != null)
|
---|
| 134 | foundConnector.Hovered = false;
|
---|
| 135 | //drop the painted ghost
|
---|
| 136 | Controller.View.ResetGhost();
|
---|
[2901] | 137 | this.doDraw = false;
|
---|
[2861] | 138 | }
|
---|
[2768] | 139 | }
|
---|
[2861] | 140 | #endregion
|
---|
| 141 | }
|
---|
[2768] | 142 |
|
---|
| 143 | }
|
---|