|
1 | 1 | using System; |
| 2 | +using System.ComponentModel; |
2 | 3 | using System.Drawing; |
| 4 | +using System.Runtime.CompilerServices; |
3 | 5 | using MapWinGIS; |
4 | 6 | using MW5.Api.Enums; |
5 | 7 | using MW5.Api.Helpers; |
|
8 | 10 |
|
9 | 11 | namespace MW5.Api.Concrete |
10 | 12 | { |
| 13 | + |
11 | 14 | public class GeometryEditor: IGeometryEditor |
12 | 15 | { |
13 | 16 | private readonly ShapeEditor _editor; |
14 | 17 |
|
| 18 | + private class CustomGeometryEditorEvents { |
| 19 | + public event EventHandler BeforeClearChangesEvent; |
| 20 | + internal void InvokeBeforeClearChangesEvent(object sender) |
| 21 | + => BeforeClearChangesEvent?.Invoke(sender, new EventArgs()); |
| 22 | + |
| 23 | + public event CancelEventHandler BeforeSaveChangesEvent; |
| 24 | + internal bool InvokeBeforeSaveChangesEvent(object sender) |
| 25 | + { |
| 26 | + if (BeforeSaveChangesEvent != null) |
| 27 | + { |
| 28 | + var args = new CancelEventArgs(); |
| 29 | + foreach (Delegate handler in BeforeSaveChangesEvent.GetInvocationList()) |
| 30 | + { |
| 31 | + handler.DynamicInvoke(this, args); |
| 32 | + if (args.Cancel) |
| 33 | + { |
| 34 | + return false; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + return true; |
| 39 | + } |
| 40 | + |
| 41 | + // The C# GeometryEditor gets re-created on the fly each time it is accesed from the map. |
| 42 | + // This table maps the actual COM object to the correct events we've added. |
| 43 | + private static ConditionalWeakTable<object, object> eventDict = new ConditionalWeakTable<object, object>(); |
| 44 | + internal static CustomGeometryEditorEvents Get(object _editor) { |
| 45 | + eventDict.TryGetValue(_editor, out var fetched); |
| 46 | + if (!(fetched is CustomGeometryEditorEvents events)) |
| 47 | + { |
| 48 | + events = new CustomGeometryEditorEvents(); |
| 49 | + eventDict.Remove(_editor); |
| 50 | + eventDict.Add(_editor, events); |
| 51 | + } |
| 52 | + return events; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + #region BeforeClearChangesEvent |
| 58 | + public event EventHandler BeforeClearChangesEvent |
| 59 | + { |
| 60 | + add |
| 61 | + { |
| 62 | + CustomGeometryEditorEvents.Get(_editor).BeforeClearChangesEvent += value; |
| 63 | + } |
| 64 | + remove |
| 65 | + { |
| 66 | + CustomGeometryEditorEvents.Get(_editor).BeforeClearChangesEvent -= value; |
| 67 | + } |
| 68 | + } |
| 69 | + protected virtual void RaiseBeforeClearChangesEvent() => CustomGeometryEditorEvents.Get(_editor).InvokeBeforeClearChangesEvent(this); |
| 70 | + #endregion |
| 71 | + |
| 72 | + #region BeforeSaveChangesEvent |
| 73 | + public event CancelEventHandler BeforeSaveChangesEvent |
| 74 | + { |
| 75 | + add |
| 76 | + { |
| 77 | + CustomGeometryEditorEvents.Get(_editor).BeforeSaveChangesEvent += value; |
| 78 | + } |
| 79 | + remove |
| 80 | + { |
| 81 | + CustomGeometryEditorEvents.Get(_editor).BeforeSaveChangesEvent -= value; |
| 82 | + } |
| 83 | + } |
| 84 | + protected virtual bool RaiseBeforeSaveChangesEvent() => CustomGeometryEditorEvents.Get(_editor).InvokeBeforeSaveChangesEvent(this); |
| 85 | + #endregion |
| 86 | + |
15 | 87 | internal GeometryEditor(ShapeEditor editor) |
16 | 88 | { |
17 | 89 | _editor = editor; |
@@ -128,11 +200,6 @@ public EditorValidation ValidationMode |
128 | 200 | get { return (EditorValidation)_editor.ValidationMode; } |
129 | 201 | set { _editor.ValidationMode = (tkEditorValidation)value; } |
130 | 202 | } |
131 | | - |
132 | | - public void Clear() |
133 | | - { |
134 | | - _editor.Clear(); |
135 | | - } |
136 | 203 |
|
137 | 204 | public void CopyStyleFrom(IGeometryStyle style) |
138 | 205 | { |
@@ -174,9 +241,17 @@ public bool AddPoint(ICoordinate pnt) |
174 | 241 | return _editor.AddPoint((MapWinGIS.Point) pnt.InternalObject); |
175 | 242 | } |
176 | 243 |
|
| 244 | + public void Clear() |
| 245 | + { |
| 246 | + RaiseBeforeClearChangesEvent(); |
| 247 | + _editor.Clear(); |
| 248 | + } |
| 249 | + |
177 | 250 | public bool SaveChanges() |
178 | 251 | { |
179 | | - return _editor.SaveChanges(); |
| 252 | + if (RaiseBeforeSaveChangesEvent()) |
| 253 | + return _editor.SaveChanges(); |
| 254 | + return false; |
180 | 255 | } |
181 | 256 |
|
182 | 257 | public bool ShowArea |
|
0 commit comments