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.

KeepProperty.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.properties;
  19. import org.apache.fop.datatypes.CompoundDatatype;
  20. import org.apache.fop.fo.FObj;
  21. import org.apache.fop.fo.PropertyList;
  22. import org.apache.fop.fo.expr.PropertyException;
  23. /**
  24. * Class for properties that wrap Keep values
  25. */
  26. public final class KeepProperty extends Property implements CompoundDatatype {
  27. /** class holding all canonical KeepProperty instances*/
  28. private static final PropertyCache<KeepProperty> CACHE
  29. = new PropertyCache<KeepProperty>();
  30. private boolean isCachedValue;
  31. private Property withinLine;
  32. private Property withinColumn;
  33. private Property withinPage;
  34. /**
  35. * Inner class for creating instances of KeepProperty
  36. */
  37. public static class Maker extends CompoundPropertyMaker {
  38. /**
  39. * @param propId the id of the property for which a Maker should be created
  40. */
  41. public Maker(int propId) {
  42. super(propId);
  43. }
  44. /**
  45. * Create a new empty instance of KeepProperty.
  46. * @return the new instance.
  47. */
  48. public Property makeNewProperty() {
  49. return new KeepProperty();
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. public Property convertProperty(Property p, PropertyList propertyList, FObj fo)
  55. throws PropertyException {
  56. if (p instanceof KeepProperty) {
  57. return p;
  58. }
  59. return super.convertProperty(p, propertyList, fo);
  60. }
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public void setComponent(int cmpId, Property cmpnValue,
  66. boolean bIsDefault) {
  67. if (isCachedValue) {
  68. log.warn("KeepProperty.setComponent() called on cached value. Ignoring...");
  69. return;
  70. }
  71. if (cmpId == CP_WITHIN_LINE) {
  72. setWithinLine(cmpnValue, bIsDefault);
  73. } else if (cmpId == CP_WITHIN_COLUMN) {
  74. setWithinColumn(cmpnValue, bIsDefault);
  75. } else if (cmpId == CP_WITHIN_PAGE) {
  76. setWithinPage(cmpnValue, bIsDefault);
  77. }
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. public Property getComponent(int cmpId) {
  83. if (cmpId == CP_WITHIN_LINE) {
  84. return getWithinLine();
  85. } else if (cmpId == CP_WITHIN_COLUMN) {
  86. return getWithinColumn();
  87. } else if (cmpId == CP_WITHIN_PAGE) {
  88. return getWithinPage();
  89. } else {
  90. return null;
  91. }
  92. }
  93. /**
  94. * @param withinLine withinLine property to set
  95. * @param bIsDefault not used (??)
  96. */
  97. public void setWithinLine(Property withinLine, boolean bIsDefault) {
  98. this.withinLine = withinLine;
  99. }
  100. /**
  101. * @param withinColumn withinColumn property to set
  102. * @param bIsDefault not used (??)
  103. */
  104. protected void setWithinColumn(Property withinColumn,
  105. boolean bIsDefault) {
  106. this.withinColumn = withinColumn;
  107. }
  108. /**
  109. * @param withinPage withinPage property to set
  110. * @param bIsDefault not used (??)
  111. */
  112. public void setWithinPage(Property withinPage, boolean bIsDefault) {
  113. this.withinPage = withinPage;
  114. }
  115. /**
  116. * @return the withinLine property
  117. */
  118. public Property getWithinLine() {
  119. return this.withinLine;
  120. }
  121. /**
  122. * @return the withinColumn property
  123. */
  124. public Property getWithinColumn() {
  125. return this.withinColumn;
  126. }
  127. /**
  128. * @return the withinPage property
  129. */
  130. public Property getWithinPage() {
  131. return this.withinPage;
  132. }
  133. /**
  134. * Not sure what to do here. There isn't really a meaningful single value.
  135. * @return String representation
  136. */
  137. public String toString() {
  138. return "Keep["
  139. + "withinLine:" + getWithinLine().getObject()
  140. + ", withinColumn:" + getWithinColumn().getObject()
  141. + ", withinPage:" + getWithinPage().getObject() + "]";
  142. }
  143. /**
  144. * @return the canonical KeepProperty instance corresponding to
  145. * this property
  146. */
  147. public KeepProperty getKeep() {
  148. KeepProperty keep = CACHE.fetch(this);
  149. /* make sure setComponent() can never alter cached values */
  150. keep.isCachedValue = true;
  151. return keep;
  152. }
  153. /**
  154. * @return this.keep cast as Object
  155. */
  156. public Object getObject() {
  157. return this;
  158. }
  159. /** {@inheritDoc} */
  160. public boolean equals(Object o) {
  161. if (this == o) {
  162. return true;
  163. }
  164. if (o instanceof KeepProperty) {
  165. KeepProperty keep = (KeepProperty) o;
  166. return (keep.withinColumn == this.withinColumn)
  167. && (keep.withinLine == this.withinLine)
  168. && (keep.withinPage == this.withinPage);
  169. }
  170. return false;
  171. }
  172. /** {@inheritDoc} */
  173. public int hashCode() {
  174. int hash = 17;
  175. hash = 37 * hash + (withinColumn == null ? 0 : withinColumn.hashCode());
  176. hash = 37 * hash + (withinLine == null ? 0 : withinLine.hashCode());
  177. hash = 37 * hash + (withinPage == null ? 0 : withinPage.hashCode());
  178. return hash;
  179. }
  180. }