PageRenderTime 32ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/VSXTra/Runtime/VSXTra/Selection/SelectionTracker.cs

#
C# | 239 lines | 109 code | 28 blank | 102 comment | 9 complexity | c0c2e195f356d59810e7fe0ad15b55f2 MD5 | raw file
  1. // ================================================================================================
  2. // SelectionTracker.cs
  3. //
  4. // Created: 2008.08.04, by Istvan Novak (DeepDiver)
  5. // ================================================================================================
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using Microsoft.VisualStudio.Shell;
  11. using Microsoft.VisualStudio.Shell.Interop;
  12. namespace VSXtra.Selection
  13. {
  14. // ================================================================================================
  15. /// <summary>
  16. /// This class is a wrapper around the STrackSelection service.
  17. /// </summary>
  18. // ================================================================================================
  19. public class SelectionTracker
  20. {
  21. #region Private fields
  22. private readonly ITrackSelection _TrackSelection;
  23. private SelectionContainer _Container;
  24. #endregion
  25. #region Lifecycle methods
  26. // --------------------------------------------------------------------------------------------
  27. /// <summary>
  28. /// Creates a new selection tracker instance.
  29. /// </summary>
  30. /// <param name="provider">Service provider to obtain the selection tracker from.</param>
  31. /// <param name="selectableRO">Is the collection of selectable objects read only?</param>
  32. /// <param name="selectedRO">Is the collection of selected objects read only?</param>
  33. // --------------------------------------------------------------------------------------------
  34. public SelectionTracker(IServiceProvider provider, bool selectableRO, bool selectedRO)
  35. {
  36. _TrackSelection = provider.GetService<STrackSelection, ITrackSelection>();
  37. _Container = new SelectionContainer(selectableRO, selectedRO);
  38. Unselect();
  39. }
  40. // --------------------------------------------------------------------------------------------
  41. /// <summary>
  42. /// Creates a new selection tracker instance.
  43. /// </summary>
  44. /// <param name="provider">Service provider to obtain the selection tracker from.</param>
  45. // --------------------------------------------------------------------------------------------
  46. public SelectionTracker(IServiceProvider provider)
  47. : this(provider, false, false)
  48. {
  49. }
  50. #endregion
  51. #region Public properties
  52. // --------------------------------------------------------------------------------------------
  53. /// <summary>
  54. /// Gets or sets the selection container.
  55. /// </summary>
  56. // --------------------------------------------------------------------------------------------
  57. public SelectionContainer Container
  58. {
  59. get { return _Container; }
  60. set { _Container = value; }
  61. }
  62. // --------------------------------------------------------------------------------------------
  63. /// <summary>
  64. /// Gets the ITrackSelection object behind this instance.
  65. /// </summary>
  66. // --------------------------------------------------------------------------------------------
  67. public ITrackSelection TrackSelection
  68. {
  69. get { return _TrackSelection; }
  70. }
  71. // --------------------------------------------------------------------------------------------
  72. /// <summary>
  73. /// Gets the selected objects.
  74. /// </summary>
  75. // --------------------------------------------------------------------------------------------
  76. public IEnumerable SelectedObjects
  77. {
  78. get { return _Container == null ? new ArrayList() : _Container.SelectedObjects; }
  79. }
  80. #endregion
  81. #region Public events
  82. // --------------------------------------------------------------------------------------------
  83. /// <summary>
  84. /// The current selection has been changed.
  85. /// </summary>
  86. // --------------------------------------------------------------------------------------------
  87. public event EventHandler SelectionChanged
  88. {
  89. add
  90. {
  91. if (_Container != null)
  92. _Container.SelectedObjectsChanged += value;
  93. }
  94. remove
  95. {
  96. if (_Container != null)
  97. _Container.SelectedObjectsChanged -= value;
  98. }
  99. }
  100. #endregion
  101. #region Public methods
  102. // --------------------------------------------------------------------------------------------
  103. /// <summary>
  104. /// Gets all the selected objects with the specified type.
  105. /// </summary>
  106. /// <typeparam name="T">Type to select.</typeparam>
  107. /// <returns>A collection of selected object instances with the specified type.</returns>
  108. // --------------------------------------------------------------------------------------------
  109. public IEnumerable<T> GetSelectedObjects<T>()
  110. {
  111. return SelectedObjects.OfType<T>();
  112. }
  113. // --------------------------------------------------------------------------------------------
  114. /// <summary>
  115. /// Gets the first selected objects with the specified type.
  116. /// </summary>
  117. /// <typeparam name="T">Type to select.</typeparam>
  118. /// <returns>
  119. /// The first selected object instance with the specified type or null if there is no selected
  120. /// object with the specified type.
  121. /// </returns>
  122. // --------------------------------------------------------------------------------------------
  123. public T GetSelectedObject<T>()
  124. {
  125. return GetSelectedObjects<T>().FirstOrDefault();
  126. }
  127. // --------------------------------------------------------------------------------------------
  128. /// <summary>
  129. /// Unselects any object selected previously.
  130. /// </summary>
  131. // --------------------------------------------------------------------------------------------
  132. public void Unselect()
  133. {
  134. SetSelection(null, null);
  135. }
  136. // --------------------------------------------------------------------------------------------
  137. /// <summary>
  138. /// Sets the provided object as selected and the only selectable.
  139. /// </summary>
  140. /// <param name="selectedObject">Object to select.</param>
  141. // --------------------------------------------------------------------------------------------
  142. public void SelectObject(object selectedObject)
  143. {
  144. ICollection container = new ArrayList {selectedObject};
  145. SetSelection(container, container);
  146. }
  147. // --------------------------------------------------------------------------------------------
  148. /// <summary>
  149. /// Sets the provided object as selected and the list of objects as selectable.
  150. /// </summary>
  151. /// <param name="selectedObject">Object to select.</param>
  152. /// <param name="selectableObjects">Objects that can be selected.</param>
  153. // --------------------------------------------------------------------------------------------
  154. public void SelectObject(object selectedObject, IEnumerable selectableObjects)
  155. {
  156. SetSelection(new ArrayList {selectedObject}, ToArrayList(selectableObjects));
  157. }
  158. // --------------------------------------------------------------------------------------------
  159. /// <summary>
  160. /// Sets the list of objects as selected and selectable.
  161. /// </summary>
  162. /// <param name="selectedObjects">Objects to select.</param>
  163. // --------------------------------------------------------------------------------------------
  164. public void SelectObjects(IEnumerable selectedObjects)
  165. {
  166. SetSelection(ToArrayList(selectedObjects), ToArrayList(selectedObjects));
  167. }
  168. // --------------------------------------------------------------------------------------------
  169. /// <summary>
  170. /// Sets the provided objects as selected and the list of objects as selectable.
  171. /// </summary>
  172. /// <param name="selectedObjects">Object to select.</param>
  173. /// <param name="selectableObjects">Objects that can be selected.</param>
  174. // --------------------------------------------------------------------------------------------
  175. public void SelectObjects(IEnumerable selectedObjects, IEnumerable selectableObjects)
  176. {
  177. SetSelection(ToArrayList(selectedObjects), ToArrayList(selectableObjects));
  178. }
  179. #endregion
  180. #region Private methods
  181. // --------------------------------------------------------------------------------------------
  182. /// <summary>
  183. /// Converst an IList to an ArrayList
  184. /// </summary>
  185. // --------------------------------------------------------------------------------------------
  186. private static ArrayList ToArrayList(IEnumerable list)
  187. {
  188. var result = new ArrayList();
  189. foreach (var item in list) result.Add(item);
  190. return result;
  191. }
  192. // --------------------------------------------------------------------------------------------
  193. /// <summary>
  194. /// Carries out the selection.
  195. /// </summary>
  196. // --------------------------------------------------------------------------------------------
  197. private void SetSelection(ICollection selected, ICollection selectable)
  198. {
  199. if (_Container != null)
  200. {
  201. _Container.SelectableObjects = selectable;
  202. _Container.SelectedObjects = selected;
  203. }
  204. if (_TrackSelection != null)
  205. {
  206. _TrackSelection.OnSelectChange(_Container);
  207. }
  208. }
  209. #endregion
  210. }
  211. }