Skip to content

Commit fceeb85

Browse files
authored
LT-21519: Implement ConsideringClosing in Pub/Sub system
Note: Currently there are no code paths that will block the close.
1 parent 90d4933 commit fceeb85

8 files changed

Lines changed: 83 additions & 47 deletions

File tree

Src/Common/Controls/XMLViews/XmlBrowseRDEView.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ protected override void Dispose( bool disposing )
7676
if( disposing )
7777
{
7878
Subscriber.Unsubscribe(EventConstants.DeleteRecord, DeleteRecord);
79+
Subscriber.Unsubscribe(EventConstants.ConsideringClosing, ConsideringClosing);
7980

8081
if (components != null)
8182
{
@@ -104,6 +105,7 @@ public override void Init(XmlNode nodeSpec, int hvoRoot, int fakeFlid,
104105
base.Init(nodeSpec, hvoRoot, fakeFlid, cache, mediator, bv);
105106

106107
Subscriber.Subscribe(EventConstants.DeleteRecord, DeleteRecord);
108+
Subscriber.Subscribe(EventConstants.ConsideringClosing, ConsideringClosing);
107109
}
108110

109111
#endregion Construction, initialization, and disposal.
@@ -167,17 +169,22 @@ public override void SetSelectedRowHighlighting()
167169
#region XCore message handlers
168170

169171
/// <summary>
170-
/// This name is magic for an xCoreColleague that is active at the time when an xWindow is being closed.
171-
/// If some active colleague implements this method, it gets a chance to do something special as the
172-
/// xWindow closes (and can veto the close, though we aren't really using that here).
172+
/// Cleanup any pending edits before a potential xWindow close.
173173
/// </summary>
174-
/// <returns></returns>
175-
public bool OnConsideringClosing(object sender, CancelEventArgs arg)
174+
private void ConsideringClosing(object obj)
176175
{
177176
CheckDisposed();
178-
179-
arg.Cancel = CleanupPendingEdits();
180-
return arg.Cancel; // if we want to cancel, others don't need to be asked.
177+
if (!(obj is CancelEventArgs arg))
178+
{
179+
Debug.Assert(false, "Received unexpected object type.");
180+
return;
181+
}
182+
// Return if the close has already been canceled by another Subscriber.
183+
if (arg.Cancel)
184+
{
185+
return;
186+
}
187+
CleanupPendingEdits();
181188
}
182189

183190
/// <summary>
@@ -288,10 +295,8 @@ private void SetSelection()
288295
/// <summary>
289296
/// Cleanup any pending edits.
290297
/// </summary>
291-
/// <returns>True to cancel the window closing, otherwise false.</returns>
292-
private bool CleanupPendingEdits()
298+
private void CleanupPendingEdits()
293299
{
294-
bool cancelClose = false;
295300
ITsString[] rgtss;
296301
if (CanGotoNextRow(out rgtss))
297302
{
@@ -308,8 +313,6 @@ private bool CleanupPendingEdits()
308313
CreateObjectFromEntryRow(rgtss, false);
309314
}
310315
DoMerges();
311-
312-
return cancelClose;
313316
}
314317
/// <summary>
315318
/// Check whether we have enough data entered in this row to go on to create another row.

Src/Common/FwUtils/EventConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static class EventConstants
1111
public const string ClerkOwningObjChanged = "ClerkOwningObjChanged";
1212
public const string ConfigureCustomFields = "ConfigureCustomFields";
1313
public const string ConfigureHeadwordNumbers = "ConfigureHeadwordNumbers";
14+
public const string ConsideringClosing = "ConsideringClosing";
1415
public const string CreateFirstRecord = "CreateFirstRecord";
1516
public const string DataTreeDelete = "DataTreeDelete";
1617
public const string DeleteRecord = "DeleteRecord";

Src/LexText/Interlinear/ConcordanceControl.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using SIL.LCModel.Infrastructure;
2222
using SIL.FieldWorks.LexText.Controls;
2323
using SIL.FieldWorks.Common.FwUtils;
24+
using static SIL.FieldWorks.Common.FwUtils.FwUtils;
2425
using SIL.LCModel;
2526
using SIL.FieldWorks.Filters;
2627
using SIL.FieldWorks.FwCoreDlgs;
@@ -97,6 +98,8 @@ public override void Init(Mediator mediator, PropertyTable propertyTable, XmlNod
9798
}
9899
// Load any saved settings.
99100
LoadSettings();
101+
102+
Subscriber.Subscribe(EventConstants.ConsideringClosing, ConsideringClosing);
100103
}
101104

102105
/// <summary>
@@ -108,6 +111,8 @@ protected override void Dispose(bool disposing)
108111
Debug.WriteLineIf(!disposing, "****************** Missing Dispose() call for " + GetType().Name + ". ******************");
109112
if (disposing)
110113
{
114+
Subscriber.Unsubscribe(EventConstants.ConsideringClosing, ConsideringClosing);
115+
111116
if (components != null)
112117
components.Dispose();
113118
if (m_clerk != null)
@@ -264,13 +269,19 @@ public override string AccName
264269
/// <summary>
265270
/// This is called when the main window is closing.
266271
/// </summary>
267-
/// <param name="sender"></param>
268-
/// <param name="arg"></param>
269-
/// <returns></returns>
270-
public bool OnConsideringClosing(object sender, CancelEventArgs arg)
272+
private void ConsideringClosing(object obj)
271273
{
274+
if (!(obj is CancelEventArgs arg))
275+
{
276+
Debug.Assert(false, "Received unexpected object type.");
277+
return;
278+
}
279+
// Return if the close has already been canceled by another Subscriber.
280+
if (arg.Cancel)
281+
{
282+
return;
283+
}
272284
arg.Cancel = !PrepareToGoAway();
273-
return arg.Cancel;
274285
}
275286

276287
#endregion

Src/XCore/xCoreInterfaces/Mediator.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,25 +1108,6 @@ public bool HasReceiver(string messageName)
11081108
true); //we are just checking, don't invoke anything
11091109
}
11101110

1111-
/// <returns>true if the message was canceled, otherwise false.</returns>
1112-
[Obsolete("Use the the FwUtils Publisher and Subscriber classes instead.", false)]
1113-
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
1114-
public bool SendCancellableMessage(string messageName, object parameter)
1115-
{
1116-
CheckDisposed();
1117-
#if DEBUG
1118-
if(messageName.Substring(0,2) == "On")
1119-
Debug.Fail("The convention is to send messages without the 'On' prefix. That is added by the message sending code.");
1120-
#endif
1121-
1122-
System.ComponentModel.CancelEventArgs cancelArguments = new System.ComponentModel.CancelEventArgs(false);
1123-
InvokeOnColleagues("On"+messageName,
1124-
null, // types see note above
1125-
new Object[] { parameter, cancelArguments },
1126-
true, false);
1127-
return cancelArguments.Cancel;
1128-
}
1129-
11301111
private bool InvokeOnColleagues(string methodName, Type[] parameterTypes,
11311112
object[] parameterList, bool stopWhenHandled, bool justCheckingForReceivers)
11321113
{

Src/XCore/xWindow.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,11 +2257,10 @@ protected virtual void XWindow_Closing(object sender, CancelEventArgs e)
22572257

22582258
m_widgetUpdateTimer.Enabled = false;
22592259

2260-
#pragma warning disable 618 // suppress obsolete warning
2261-
if (m_mediator.SendCancellableMessage("ConsideringClosing", this))
2262-
#pragma warning restore 618
2260+
e.Cancel = false;
2261+
Publisher.Publish(new PublisherParameterObject(EventConstants.ConsideringClosing, e));
2262+
if (e.Cancel)
22632263
{
2264-
e.Cancel = true;
22652264
m_widgetUpdateTimer.Enabled = true;
22662265
return;
22672266
}

Src/xWorks/RecordBrowseView.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// (http://www.gnu.org/licenses/lgpl-2.1.html)
44
using System;
55
using System.Collections.Generic;
6+
using System.ComponentModel;
67
using System.Diagnostics;
78
using System.Windows.Forms;
89
using System.Xml;
@@ -67,6 +68,7 @@ protected override void Dispose(bool disposing)
6768
if (disposing)
6869
{
6970
Subscriber.Unsubscribe(EventConstants.ClerkOwningObjChanged, ClerkOwningObjChanged);
71+
Subscriber.Unsubscribe(EventConstants.ConsideringClosing, ConsideringClosing);
7072

7173
if (ExistingClerk != null) // ExistingClerk, *not* Clerk (see doc on ExistingClerk)
7274
{
@@ -677,6 +679,7 @@ public override void Init(Mediator mediator, PropertyTable propertyTable, XmlNod
677679
ShowRecord();
678680

679681
Subscriber.Subscribe(EventConstants.ClerkOwningObjChanged, ClerkOwningObjChanged);
682+
Subscriber.Subscribe(EventConstants.ConsideringClosing, ConsideringClosing);
680683
}
681684

682685
private void CheckExpectedListItemsClassInSync()
@@ -699,12 +702,21 @@ protected override void GetMessageAdditionalTargets(List<IxCoreColleague> collec
699702

700703
#endregion // IxCoreColleague implementation
701704

702-
public bool OnConsideringClosing(object argument, System.ComponentModel.CancelEventArgs args)
705+
private void ConsideringClosing(object obj)
703706
{
704707
CheckDisposed();
705708

709+
if (!(obj is CancelEventArgs args))
710+
{
711+
Debug.Assert(false, "Received unexpected object type.");
712+
return;
713+
}
714+
// Return if the close has already been canceled by another Subscriber.
715+
if (args.Cancel)
716+
{
717+
return;
718+
}
706719
args.Cancel = !PrepareToGoAway();
707-
return args.Cancel; // if we want to cancel, others don't need to be asked.
708720
}
709721

710722
#region IxCoreContentControl implementation

Src/xWorks/RecordDocView.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// <remarks>
1010
// </remarks>
1111
using System;
12+
using System.ComponentModel;
1213
using System.Diagnostics;
1314
using System.Diagnostics.CodeAnalysis;
1415
using System.Drawing;
@@ -18,6 +19,7 @@
1819
using SIL.FieldWorks.Common.Controls;
1920
using SIL.FieldWorks.Common.Framework;
2021
using SIL.FieldWorks.Common.FwUtils;
22+
using static SIL.FieldWorks.Common.FwUtils.FwUtils;
2123
using SIL.FieldWorks.Common.RootSites;
2224
using SIL.FieldWorks.Common.Widgets;
2325
using SIL.LCModel.Core.KernelInterfaces;
@@ -65,6 +67,8 @@ public override void Init(Mediator mediator, PropertyTable propertyTable, XmlNod
6567

6668
InitBase(mediator, propertyTable, configurationParameters);
6769
m_fullyInitialized = true;
70+
71+
Subscriber.Subscribe(EventConstants.ConsideringClosing, ConsideringClosing);
6872
}
6973

7074
protected override void GetMessageAdditionalTargets(System.Collections.Generic.List<IxCoreColleague> collector)
@@ -89,6 +93,8 @@ protected override void Dispose(bool disposing)
8993

9094
if (disposing)
9195
{
96+
Subscriber.Unsubscribe(EventConstants.ConsideringClosing, ConsideringClosing);
97+
9298
if (m_rootSite != null)
9399
m_rootSite.Dispose();
94100
}
@@ -102,12 +108,21 @@ protected override void Dispose(bool disposing)
102108

103109
#region Message Handlers
104110

105-
public bool OnConsideringClosing(object argument, System.ComponentModel.CancelEventArgs args)
111+
private void ConsideringClosing(object obj)
106112
{
107113
CheckDisposed();
108114

115+
if (!(obj is CancelEventArgs args))
116+
{
117+
Debug.Assert(false, "Received unexpected object type.");
118+
return;
119+
}
120+
// Return if the close has already been canceled by another Subscriber.
121+
if (args.Cancel)
122+
{
123+
return;
124+
}
109125
args.Cancel = !PrepareToGoAway();
110-
return args.Cancel; // if we want to cancel, others don't need to be asked.
111126
}
112127

113128
#endregion // Message Handlers

Src/xWorks/RecordEditView.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Collections.Generic;
1515
using SIL.FieldWorks.Common.Widgets;
1616
using SIL.FieldWorks.Common.FwUtils;
17+
using static SIL.FieldWorks.Common.FwUtils.FwUtils;
1718
using SIL.FieldWorks.Common.RootSites;
1819
using SIL.LCModel.Core.KernelInterfaces;
1920
using SIL.PlatformUtilities;
@@ -124,6 +125,8 @@ public override void Init(Mediator mediator, PropertyTable propertyTable, XmlNod
124125
// If possible make it use the style sheet appropriate for its main window.
125126
m_dataEntryForm.StyleSheet = FontHeightAdjuster.StyleSheetFromPropertyTable(m_propertyTable);
126127
m_fullyInitialized = true;
128+
129+
Subscriber.Subscribe(EventConstants.ConsideringClosing, ConsideringClosing);
127130
}
128131

129132
/// -----------------------------------------------------------------------------------
@@ -143,6 +146,8 @@ protected override void Dispose(bool disposing)
143146

144147
if (disposing)
145148
{
149+
Subscriber.Unsubscribe(EventConstants.ConsideringClosing, ConsideringClosing);
150+
146151
if (components != null)
147152
components.Dispose();
148153
if (m_dataEntryForm != null)
@@ -190,12 +195,21 @@ public override bool OnRecordNavigation(object argument)
190195
return true; //we handled this.
191196
}
192197

193-
public bool OnConsideringClosing(object argument, CancelEventArgs args)
198+
private void ConsideringClosing(object obj)
194199
{
195200
CheckDisposed();
196201

202+
if (!(obj is CancelEventArgs args))
203+
{
204+
Debug.Assert(false, "Received unexpected object type.");
205+
return;
206+
}
207+
// Return if the close has already been canceled by another Subscriber.
208+
if (args.Cancel)
209+
{
210+
return;
211+
}
197212
args.Cancel = !PrepareToGoAway();
198-
return args.Cancel; // if we want to cancel, others don't need to be asked.
199213
}
200214

201215
/// <summary>

0 commit comments

Comments
 (0)