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

/AODL/Document/Content/Text/List.cs

https://bitbucket.org/chrisc/aodl
C# | 237 lines | 105 code | 33 blank | 99 comment | 11 complexity | 7ce478fc8625a4c449901325e58edeec MD5 | raw file
  1. /*************************************************************************
  2. *
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
  4. *
  5. * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
  6. *
  7. * Use is subject to license terms.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  10. * use this file except in compliance with the License. You may obtain a copy
  11. * of the License at http://www.apache.org/licenses/LICENSE-2.0. You can also
  12. * obtain a copy of the License at http://odftoolkit.org/docs/license.txt
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. *
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. ************************************************************************/
  22. using System.Xml.Linq;
  23. using AODL.Document.Styles;
  24. namespace AODL.Document.Content.Text
  25. {
  26. /// <summary>
  27. /// Represent a list which could be a numbered or bullet style list.
  28. /// </summary>
  29. public class List : IContent, IContentContainer, IHtml
  30. {
  31. private readonly ListStyles _type;
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="List"/> class.
  34. /// </summary>
  35. /// <param name="document">The document.</param>
  36. /// <param name="node">The node.</param>
  37. public List(IDocument document, XElement node)
  38. {
  39. Document = document;
  40. Node = node;
  41. InitStandards();
  42. }
  43. /// <summary>
  44. /// Create a new List object
  45. /// </summary>
  46. /// <param name="document">The IDocument</param>
  47. /// <param name="styleName">The style name</param>
  48. /// <param name="typ">The list typ bullet, ..</param>
  49. /// <param name="paragraphStyleName">The style name for the ParagraphStyle.</param>
  50. public List(IDocument document, string styleName, ListStyles typ, string paragraphStyleName)
  51. {
  52. Document = document;
  53. Node = new XElement(Ns.Text + "list");
  54. InitStandards();
  55. Style = new ListStyle(Document, styleName);
  56. ParagraphStyle = new ParagraphStyle(Document, paragraphStyleName);
  57. Document.Styles.Add(Style);
  58. Document.Styles.Add(ParagraphStyle);
  59. ParagraphStyle.ListStyleName = styleName;
  60. _type = typ;
  61. ((ListStyle) Style).AutomaticAddListLevelStyles(typ);
  62. }
  63. /// <summary>
  64. /// Create a new List which is used to represent a inner list.
  65. /// </summary>
  66. /// <param name="document">The IDocument</param>
  67. /// <param name="outerlist">The List to which this List belongs.</param>
  68. public List(IDocument document, List outerlist)
  69. {
  70. Document = document;
  71. ParagraphStyle = outerlist.ParagraphStyle;
  72. InitStandards();
  73. _type = outerlist.ListType;
  74. //Create an inner list node, don't need a style
  75. //use the parents list style
  76. Node = new XElement(Ns.Text + "list");
  77. }
  78. /// <summary>
  79. /// The ParagraphStyle to which this List belongs.
  80. /// There is only one ParagraphStyle per List and
  81. /// its ListItems.
  82. /// </summary>
  83. public ParagraphStyle ParagraphStyle { get; set; }
  84. /// <summary>
  85. /// Gets or sets the list style.
  86. /// </summary>
  87. /// <value>The list style.</value>
  88. public ListStyle ListStyle
  89. {
  90. get { return (ListStyle) Style; }
  91. set { Style = value; }
  92. }
  93. /// <summary>
  94. /// Gets the type of the list.
  95. /// </summary>
  96. /// <value>The type of the list.</value>
  97. public ListStyles ListType
  98. {
  99. get { return _type; }
  100. }
  101. #region IContentContainer Members
  102. /// <summary>
  103. /// The ContentCollection of access
  104. /// to their list items.
  105. /// </summary>
  106. public ContentCollection Content { get; set; }
  107. #endregion
  108. /// <summary>
  109. /// Inits the standards.
  110. /// </summary>
  111. private void InitStandards()
  112. {
  113. Content = new ContentCollection();
  114. Content.Inserted += Content_Inserted;
  115. Content.Removed += Content_Removed;
  116. }
  117. /// <summary>
  118. /// Content_s the inserted.
  119. /// </summary>
  120. /// <param name="index">The index.</param>
  121. /// <param name="value">The value.</param>
  122. private void Content_Inserted(int index, object value)
  123. {
  124. Node.Add(((IContent) value).Node);
  125. }
  126. /// <summary>
  127. /// Content_s the removed.
  128. /// </summary>
  129. /// <param name="index">The index.</param>
  130. /// <param name="value">The value.</param>
  131. private static void Content_Removed(int index, object value)
  132. {
  133. ((IContent) value).Node.Remove();
  134. }
  135. #region IHtml Member
  136. /// <summary>
  137. /// Return the content as Html string
  138. /// </summary>
  139. /// <returns>The html string</returns>
  140. public string GetHtml()
  141. {
  142. string html = null;
  143. if (ListType == ListStyles.Bullet)
  144. html = "<ul>\n";
  145. else if (ListType == ListStyles.Number)
  146. html = "<ol>\n";
  147. foreach (IContent content in Content)
  148. if (content is IHtml)
  149. html += ((IHtml) content).GetHtml();
  150. if (ListType == ListStyles.Bullet)
  151. html += "</ul>\n";
  152. else if (ListType == ListStyles.Number)
  153. html += "</ol>\n";
  154. //html += "</ul>\n";
  155. return html;
  156. }
  157. #endregion
  158. #region IContent Member
  159. private IStyle _style;
  160. /// <summary>
  161. /// Gets or sets the node.
  162. /// </summary>
  163. /// <value>The node.</value>
  164. public XElement Node { get; set; }
  165. /// <summary>
  166. /// Gets or sets the name of the style.
  167. /// </summary>
  168. /// <value>The name of the style.</value>
  169. public string StyleName
  170. {
  171. get { return (string) Node.Attribute(Ns.Text + "style-name"); }
  172. set { Node.SetAttributeValue(Ns.Text + "style-name", value); }
  173. }
  174. /// <summary>
  175. /// Every object (typeof(IContent)) have to know his document.
  176. /// </summary>
  177. /// <value></value>
  178. public IDocument Document { get; set; }
  179. /// <summary>
  180. /// A Style class wich is referenced with the content object.
  181. /// If no style is available this is null.
  182. /// </summary>
  183. /// <value></value>
  184. public IStyle Style
  185. {
  186. get { return _style; }
  187. set
  188. {
  189. StyleName = value.StyleName;
  190. _style = value;
  191. }
  192. }
  193. /// <summary>
  194. /// Gets or sets the node.
  195. /// </summary>
  196. /// <value>The node.</value>
  197. XNode IContent.Node
  198. {
  199. get { return Node; }
  200. set { Node = (XElement) value; }
  201. }
  202. #endregion
  203. }
  204. }