You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RtfListItem.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.rtf.rtflib.rtfdoc;
  19. /*
  20. * This file is part of the RTF library of the FOP project, which was originally
  21. * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
  22. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  23. * the FOP project.
  24. */
  25. import java.io.IOException;
  26. import java.io.Writer;
  27. /**
  28. * <p>Model of an RTF list item, which can contain RTF paragraphs.</p>
  29. *
  30. * <p>This work was authored by Bertrand Delacretaz (bdelacretaz@codeconsult.ch)
  31. * and Andreas Putz (a.putz@skynamics.com).</p>
  32. */
  33. public class RtfListItem extends RtfContainer
  34. implements IRtfTextrunContainer,
  35. IRtfListContainer,
  36. IRtfParagraphContainer {
  37. private RtfList parentList;
  38. private RtfParagraph paragraph;
  39. private RtfListStyle listStyle;
  40. private int number;
  41. /**
  42. * special RtfParagraph that writes list item setup code before its content
  43. */
  44. private class RtfListItemParagraph extends RtfParagraph {
  45. RtfListItemParagraph(RtfListItem rli, RtfAttributes attrs)
  46. throws IOException {
  47. super(rli, rli.writer, attrs);
  48. }
  49. protected void writeRtfPrefix() throws IOException {
  50. super.writeRtfPrefix();
  51. getRtfListStyle().writeParagraphPrefix(this);
  52. }
  53. }
  54. /**
  55. * special RtfTextrun that is used as list item label
  56. */
  57. public class RtfListItemLabel extends RtfTextrun implements IRtfTextrunContainer {
  58. private RtfListItem rtfListItem;
  59. /**
  60. * Constructs the RtfListItemLabel
  61. * @param item The RtfListItem the label belongs to
  62. * @throws IOException Thrown when an IO-problem occurs
  63. */
  64. public RtfListItemLabel(RtfListItem item) throws IOException {
  65. super(null, item.writer, null);
  66. rtfListItem = item;
  67. }
  68. /**
  69. * Returns the current RtfTextrun object.
  70. * Opens a new one if necessary.
  71. * @return The RtfTextrun object
  72. * @throws IOException Thrown when an IO-problem occurs
  73. */
  74. public RtfTextrun getTextrun() throws IOException {
  75. return this;
  76. }
  77. /**
  78. * Sets the content of the list item label.
  79. * @param s Content of the list item label.
  80. * @throws IOException Thrown when an IO-problem occurs
  81. */
  82. public void addString(String s) throws IOException {
  83. final String label = s.trim();
  84. if (label.length() > 0 && Character.isDigit(label.charAt(0))) {
  85. rtfListItem.setRtfListStyle(new RtfListStyleNumber());
  86. } else {
  87. rtfListItem.setRtfListStyle(new RtfListStyleText(label));
  88. }
  89. }
  90. }
  91. /** Create an RTF list item as a child of given container with default attributes */
  92. RtfListItem(RtfList parent, Writer w) throws IOException {
  93. super((RtfContainer)parent, w);
  94. parentList = parent;
  95. }
  96. /**
  97. * Close current paragraph if any and start a new one
  98. * @param attrs attributes of new paragraph
  99. * @return new RtfParagraph
  100. * @throws IOException Thrown when an IO-problem occurs
  101. */
  102. public RtfParagraph newParagraph(RtfAttributes attrs) throws IOException {
  103. if (paragraph != null) {
  104. paragraph.close();
  105. }
  106. paragraph = new RtfListItemParagraph(this, attrs);
  107. return paragraph;
  108. }
  109. /**
  110. * Close current paragraph if any and start a new one with default attributes
  111. * @return new RtfParagraph
  112. * @throws IOException Thrown when an IO-problem occurs
  113. */
  114. public RtfParagraph newParagraph() throws IOException {
  115. return newParagraph(null);
  116. }
  117. /** Create an RTF list item as a child of given container with given attributes */
  118. RtfListItem(RtfList parent, Writer w, RtfAttributes attr) throws IOException {
  119. super((RtfContainer)parent, w, attr);
  120. parentList = parent;
  121. }
  122. /**
  123. * Get the current textrun.
  124. * @return current RtfTextrun object
  125. * @throws IOException Thrown when an IO-problem occurs
  126. */
  127. public RtfTextrun getTextrun() throws IOException {
  128. RtfTextrun textrun = RtfTextrun.getTextrun(this, writer, null);
  129. textrun.setRtfListItem(this);
  130. return textrun;
  131. }
  132. /**
  133. * Start a new list after closing current paragraph, list and table
  134. * @param attrs attributes of new RftList object
  135. * @return new RtfList
  136. * @throws IOException for I/O problems
  137. */
  138. public RtfList newList(RtfAttributes attrs) throws IOException {
  139. RtfList list = new RtfList(this, writer, attrs);
  140. return list;
  141. }
  142. /**
  143. * Overridden to setup the list: start a group with appropriate attributes
  144. * @throws IOException for I/O problems
  145. */
  146. protected void writeRtfPrefix() throws IOException {
  147. // pard causes word97 (and sometimes 2000 too) to crash if the list is nested in a table
  148. if (!parentList.getHasTableParent()) {
  149. writeControlWord("pard");
  150. }
  151. writeOneAttribute(RtfText.LEFT_INDENT_FIRST,
  152. "360"); //attrib.getValue(RtfListTable.LIST_INDENT));
  153. writeOneAttribute(RtfText.LEFT_INDENT_BODY,
  154. attrib.getValue(RtfText.LEFT_INDENT_BODY));
  155. // group for list setup info
  156. writeGroupMark(true);
  157. writeStarControlWord("pn");
  158. //Modified by Chris Scott
  159. //fixes second line indentation
  160. getRtfListStyle().writeListPrefix(this);
  161. writeGroupMark(false);
  162. writeOneAttribute(RtfListTable.LIST_NUMBER, number);
  163. }
  164. /**
  165. * End the list group
  166. * @throws IOException for I/O problems
  167. */
  168. protected void writeRtfSuffix() throws IOException {
  169. super.writeRtfSuffix();
  170. /* reset paragraph defaults to make sure list ends
  171. * but pard causes word97 (and sometimes 2000 too) to crash if the list
  172. * is nested in a table
  173. */
  174. if (!parentList.getHasTableParent()) {
  175. writeControlWord("pard");
  176. }
  177. }
  178. /**
  179. * Change list style
  180. * @param ls ListStyle to set
  181. */
  182. public void setRtfListStyle(RtfListStyle ls) {
  183. listStyle = ls;
  184. listStyle.setRtfListItem(this);
  185. number = getRtfFile().getListTable().addRtfListStyle(ls);
  186. }
  187. /**
  188. * Get list style
  189. * @return ListSytle of the List
  190. */
  191. public RtfListStyle getRtfListStyle() {
  192. if (listStyle == null) {
  193. return parentList.getRtfListStyle();
  194. } else {
  195. return listStyle;
  196. }
  197. }
  198. /**
  199. * Get the parent list.
  200. * @return the parent list
  201. */
  202. public RtfList getParentList() {
  203. return parentList;
  204. }
  205. /**
  206. * Returns the list number
  207. * @return list number
  208. */
  209. public int getNumber() {
  210. return number;
  211. }
  212. }