diff --git a/Src/Common/FwUtils/EndOfActionManager.cs b/Src/Common/FwUtils/EndOfActionManager.cs index 784a74e7c4..d243a62458 100644 --- a/Src/Common/FwUtils/EndOfActionManager.cs +++ b/Src/Common/FwUtils/EndOfActionManager.cs @@ -52,7 +52,7 @@ internal void AddEvent(PublisherParameterObject publisherParameterObject) } // Add the dictionary entry if the key is not present. Overwrite the value if the key is present. - m_events[publisherParameterObject.Message] = publisherParameterObject.NewValue; + m_events[publisherParameterObject.Message] = publisherParameterObject.Data; } /// Should be private, but is public to support testing. diff --git a/Src/Common/FwUtils/EventConstants.cs b/Src/Common/FwUtils/EventConstants.cs index 2c162065b8..5ca95ff8b6 100644 --- a/Src/Common/FwUtils/EventConstants.cs +++ b/Src/Common/FwUtils/EventConstants.cs @@ -14,6 +14,7 @@ public static class EventConstants public const string CreateFirstRecord = "CreateFirstRecord"; public const string DataTreeDelete = "DataTreeDelete"; public const string DeleteRecord = "DeleteRecord"; + public const string DialogInsertItemInVector = "DialogInsertItemInVector"; public const string DictionaryConfigured = "DictionaryConfigured"; public const string FilterListChanged = "FilterListChanged"; public const string FollowLink = "FollowLink"; diff --git a/Src/Common/FwUtils/Publisher.cs b/Src/Common/FwUtils/Publisher.cs index 36f662da9d..672c763059 100644 --- a/Src/Common/FwUtils/Publisher.cs +++ b/Src/Common/FwUtils/Publisher.cs @@ -70,7 +70,7 @@ public void Publish(PublisherParameterObject publisherParameterObject) { Guard.AgainstNull(publisherParameterObject, nameof(publisherParameterObject)); - PublishMessage(publisherParameterObject.Message, publisherParameterObject.NewValue); + PublishMessage(publisherParameterObject.Message, publisherParameterObject.Data); } /// @@ -89,7 +89,7 @@ public void Publish(IList publisherParameterObjects) foreach (var publisherParameterObject in publisherParameterObjects) { - PublishMessage(publisherParameterObject.Message, publisherParameterObject.NewValue); + PublishMessage(publisherParameterObject.Message, publisherParameterObject.Data); } } #endregion diff --git a/Src/Common/FwUtils/PublisherParameterObject.cs b/Src/Common/FwUtils/PublisherParameterObject.cs index 06ecade733..93a8d8a4a7 100644 --- a/Src/Common/FwUtils/PublisherParameterObject.cs +++ b/Src/Common/FwUtils/PublisherParameterObject.cs @@ -8,7 +8,7 @@ namespace SIL.FieldWorks.Common.FwUtils { public sealed class PublisherParameterObject { - public PublisherParameterObject(string message, object newValue = null) + public PublisherParameterObject(string message, object data = null) { if (string.IsNullOrWhiteSpace(message)) { @@ -16,10 +16,25 @@ public PublisherParameterObject(string message, object newValue = null) } Message = message; - NewValue = newValue; + Data = data; } public string Message { get; } - public object NewValue { get; } + public object Data { get; } + } + + /// + /// Convenience class for use when we want to pass a return value back through the PublisherParameterObject. + /// + public class ReturnObject + { + public ReturnObject(object data) + { + Data = data; + ReturnValue = false; + } + + public object Data { get; set; } + public bool ReturnValue { get; set; } } } \ No newline at end of file diff --git a/Src/LexText/LexTextControls/EntryDlgListener.cs b/Src/LexText/LexTextControls/EntryDlgListener.cs index d7c31e7de2..2a91776b0f 100644 --- a/Src/LexText/LexTextControls/EntryDlgListener.cs +++ b/Src/LexText/LexTextControls/EntryDlgListener.cs @@ -7,6 +7,8 @@ using System; using System.Diagnostics; using System.Windows.Forms; +using SIL.FieldWorks.Common.FwUtils; +using static SIL.FieldWorks.Common.FwUtils.FwUtils; using SIL.LCModel; using SIL.LCModel.Infrastructure; using XCore; @@ -32,28 +34,51 @@ protected override string PersistentLabel public InsertEntryDlgListener() { + Subscriber.Subscribe(EventConstants.DialogInsertItemInVector, DialogInsertItemInVector); } #endregion Construction and Initialization + #region IDisposable + protected override void Dispose(bool disposing) + { + if (disposing) + { + Subscriber.Unsubscribe(EventConstants.DialogInsertItemInVector, DialogInsertItemInVector); + } + base.Dispose(disposing); + } + #endregion IDisposable + #region XCORE Message Handlers /// - /// Handles the xWorks message to insert a new lexical entry. + /// Handles the message to insert a new lexical entry. /// Invoked by the RecordClerk /// - /// The xCore Command object. - /// true, if we handled the message, otherwise false, if there was an unsupported 'classname' parameter - public bool OnDialogInsertItemInVector(object argument) + /// Object that contains the xCore Command object and has a ReturnValue. The + /// ReturnValue is true if we handled the message. + private void DialogInsertItemInVector(object obj) { CheckDisposed(); - Debug.Assert(argument != null && argument is XCore.Command); - string className = XmlUtils.GetOptionalAttributeValue( - (argument as Command).Parameters[0], - "className"); - if (className == null || className != "LexEntry") - return false; + if (!(obj is ReturnObject retObj) || + !(retObj.Data is Command command)) + { + Debug.Assert(false, "Received unexpected object type."); + return; + } + // Return if already handled by another Subscriber. + if (retObj.ReturnValue) + { + return; + } + // Only handle "LexEntry" class. + string className = XmlUtils.GetOptionalAttributeValue(command.Parameters[0], "className"); + if (className != "LexEntry") + { + return; + } using (InsertEntryDlg dlg = new InsertEntryDlg()) { @@ -72,7 +97,7 @@ public bool OnDialogInsertItemInVector(object argument) #pragma warning restore 618 } } - return true; // We "handled" the message, regardless of what happened. + retObj.ReturnValue = true; // We "handled" the message, regardless of what happened. } #endregion XCORE Message Handlers diff --git a/Src/LexText/LexTextControls/RecordDlgListener.cs b/Src/LexText/LexTextControls/RecordDlgListener.cs index 0cb4a8145f..82458b4de4 100644 --- a/Src/LexText/LexTextControls/RecordDlgListener.cs +++ b/Src/LexText/LexTextControls/RecordDlgListener.cs @@ -4,6 +4,8 @@ using System.Diagnostics; using System.Windows.Forms; +using SIL.FieldWorks.Common.FwUtils; +using static SIL.FieldWorks.Common.FwUtils.FwUtils; using SIL.LCModel; using SIL.Utils; using XCore; @@ -24,23 +26,55 @@ protected override string PersistentLabel #endregion Properties + #region Construction and Initialization + + public InsertRecordDlgListener() + { + Subscriber.Subscribe(EventConstants.DialogInsertItemInVector, DialogInsertItemInVector); + } + + #endregion Construction and Initialization + + #region IDisposable + protected override void Dispose(bool disposing) + { + if (disposing) + { + Subscriber.Unsubscribe(EventConstants.DialogInsertItemInVector, DialogInsertItemInVector); + } + base.Dispose(disposing); + } + #endregion IDisposable #region XCORE Message Handlers /// - /// Handles the xWorks message to insert a new Data Notebook record. + /// Handles the message to insert a new Data Notebook record. /// Invoked by the RecordClerk /// - /// The xCore Command object. - /// true, if we handled the message, otherwise false, if there was an unsupported 'classname' parameter - public bool OnDialogInsertItemInVector(object argument) + /// Object that contains the xCore Command object and has a ReturnValue. The + /// ReturnValue is true if we handled the message. + private void DialogInsertItemInVector(object arg) { CheckDisposed(); - var command = (Command) argument; + if (!(arg is ReturnObject retObj) || + !(retObj.Data is Command command)) + { + Debug.Assert(false, "Received unexpected object type."); + return; + } + // Return if already handled by another Subscriber. + if (retObj.ReturnValue) + { + return; + } + // Only handle "RnGenericRec" class. string className = XmlUtils.GetOptionalAttributeValue(command.Parameters[0], "className"); - if (className == null || className != "RnGenericRec") - return false; + if (className != "RnGenericRec") + { + return; + } bool subrecord = XmlUtils.GetOptionalBooleanAttributeValue(command.Parameters[0], "subrecord", false); bool subsubrecord = XmlUtils.GetOptionalBooleanAttributeValue(command.Parameters[0], "subsubrecord", false); @@ -74,7 +108,7 @@ public bool OnDialogInsertItemInVector(object argument) #pragma warning restore 618 } } - return true; // We "handled" the message, regardless of what happened. + retObj.ReturnValue = true; // We "handled" the message, regardless of what happened. } #endregion XCORE Message Handlers diff --git a/Src/LexText/Morphology/MasterCatDlgListener.cs b/Src/LexText/Morphology/MasterCatDlgListener.cs index e1c7470cf2..540454fd7d 100644 --- a/Src/LexText/Morphology/MasterCatDlgListener.cs +++ b/Src/LexText/Morphology/MasterCatDlgListener.cs @@ -4,9 +4,12 @@ using System.Diagnostics; using System.Windows.Forms; +using SIL.FieldWorks.Common.FwUtils; +using static SIL.FieldWorks.Common.FwUtils.FwUtils; using SIL.LCModel; using SIL.FieldWorks.LexText.Controls; using SIL.Utils; +using XCore; namespace SIL.FieldWorks.XWorks.MorphologyEditor { @@ -32,6 +35,7 @@ protected override string PersistentLabel /// public MasterCatDlgListener() { + Subscriber.Subscribe(EventConstants.DialogInsertItemInVector, DialogInsertItemInVector); } #endregion Construction and Initialization @@ -52,26 +56,46 @@ public MasterCatDlgListener() // The base class finalizer is called automatically. } + protected override void Dispose(bool disposing) + { + if (disposing) + { + Subscriber.Unsubscribe(EventConstants.DialogInsertItemInVector, DialogInsertItemInVector); + } + base.Dispose(disposing); + } #endregion IDisposable & Co. implementation #region XCORE Message Handlers /// - /// Handles the xWorks message to insert a new PartOfSpeech. + /// Handles the message to insert a new PartOfSpeech. /// Invoked by the RecordClerk via a main menu. /// - /// The xCore Command object. - /// true, if we handled the message, otherwise false, if there was an unsupported 'classname' parameter - public override bool OnDialogInsertItemInVector(object argument) + /// Object that contains the xCore Command object and has a ReturnValue. The + /// ReturnValue is true if we handled the message. + private void DialogInsertItemInVector(object obj) { CheckDisposed(); - Debug.Assert(argument != null && argument is XCore.Command); - string className = XmlUtils.GetOptionalAttributeValue( - (argument as XCore.Command).Parameters[0], "className"); - if (className == null || className != "PartOfSpeech") - return false; + if (!(obj is ReturnObject retObj) || + !(retObj.Data is Command command)) + { + Debug.Assert(false, "Received unexpected object type."); + return; + } + // Return if already handled by another Subscriber. + if (retObj.ReturnValue) + { + return; + } + // Only handle "PartOfSpeech" class. + string className = XmlUtils.GetOptionalAttributeValue(command.Parameters[0], "className"); + if (className != "PartOfSpeech") + { + return; + } using (var dlg = new MasterCategoryListDlg()) { @@ -91,7 +115,7 @@ public override bool OnDialogInsertItemInVector(object argument) break; } } - return true; // We "handled" the message, regardless of what happened. + retObj.ReturnValue = true; // We "handled" the message, regardless of what happened. } #endregion XCORE Message Handlers diff --git a/Src/LexText/Morphology/MasterDlgListener.cs b/Src/LexText/Morphology/MasterDlgListener.cs index c165d7ca8e..841bb7867e 100644 --- a/Src/LexText/Morphology/MasterDlgListener.cs +++ b/Src/LexText/Morphology/MasterDlgListener.cs @@ -193,20 +193,5 @@ public int Priority } #endregion IxCoreColleague implementation - - #region XCORE Message Handlers - - /// - /// Handles the xWorks message to insert a new class. - /// Invoked by the RecordClerk via a main menu. - /// - /// The xCore Command object. - /// true, if we handled the message, otherwise false, if there was an unsupported 'classname' parameter - public virtual bool OnDialogInsertItemInVector(object argument) - { - return false; // Needs to be handled by the override method - } - - #endregion XCORE Message Handlers } } diff --git a/Src/LexText/Morphology/MasterInflFeatDlgListener.cs b/Src/LexText/Morphology/MasterInflFeatDlgListener.cs index 34629c7893..9f7f22e245 100644 --- a/Src/LexText/Morphology/MasterInflFeatDlgListener.cs +++ b/Src/LexText/Morphology/MasterInflFeatDlgListener.cs @@ -4,9 +4,12 @@ using System.Diagnostics; using System.Windows.Forms; +using SIL.FieldWorks.Common.FwUtils; +using static SIL.FieldWorks.Common.FwUtils.FwUtils; using SIL.LCModel; using SIL.FieldWorks.LexText.Controls; using SIL.Utils; +using XCore; namespace SIL.FieldWorks.XWorks.MorphologyEditor { @@ -33,6 +36,7 @@ protected override string PersistentLabel /// public MasterInflFeatDlgListener() { + Subscriber.Subscribe(EventConstants.DialogInsertItemInVector, DialogInsertItemInVector); } #endregion Construction and Initialization @@ -53,28 +57,51 @@ public MasterInflFeatDlgListener() // The base class finalizer is called automatically. } + protected override void Dispose(bool disposing) + { + if (disposing) + { + Subscriber.Unsubscribe(EventConstants.DialogInsertItemInVector, DialogInsertItemInVector); + } + base.Dispose(disposing); + } #endregion IDisposable & Co. implementation #region XCORE Message Handlers /// - /// Handles the xWorks message to insert a new FsFeatDefn. + /// Handles the message to insert a new FsFeatDefn. /// Invoked by the RecordClerk via a main menu. /// - /// The xCore Command object. - /// true, if we handled the message, otherwise false, if there was an unsupported 'classname' parameter - public override bool OnDialogInsertItemInVector(object argument) + /// Object that contains the xCore Command object and has a ReturnValue. The + /// ReturnValue is true if we handled the message. + private void DialogInsertItemInVector(object obj) { CheckDisposed(); - Debug.Assert(argument != null && argument is XCore.Command); - string className = XmlUtils.GetOptionalAttributeValue( - (argument as XCore.Command).Parameters[0], "className"); - if (className == null || ((className != "FsClosedFeature") && (className != "FsComplexFeature"))) - return false; - if (className == "FsClosedFeature" && (argument as XCore.Command).Id != "CmdInsertClosedFeature") - return false; + if (!(obj is ReturnObject retObj) || + !(retObj.Data is Command command)) + { + Debug.Assert(false, "Received unexpected object type."); + return; + } + // Return if already handled by another Subscriber. + if (retObj.ReturnValue) + { + return; + } + // Only handle "FsClosedFeature" and "FsComplexFeature" classes. + string className = XmlUtils.GetOptionalAttributeValue(command.Parameters[0], "className"); + if ((className != "FsClosedFeature") && (className != "FsComplexFeature")) + { + return; + } + // Only handle "FsClosedFeature" for "CmdInsertClosedFeature". + if (className == "FsClosedFeature" && command.Id != "CmdInsertClosedFeature") + { + return; + } using (MasterInflectionFeatureListDlg dlg = new MasterInflectionFeatureListDlg(className)) { @@ -101,7 +128,7 @@ public override bool OnDialogInsertItemInVector(object argument) break; } } - return true; // We "handled" the message, regardless of what happened. + retObj.ReturnValue = true; // We "handled" the message, regardless of what happened. } #endregion XCORE Message Handlers diff --git a/Src/LexText/Morphology/MasterPhonFeatDlgListener.cs b/Src/LexText/Morphology/MasterPhonFeatDlgListener.cs index 07d5c1f419..1e36f1471c 100644 --- a/Src/LexText/Morphology/MasterPhonFeatDlgListener.cs +++ b/Src/LexText/Morphology/MasterPhonFeatDlgListener.cs @@ -9,8 +9,10 @@ using SIL.LCModel; using SIL.FieldWorks.LexText.Controls; using SIL.FieldWorks.Common.FwUtils; +using static SIL.FieldWorks.Common.FwUtils.FwUtils; using SIL.LCModel.Infrastructure; using SIL.Utils; +using XCore; namespace SIL.FieldWorks.XWorks.MorphologyEditor { @@ -37,6 +39,7 @@ protected override string PersistentLabel /// public MasterPhonFeatDlgListener() { + Subscriber.Subscribe(EventConstants.DialogInsertItemInVector, DialogInsertItemInVector); } #endregion Construction and Initialization @@ -57,27 +60,46 @@ public MasterPhonFeatDlgListener() // The base class finalizer is called automatically. } + protected override void Dispose(bool disposing) + { + if (disposing) + { + Subscriber.Unsubscribe(EventConstants.DialogInsertItemInVector, DialogInsertItemInVector); + } + base.Dispose(disposing); + } + #endregion IDisposable & Co. implementation #region XCORE Message Handlers /// - /// Handles the xWorks message to insert a new FsFeatDefn. + /// Handles the message to insert a new FsFeatDefn. /// Invoked by the RecordClerk via a main menu. /// - /// The xCore Command object. - /// true, if we handled the message, otherwise false, if there was an unsupported 'classname' parameter - public override bool OnDialogInsertItemInVector(object argument) + /// Object that contains the xCore Command object and has a ReturnValue. The + /// ReturnValue is true if we handled the message. + private void DialogInsertItemInVector(object obj) { CheckDisposed(); - Debug.Assert(argument != null && argument is XCore.Command); - string className = XmlUtils.GetOptionalAttributeValue( - (argument as XCore.Command).Parameters[0], "className"); - if ((className == null) || (className != "FsClosedFeature")) - return false; - if (className == "FsClosedFeature" && (argument as XCore.Command).Id != "CmdInsertPhonologicalClosedFeature") - return false; + if (!(obj is ReturnObject retObj) || + !(retObj.Data is Command command)) + { + Debug.Assert(false, "Received unexpected object type."); + return; + } + // Return if already handled by another Subscriber. + if (retObj.ReturnValue) + { + return; + } + // Only handle "FsClosedFeature" for "CmdInsertPhonologicalClosedFeature". + string className = XmlUtils.GetOptionalAttributeValue(command.Parameters[0], "className"); + if (className != "FsClosedFeature" || command.Id != "CmdInsertPhonologicalClosedFeature") + { + return; + } using (MasterPhonologicalFeatureListDlg dlg = new MasterPhonologicalFeatureListDlg(className)) { @@ -118,7 +140,7 @@ public override bool OnDialogInsertItemInVector(object argument) break; } } - return true; // We "handled" the message, regardless of what happened. + retObj.ReturnValue = true; // We "handled" the message, regardless of what happened. } #endregion XCORE Message Handlers diff --git a/Src/xWorks/RecordClerk.cs b/Src/xWorks/RecordClerk.cs index 6dc6970387..bb50363496 100644 --- a/Src/xWorks/RecordClerk.cs +++ b/Src/xWorks/RecordClerk.cs @@ -2551,10 +2551,10 @@ public bool OnInsertItemInVector(object argument) m_suppressSaveOnChangeRecord = true; try { -#pragma warning disable 618 // suppress obsolete warning - if (m_mediator.SendMessage("DialogInsertItemInVector", argument)) + var retObj = new ReturnObject(argument); + Publisher.Publish(new PublisherParameterObject(EventConstants.DialogInsertItemInVector, retObj)); + if (retObj.ReturnValue) return true; -#pragma warning restore 618 } finally {