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.

TableFObj.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.fo.flow.table;
  19. import org.xml.sax.Attributes;
  20. import org.xml.sax.Locator;
  21. import org.apache.fop.apps.FOPException;
  22. import org.apache.fop.datatypes.Numeric;
  23. import org.apache.fop.datatypes.ValidationPercentBaseContext;
  24. import org.apache.fop.fo.Constants;
  25. import org.apache.fop.fo.FONode;
  26. import org.apache.fop.fo.FObj;
  27. import org.apache.fop.fo.PropertyList;
  28. import org.apache.fop.fo.expr.PropertyException;
  29. import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
  30. import org.apache.fop.fo.properties.EnumNumber;
  31. import org.apache.fop.fo.properties.EnumProperty;
  32. import org.apache.fop.fo.properties.NumberProperty;
  33. import org.apache.fop.fo.properties.Property;
  34. import org.apache.fop.fo.properties.PropertyMaker;
  35. import org.apache.fop.fo.properties.StructurePointerPropertySet;
  36. import org.apache.fop.layoutmgr.table.CollapsingBorderModel;
  37. /**
  38. * Common base class for table-related FOs
  39. */
  40. public abstract class TableFObj extends FObj implements StructurePointerPropertySet {
  41. private Numeric borderAfterPrecedence;
  42. private Numeric borderBeforePrecedence;
  43. private Numeric borderEndPrecedence;
  44. private Numeric borderStartPrecedence;
  45. private String ptr;
  46. ConditionalBorder borderBefore; // CSOK: VisibilityModifier
  47. ConditionalBorder borderAfter; // CSOK: VisibilityModifier
  48. BorderSpecification borderStart; // CSOK: VisibilityModifier
  49. BorderSpecification borderEnd; // CSOK: VisibilityModifier
  50. CollapsingBorderModel collapsingBorderModel; // CSOK: VisibilityModifier
  51. /**
  52. * Create a TableFObj instance that is a child
  53. * of the given {@link FONode}
  54. *
  55. * @param parent the parent {@link FONode}
  56. */
  57. public TableFObj(FONode parent) {
  58. super(parent);
  59. }
  60. /** {@inheritDoc} */
  61. public void bind(PropertyList pList) throws FOPException {
  62. super.bind(pList);
  63. borderAfterPrecedence = pList.get(PR_BORDER_AFTER_PRECEDENCE).getNumeric();
  64. borderBeforePrecedence = pList.get(PR_BORDER_BEFORE_PRECEDENCE).getNumeric();
  65. borderEndPrecedence = pList.get(PR_BORDER_END_PRECEDENCE).getNumeric();
  66. borderStartPrecedence = pList.get(PR_BORDER_START_PRECEDENCE).getNumeric();
  67. if (getNameId() != FO_TABLE //Separate check for fo:table in Table.java
  68. && getNameId() != FO_TABLE_CELL
  69. && getCommonBorderPaddingBackground().hasPadding(
  70. ValidationPercentBaseContext.getPseudoContext())) {
  71. TableEventProducer eventProducer = TableEventProducer.Provider.get(
  72. getUserAgent().getEventBroadcaster());
  73. eventProducer.paddingNotApplicable(this, getName(), getLocator());
  74. }
  75. }
  76. /**
  77. * Return the value for the "border-precedence" property
  78. * for the given side.
  79. *
  80. * @param side the side for which to return the border precedence
  81. * @return the "border-precedence" value for the given side
  82. */
  83. public Numeric getBorderPrecedence(int side) {
  84. switch (side) {
  85. case CommonBorderPaddingBackground.BEFORE:
  86. return borderBeforePrecedence;
  87. case CommonBorderPaddingBackground.AFTER:
  88. return borderAfterPrecedence;
  89. case CommonBorderPaddingBackground.START:
  90. return borderStartPrecedence;
  91. case CommonBorderPaddingBackground.END:
  92. return borderEndPrecedence;
  93. default:
  94. return null;
  95. }
  96. }
  97. /**
  98. * Convenience method to returns a reference
  99. * to the base {@link Table} instance.
  100. *
  101. * @return the base table instance
  102. *
  103. */
  104. public Table getTable() {
  105. // Overridden in Table; for any other Table-node, recursive call to
  106. // parent.getTable()
  107. return ((TableFObj) parent).getTable();
  108. }
  109. /**
  110. * @return the Common Border, Padding, and Background Properties.
  111. */
  112. public abstract CommonBorderPaddingBackground getCommonBorderPaddingBackground();
  113. /**
  114. * {@link PropertyMaker} subclass for the column-number property
  115. */
  116. public static class ColumnNumberPropertyMaker extends PropertyMaker {
  117. /**
  118. * Constructor
  119. *
  120. * @param propId the id of the property for which the maker should
  121. * be created
  122. */
  123. public ColumnNumberPropertyMaker(int propId) {
  124. super(propId);
  125. }
  126. /** {@inheritDoc} */
  127. public Property make(PropertyList propertyList)
  128. throws PropertyException {
  129. FObj fo = propertyList.getFObj();
  130. return NumberProperty.getInstance(((ColumnNumberManagerHolder) fo.getParent())
  131. .getColumnNumberManager().getCurrentColumnNumber());
  132. }
  133. /**
  134. * {@inheritDoc}
  135. * Check the value of the column-number property.
  136. */
  137. public Property make(PropertyList propertyList, String value, FObj fo)
  138. throws PropertyException {
  139. Property p = super.make(propertyList, value, fo);
  140. int columnIndex = p.getNumeric().getValue();
  141. int colSpan = propertyList.get(Constants.PR_NUMBER_COLUMNS_SPANNED)
  142. .getNumeric().getValue();
  143. // only check whether the column-number is occupied in case it was
  144. // specified on a fo:table-cell or fo:table-column
  145. int foId = propertyList.getFObj().getNameId();
  146. if (foId == FO_TABLE_COLUMN || foId == FO_TABLE_CELL) {
  147. ColumnNumberManagerHolder parent
  148. = (ColumnNumberManagerHolder) propertyList.getParentFObj();
  149. ColumnNumberManager columnIndexManager = parent.getColumnNumberManager();
  150. int lastIndex = columnIndex - 1 + colSpan;
  151. for (int i = columnIndex; i <= lastIndex; ++i) {
  152. if (columnIndexManager.isColumnNumberUsed(i)) {
  153. /* if column-number is already in use by another
  154. * cell/column => error!
  155. */
  156. TableEventProducer eventProducer
  157. = TableEventProducer.Provider.get(
  158. fo.getUserAgent().getEventBroadcaster());
  159. eventProducer.cellOverlap(
  160. this, propertyList.getFObj().getName(),
  161. i, propertyList.getFObj().getLocator());
  162. }
  163. }
  164. }
  165. return p;
  166. }
  167. /**
  168. * {@inheritDoc}
  169. * If the value is not positive, return a property whose value
  170. * is the next column number.
  171. */
  172. public Property convertProperty(Property p,
  173. PropertyList propertyList, FObj fo)
  174. throws PropertyException {
  175. if (p instanceof EnumProperty) {
  176. return EnumNumber.getInstance(p);
  177. }
  178. Number val = p.getNumber();
  179. if (val != null) {
  180. int i = Math.round(val.floatValue());
  181. int foId = propertyList.getFObj().getNameId();
  182. if (i <= 0) {
  183. if (foId == FO_TABLE_CELL || foId == FO_TABLE_COLUMN) {
  184. ColumnNumberManagerHolder parent
  185. = (ColumnNumberManagerHolder) propertyList.getParentFObj();
  186. ColumnNumberManager columnIndexManager
  187. = parent.getColumnNumberManager();
  188. i = columnIndexManager.getCurrentColumnNumber();
  189. } else {
  190. /* very exceptional case:
  191. * negative column-number specified on
  192. * a FO that is not a fo:table-cell or fo:table-column
  193. */
  194. i = 1;
  195. }
  196. TableEventProducer eventProducer = TableEventProducer.Provider.get(
  197. fo.getUserAgent().getEventBroadcaster());
  198. eventProducer.forceNextColumnNumber(this, propertyList.getFObj().getName(),
  199. val, i, propertyList.getFObj().getLocator());
  200. }
  201. return NumberProperty.getInstance(i);
  202. }
  203. return convertPropertyDatatype(p, propertyList, fo);
  204. }
  205. }
  206. /** {@inheritDoc} */
  207. public void processNode(String elementName, Locator locator, Attributes attlist,
  208. PropertyList pList) throws FOPException {
  209. super.processNode(elementName, locator, attlist, pList);
  210. Table table = getTable();
  211. if (!inMarker() && !table.isSeparateBorderModel()) {
  212. collapsingBorderModel = CollapsingBorderModel.getBorderModelFor(table
  213. .getBorderCollapse());
  214. setCollapsedBorders();
  215. }
  216. }
  217. @Override
  218. public void setPtr(String ptr) {
  219. this.ptr = ptr;
  220. }
  221. /** {@inheritDoc} */
  222. public String getPtr() {
  223. return ptr;
  224. }
  225. /**
  226. * Prepares the borders of this element if the collapsing-border model is in use.
  227. * Conflict resolution with parent elements is done where applicable.
  228. */
  229. protected void setCollapsedBorders() {
  230. createBorder(CommonBorderPaddingBackground.START);
  231. createBorder(CommonBorderPaddingBackground.END);
  232. createBorder(CommonBorderPaddingBackground.BEFORE);
  233. createBorder(CommonBorderPaddingBackground.AFTER);
  234. }
  235. /**
  236. * Creates a BorderSpecification from the border set on the given side. If no border
  237. * is set, a BorderSpecification with border-style none is created.
  238. *
  239. * @param side one of CommonBorderPaddingBackground.BEFORE|AFTER|START|END
  240. */
  241. private void createBorder(int side) {
  242. BorderSpecification borderSpec = new BorderSpecification(
  243. getCommonBorderPaddingBackground().getBorderInfo(side), getNameId());
  244. switch (side) {
  245. case CommonBorderPaddingBackground.BEFORE:
  246. borderBefore = new ConditionalBorder(borderSpec, collapsingBorderModel);
  247. break;
  248. case CommonBorderPaddingBackground.AFTER:
  249. borderAfter = new ConditionalBorder(borderSpec, collapsingBorderModel);
  250. break;
  251. case CommonBorderPaddingBackground.START:
  252. borderStart = borderSpec;
  253. break;
  254. case CommonBorderPaddingBackground.END:
  255. borderEnd = borderSpec;
  256. break;
  257. default: assert false;
  258. }
  259. }
  260. }