Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2389-EpsLexicase/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Tools/ConnectionTool.cs

Last change on this file was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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