1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using System.Windows.Forms;
|
---|
5 | using System.Diagnostics;
|
---|
6 | namespace Netron.Diagramming.Core
|
---|
7 | {
|
---|
8 | // ----------------------------------------------------------------------
|
---|
9 | /// <summary>
|
---|
10 | /// The base class for any tool <see cref="ITool"/>.
|
---|
11 | /// </summary>
|
---|
12 | // ----------------------------------------------------------------------
|
---|
13 | public abstract class AbstractTool : ITool
|
---|
14 | {
|
---|
15 | #region Events
|
---|
16 |
|
---|
17 | // ------------------------------------------------------------------
|
---|
18 | /// <summary>
|
---|
19 | /// Occurs when this tool is deactivated.
|
---|
20 | /// </summary>
|
---|
21 | // ------------------------------------------------------------------
|
---|
22 | public event EventHandler<ToolEventArgs> OnToolDeactivate;
|
---|
23 |
|
---|
24 | // ------------------------------------------------------------------
|
---|
25 | /// <summary>
|
---|
26 | /// Occurs when this tool is activated.
|
---|
27 | /// </summary>
|
---|
28 | // ------------------------------------------------------------------
|
---|
29 | public event EventHandler<ToolEventArgs> OnToolActivate;
|
---|
30 |
|
---|
31 | #endregion
|
---|
32 |
|
---|
33 | #region Fields
|
---|
34 | /// <summary>
|
---|
35 | /// the Name field
|
---|
36 | /// </summary>
|
---|
37 | private string mName;
|
---|
38 | /// <summary>
|
---|
39 | /// the Enabled field
|
---|
40 | /// </summary>
|
---|
41 | private bool mEnabled = true;
|
---|
42 | /// <summary>
|
---|
43 | /// a pointer to the controller
|
---|
44 | /// </summary>
|
---|
45 | private IController mController;
|
---|
46 | /// <summary>
|
---|
47 | /// the tool's cursor
|
---|
48 | /// </summary>
|
---|
49 | private Cursor mCursor = Cursors.Default;
|
---|
50 | /// <summary>
|
---|
51 | /// whether the tool is currently active
|
---|
52 | /// </summary>
|
---|
53 | private bool mIsActive;
|
---|
54 | /// <summary>
|
---|
55 | /// keeps a reference to the previous cursor
|
---|
56 | /// </summary>
|
---|
57 | Cursor previousCursor ;
|
---|
58 | /// <summary>
|
---|
59 | /// the suspend state of the tool
|
---|
60 | /// </summary>
|
---|
61 | private bool mIsSuspended;
|
---|
62 | #endregion
|
---|
63 |
|
---|
64 | #region Properties
|
---|
65 |
|
---|
66 | /// <summary>
|
---|
67 | /// Gets or sets a value indicating whether this instance is suspended. A tool enters in a suspended mode when another tool has been activated and disallows another to continue its normal activity. For example, the <see cref="MoveTool"/> and <see cref="SelectionTool"/> are
|
---|
68 | /// mutually exclusive and similarly for the drawing tools and the selection tool.
|
---|
69 | /// <para>This suspended state is independent of the <see cref="IsActive"/> and the <see cref="Enabled"/> states.</para>
|
---|
70 | /// </summary>
|
---|
71 | /// <value>
|
---|
72 | /// <c>true</c> if this instance is suspended; otherwise, <c>false</c>.
|
---|
73 | /// </value>
|
---|
74 | public bool IsSuspended
|
---|
75 | {
|
---|
76 | get { return mIsSuspended; }
|
---|
77 | set { mIsSuspended = value; }
|
---|
78 | }
|
---|
79 |
|
---|
80 | /// <summary>
|
---|
81 | /// Gets or sets a value indicating whether this tool is active.
|
---|
82 | /// </summary>
|
---|
83 | /// <value><c>true</c> if this instance is active; otherwise, <c>false</c>.</value>
|
---|
84 | public bool IsActive
|
---|
85 | {
|
---|
86 | get { return mIsActive; }
|
---|
87 | set { mIsActive = value; }
|
---|
88 | }
|
---|
89 | /// <summary>
|
---|
90 | /// Gets or sets the name of the tool.
|
---|
91 | /// </summary>
|
---|
92 | /// <value>The name.</value>
|
---|
93 | public string Name
|
---|
94 | {
|
---|
95 | get { return mName; }
|
---|
96 | set { mName = value; }
|
---|
97 | }
|
---|
98 | /// <summary>
|
---|
99 | /// Gets or sets the controller.
|
---|
100 | /// </summary>
|
---|
101 | /// <value>The controller.</value>
|
---|
102 | public IController Controller
|
---|
103 | {
|
---|
104 | get
|
---|
105 | {
|
---|
106 | return mController;
|
---|
107 | }
|
---|
108 |
|
---|
109 | set
|
---|
110 | {
|
---|
111 | mController = value;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | /// <summary>
|
---|
116 | /// Gets or sets the cursor.
|
---|
117 | /// </summary>
|
---|
118 | /// <value>The cursor.</value>
|
---|
119 | public Cursor Cursor
|
---|
120 | {
|
---|
121 | get
|
---|
122 | {
|
---|
123 | return mCursor;
|
---|
124 | }
|
---|
125 | set
|
---|
126 | {
|
---|
127 | mCursor = value;
|
---|
128 | //prevCursor = Controller.View.CurrentCursor;
|
---|
129 | Controller.View.CurrentCursor = value;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | /// <summary>
|
---|
134 | /// Gets or sets the Enabled
|
---|
135 | /// </summary>
|
---|
136 | public bool Enabled
|
---|
137 | {
|
---|
138 | get
|
---|
139 | {
|
---|
140 | return this.mEnabled;
|
---|
141 | }
|
---|
142 |
|
---|
143 | set
|
---|
144 | {
|
---|
145 | //disable the tool first if it is active
|
---|
146 | if (!value && IsActive)
|
---|
147 | {
|
---|
148 | DeactivateTool();
|
---|
149 | }
|
---|
150 | mEnabled = value;
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | /// <summary>
|
---|
155 | /// Gets a value indicating whether this tool excludes other tools.
|
---|
156 | /// </summary>
|
---|
157 | /// <value>
|
---|
158 | /// <c>true</c> if this instance is exclusive; otherwise, <c>false</c>.
|
---|
159 | /// </value>
|
---|
160 | public virtual bool IsExclusive
|
---|
161 | {
|
---|
162 | get
|
---|
163 | {
|
---|
164 | return true;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | /// <summary>
|
---|
169 | /// Gets or sets a value indicating whether this tool can activated.
|
---|
170 | /// </summary>
|
---|
171 | /// <value>
|
---|
172 | /// <c>true</c> if this instance can activate; otherwise, <c>false</c>.
|
---|
173 | /// </value>
|
---|
174 | public virtual bool CanActivate
|
---|
175 | {
|
---|
176 | get
|
---|
177 | {
|
---|
178 | if (mEnabled )
|
---|
179 | {
|
---|
180 | return !IsActive ;
|
---|
181 | }
|
---|
182 | else
|
---|
183 | {
|
---|
184 | return false;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | }
|
---|
189 | #endregion
|
---|
190 |
|
---|
191 | #region Constructor
|
---|
192 | /// <summary>
|
---|
193 | /// Default constructor
|
---|
194 | /// </summary>
|
---|
195 | /// <param name="name">The name of the tool.</param>
|
---|
196 | public AbstractTool(string name)
|
---|
197 | {
|
---|
198 | this.mName = name;
|
---|
199 | }
|
---|
200 | #endregion
|
---|
201 |
|
---|
202 | #region Methods
|
---|
203 |
|
---|
204 | protected void RestoreCursor()
|
---|
205 | {
|
---|
206 | // I ran into an issue where the RectangleTool was activated,
|
---|
207 | // nothing drawn, then the EllipseTool was activated. When
|
---|
208 | // the EllipseTool was deactivated the cursor was set to the
|
---|
209 | // previous cursor, which was the cursor for the RectangleTool.
|
---|
210 |
|
---|
211 | // Update: Rather than fixing it here, I changed
|
---|
212 | // ControllerBase.ActivateTool(string) so it deactivates
|
---|
213 | // the current tool first.
|
---|
214 | //Controller.View.CurrentCursor = Cursors.Default;
|
---|
215 |
|
---|
216 | if (previousCursor != null)
|
---|
217 | {
|
---|
218 | if (Controller != null)
|
---|
219 | {
|
---|
220 | Controller.View.CurrentCursor = previousCursor;
|
---|
221 | }
|
---|
222 | previousCursor = null;
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | // ------------------------------------------------------------------
|
---|
227 | /// <summary>
|
---|
228 | /// Raises the <see cref="OnToolActivate"/> event
|
---|
229 | /// </summary>
|
---|
230 | /// <param name="e">ConnectionCollection event argument</param>
|
---|
231 | // ------------------------------------------------------------------
|
---|
232 | public virtual void RaiseOnToolActivate(ToolEventArgs e)
|
---|
233 | {
|
---|
234 | EventHandler<ToolEventArgs> handler = OnToolActivate;
|
---|
235 | if (handler != null)
|
---|
236 | {
|
---|
237 | handler(this, e);
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | // ------------------------------------------------------------------
|
---|
242 | /// <summary>
|
---|
243 | /// Raises the <see cref="OnToolDeactivate"/> event.
|
---|
244 | /// </summary>
|
---|
245 | /// <param name="e">ConnectionCollection event argument</param>
|
---|
246 | // ------------------------------------------------------------------
|
---|
247 | public virtual void RaiseOnToolDeactivate(ToolEventArgs e)
|
---|
248 | {
|
---|
249 | EventHandler<ToolEventArgs> handler = OnToolDeactivate;
|
---|
250 | if (handler != null)
|
---|
251 | {
|
---|
252 | handler(this, e);
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | #region Activation & deactivation
|
---|
257 |
|
---|
258 | /// <summary>
|
---|
259 | /// Deactivates the tool.
|
---|
260 | /// </summary>
|
---|
261 | /// <returns></returns>
|
---|
262 | public bool DeactivateTool()
|
---|
263 | {
|
---|
264 | if (IsActive)
|
---|
265 | {
|
---|
266 | OnDeactivateTool();
|
---|
267 | IsActive = false;
|
---|
268 | RestoreCursor();
|
---|
269 | UnsuspendTools();
|
---|
270 | RaiseOnToolDeactivate(new ToolEventArgs(this));
|
---|
271 | return true;
|
---|
272 | }
|
---|
273 | return false;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /// <summary>
|
---|
277 | /// Activates the tool.
|
---|
278 | /// </summary>
|
---|
279 | /// <returns></returns>
|
---|
280 | public bool ActivateTool()
|
---|
281 | {
|
---|
282 | //halt other actions
|
---|
283 | SuspendOtherTools();
|
---|
284 |
|
---|
285 | if (Enabled && !IsActive)
|
---|
286 | {
|
---|
287 | previousCursor = this.Controller.View.CurrentCursor;
|
---|
288 | IsActive = true;
|
---|
289 | OnActivateTool();
|
---|
290 | RaiseOnToolActivate(new ToolEventArgs(this));
|
---|
291 | }
|
---|
292 | return IsActive;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /// <summary>
|
---|
296 | /// Suspends the other tools.
|
---|
297 | /// </summary>
|
---|
298 | public void SuspendOtherTools()
|
---|
299 | {
|
---|
300 | foreach (ITool tool in Controller.Tools)
|
---|
301 | {
|
---|
302 | if (tool != this)
|
---|
303 | tool.IsSuspended = true;
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | /// <summary>
|
---|
308 | /// Releases the previously suspeneded tools <see cref="SuspendOtherTools"/>
|
---|
309 | /// </summary>
|
---|
310 | public void UnsuspendTools()
|
---|
311 | {
|
---|
312 | foreach (ITool tool in Controller.Tools)
|
---|
313 | {
|
---|
314 | tool.IsSuspended = false;
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | #endregion
|
---|
319 |
|
---|
320 | /// <summary>
|
---|
321 | /// Called when the tool is activated.
|
---|
322 | /// </summary>
|
---|
323 | protected virtual void OnActivateTool(){}
|
---|
324 |
|
---|
325 | /// <summary>
|
---|
326 | /// Called when the tool is deactivated.
|
---|
327 | /// </summary>
|
---|
328 | protected virtual void OnDeactivateTool(){}
|
---|
329 |
|
---|
330 | /// <summary>
|
---|
331 | /// Gets the service object of the specified type.
|
---|
332 | /// </summary>
|
---|
333 | /// <param name="serviceType">An object that specifies the type of service object to get.</param>
|
---|
334 | /// <returns>
|
---|
335 | /// A service object of type serviceType.-or- null if there is no service object of type serviceType.
|
---|
336 | /// </returns>
|
---|
337 | public object GetService(Type serviceType)
|
---|
338 | {
|
---|
339 | return null;
|
---|
340 | }
|
---|
341 | #endregion
|
---|
342 |
|
---|
343 | }
|
---|
344 | }
|
---|