PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/AODL/Document/Content/Text/ListItem.cs

https://bitbucket.org/chrisc/aodl
C# | 250 lines | 91 code | 31 blank | 128 comment | 5 complexity | a6a57b939e62a16eabda2b26f4dc76b5 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. /// ListItem represent a list item which is used within a list.
  28. /// </summary>
  29. public class ListItem : IContent, IContentContainer, IHtml
  30. {
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="ListItem"/> class.
  33. /// </summary>
  34. /// <param name="document">The document.</param>
  35. public ListItem(IDocument document)
  36. {
  37. Document = document;
  38. InitStandards();
  39. }
  40. /// <summary>
  41. /// Create a new ListItem
  42. /// </summary>
  43. /// <param name="list">The List object to which this ListItem belongs.</param>
  44. public ListItem(List list)
  45. {
  46. Document = list.Document;
  47. List = list;
  48. InitStandards();
  49. }
  50. /// <summary>
  51. /// The List object to which this ListItem belongs.
  52. /// </summary>
  53. public List List { get; set; }
  54. /// <summary>
  55. /// Inits the standards.
  56. /// </summary>
  57. private void InitStandards()
  58. {
  59. NewXmlNode();
  60. Content = new ContentCollection();
  61. Content.Inserted += Content_Inserted;
  62. Content.Removed += Content_Removed;
  63. }
  64. /// <summary>
  65. /// Create a new XElement.
  66. /// </summary>
  67. private void NewXmlNode()
  68. {
  69. Node = new XElement(Ns.Text + "list-item");
  70. }
  71. /// <summary>
  72. /// Content_s the inserted.
  73. /// </summary>
  74. /// <param name="index">The index.</param>
  75. /// <param name="value">The value.</param>
  76. private void Content_Inserted(int index, object value)
  77. {
  78. Node.Add(((IContent) value).Node);
  79. }
  80. /// <summary>
  81. /// Content_s the removed.
  82. /// </summary>
  83. /// <param name="index">The index.</param>
  84. /// <param name="value">The value.</param>
  85. private static void Content_Removed(int index, object value)
  86. {
  87. ((IContent) value).Node.Remove();
  88. }
  89. #region IHtml Member
  90. /// <summary>
  91. /// Return the content as Html string
  92. /// </summary>
  93. /// <returns>The html string</returns>
  94. public string GetHtml()
  95. {
  96. string html = "<li>\n";
  97. //Support for vers. < 1.1.1.0
  98. // if (this.Paragraph != null)
  99. // {
  100. // string pHtml = this.Paragraph.GetHtml();
  101. // if (pHtml != "<p >\n&nbsp;</p>\n"
  102. // && !pHtml.StartsWith("<p >\n</p>")
  103. // && pHtml != "<p >\n </p>\n"
  104. // && pHtml != "<p >\n</p>\n")
  105. // html += pHtml+"\n";
  106. // }
  107. foreach (IContent content in Content)
  108. if (content is IHtml)
  109. html += ((IHtml) content).GetHtml();
  110. html += "</li>\n";
  111. return html;
  112. }
  113. #endregion
  114. #region IContentContainer Member
  115. private ContentCollection _content;
  116. /// <summary>
  117. /// Gets or sets the content.
  118. /// </summary>
  119. /// <value>The content.</value>
  120. public ContentCollection Content
  121. {
  122. get { return _content; }
  123. set
  124. {
  125. if (_content != null)
  126. foreach (IContent content in _content)
  127. content.Node.Remove();
  128. _content = value;
  129. if (_content != null)
  130. foreach (IContent content in _content)
  131. Node.Add(content.Node);
  132. }
  133. }
  134. #endregion
  135. #region IContent Member
  136. private IStyle _style;
  137. /// <summary>
  138. /// Gets or sets the node.
  139. /// </summary>
  140. /// <value>The node.</value>
  141. public XElement Node { get; set; }
  142. /// <summary>
  143. /// Gets or sets the name of the style.
  144. /// </summary>
  145. /// <value>The name of the style.</value>
  146. public string StyleName
  147. {
  148. get { return (string) Node.Attribute(Ns.Text + "style-name"); }
  149. set { Node.SetAttributeValue(Ns.Text + "style-name", value); }
  150. }
  151. /// <summary>
  152. /// Every object (typeof(IContent)) have to know his document.
  153. /// </summary>
  154. /// <value></value>
  155. public IDocument Document { get; set; }
  156. /// <summary>
  157. /// A Style class wich is referenced with the content object.
  158. /// If no style is available this is null.
  159. /// </summary>
  160. /// <value></value>
  161. public IStyle Style
  162. {
  163. get { return _style; }
  164. set
  165. {
  166. StyleName = value.StyleName;
  167. _style = value;
  168. }
  169. }
  170. /// <summary>
  171. /// Gets or sets the node.
  172. /// </summary>
  173. /// <value>The node.</value>
  174. XNode IContent.Node
  175. {
  176. get { return Node; }
  177. set { Node = (XElement) value; }
  178. }
  179. #endregion
  180. }
  181. }
  182. /*
  183. * $Log: ListItem.cs,v $
  184. * Revision 1.2 2008/04/29 15:39:46 mt
  185. * new copyright header
  186. *
  187. * Revision 1.1 2007/02/25 08:58:39 larsbehr
  188. * initial checkin, import from Sourceforge.net to OpenOffice.org
  189. *
  190. * Revision 1.2 2006/02/05 20:02:25 larsbm
  191. * - Fixed several bugs
  192. * - clean up some messy code
  193. *
  194. * Revision 1.1 2006/01/29 11:28:22 larsbm
  195. * - Changes for the new version. 1.2. see next changelog for details
  196. *
  197. * Revision 1.4 2005/12/18 18:29:46 larsbm
  198. * - AODC Gui redesign
  199. * - AODC HTML exporter refecatored
  200. * - Full Meta Data Support
  201. * - Increase textprocessing performance
  202. *
  203. * Revision 1.3 2005/12/12 19:39:17 larsbm
  204. * - Added Paragraph Header
  205. * - Added Table Row Header
  206. * - Fixed some bugs
  207. * - better whitespace handling
  208. * - Implmemenation of HTML Exporter
  209. *
  210. * Revision 1.2 2005/10/23 16:47:48 larsbm
  211. * - Bugfix ListItem throws IStyleInterface not implemented exeption
  212. * - now. build the document after call saveto instead prepare the do at runtime
  213. * - add remove support for IText objects in the paragraph class
  214. *
  215. * Revision 1.1 2005/10/09 15:52:47 larsbm
  216. * - Changed some design at the paragraph usage
  217. * - add list support
  218. *
  219. */