Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Tools/ConnectionTool.cs @ 3038

Last change on this file since 3038 was 2901, checked in by mkommend, 15 years ago

enable disable action buttons (ticket #867)

File size: 5.0 KB
Line 
1using System;
2using System.Diagnostics;
3using System.Collections;
4using System.Drawing;
5using System.Windows.Forms;
6
7namespace 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 = this.Controller.Model.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        // 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));
90
91        if (!(maxX > ConnectionBase.MinLength) ||
92            !(maxY > ConnectionBase.MinLength)) {
93          return;
94        }
95
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!
103        IConnector startConnector = this.Controller.Model.Selection.FindConnectorAt(initialPoint);
104        IConnector endConnector = this.Controller.Model.Selection.FindConnectorAt(e.Location);
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);
116        }
117        #endregion
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();
137        this.doDraw = false;
138      }
139    }
140    #endregion
141  }
142
143}
Note: See TracBrowser for help on using the repository browser.