PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/JavApi/org/apache/harmony/awt/ListenerList.cs

#
C# | 215 lines | 135 code | 23 blank | 57 comment | 25 complexity | 6eb9dd66474f39139d55fd44b73d6aa9 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. using System;
  15. using java = biz.ritter.javapi;
  16. namespace org.apache.harmony.awt
  17. {
  18. /**
  19. * List of AWT listeners. It is for 3 purposes.
  20. * 1. To support list modification from listeners
  21. * 2. To ensure call for all listeners as atomic operation
  22. * 3. To support system listeners that are needed for built-in AWT components
  23. */
  24. [Serializable]
  25. internal class ListenerList<T> : java.io.Serializable
  26. {
  27. private const long serialVersionUID = 9180703263299648154L;
  28. [NonSerialized]
  29. private java.util.ArrayList<T> systemList;
  30. private java.util.ArrayList<T> userList;
  31. public ListenerList()
  32. : base()
  33. {
  34. }
  35. /**
  36. * Adds system listener to this list.
  37. *
  38. * @param listener - listener to be added.
  39. */
  40. public void addSystemListener(T listener)
  41. {
  42. if (systemList == null)
  43. {
  44. systemList = new java.util.ArrayList<T>();
  45. }
  46. systemList.add(listener);
  47. }
  48. /**
  49. * Adds user (public) listener to this list.
  50. *
  51. * @param listener - listener to be added.
  52. */
  53. public void addUserListener(T listener)
  54. {
  55. if (listener == null)
  56. {
  57. return;
  58. }
  59. // transactionally replace old list
  60. lock (this)
  61. {
  62. if (userList == null)
  63. {
  64. userList = new java.util.ArrayList<T>();
  65. userList.add(listener);
  66. return;
  67. }
  68. java.util.ArrayList<T> newList = new java.util.ArrayList<T>(userList);
  69. newList.add(listener);
  70. userList = newList;
  71. }
  72. }
  73. /**
  74. * Removes user (public) listener to this list.
  75. *
  76. * @param listener - listener to be removed.
  77. */
  78. public void removeUserListener(Object listener)
  79. {
  80. if (listener == null)
  81. {
  82. return;
  83. }
  84. // transactionally replace old list
  85. lock (this)
  86. {
  87. if (userList == null || !userList.contains(listener))
  88. {
  89. return;
  90. }
  91. java.util.ArrayList<T> newList = new java.util.ArrayList<T>(userList);
  92. newList.remove(listener);
  93. userList = (newList.size() > 0 ? newList : null);
  94. }
  95. }
  96. /**
  97. * Gets all user (public) listeners in one array.
  98. *
  99. * @param emptyArray - empty array, it's for deriving particular listeners class.
  100. * @return array of all user listeners.
  101. */
  102. public AT[] getUserListeners<AT>(AT[] emptyArray)
  103. {
  104. lock (this)
  105. {
  106. return (userList != null ? userList.toArray(emptyArray) : emptyArray);
  107. }
  108. }
  109. /**
  110. * Gets all user (public) listeners in one list.
  111. *
  112. * @return list of all user listeners.
  113. */
  114. public java.util.List<T> getUserListeners()
  115. {
  116. lock (this)
  117. {
  118. if (userList == null || userList.isEmpty())
  119. {
  120. return java.util.Collections<T>.emptyList();
  121. }
  122. return new java.util.ArrayList<T>(userList);
  123. }
  124. }
  125. public java.util.List<T> getSystemListeners()
  126. {
  127. lock (this)
  128. {
  129. if (systemList == null || systemList.isEmpty())
  130. {
  131. return java.util.Collections<T>.emptyList();
  132. }
  133. return new java.util.ArrayList<T>(systemList);
  134. }
  135. }
  136. /**
  137. * Gets iterator for user listeners.
  138. *
  139. * @return iterator for user listeners.
  140. */
  141. public java.util.Iterator<T> getUserIterator()
  142. {
  143. lock (this)
  144. {
  145. if (userList == null)
  146. {
  147. java.util.List<T> emptyList = java.util.Collections<T>.emptyList();
  148. return emptyList.iterator();
  149. }
  150. return new ReadOnlyIterator<T>(userList.iterator());
  151. }
  152. }
  153. /**
  154. * Gets iterator for system listeners.
  155. *
  156. * @return iterator for system listeners.
  157. */
  158. public java.util.Iterator<T> getSystemIterator()
  159. {
  160. return systemList.iterator();
  161. }
  162. private static java.util.ArrayList<Object> getOnlySerializable(java.util.ArrayList<T> list)
  163. {
  164. if (list == null)
  165. {
  166. return null;
  167. }
  168. java.util.ArrayList<Object> result = new java.util.ArrayList<Object>();
  169. for (java.util.Iterator<T> it = list.iterator(); it.hasNext(); )
  170. {
  171. Object obj = it.next();
  172. if (obj is java.io.Serializable)
  173. {
  174. result.add(obj);
  175. }
  176. }
  177. return (result.size() != 0) ? result : null;
  178. }
  179. private void writeObject(java.io.ObjectOutputStream stream)
  180. {//throws IOException {
  181. stream.defaultWriteObject();
  182. stream.writeObject(getOnlySerializable(systemList));
  183. stream.writeObject(getOnlySerializable(userList));
  184. }
  185. private void readObject(java.io.ObjectInputStream stream)
  186. {//throws IOException, ClassNotFoundException {
  187. stream.defaultReadObject();
  188. systemList = (java.util.ArrayList<T>)stream.readObject();
  189. userList = (java.util.ArrayList<T>)stream.readObject();
  190. }
  191. }
  192. }