PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/source/library/Interlace.UserInterface/NamedObjects/NamedObjectCollectionController.cs

https://bitbucket.org/VahidN/interlace
C# | 222 lines | 145 code | 49 blank | 28 comment | 9 complexity | 66fb7d7b9642060001b2c98c4fd1ee26 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Drawing;
  31. using System.Text;
  32. using System.Text.RegularExpressions;
  33. using System.Windows.Forms;
  34. using DevExpress.XtraEditors;
  35. #endregion
  36. namespace Interlace.NamedObjects
  37. {
  38. public class NamedObjectCollectionController
  39. {
  40. TextEdit _editor;
  41. Form _parentForm;
  42. NamedObjectRangeSet _ranges = new NamedObjectRangeSet();
  43. INamedObjectSource _source;
  44. /// <summary>
  45. /// Keep a single form (value) for each parent form (key).
  46. /// </summary>
  47. static Dictionary<Form, NamedObjectSuggestionsForm> _checkNamesForms =
  48. new Dictionary<Form, NamedObjectSuggestionsForm>();
  49. public NamedObjectCollectionController(TextEdit editor, Form parentForm, INamedObjectSource source)
  50. {
  51. _editor = editor;
  52. _parentForm = parentForm;
  53. _source = source;
  54. _parentForm.FormClosing += new FormClosingEventHandler(_parentForm_FormClosing);
  55. _editor.LostFocus += new EventHandler(_editor_LostFocus);
  56. }
  57. void _parentForm_FormClosing(object sender, FormClosingEventArgs e)
  58. {
  59. _editor.LostFocus -= new EventHandler(_editor_LostFocus);
  60. if (_checkNamesForms.ContainsKey(_parentForm))
  61. {
  62. _checkNamesForms[_parentForm].Close();
  63. }
  64. _parentForm.FormClosing -= new FormClosingEventHandler(_parentForm_FormClosing);
  65. }
  66. internal INamedObjectSource Source
  67. {
  68. get { return _source; }
  69. }
  70. internal NamedObjectRangeSet Ranges
  71. {
  72. get { return _ranges; }
  73. }
  74. void FindValuesForUnambiguousRanges()
  75. {
  76. foreach (NamedObjectRange range in _ranges)
  77. {
  78. ICollection suggestions = _source.GetSuggestionsFor(range);
  79. if (suggestions.Count == 1)
  80. {
  81. IEnumerator enumerator = suggestions.GetEnumerator();
  82. enumerator.MoveNext();
  83. object suggestion = enumerator.Current;
  84. range.Value = _source.GetRangeValueFromSuggestion(suggestion);
  85. range.Text = _source.GetCanonicalTextFromValue(range.Value);
  86. }
  87. }
  88. }
  89. bool _checkingNames = false;
  90. public bool CheckNamesModal()
  91. {
  92. _checkingNames = true;
  93. try
  94. {
  95. _ranges.ReplaceWith(_editor.EditValue as string ?? "");
  96. FindValuesForUnambiguousRanges();
  97. UpdateFromRanges();
  98. if (_ranges.HasUnvaluedRange)
  99. {
  100. _editor.Properties.HideSelection = false;
  101. NamedObjectSuggestionsForm form = new NamedObjectSuggestionsForm();
  102. form.AttachController(this);
  103. Rectangle editorScreenBounds = _editor.Parent.RectangleToScreen(_editor.Bounds);
  104. Point formLocation = new Point(editorScreenBounds.Left + 40, editorScreenBounds.Bottom + 10);
  105. form.Location = formLocation;
  106. form.ShowDialog(_parentForm);
  107. }
  108. return !_ranges.HasUnvaluedRange;
  109. }
  110. finally
  111. {
  112. _checkingNames = false;
  113. }
  114. }
  115. public bool ValuesAvailable
  116. {
  117. get
  118. {
  119. _ranges.ReplaceWith(_editor.EditValue as string ?? "");
  120. FindValuesForUnambiguousRanges();
  121. return !_ranges.HasUnvaluedRange;
  122. }
  123. }
  124. public void SetValues<T>(ICollection<T> collection)
  125. {
  126. List<string> components = new List<string>();
  127. foreach (T value in collection)
  128. {
  129. components.Add(_source.GetCanonicalTextFromValue(value));
  130. }
  131. _editor.EditValue = string.Join("; ", components.ToArray());
  132. }
  133. public void GetValues<T>(ICollection<T> collection) where T : class
  134. {
  135. _ranges.ReplaceWith(_editor.EditValue as string ?? "");
  136. FindValuesForUnambiguousRanges();
  137. collection.Clear();
  138. foreach (NamedObjectRange range in _ranges)
  139. {
  140. T value = range.Value as T;
  141. if (value != null) collection.Add(value);
  142. }
  143. }
  144. void _editor_LostFocus(object sender, EventArgs e)
  145. {
  146. if (!_checkingNames)
  147. {
  148. _ranges.ReplaceWith(_editor.EditValue as string ?? "");
  149. FindValuesForUnambiguousRanges();
  150. UpdateFromRanges();
  151. }
  152. }
  153. void form_FormClosed(object sender, FormClosedEventArgs e)
  154. {
  155. NamedObjectSuggestionsForm form = sender as NamedObjectSuggestionsForm;
  156. if (form == null) return;
  157. form.FormClosed -= new FormClosedEventHandler(form_FormClosed);
  158. _editor.Select(_editor.Text.Length, 0);
  159. _checkNamesForms.Remove(_parentForm);
  160. }
  161. internal void SelectRange(NamedObjectRange range)
  162. {
  163. int offset, length;
  164. _ranges.GetSelectionFor(range, out offset, out length);
  165. _editor.Select(offset, length);
  166. }
  167. internal void UpdateFromRanges()
  168. {
  169. _editor.Text = _ranges.Text;
  170. _editor.Select(_editor.Text.Length, 0);
  171. }
  172. }
  173. }