-
Notifications
You must be signed in to change notification settings - Fork 0
/
External.cs
331 lines (288 loc) · 11 KB
/
External.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// <copyright file="External.cs" company="FC">
// Copyright (c) 2008 Fraser Chapman
// </copyright>
// <author>Fraser Chapman</author>
// <email>[email protected]</email>
// <date>2008-12-22</date>
// <summary>This file is part of FC.GEPluginCtrls
// FC.GEPluginCtrls is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
// </summary>
namespace FC.GEPluginCtrls
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.CSharp.RuntimeBinder;
/// <summary>
/// This COM Visible class contains all the public methods to be called from JavaScript.
/// The various events are used by the <see cref="GEWebBrowser"/> when dealing with the plug-in
/// </summary>
[ComVisible(true)]
public class External
{
#region Private fields
/// <summary>
/// Cache of kml event objects
/// </summary>
private static readonly Dictionary<string, AutoResetEvent> AutoResetDictionary =
new Dictionary<string, AutoResetEvent>();
/// <summary>
/// Stores fetched Kml Objects
/// </summary>
private static readonly Dictionary<string, object> KmlObjectDictionary =
new Dictionary<string, object>();
#endregion
#region Public events
/// <summary>
/// Raised when the plugin is ready
/// </summary>
public event EventHandler<GEEventArgs> PluginReady;
/// <summary>
/// Raised when there is a kml event
/// </summary>
public event EventHandler<GEEventArgs> KmlEvent;
/// <summary>
/// Raised when a KML/KMZ file has loaded
/// </summary>
public event EventHandler<GEEventArgs> KmlLoaded;
/// <summary>
/// Raised when there is a script error in the document
/// </summary>
public event EventHandler<GEEventArgs> ScriptError;
/// <summary>
/// Raised when there is a GEPlugin event (frameend, balloonclose)
/// </summary>
public event EventHandler<GEEventArgs> PluginEvent;
/// <summary>
/// Raised when there is a viewchangebegin, viewchange or viewchangeend event
/// </summary>
public event EventHandler<GEEventArgs> ViewEvent;
#endregion
#region Internal properites
/// <summary>
/// Gets the store of fetched IKmlObjects.
/// Used in the process of synchronously loading network links
/// </summary>
internal static Dictionary<string, object> KmlDictionary
{
get { return KmlObjectDictionary; }
}
internal static Dictionary<string, AutoResetEvent> ResetDictionary
{
get
{
return AutoResetDictionary;
}
}
#endregion
#region Public methods
/// <summary>
/// Allows JavaScript to send debug messages
/// </summary>
/// <param name="category">the category of the message</param>
/// <param name="message">the debug message</param>
public void DebugMessage(string category, string message)
{
Debug.WriteLine(message, category);
}
/// <summary>
/// Called from JavaScript when the plugin is ready
/// </summary>
/// <param name="ge">the plugin instance</param>
public void Ready(dynamic ge)
{
if (!GEHelpers.IsGE(ge))
{
throw new ArgumentException("ge is not of the type GEPlugin");
}
this.OnPluginReady(this, new GEEventArgs("Ready", "None", ge));
}
/// <summary>
/// Called from JavaScript when there is an error
/// </summary>
/// <param name="type">the error message</param>
/// <param name="message">the error type</param>
public void SendError(string type, string message)
{
this.OnScriptError(
this,
new GEEventArgs(type, message));
}
/// <summary>
/// Called from JavaScript when there is a kml event
/// </summary>
/// <param name="kmlEvent">the kml event</param>
/// <param name="action">the event id</param>
public void KmlEventCallback(object kmlEvent, string action)
{
dynamic eventObject = kmlEvent;
try
{
this.OnKmlEvent(
this,
new GEEventArgs(eventObject.getType(), action, eventObject));
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("KmlEventCallBack: " + rbex.Message, "External");
}
}
/// <summary>
/// Called from JavaScript when there is a GEPlugin event
/// </summary>
/// <param name="sender">The plugin object</param>
/// <param name="action">The event action</param>
public void PluginEventCallback(object sender, string action)
{
dynamic pluginEvent = sender;
try
{
this.OnPluginEvent(
this,
new GEEventArgs(pluginEvent.getType(), action, pluginEvent));
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("PluginEventCallBack: " + rbex.Message, "External");
}
}
/// <summary>
/// Called from JavaScript when there is a View event
/// </summary>
/// <param name="sender">The plug-in object</param>
/// <param name="action">The event action</param>
public void ViewEventCallback(object sender, string action)
{
dynamic viewEvent = sender;
try
{
this.OnViewEvent(
this,
new GEEventArgs(viewEvent.getType(), action, viewEvent));
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("ViewEventCallBack: " + rbex.Message, "External");
}
}
/// <summary>
/// Called from JavaScript when Kml has been fetched
/// </summary>
/// <param name="KmlObject">The fetched Kml object</param>
public void FetchKmlCallback(object KmlObject)
{
this.OnFetchKml(new GEEventArgs(KmlObject));
}
/// <summary>
/// Called from JavaScript when Kml has been synchronously fetched
/// </summary>
/// <param name="KmlObject">The fetched Kml object</param>
/// <param name="url">The url that the object was fetched from</param>
public void FetchKmlSynchronousCallback(object KmlObject, string url)
{
this.OnFetchKmlSynchronous(new GEEventArgs("FetchKmlSynchronous", url, KmlObject));
}
#endregion
#region Protected methods
/// <summary>
/// Protected method for raising the PluginReady event
/// </summary>
/// <param name="sender">The object that raised the event.</param>
/// <param name="e">Event arguments.</param>
protected virtual void OnPluginReady(object sender, GEEventArgs e)
{
EventHandler<GEEventArgs> handler = this.PluginReady;
if (handler != null)
{
handler(this, e);
}
}
/// <summary>
/// Protected method for raising the KmlEvent event
/// </summary>
/// <param name="kmlEvent">The kmlEvent object</param>
/// <param name="e">The Event arguments</param>
protected virtual void OnKmlEvent(dynamic kmlEvent, GEEventArgs e)
{
EventHandler<GEEventArgs> handler = this.KmlEvent;
if (handler != null)
{
handler(this, e);
}
}
/// <summary>
/// Protected method for raising the KmlLoaded event
/// </summary>
/// <param name="e">The Event arguments</param>
protected virtual void OnFetchKml(GEEventArgs e)
{
EventHandler<GEEventArgs> handler = this.KmlLoaded;
dynamic kmlObject = e.ApiObject;
if (handler != null)
{
handler(this, new GEEventArgs(kmlObject));
}
}
/// <summary>
/// Protected method for capturing fetched IKmlObjects
/// </summary>
/// <param name="e">The Event arguments</param>
protected virtual void OnFetchKmlSynchronous(GEEventArgs e)
{
lock (KmlDictionary)
{
KmlDictionary[e.Data] = e.ApiObject;
ResetDictionary[e.Data].Set();
}
}
/// <summary>
/// Protected method for raising the ScriptError event
/// </summary>
/// <param name="sender">The object that raised the event.</param>
/// <param name="e">Event arguments.</param>
protected virtual void OnScriptError(object sender, GEEventArgs e)
{
if (this.ScriptError != null)
{
this.ScriptError(this, e);
}
}
/// <summary>
/// Protected method for raising the PluginEvent event
/// </summary>
/// <param name="sender">The object that raised the event.</param>
/// <param name="e">Event arguments.</param>
protected virtual void OnPluginEvent(object sender, GEEventArgs e)
{
if (this.PluginEvent != null)
{
this.PluginEvent(this, e);
}
}
/// <summary>
/// Protected method for raising the ViewEvent event
/// </summary>
/// <param name="sender">The object that raised the event.</param>
/// <param name="e">Event arguments.</param>
protected virtual void OnViewEvent(object sender, GEEventArgs e)
{
if (this.ViewEvent != null)
{
this.ViewEvent(this, e);
}
}
#endregion
}
}