Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Tools/HitTool.cs @ 9503

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

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

File size: 7.2 KB
Line 
1using System;
2using System.Windows.Forms;
3
4namespace Netron.Diagramming.Core {
5  // ----------------------------------------------------------------------
6  /// <summary>
7  /// This tool implement the action of hitting an entity on the canvas.
8  /// </summary>
9  // ----------------------------------------------------------------------
10  public class HitTool : AbstractTool, IMouseListener, IKeyboardListener {
11    #region Fields
12
13    // ------------------------------------------------------------------
14    /// <summary>
15    /// Specifies if one of the multi-select keys are pressed.  If this
16    /// is false, then the current Selection is cleared before adding
17    /// any selected entities to the Selection.
18    /// </summary>
19    // ------------------------------------------------------------------
20    bool isMultiSelectKeyPressed = false;
21
22    // ------------------------------------------------------------------
23    /// <summary>
24    /// The keyboard keys that enable/disable multi-selection.
25    /// </summary>
26    // ------------------------------------------------------------------
27    Keys[] myMultiSelectKeys = new Keys[]
28            {
29                Keys.ShiftKey,
30                Keys.ControlKey
31            };
32
33    #endregion
34
35    #region Constructor
36
37    // ------------------------------------------------------------------
38    /// <summary>
39    /// Initializes a new instance of the <see cref="T:HitTool"/> class.
40    /// </summary>
41    /// <param name="name">The name of the tool.</param>
42    // ------------------------------------------------------------------
43    public HitTool(string name)
44      : base(name) {
45    }
46    #endregion
47
48    #region Methods
49
50    // ------------------------------------------------------------------
51    /// <summary>
52    /// Called when the tool is activated.
53    /// </summary>
54    // ------------------------------------------------------------------
55    protected override void OnActivateTool() {
56    }
57
58    // ------------------------------------------------------------------
59    /// <summary>
60    /// Handles the mouse down event
61    /// </summary>
62    /// <param name="e">The
63    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
64    /// containing the event data.</param>
65    // ------------------------------------------------------------------
66    public virtual bool MouseDown(MouseEventArgs e) {
67      if (e == null) {
68        throw new ArgumentNullException("The argument object is 'null'");
69      }
70
71      //if(e.Button == MouseButtons.Left  && Enabled && !IsSuspended)
72      if (Enabled && !IsSuspended) {
73        // Only if one of the multi-select keys are pressed do we NOT
74        // clear the selection before adding to it the selected entity.
75        bool clearSelectionFirst = true;
76        if (this.isMultiSelectKeyPressed == true) {
77          clearSelectionFirst = false;
78        }
79
80        // Also don't clear the selection if a group is currently
81        // selected so we can drill-down into it.
82        if (this.Controller.Model.Selection.SelectedItems.Count > 0) {
83          foreach (IDiagramEntity entity in this.Controller.Model.Selection.SelectedItems) {
84            if ((entity is IGroup) &&
85                (entity.Hit(e.Location) == true)) {
86              clearSelectionFirst = false;
87            }
88          }
89        }
90        this.Controller.Model.Selection.CollectEntitiesAt(e.Location, clearSelectionFirst);
91
92        if (this.Controller.Model.Selection.SelectedItems.Count > 0) {
93          IMouseListener listener =
94             this.Controller.Model.Selection.SelectedItems[0].GetService(
95              typeof(IMouseListener)) as IMouseListener;
96
97          if (listener != null) {
98            if (listener.MouseDown(e))
99              return true;
100          }
101        }
102
103        if ((this.Controller.Model.Selection.SelectedItems.Count > 0) &&
104            (this.Controller.Model.Selection.SelectedItems[0] is ITextProvider)) {
105          //ActivateTool();
106          ITextProvider textProvider =
107             this.Controller.Model.Selection.SelectedItems[0] as ITextProvider;
108
109          if ((e.Clicks == textProvider.EditTextClicks) &&
110              (textProvider.AllowTextEditing)) {
111            return Controller.ActivateTextEditor(textProvider);
112          }
113        }
114      }
115      return false;
116    }
117
118    // ------------------------------------------------------------------
119    /// <summary>
120    /// Handles the mouse move event.
121    /// </summary>
122    /// <param name="e">The
123    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
124    /// containing the event data.</param>
125    // ------------------------------------------------------------------
126    public virtual void MouseMove(MouseEventArgs e) {
127
128    }
129
130    // ------------------------------------------------------------------
131    /// <summary>
132    /// Handles the mouse up event.
133    /// </summary>
134    /// <param name="e">The
135    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
136    /// containing the event data.</param>
137    // ------------------------------------------------------------------
138    public virtual void MouseUp(MouseEventArgs e) {
139      if (IsActive) {
140        DeactivateTool();
141      }
142    }
143    #endregion
144
145    #region IKeyboardListener Members
146
147    // ------------------------------------------------------------------
148    /// <summary>
149    /// Handles the key down event.  If the key pressed is one of our
150    /// keys defined as a multi-select key, then selected items are
151    /// appended to the Selection (i.e. the Selection is not cleared
152    /// first).
153    /// </summary>
154    /// <param name="e">The
155    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
156    /// containing the event data.</param>
157    // ------------------------------------------------------------------
158    public void KeyDown(KeyEventArgs e) {
159      foreach (Keys k in this.myMultiSelectKeys) {
160        if (e.KeyCode == k) {
161          this.isMultiSelectKeyPressed = true;
162          return;
163        }
164      }
165    }
166
167    // ------------------------------------------------------------------
168    /// <summary>
169    /// Handles the key up event.  If the key pressed is one of our
170    /// keys defined as a multi-select key, then multi-select mode is
171    /// disabled.
172    /// </summary>
173    /// <param name="e">The
174    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
175    /// containing the event data.</param>
176    // ------------------------------------------------------------------
177    public void KeyUp(KeyEventArgs e) {
178      foreach (Keys k in this.myMultiSelectKeys) {
179        if (e.KeyCode == k) {
180          this.isMultiSelectKeyPressed = false;
181          return;
182        }
183      }
184    }
185
186    // ------------------------------------------------------------------
187    /// <summary>
188    /// Handles the key press event.  Nothing is performed here.
189    /// </summary>
190    /// <param name="e">The
191    /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
192    /// containing the event data.</param>
193    // ------------------------------------------------------------------
194    public void KeyPress(KeyPressEventArgs e) { }
195
196    #endregion
197  }
198
199}
Note: See TracBrowser for help on using the repository browser.