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