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 @ 2861

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

intermediate version of graph visualization (ticket #867)

File size: 5.0 KB
RevLine 
[2768]1using System;
2using System.Diagnostics;
3using System.Collections;
4using System.Drawing;
5using System.Windows.Forms;
6
[2861]7namespace 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;
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));
[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        DeactivateTool();
[2768]87
[2861]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));
[2768]92
[2861]93        if (!(maxX > ConnectionBase.MinLength) ||
94            !(maxY > ConnectionBase.MinLength)) {
95          return;
96        }
[2768]97
[2861]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);
[2768]118        }
119        #endregion
[2861]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      }
[2768]142    }
[2861]143    #endregion
144  }
[2768]145
146}
Note: See TracBrowser for help on using the repository browser.