-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathCommand.cs
More file actions
766 lines (702 loc) · 20.2 KB
/
Command.cs
File metadata and controls
766 lines (702 loc) · 20.2 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
// Copyright (c) 2003-2017 SIL International
// This software is licensed under the LGPL, version 2.1 or later
// (http://www.gnu.org/licenses/lgpl-2.1.html)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using System.Xml;
using SIL.FieldWorks.Common.FwUtils;
using SIL.Reporting;
using SIL.Utils;
namespace XCore
{
/// <summary>
/// Summary description for Commands.
/// </summary>
public class CommandSet : Hashtable, IDisposable
{
protected Mediator m_mediator;
/// -----------------------------------------------------------------------------------
/// <summary>
/// Initializes a new instance of the <see cref="Commands"/> class.
/// </summary>
/// -----------------------------------------------------------------------------------
public CommandSet(Mediator mediator)
{
m_mediator = mediator;
}
#region IDisposable & Co. implementation
// Region last reviewed: never
/// <summary>
/// Check to see if the object has been disposed.
/// All public Properties and Methods should call this
/// before doing anything else.
/// </summary>
public void CheckDisposed()
{
if (IsDisposed)
throw new ObjectDisposedException(String.Format("'{0}' in use after being disposed.", GetType().Name));
}
/// <summary>
/// True, if the object has been disposed.
/// </summary>
private bool m_isDisposed = false;
/// <summary>
/// See if the object has been disposed.
/// </summary>
public bool IsDisposed
{
get { return m_isDisposed; }
}
/// <summary>
/// Finalizer, in case client doesn't dispose it.
/// Force Dispose(false) if not already called (i.e. m_isDisposed is true)
/// </summary>
/// <remarks>
/// In case some clients forget to dispose it directly.
/// </remarks>
~CommandSet()
{
Dispose(false);
// The base class finalizer is called automatically.
}
/// <summary>
///
/// </summary>
/// <remarks>Must not be virtual.</remarks>
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
/// <summary>
/// Executes in two distinct scenarios.
///
/// 1. If disposing is true, the method has been called directly
/// or indirectly by a user's code via the Dispose method.
/// Both managed and unmanaged resources can be disposed.
///
/// 2. If disposing is false, the method has been called by the
/// runtime from inside the finalizer and you should not reference (access)
/// other managed objects, as they already have been garbage collected.
/// Only unmanaged resources can be disposed.
/// </summary>
/// <param name="disposing"></param>
/// <remarks>
/// If any exceptions are thrown, that is fine.
/// If the method is being done in a finalizer, it will be ignored.
/// If it is thrown by client code calling Dispose,
/// it needs to be handled by fixing the bug.
///
/// If subclasses override this method, they should call the base implementation.
/// </remarks>
protected virtual void Dispose(bool disposing)
{
System.Diagnostics.Debug.WriteLineIf(!disposing, "****** Missing Dispose() call for " + GetType().Name + ". ****** ");
// Must not be run more than once.
if (m_isDisposed)
return;
if (disposing)
{
// Dispose managed resources here.
foreach (Command cmd in Values)
cmd.Dispose();
Clear();
}
// Dispose unmanaged resources here, whether disposing is true or false.
m_mediator = null;
m_isDisposed = true;
}
#endregion IDisposable & Co. implementation
public void Init(XmlNode windowNode)
{
CheckDisposed();
XmlNodeList commands =windowNode.SelectNodes("commands/command");
foreach (XmlNode node in commands)
{
Command command = new Command(m_mediator, node);
this.Add(command.Id, command);
}
}
}
//!!!!!!!!!!!!!!!!!
//listSets objects are currently unused, as we are just accessing the raw XML for now.
//I have not figured out whether this raw approach will last once we have dynamic lists
//randy, now please resist the temptation to delete this stuff
// public class ChoiceListSet : Hashtable
// {
// /// -----------------------------------------------------------------------------------
// /// <summary>
// /// Initializes a new instance of the <see cref="ListSet"/> class.
// /// </summary>
// /// -----------------------------------------------------------------------------------
// public ChoiceListSet()
// {
// }
//
// public void Init(Mediator mediator,XmlNode windowNode)
// {
// XmlNodeList lists =windowNode.SelectNodes("lists/list");
// foreach (XmlNode node in lists)
// {
// ChoiceList list = new ChoiceList(mediator, node);
// this.Add(list.ID, list);
// }
// }
// }
/*
public class ChoiceList : ArrayList
{
protected string m_id;
protected XmlNode m_configurationNode;
public ChoiceList(Mediator mediator, XmlNode configurationNode)
{
m_configurationNode= configurationNode;
m_id = XmlUtils.GetAttributeValue(configurationNode, "id");
XmlNodeList lists =configurationNode.SelectNodes("item");
foreach (XmlNode node in lists)
{
Command command = new Command(mediator, node);
this.Add(command);
}
}
public string ID
{
get
{
return m_id;
}
}
public XmlNode ConfigurationXml
{
get
{
return m_configurationNode;
}
}
}
*/
public interface ICommandUndoRedoText
{
string UndoText { get; }
string RedoText { get; }
}
public class Command : IDisposable, ICommandUndoRedoText
{
#region Fields
protected Mediator m_mediator;
protected string m_id;
protected string m_label;
protected string m_shortcut;
protected string m_toolTipInsert;
protected string m_tooltip;
//only one of these two will be used
protected string m_valueString;
protected string m_messageString;
private bool m_oneAtATime; // true if only one instance of a given command can run at a time
private static HashSet<string> m_OneAtATimeSet = new HashSet<string>(); // set of current commands that are 'oneatatime'
// add the ability to trace (dynamically) flow of commands through the system
private TraceSwitch m_traceCMDSwitch = new TraceSwitch("Command.Trace", "Flow of each command", "Off");
protected XmlNode m_configurationNode;
#endregion Fields
#region Properties
public string Id
{
get
{
CheckDisposed();
return m_id;
}
}
public string Label
{
get
{
CheckDisposed();
return m_label;
}
}
public string Message
{
get
{
CheckDisposed();
return m_messageString;
}
}
public bool OneAtATime
{
get
{
CheckDisposed();
return m_oneAtATime;
}
}
/// <summary>
/// A string to be inserted later into a toolTip associated with the command.
/// Set by AdjustInsertCommandName().
/// </summary>
public string ToolTipInsert
{
get
{
CheckDisposed();
return m_toolTipInsert;
}
set
{
CheckDisposed();
m_toolTipInsert = value;
}
}
/// <summary>
/// String to be used as a tool tip.
/// </summary>
public string ToolTip
{
get
{
CheckDisposed();
if (String.IsNullOrEmpty(m_tooltip) && !String.IsNullOrEmpty(m_label))
{
// generate a tooltip from the label.
return m_label.Replace("_", string.Empty);
}
return m_tooltip;
}
}
public string UndoRedoTextInsert
{
get
{
CheckDisposed();
string text = ToolTip;
if (text.EndsWith("..."))
text = text.Remove(text.Length - 3);
return text;
}
}
/// <summary>
/// Target for "Show in ..." commands.
/// </summary>
public Guid TargetId { get; set; }
#region ICommandUndoRedoText
public string UndoText
{
get
{
return string.Format(xCoreInterfaces.ksUndoCommand, UndoRedoTextInsert);
}
}
public string RedoText
{
get
{
return string.Format(xCoreInterfaces.ksRedoCommand, UndoRedoTextInsert);
}
}
#endregion
/// <summary>
/// It might seem strange to give this a way to strangers, but it is needed for the strangers to create helpful error messages.
/// </summary>
public XmlNode ConfigurationNode
{
get
{
CheckDisposed();
return m_configurationNode;
}
}
public string ValueString
{
get
{
CheckDisposed();
return m_valueString;
}
}
public string IconName
{
get
{
CheckDisposed();
return XmlUtils.GetAttributeValue(m_configurationNode, "icon");
}
}
//used for choice lists
//used for command items
public string MessageString
{
get
{
CheckDisposed();
return m_messageString;
}
}
public Keys Shortcut
{
get
{
CheckDisposed();
if (m_configurationNode == null)
return Keys.None;
string sc = XmlUtils.GetAttributeValue(m_configurationNode, "shortcut", "");
return GetShortcutKeys(sc);
}
}
public Keys Shortcut2
{
get
{
CheckDisposed();
if (m_configurationNode == null)
return Keys.None;
string sc = XmlUtils.GetAttributeValue(m_configurationNode, "shortcut2", "");
return GetShortcutKeys(sc);
}
}
private Keys GetShortcutKeys(string sc)
{
if (sc != "")
{
// Note: On a German system, KeysConverter().ConvertFromString, ConvertToString,
// ConvertToInvariantString, etc. all use German strings (e.g., Strg for Ctrl)
// regardless of using the locale specific calls. So to get things to work, we
// need to parse the English shortcut string and reconstruct the internal keys.
Keys keys = Keys.None;
try
{
string s;
string srest = sc.ToLower();
while (srest.Length > 0)
{
int i = srest.IndexOf('+');
if (i < 0)
{
s = srest;
srest = "";
}
else
{
s = srest.Substring(0, i);
srest = srest.Substring(i + 1);
}
switch (s)
{
default:
keys |= (Keys)new KeysConverter().ConvertFromString(s.ToUpper());
break;
case "alt":
keys |= Keys.Alt;
break;
case "back":
keys |= Keys.Back;
break;
case "cancel":
keys |= Keys.Cancel;
break;
case "ctrl":
keys |= Keys.Control;
break;
case "del":
keys |= Keys.Delete;
break;
case "down":
keys |= Keys.Down;
break;
case "end":
keys |= Keys.End;
break;
case "enter":
keys |= Keys.Enter;
break;
case "esc":
keys |= Keys.Escape;
break;
case "home":
keys |= Keys.Home;
break;
case "ins":
keys |= Keys.Insert;
break;
case "left":
keys |= Keys.Left;
break;
case "pgdwn":
keys |= Keys.PageDown;
break;
case "pgup":
keys |= Keys.PageUp;
break;
case "right":
keys |= Keys.Right;
break;
case "shift":
keys |= Keys.Shift;
break;
case "tab":
keys |= Keys.Tab;
break;
case "up":
keys |= Keys.Up;
break;
}
;
}
;
}
catch (Exception e)
{
throw new SIL.Utils.ConfigurationException("The System.Windows.Forms.KeysConverter() did not understand this key description:"
+ sc + ".", m_configurationNode, e);
}
return keys;
}
return Keys.None;
}
#endregion
#region Initialization
public Command(Mediator mediator, XmlNode commandNode)
{
m_mediator = mediator;
m_configurationNode = commandNode;
m_id = XmlUtils.GetAttributeValue(commandNode, "id");
m_label = XmlUtils.GetLocalizedAttributeValue(commandNode, "label", null);
m_tooltip = XmlUtils.GetLocalizedAttributeValue(commandNode, "tooltip", null);
m_shortcut = XmlUtils.GetAttributeValue(commandNode,"shortcut");
m_messageString = XmlUtils.GetAttributeValue(commandNode,"message");
m_valueString = XmlUtils.GetAttributeValue(commandNode,"value");
string boolValue = XmlUtils.GetAttributeValue(commandNode, "oneatatime", "false");
m_oneAtATime = Convert.ToBoolean(boolValue);
Trace.WriteLineIf(m_traceCMDSwitch.TraceVerbose, BuildDebugMsg("Constructor"), m_traceCMDSwitch.DisplayName);
}
#endregion
#region Trace helper methods
/// <summary>
/// Method to build the debug string for the trace output.
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
private string BuildDebugMsg(string msg)
{
System.Text.StringBuilder msgOut = new System.Text.StringBuilder();
msgOut.Append(DateTime.Now.ToString("HH:mm:ss"));
msgOut.Append("-");
msgOut.Append(msg);
msgOut.Append(": ");
msgOut.Append(m_id);
msgOut.Append("(" + m_messageString + ")");
return msgOut.ToString();
}
/// <summary>The MessageString isn't unique by it self so use the ID too.</summary>
private string CmdKey { get { return m_id + m_messageString; } }
#endregion
#region IDisposable & Co. implementation
// Region last reviewed: never
/// <summary>
/// Check to see if the object has been disposed.
/// All public Properties and Methods should call this
/// before doing anything else.
/// </summary>
public void CheckDisposed()
{
if (IsDisposed)
throw new ObjectDisposedException(String.Format("'{0}' in use after being disposed.", GetType().Name));
}
/// <summary>
/// True, if the object has been disposed.
/// </summary>
private bool m_isDisposed = false;
/// <summary>
/// See if the object has been disposed.
/// </summary>
public bool IsDisposed
{
get { return m_isDisposed; }
}
/// <summary>
/// Finalizer, in case client doesn't dispose it.
/// Force Dispose(false) if not already called (i.e. m_isDisposed is true)
/// </summary>
/// <remarks>
/// In case some clients forget to dispose it directly.
/// </remarks>
~Command()
{
Trace.WriteLineIf(m_traceCMDSwitch.TraceVerbose, BuildDebugMsg("~Destructor"), m_traceCMDSwitch.DisplayName);
Dispose(false);
// The base class finalizer is called automatically.
}
/// <summary>
///
/// </summary>
/// <remarks>Must not be virtual.</remarks>
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
/// <summary>
/// Executes in two distinct scenarios.
///
/// 1. If disposing is true, the method has been called directly
/// or indirectly by a user's code via the Dispose method.
/// Both managed and unmanaged resources can be disposed.
///
/// 2. If disposing is false, the method has been called by the
/// runtime from inside the finalizer and you should not reference (access)
/// other managed objects, as they already have been garbage collected.
/// Only unmanaged resources can be disposed.
/// </summary>
/// <param name="disposing"></param>
/// <remarks>
/// If any exceptions are thrown, that is fine.
/// If the method is being done in a finalizer, it will be ignored.
/// If it is thrown by client code calling Dispose,
/// it needs to be handled by fixing the bug.
///
/// If subclasses override this method, they should call the base implementation.
///
/// IMPORTANT NOTE!
/// If CommandSets and Commands are not disposed of by the Mediator they will cause crashes
/// when the Mediator Dispose is called during the execution of the Command. This Dispose method is
/// needed for those edge cases. e.g. OnFLExBridge when it calls RefreshCacheWindowAndAll
/// </remarks>
protected virtual void Dispose(bool disposing)
{
System.Diagnostics.Debug.WriteLineIf(!disposing, "****** Missing Dispose() call for " + GetType().Name + ". ****** ");
// Must not be run more than once.
if (m_isDisposed)
return;
Trace.WriteLineIf(m_traceCMDSwitch.TraceVerbose, BuildDebugMsg("Dispose"), m_traceCMDSwitch.DisplayName);
if (disposing)
{
// Dispose managed resources here.
}
// Dispose unmanaged resources here, whether disposing is true or false.
m_mediator = null;
m_id = null;
m_label = null;
m_shortcut = null;
m_valueString = null;
m_messageString = null;
m_configurationNode = null;
m_isDisposed = true;
}
#endregion IDisposable & Co. implementation
/// <summary>
/// a list of child elements of the command element.
/// </summary>
public XmlNodeList Parameters
{
get
{
CheckDisposed();
return m_configurationNode.ChildNodes;
}
}
/// <summary>
/// Get a value of an optional attribute of a parameter element
/// </summary>
/// <param name="elementName">the name of the element containing the attribute</param>
/// <param name="attributeName">the name of the attribute</param>
/// <param name="defaultValue">the default value if the element or attribute are not found</param>
/// <returns></returns>
/// <remarks> this version allows you to use any element names you want.</remarks>
public string GetParameter(string elementName, string attributeName, string defaultValue)
{
CheckDisposed();
foreach(XmlNode element in Parameters)
{
if (element.Name==elementName)
{
return XmlUtils.GetAttributeValue(element, attributeName, defaultValue);
}
}
return defaultValue;
}
/// <summary>
/// Get a value of an optional attribute of a <parameters> element
/// </summary>
/// <param name="attributeName">the name of the attribute</param>
/// <param name="defaultValue">the default value if the element or attribute are not found</param>
/// <returns></returns>
///<remarks> this version assumes the element containing attribute is named "parameters"</remarks>
public string GetParameter(string attributeName, string defaultValue)
{
CheckDisposed();
return GetParameter("parameters", attributeName, defaultValue);
}
/// <summary>
/// Get a value of a mandatory attribute of a <parameters> element
/// </summary>
/// <param name="attributeName">the name of the attribute</param>
/// <exception cref="System.Configuration.ConfigurationException">in the parameter is not found</exception>
/// <returns></returns>
///<remarks> this version assumes the element containing attribute is named "parameters"</remarks>
public string GetParameter(string attributeName)
{
CheckDisposed();
string result = GetParameter(attributeName, null);
if (result == null)
throw new SIL.Utils.ConfigurationException("The command '"+this.Id+"' must have a parameter attribute named '"+attributeName +"'.",this.ConfigurationNode);
return result;
}
public void InvokeCommand()
{
Trace.WriteLineIf(m_traceCMDSwitch.TraceInfo, BuildDebugMsg("InvokeCommand-Start"), m_traceCMDSwitch.DisplayName);
CheckDisposed();
//if (m_mediator.AllowCommandsToExecute == false)
//{
// Trace.WriteLineIf(m_traceCMDSwitch.TraceInfo, BuildDebugMsg("InvokeCommand-Mediator not allowing commands"), m_traceCMDSwitch.DisplayName);
// return; // don't process any commands yet.
//}
// need locals as the command can be disposed of while we're processing it "MasterRefresh" is one such animal
bool oneAtATime = OneAtATime;
bool addedToSet = false;
string msgString = m_messageString;
string keyString = "empty";
try
{
if (!string.IsNullOrEmpty(msgString))
{
if (oneAtATime) // can only run one of these commands at a time
{
keyString = CmdKey;
if (m_OneAtATimeSet.Contains(keyString)) // already one running
{
Debug.WriteLine("*** Command is OneAtaTime and is still running.");
return;
}
m_OneAtATimeSet.Add(keyString);
addedToSet = true;
}
using (new WaitCursor(Form.ActiveForm))
{
Logger.WriteEvent("Start: " + msgString);
m_mediator.SendMessage(msgString, this);
Logger.WriteEvent("Done: " + msgString);
}
}
}
finally
{
if (addedToSet) // can only run one of these commands at a time
m_OneAtATimeSet.Remove(keyString);
Trace.WriteLineIf(m_traceCMDSwitch.TraceInfo, BuildDebugMsg("InvokeCommand- End"), m_traceCMDSwitch.DisplayName);
}
}
}
}