PageRenderTime 34ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/Backup/FireballFX/Fireball.CodeEditor/Fireball.CodeEditor/Editors/KeyboadActionList.cs

http://gluar.googlecode.com/
C# | 538 lines | 272 code | 90 blank | 176 comment | 26 complexity | 6153c6a10f9a659d731f2f0a323f7092 MD5 | raw file
  1. //ORIGINAL LGPL SOURCE CODE FINDED ON COMPONA LGPL SOURCE CODE
  2. /* This file has been automatically generated by X-Code .NET -- DO NOT EDIT! */
  3. /*
  4. WordCollection
  5. WordCollection.Enumerator
  6. These C# classes implement a strongly-typed collection of
  7. Rabbit objects.
  8. The public representation is an array of Rabbit, so the performance
  9. characteristics will be more like a vector than a list, to use STL terminology.
  10. The implementation is optimized for value-types, as it goes to great length to
  11. avoid the overhead of boxing and unboxing. But it should also work well for
  12. reference types.
  13. Mad props to Nick Wienholt, Kit George, Joel Mueller, Ethan Smith, Chris Sells,
  14. and Patrice Lafond for assisting me, at various times and in various ways.
  15. Especially to Kit George, for his generous contribution to the dotnet mailing
  16. list -- a code-generator for CollectionBase-derived classes:
  17. http://discuss.develop.com/archives/wa.exe?A2=ind0107C&L=DOTNET&P=R35911
  18. This was the original inspiration for the fine code you are now enjoying.
  19. - Shawn A. Van Ness
  20. */
  21. using System;
  22. using System.Collections;
  23. using System.Windows.Forms;
  24. using T = Fireball.Windows.Forms.CodeEditor.KeyboardAction;
  25. namespace Fireball.Windows.Forms.CodeEditor
  26. {
  27. /// <summary>
  28. /// Collection of KeyboardActions
  29. /// </summary>
  30. public class KeyboardActionList : ICollection, IList, IEnumerable, ICloneable
  31. {
  32. private const int DefaultMinimumCapacity = 16;
  33. private T[] m_array = new T[DefaultMinimumCapacity];
  34. private int m_count = 0;
  35. private int m_version = 0;
  36. // Construction
  37. /// <summary>
  38. /// Default Constructor
  39. /// </summary>
  40. public KeyboardActionList()
  41. {
  42. }
  43. /// <summary>
  44. ///
  45. /// </summary>
  46. /// <param name="collection"></param>
  47. public KeyboardActionList(KeyboardActionList collection)
  48. {
  49. AddRange(collection);
  50. }
  51. /// <summary>
  52. ///
  53. /// </summary>
  54. /// <param name="array"></param>
  55. public KeyboardActionList(T[] array)
  56. {
  57. AddRange(array);
  58. }
  59. // Operations (type-safe ICollection)
  60. /// <summary>
  61. /// Returns the KeyboardAction count in this list
  62. /// </summary>
  63. public int Count
  64. {
  65. get { return m_count; }
  66. }
  67. /// <summary>
  68. /// Copies this list into another KeyboardrActionList
  69. /// </summary>
  70. /// <param name="array">Target list</param>
  71. public void CopyTo(T[] array)
  72. {
  73. this.CopyTo(array, 0);
  74. }
  75. /// <summary>
  76. /// Copies this list into another KeyboardrActionList
  77. /// </summary>
  78. /// <param name="array">Target list</param>
  79. /// <param name="start">Start index</param>
  80. public void CopyTo(T[] array, int start)
  81. {
  82. if (m_count > array.GetUpperBound(0) + 1 - start)
  83. throw new ArgumentException("Destination array was not long enough.");
  84. // for (int i=0; i < m_count; ++i) array[start+i] = m_array[i];
  85. Array.Copy(m_array, 0, array, start, m_count);
  86. }
  87. // Operations (type-safe IList)
  88. /// <summary>
  89. /// Returns the KeyboardAction at the specified index
  90. /// </summary>
  91. public T this[int index]
  92. {
  93. get
  94. {
  95. ValidateIndex(index); // throws
  96. return m_array[index];
  97. }
  98. set
  99. {
  100. ValidateIndex(index); // throws
  101. ++m_version;
  102. m_array[index] = value;
  103. }
  104. }
  105. /// <summary>
  106. /// Adds a KeyboardAction to the list
  107. /// </summary>
  108. /// <param name="item"></param>
  109. /// <returns></returns>
  110. public int Add(T item)
  111. {
  112. if (NeedsGrowth())
  113. Grow();
  114. ++m_version;
  115. m_array[m_count] = item;
  116. return m_count++;
  117. }
  118. /// <summary>
  119. /// Removes all KeyboardActions from the list
  120. /// </summary>
  121. public void Clear()
  122. {
  123. ++m_version;
  124. m_array = new T[DefaultMinimumCapacity];
  125. m_count = 0;
  126. }
  127. /// <summary>
  128. /// Returns true if the list contains the specified KeyboardAction
  129. /// </summary>
  130. /// <param name="item">KeyboardAction to find</param>
  131. /// <returns></returns>
  132. public bool Contains(T item)
  133. {
  134. return ((IndexOf(item) == -1) ? false : true);
  135. }
  136. /// <summary>
  137. /// Returns the index of a specified KeyboardAction
  138. /// </summary>
  139. /// <param name="item">KeyboardAction to find</param>
  140. /// <returns></returns>
  141. public int IndexOf(T item)
  142. {
  143. for (int i = 0; i < m_count; ++i)
  144. if (m_array[i] == (item))
  145. return i;
  146. return -1;
  147. }
  148. /// <summary>
  149. /// Inserts a KeyboardAction at a specified position
  150. /// </summary>
  151. /// <param name="position">Insert position</param>
  152. /// <param name="item">Item to insert</param>
  153. public void Insert(int position, T item)
  154. {
  155. ValidateIndex(position, true); // throws
  156. if (NeedsGrowth())
  157. Grow();
  158. ++m_version;
  159. // for (int i=m_count; i > position; --i) m_array[i] = m_array[i-1];
  160. Array.Copy(m_array, position, m_array, position + 1, m_count - position);
  161. m_array[position] = item;
  162. m_count++;
  163. }
  164. /// <summary>
  165. /// Remove a specified item from the list
  166. /// </summary>
  167. /// <param name="item">Item to remove</param>
  168. public void Remove(T item)
  169. {
  170. int index = IndexOf(item);
  171. if (index < 0)
  172. throw new ArgumentException("Cannot remove the specified item because it was not found in the specified Collection.");
  173. RemoveAt(index);
  174. }
  175. /// <summary>
  176. /// Remove any keyboard action that matches the given criterias.
  177. /// </summary>
  178. /// <param name="key">Key down</param>
  179. /// <param name="shift">Shift down</param>
  180. /// <param name="control">Control down</param>
  181. /// <param name="alt">Alt down</param>
  182. public void Remove(Keys key, bool shift, bool control, bool alt)
  183. {
  184. foreach (KeyboardAction ka in this)
  185. {
  186. if (ka.Key == key &&
  187. ka.Alt == alt &&
  188. ka.Shift == shift &&
  189. ka.Control == control)
  190. {
  191. this.Remove(ka);
  192. return;
  193. }
  194. }
  195. }
  196. /// <summary>
  197. /// Remove the item at a specified index
  198. /// </summary>
  199. /// <param name="index">Index to remove at</param>
  200. public void RemoveAt(int index)
  201. {
  202. ValidateIndex(index); // throws
  203. ++m_version;
  204. m_count--;
  205. // for (int i=index; i < m_count; ++i) m_array[i] = m_array[i+1];
  206. Array.Copy(m_array, index + 1, m_array, index, m_count - index);
  207. if (NeedsTrimming())
  208. Trim();
  209. }
  210. // Operations (type-safe IEnumerable)
  211. /// <summary>
  212. /// Gets the KeyboardActionList Enumerator
  213. /// </summary>
  214. /// <returns></returns>
  215. public Enumerator GetEnumerator()
  216. {
  217. return new Enumerator(this);
  218. }
  219. // Operations (type-safe ICloneable)
  220. /// <summary>
  221. /// Clones the list
  222. /// </summary>
  223. /// <returns></returns>
  224. public KeyboardActionList Clone()
  225. {
  226. KeyboardActionList tc = new KeyboardActionList();
  227. tc.AddRange(this);
  228. tc.Capacity = this.m_array.Length;
  229. tc.m_version = this.m_version;
  230. return tc;
  231. }
  232. // Public helpers (just to mimic some nice features of ArrayList)
  233. /// <summary>
  234. ///
  235. /// </summary>
  236. public int Capacity
  237. {
  238. get { return m_array.Length; }
  239. set
  240. {
  241. if (value < m_count) value = m_count;
  242. if (value < DefaultMinimumCapacity) value = DefaultMinimumCapacity;
  243. if (m_array.Length == value) return;
  244. ++m_version;
  245. T[] temp = new T[value];
  246. // for (int i=0; i < m_count; ++i) temp[i] = m_array[i];
  247. Array.Copy(m_array, 0, temp, 0, m_count);
  248. m_array = temp;
  249. }
  250. }
  251. /// <summary>
  252. /// Add the content of another KeyboardActionList to this list
  253. /// </summary>
  254. /// <param name="collection">List to copy items from</param>
  255. public void AddRange(KeyboardActionList collection)
  256. {
  257. // for (int i=0; i < collection.Count; ++i) Add(collection[i]);
  258. ++m_version;
  259. Capacity += collection.Count;
  260. Array.Copy(collection.m_array, 0, this.m_array, m_count, collection.m_count);
  261. m_count += collection.Count;
  262. }
  263. /// <summary>
  264. /// Add the content of a KeyboardAction array to this list
  265. /// </summary>
  266. /// <param name="array"></param>
  267. public void AddRange(T[] array)
  268. {
  269. // for (int i=0; i < array.Length; ++i) Add(array[i]);
  270. ++m_version;
  271. Capacity += array.Length;
  272. Array.Copy(array, 0, this.m_array, m_count, array.Length);
  273. m_count += array.Length;
  274. }
  275. // Implementation (helpers)
  276. private void ValidateIndex(int index)
  277. {
  278. ValidateIndex(index, false);
  279. }
  280. private void ValidateIndex(int index, bool allowEqualEnd)
  281. {
  282. int max = (allowEqualEnd) ? (m_count) : (m_count - 1);
  283. if (index < 0 || index > max)
  284. throw new ArgumentOutOfRangeException("Index was out of range. Must be non-negative and less than the size of the collection.", (object) index, "Specified argument was out of the range of valid values.");
  285. }
  286. private bool NeedsGrowth()
  287. {
  288. return (m_count >= Capacity);
  289. }
  290. private void Grow()
  291. {
  292. if (NeedsGrowth())
  293. Capacity = m_count*2;
  294. }
  295. private bool NeedsTrimming()
  296. {
  297. return (m_count <= Capacity/2);
  298. }
  299. private void Trim()
  300. {
  301. if (NeedsTrimming())
  302. Capacity = m_count;
  303. }
  304. // Implementation (ICollection)
  305. /* redundant w/ type-safe method
  306. int ICollection.Count
  307. {
  308. get
  309. { return m_count; }
  310. }
  311. */
  312. bool ICollection.IsSynchronized
  313. {
  314. get { return m_array.IsSynchronized; }
  315. }
  316. object ICollection.SyncRoot
  317. {
  318. get { return m_array.SyncRoot; }
  319. }
  320. void ICollection.CopyTo(Array array, int start)
  321. {
  322. this.CopyTo((T[]) array, start);
  323. }
  324. // Implementation (IList)
  325. bool IList.IsFixedSize
  326. {
  327. get { return false; }
  328. }
  329. bool IList.IsReadOnly
  330. {
  331. get { return false; }
  332. }
  333. object IList.this[int index]
  334. {
  335. get { return (object) this[index]; }
  336. set { this[index] = (T) value; }
  337. }
  338. int IList.Add(object item)
  339. {
  340. return this.Add((T) item);
  341. }
  342. /* redundant w/ type-safe method
  343. void IList.Clear()
  344. {
  345. this.Clear();
  346. }
  347. */
  348. bool IList.Contains(object item)
  349. {
  350. return this.Contains((T) item);
  351. }
  352. int IList.IndexOf(object item)
  353. {
  354. return this.IndexOf((T) item);
  355. }
  356. void IList.Insert(int position, object item)
  357. {
  358. this.Insert(position, (T) item);
  359. }
  360. void IList.Remove(object item)
  361. {
  362. this.Remove((T) item);
  363. }
  364. /* redundant w/ type-safe method
  365. void IList.RemoveAt(int index)
  366. {
  367. this.RemoveAt(index);
  368. }
  369. */
  370. // Implementation (IEnumerable)
  371. IEnumerator IEnumerable.GetEnumerator()
  372. {
  373. return (IEnumerator) (this.GetEnumerator());
  374. }
  375. // Implementation (ICloneable)
  376. object ICloneable.Clone()
  377. {
  378. return (object) (this.Clone());
  379. }
  380. // Nested enumerator class
  381. /// <summary>
  382. ///
  383. /// </summary>
  384. public class Enumerator : IEnumerator
  385. {
  386. private KeyboardActionList m_collection;
  387. private int m_index;
  388. private int m_version;
  389. // Construction
  390. public Enumerator(KeyboardActionList tc)
  391. {
  392. m_collection = tc;
  393. m_index = -1;
  394. m_version = tc.m_version;
  395. }
  396. /// <summary>
  397. ///
  398. /// </summary>
  399. public T Current
  400. {
  401. get { return m_collection[m_index]; }
  402. }
  403. /// <summary>
  404. ///
  405. /// </summary>
  406. /// <returns></returns>
  407. public bool MoveNext()
  408. {
  409. if (m_version != m_collection.m_version)
  410. throw new InvalidOperationException("Collection was modified; enumeration operation may not execute.");
  411. ++m_index;
  412. return (m_index < m_collection.Count) ? true : false;
  413. }
  414. /// <summary>
  415. ///
  416. /// </summary>
  417. public void Reset()
  418. {
  419. if (m_version != m_collection.m_version)
  420. throw new InvalidOperationException("Collection was modified; enumeration operation may not execute.");
  421. m_index = -1;
  422. }
  423. // Implementation (IEnumerator)
  424. object IEnumerator.Current
  425. {
  426. get { return (object) (this.Current); }
  427. }
  428. /* redundant w/ type-safe method
  429. bool IEnumerator.MoveNext()
  430. {
  431. return this.MoveNext();
  432. }
  433. */
  434. /* redundant w/ type-safe method
  435. void IEnumerator.Reset()
  436. {
  437. this.Reset();
  438. }
  439. */
  440. }
  441. }
  442. }