PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/VahidN/interlace
C# | 213 lines | 146 code | 40 blank | 27 comment | 19 complexity | a61aee74f368f41885fa672b1c67af16 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.ComponentModel;
  30. using System.Data;
  31. using System.Drawing;
  32. using System.Text;
  33. using System.Windows.Forms;
  34. using DevExpress.XtraEditors;
  35. using DevExpress.XtraGrid;
  36. #endregion
  37. namespace Interlace.NamedObjects
  38. {
  39. public partial class NamedObjectSuggestionsForm : DevExpress.XtraEditors.XtraForm
  40. {
  41. NamedObjectCollectionController _controller;
  42. NamedObjectRange _currentRange;
  43. static string _prompt = "The following {0} was not found or is ambiguous:";
  44. bool _runningNonModally = false;
  45. public NamedObjectSuggestionsForm()
  46. {
  47. _controller = null;
  48. InitializeComponent();
  49. }
  50. public void AttachController(NamedObjectCollectionController controller)
  51. {
  52. _controller = controller;
  53. SetControlsEnabled(true);
  54. _textLabel.Text = string.Format(_prompt, _controller.Source.ObjectDescription);
  55. _controller.Source.InitializeGridView(_suggestionsView);
  56. }
  57. NamedObjectRange GetNextUnvaluedRange()
  58. {
  59. foreach (NamedObjectRange range in _controller.Ranges)
  60. {
  61. if (range.Value == null) return range;
  62. }
  63. return null;
  64. }
  65. void FillSuggestions()
  66. {
  67. // Fill the grid:
  68. ICollection suggestionCollection =
  69. _controller.Source.GetSuggestionsFor(_currentRange);
  70. _suggestionsGrid.DataSource = suggestionCollection;
  71. // Select the first one:
  72. if (suggestionCollection.Count >= 1)
  73. {
  74. _suggestionsView.SelectRow(_suggestionsView.GetRowHandle(0));
  75. }
  76. _change.Enabled = suggestionCollection.Count != 0;
  77. }
  78. void MoveToNextUnvaluedRange()
  79. {
  80. _currentRange = GetNextUnvaluedRange();
  81. if (_currentRange != null)
  82. {
  83. _controller.SelectRange(_currentRange);
  84. _text.Text = _currentRange.Text;
  85. _text.Select(_text.Text.Length, 0);
  86. if ((_suggestionsGrid.DataSource as ICollection).Count == 0)
  87. {
  88. _text.Select();
  89. }
  90. else
  91. {
  92. _suggestionsGrid.Select();
  93. }
  94. }
  95. else
  96. {
  97. BeginInvoke((MethodInvoker)Close);
  98. }
  99. }
  100. private void _suggestionsView_DoubleClick(object sender, EventArgs e)
  101. {
  102. _change_Click(_change, EventArgs.Empty);
  103. }
  104. private void _change_Click(object sender, EventArgs e)
  105. {
  106. if (_suggestionsView.FocusedRowHandle == GridControl.InvalidRowHandle) return;
  107. object suggestion = _suggestionsView.GetRow(_suggestionsView.FocusedRowHandle);
  108. _currentRange.Value = _controller.Source.GetRangeValueFromSuggestion(suggestion);
  109. _currentRange.Text = _controller.Source.GetCanonicalTextFromValue(_currentRange.Value);
  110. _controller.UpdateFromRanges();
  111. MoveToNextUnvaluedRange();
  112. }
  113. private void _cancel_Click(object sender, EventArgs e)
  114. {
  115. Close();
  116. }
  117. private void _text_EditValueChanged(object sender, EventArgs e)
  118. {
  119. if (_currentRange != null)
  120. {
  121. _currentRange.Text = _text.Text.Trim();
  122. FillSuggestions();
  123. }
  124. }
  125. private void _suggestionsView_KeyDown(object sender, KeyEventArgs e)
  126. {
  127. if (e.KeyCode == Keys.Enter)
  128. {
  129. _change_Click(_change, EventArgs.Empty);
  130. }
  131. }
  132. private void _text_KeyDown(object sender, KeyEventArgs e)
  133. {
  134. if (e.KeyCode == Keys.Enter)
  135. {
  136. BeginInvoke((MethodInvoker)KeyDownOnEditor);
  137. }
  138. }
  139. void KeyDownOnEditor()
  140. {
  141. ICollection suggestions = _suggestionsGrid.DataSource as ICollection;
  142. if (suggestions.Count == 0)
  143. {
  144. }
  145. else
  146. {
  147. _suggestionsGrid.Select();
  148. }
  149. }
  150. void SetControlsEnabled(bool enabled)
  151. {
  152. _textLabel.Enabled = enabled;
  153. _text.Enabled = enabled;
  154. _suggestionsLabel.Enabled = enabled;
  155. _suggestionsGrid.Enabled = enabled;
  156. _cancel.Enabled = enabled;
  157. _change.Enabled = enabled;
  158. }
  159. private void NamedObjectSuggestionsForm_Deactivate(object sender, EventArgs e)
  160. {
  161. if (_runningNonModally)
  162. {
  163. _text.Text = "";
  164. _suggestionsGrid.DataSource = null;
  165. SetControlsEnabled(false);
  166. }
  167. }
  168. private void NamedObjectSuggestionsForm_Load(object sender, EventArgs e)
  169. {
  170. MoveToNextUnvaluedRange();
  171. }
  172. }
  173. }