-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathReferenceComboBoxSlice.cs
More file actions
200 lines (181 loc) · 6.38 KB
/
ReferenceComboBoxSlice.cs
File metadata and controls
200 lines (181 loc) · 6.38 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
// 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)
//
// <remarks>
// Implements the "referenceComboBox" XDE editor.
// </remarks>
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml;
using SIL.LCModel;
using SIL.LCModel.Infrastructure;
using SIL.FieldWorks.Common.Framework.DetailControls.Resources;
using SIL.FieldWorks.Common.Controls;
using SIL.FieldWorks.Common.FwUtils;
using SIL.FieldWorks.Common.Widgets;
using SIL.Utils;
using XCore;
namespace SIL.FieldWorks.Common.Framework.DetailControls
{
/// <summary></summary>
public class ReferenceComboBoxSlice : FieldSlice
{
protected bool m_processSelectionEvent = true;
protected int m_currentSelectedIndex;
protected FwComboBox m_combo;
protected IPersistenceProvider m_persistProvider;
private string m_sNullItemLabel;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="obj">CmObject that is being displayed.</param>
/// <param name="flid">The field identifier for the attribute we are displaying.</param>
public ReferenceComboBoxSlice(LcmCache cache, ICmObject obj, int flid,
IPersistenceProvider persistenceProvider)
: base(new UserControl(), cache, obj, flid)
{
m_persistProvider = persistenceProvider;
m_combo = new FwComboBox();
m_combo.WritingSystemFactory = cache.WritingSystemFactory;
m_combo.DropDownStyle = ComboBoxStyle.DropDownList;
m_combo.Font = new System.Drawing.Font(
cache.ServiceLocator.WritingSystems.DefaultVernacularWritingSystem.DefaultFontName,
10);
if (!Application.RenderWithVisualStyles)
m_combo.HasBorder = false;
m_combo.Dock = DockStyle.Left;
m_combo.Width = 200;
Control.Height = m_combo.Height; // Combo has sensible default height, UserControl does not.
Control.Controls.Add(m_combo);
m_combo.SelectedIndexChanged += SelectionChanged;
}
/// ------------------------------------------------------------------------------------
/// <summary>
/// Inits the specified mediator.
/// </summary>
/// <param name="mediator">The mediator.</param>
/// <param name="propertyTable"></param>
/// <param name="configurationParameters">The configuration parameters.</param>
/// ------------------------------------------------------------------------------------
public override void Init(Mediator mediator, PropertyTable propertyTable, XmlNode configurationParameters)
{
base.Init(mediator, propertyTable, configurationParameters);
// Load the special strings from the string table if possible. If not, use the
// default (English) values.
m_sNullItemLabel = StringTable.Table.GetString("NullItemLabel",
"DetailControls/ReferenceComboBox");
if (string.IsNullOrEmpty(m_sNullItemLabel) || m_sNullItemLabel == "*NullItemLabel*")
m_sNullItemLabel = DetailControlsStrings.ksNullLabel;
}
#region IDisposable override
/// <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 override void Dispose(bool disposing)
{
//Debug.WriteLineIf(!disposing, "****************** " + GetType().Name + " 'disposing' is false. ******************");
// Must not be run more than once.
if (IsDisposed)
return;
if (disposing)
{
// Dispose managed resources here.
if (m_combo != null)
{
m_combo.SelectedIndexChanged -= new EventHandler(SelectionChanged);
Control.Controls.Remove(m_combo);
m_combo.Dispose();
}
}
// Dispose unmanaged resources here, whether disposing is true or false.
m_combo = null;
m_mediator = null;
m_persistProvider = null;
base.Dispose(disposing);
}
#endregion IDisposable override
protected void UpdateDisplayFromDatabase(string displayNameProperty)
{
DoUpdateDisplayFromDatabase(displayNameProperty);
}
protected override void UpdateDisplayFromDatabase()
{
DoUpdateDisplayFromDatabase(null);
}
private void DoUpdateDisplayFromDatabase(string displayNameProperty)
{
m_processSelectionEvent = false;
m_currentSelectedIndex = 0;
m_combo.Items.Clear();
var labels = ObjectLabel.CreateObjectLabels(m_cache, Object.ReferenceTargetCandidates(m_flid),
displayNameProperty);
int currentValue = m_cache.DomainDataByFlid.get_ObjectProp(Object.Hvo, m_flid);
int idx = 0;
foreach(ObjectLabel ol in labels)
{
m_combo.Items.Add(ol);
if (ol.Object.Hvo == currentValue)
{
m_combo.SelectedItem = ol;
m_currentSelectedIndex = idx;
}
idx++;
}
idx = m_combo.Items.Add(NullItemLabel);
if (currentValue == 0)
{
m_combo.SelectedIndex = idx;
m_currentSelectedIndex = idx;
}
m_processSelectionEvent = true;
}
/// <summary>
/// what is the default label for the null state
/// </summary>
protected virtual string NullItemLabel
{
get { return m_sNullItemLabel; }
}
/// <summary>
/// Event handler for selection changed in combo box.
/// </summary>
/// <param name="sender">Source control</param>
/// <param name="e"></param>
protected virtual void SelectionChanged(object sender, EventArgs e)
{
Debug.Assert(m_combo != null);
if (!m_processSelectionEvent)
return;
int newValue;
if (m_combo.SelectedItem.ToString() == NullItemLabel)
newValue = 0;
else
newValue = (m_combo.SelectedItem as ObjectLabel).Object.Hvo;
UndoableUnitOfWorkHelper.Do(string.Format(DetailControlsStrings.ksUndoSet, m_fieldName),
string.Format(DetailControlsStrings.ksRedoSet, m_fieldName), m_obj, () =>
{
m_cache.DomainDataByFlid.SetObjProp(Object.Hvo, m_flid, newValue);
});
}
}
}