1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Collections;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.Windows.Forms;
|
---|
6 |
|
---|
7 | namespace Netron.Diagramming.Core {
|
---|
8 |
|
---|
9 | class ConnectionTool : AbstractTool, IMouseListener {
|
---|
10 |
|
---|
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
|
---|
18 |
|
---|
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
|
---|
28 |
|
---|
29 | #region Methods
|
---|
30 |
|
---|
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 | }
|
---|
39 |
|
---|
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) {
|
---|
49 |
|
---|
50 | initialPoint = e.Location;
|
---|
51 | doDraw = true;
|
---|
52 | return true;
|
---|
53 | }
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 |
|
---|
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;
|
---|
68 | foundConnector = Selection.FindConnectorAt(e.Location);
|
---|
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));
|
---|
75 |
|
---|
76 |
|
---|
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 | DeactivateTool();
|
---|
87 |
|
---|
88 | // First, make sure the initial point is far enough away from
|
---|
89 | // the final point to make a connection.
|
---|
90 | int maxX = Math.Abs(Math.Max(initialPoint.X, e.Location.X));
|
---|
91 | int maxY = Math.Abs(Math.Max(initialPoint.Y, e.Location.Y));
|
---|
92 |
|
---|
93 | if (!(maxX > ConnectionBase.MinLength) ||
|
---|
94 | !(maxY > ConnectionBase.MinLength)) {
|
---|
95 | return;
|
---|
96 | }
|
---|
97 |
|
---|
98 | //whatever comes hereafter, a compund command is the most economic approach
|
---|
99 | CompoundCommand package = new CompoundCommand(this.Controller);
|
---|
100 |
|
---|
101 | //let's see if the connection endpoints hit other connectors
|
---|
102 | //note that the following can be done because the actual connection has not been created yet
|
---|
103 | //otherwise the search would find the endpoints of the newly created connection, which
|
---|
104 | //would create a loop and a stack overflow!
|
---|
105 | IConnector startConnector = Selection.FindConnectorAt(initialPoint);
|
---|
106 | IConnector endConnector = Selection.FindConnectorAt(e.Location);
|
---|
107 |
|
---|
108 | #region Create the new connection
|
---|
109 | Connection cn = new Connection(this.initialPoint, e.Location, this.Controller.Model);
|
---|
110 | AddConnectionCommand newcon = new AddConnectionCommand(this.Controller, cn);
|
---|
111 |
|
---|
112 | #endregion
|
---|
113 |
|
---|
114 | #region Initial attachment?
|
---|
115 | if (startConnector != null) {
|
---|
116 | BindConnectorsCommand bindStart = new BindConnectorsCommand(this.Controller, startConnector, cn.From);
|
---|
117 | package.Commands.Add(bindStart);
|
---|
118 | }
|
---|
119 | #endregion
|
---|
120 |
|
---|
121 | #region Final attachment?
|
---|
122 | if (endConnector != null) {
|
---|
123 | BindConnectorsCommand bindEnd = new BindConnectorsCommand(this.Controller, endConnector, cn.To);
|
---|
124 | package.Commands.Add(bindEnd);
|
---|
125 | }
|
---|
126 | #endregion
|
---|
127 | package.Text = "New connection";
|
---|
128 | package.Commands.Add(newcon);
|
---|
129 | this.Controller.UndoManager.AddUndoCommand(package);
|
---|
130 |
|
---|
131 | //do it all
|
---|
132 | package.Redo();
|
---|
133 |
|
---|
134 | //reset highlight of the found connector
|
---|
135 | if (foundConnector != null)
|
---|
136 | foundConnector.Hovered = false;
|
---|
137 | //drop the painted ghost
|
---|
138 | Controller.View.ResetGhost();
|
---|
139 | //release other tools
|
---|
140 | this.UnsuspendTools();
|
---|
141 | }
|
---|
142 | }
|
---|
143 | #endregion
|
---|
144 | }
|
---|
145 |
|
---|
146 | }
|
---|