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