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

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