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.

ConditionalBorder.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.apache.fop.layoutmgr.table.CollapsingBorderModel;
  20. /**
  21. * A class that holds the three possible values for a border-before/after on a table-cell,
  22. * in the collapsing model. These three values are (for border-before, similar for
  23. * border-after):
  24. * <ul>
  25. * <li>normal: common case, when a cell follows the cell before on a same page;</li>
  26. * <li>leading: when the table is broken and the cell appears at the top of a page, in
  27. * which case its border must be resolved with the header (or the top of the table)
  28. * instead of with the previous cell;</li>
  29. * <li>rest: when a cell is broken over several pages; same as leading but with
  30. * conditionality taken into account.</li>
  31. * </ul>
  32. */
  33. public class ConditionalBorder {
  34. /** normal border */
  35. public static final int NORMAL = 0;
  36. /** leading and trailing border */
  37. public static final int LEADING_TRAILING = 1;
  38. /** all the rest */
  39. public static final int REST = 2;
  40. /** Normal case, no break. */
  41. BorderSpecification normal; // CSOK: VisibilityModifier
  42. /** Special case: the cell is at the top or the bottom of the page. */
  43. BorderSpecification leadingTrailing; // CSOK: VisibilityModifier
  44. /** Special case: break inside the cell. */
  45. BorderSpecification rest; // CSOK: VisibilityModifier
  46. /** The model used to resolve borders. */
  47. private CollapsingBorderModel collapsingBorderModel;
  48. private ConditionalBorder(BorderSpecification normal,
  49. BorderSpecification leadingTrailing, BorderSpecification rest,
  50. CollapsingBorderModel collapsingBorderModel) {
  51. assert collapsingBorderModel != null;
  52. this.normal = normal;
  53. this.leadingTrailing = leadingTrailing;
  54. this.rest = rest;
  55. this.collapsingBorderModel = collapsingBorderModel;
  56. }
  57. /**
  58. * Creates a new conditional border.
  59. *
  60. * @param borderSpecification the border specification to take as a basis
  61. * @param collapsingBorderModel the model that will be used to resolved borders
  62. */
  63. ConditionalBorder(BorderSpecification borderSpecification,
  64. CollapsingBorderModel collapsingBorderModel) {
  65. this ( borderSpecification, borderSpecification,
  66. borderSpecification.getBorderInfo().getWidth().isDiscard()
  67. ? BorderSpecification.getDefaultBorder() : borderSpecification,
  68. collapsingBorderModel );
  69. }
  70. /**
  71. * Resolves and updates the relevant parts of this border as well as the given one.
  72. *
  73. * @param competitor
  74. * @param withNormal
  75. * @param withLeadingTrailing
  76. * @param withRest
  77. */
  78. void resolve(ConditionalBorder competitor, boolean withNormal,
  79. boolean withLeadingTrailing, boolean withRest) {
  80. if (withNormal) {
  81. BorderSpecification resolvedBorder = collapsingBorderModel.determineWinner(
  82. normal, competitor.normal);
  83. if (resolvedBorder != null) {
  84. normal = resolvedBorder;
  85. competitor.normal = resolvedBorder;
  86. }
  87. }
  88. if (withLeadingTrailing) {
  89. BorderSpecification resolvedBorder = collapsingBorderModel.determineWinner(
  90. leadingTrailing, competitor.leadingTrailing);
  91. if (resolvedBorder != null) {
  92. leadingTrailing = resolvedBorder;
  93. competitor.leadingTrailing = resolvedBorder;
  94. }
  95. }
  96. if (withRest) {
  97. BorderSpecification resolvedBorder = collapsingBorderModel.determineWinner(rest,
  98. competitor.rest);
  99. if (resolvedBorder != null) {
  100. rest = resolvedBorder;
  101. competitor.rest = resolvedBorder;
  102. }
  103. }
  104. }
  105. /**
  106. * Integrates the given segment in this border. Unlike for
  107. * {@link #integrateSegment(ConditionalBorder, boolean, boolean, boolean)}, this
  108. * method nicely handles the case where the CollapsingBorderModel returns null, by
  109. * keeping the components to their old values.
  110. *
  111. * @param competitor
  112. * @param withNormal
  113. * @param withLeadingTrailing
  114. * @param withRest
  115. */
  116. void integrateCompetingSegment(ConditionalBorder competitor, boolean withNormal,
  117. boolean withLeadingTrailing, boolean withRest) {
  118. if (withNormal) {
  119. BorderSpecification resolvedBorder = collapsingBorderModel.determineWinner(
  120. normal, competitor.normal);
  121. if (resolvedBorder != null) {
  122. normal = resolvedBorder;
  123. }
  124. }
  125. if (withLeadingTrailing) {
  126. BorderSpecification resolvedBorder = collapsingBorderModel.determineWinner(
  127. leadingTrailing, competitor.leadingTrailing);
  128. if (resolvedBorder != null) {
  129. leadingTrailing = resolvedBorder;
  130. }
  131. }
  132. if (withRest) {
  133. BorderSpecification resolvedBorder = collapsingBorderModel.determineWinner(rest,
  134. competitor.rest);
  135. if (resolvedBorder != null) {
  136. rest = resolvedBorder;
  137. }
  138. }
  139. }
  140. /**
  141. * Updates this border after taking into account the given segment. The
  142. * CollapsingBorderModel is not expected to return null.
  143. *
  144. * @param segment
  145. * @param withNormal
  146. * @param withLeadingTrailing
  147. * @param withRest
  148. */
  149. void integrateSegment(ConditionalBorder segment, boolean withNormal,
  150. boolean withLeadingTrailing, boolean withRest) {
  151. if (withNormal) {
  152. normal = collapsingBorderModel.determineWinner(normal, segment.normal);
  153. assert normal != null;
  154. }
  155. if (withLeadingTrailing) {
  156. leadingTrailing = collapsingBorderModel.determineWinner(leadingTrailing,
  157. segment.leadingTrailing);
  158. assert leadingTrailing != null;
  159. }
  160. if (withRest) {
  161. rest = collapsingBorderModel.determineWinner(rest, segment.rest);
  162. assert rest != null;
  163. }
  164. }
  165. /**
  166. * Returns a shallow copy of this border.
  167. *
  168. * @return a copy of this border
  169. */
  170. ConditionalBorder copy() {
  171. return new ConditionalBorder(normal, leadingTrailing, rest, collapsingBorderModel);
  172. }
  173. /** {@inheritDoc} */
  174. public String toString() {
  175. return "{normal: " + normal + ", leading: " + leadingTrailing + ", rest: " + rest + "}";
  176. }
  177. /**
  178. * Returns a default border specification.
  179. *
  180. * @param collapsingBorderModel the model that will be used to resolve borders
  181. * @return a border with style 'none' for all of the three components
  182. */
  183. static ConditionalBorder getDefaultBorder(CollapsingBorderModel collapsingBorderModel) {
  184. BorderSpecification defaultBorderSpec = BorderSpecification.getDefaultBorder();
  185. return new ConditionalBorder(defaultBorderSpec, defaultBorderSpec, defaultBorderSpec,
  186. collapsingBorderModel);
  187. }
  188. }