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.

PageBreakRecord.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.record;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.Map;
  20. import java.util.Spliterator;
  21. import java.util.function.Supplier;
  22. import org.apache.poi.common.usermodel.GenericRecord;
  23. import org.apache.poi.util.GenericRecordUtil;
  24. import org.apache.poi.util.LittleEndianOutput;
  25. /**
  26. * Record that contains the functionality page breaks (horizontal and vertical)<p>
  27. *
  28. * The other two classes just specifically set the SIDS for record creation.<p>
  29. *
  30. * REFERENCE: Microsoft Excel SDK page 322 and 420
  31. *
  32. * @see HorizontalPageBreakRecord
  33. * @see VerticalPageBreakRecord
  34. */
  35. public abstract class PageBreakRecord extends StandardRecord {
  36. private static final int[] EMPTY_INT_ARRAY = { };
  37. private final ArrayList<Break> _breaks = new ArrayList<>();
  38. private final Map<Integer, Break> _breakMap = new HashMap<>();
  39. /**
  40. * Since both records store 2byte integers (short), no point in
  41. * differentiating it in the records.
  42. * <p>
  43. * The subs (rows or columns, don't seem to be able to set but excel sets
  44. * them automatically)
  45. */
  46. public static final class Break implements GenericRecord {
  47. public static final int ENCODED_SIZE = 6;
  48. private int main;
  49. private int subFrom;
  50. private int subTo;
  51. public Break(Break other) {
  52. main = other.main;
  53. subFrom = other.subFrom;
  54. subTo = other.subTo;
  55. }
  56. public Break(int main, int subFrom, int subTo) {
  57. this.main = main;
  58. this.subFrom = subFrom;
  59. this.subTo = subTo;
  60. }
  61. public Break(RecordInputStream in) {
  62. main = in.readUShort() - 1;
  63. subFrom = in.readUShort();
  64. subTo = in.readUShort();
  65. }
  66. public int getMain() {
  67. return main;
  68. }
  69. public int getSubFrom() {
  70. return subFrom;
  71. }
  72. public int getSubTo() {
  73. return subTo;
  74. }
  75. public void serialize(LittleEndianOutput out) {
  76. out.writeShort(main + 1);
  77. out.writeShort(subFrom);
  78. out.writeShort(subTo);
  79. }
  80. @Override
  81. public Map<String, Supplier<?>> getGenericProperties() {
  82. return GenericRecordUtil.getGenericProperties(
  83. "main", () -> main,
  84. "subFrom", () -> subFrom,
  85. "subTo", () -> subTo
  86. );
  87. }
  88. }
  89. protected PageBreakRecord() {}
  90. protected PageBreakRecord(PageBreakRecord other) {
  91. _breaks.addAll(other._breaks);
  92. initMap();
  93. }
  94. protected PageBreakRecord(RecordInputStream in) {
  95. final int nBreaks = in.readShort();
  96. _breaks.ensureCapacity(nBreaks + 2);
  97. for(int k = 0; k < nBreaks; k++) {
  98. _breaks.add(new Break(in));
  99. }
  100. initMap();
  101. }
  102. private void initMap() {
  103. _breaks.forEach(br -> _breakMap.put(br.main, br));
  104. }
  105. public boolean isEmpty() {
  106. return _breaks.isEmpty();
  107. }
  108. protected int getDataSize() {
  109. return 2 + _breaks.size() * Break.ENCODED_SIZE;
  110. }
  111. public final void serialize(LittleEndianOutput out) {
  112. int nBreaks = _breaks.size();
  113. out.writeShort(nBreaks);
  114. for (Break aBreak : _breaks) {
  115. aBreak.serialize(out);
  116. }
  117. }
  118. public int getNumBreaks() {
  119. return _breaks.size();
  120. }
  121. public final Iterator<Break> getBreaksIterator() {
  122. return _breaks.iterator();
  123. }
  124. /**
  125. * @since POI 5.2.0
  126. */
  127. public final Spliterator<Break> getBreaksSpliterator() {
  128. return _breaks.spliterator();
  129. }
  130. /**
  131. * Adds the page break at the specified parameters
  132. * @param main Depending on sid, will determine row or column to put page break (zero-based)
  133. * @param subFrom No user-interface to set (defaults to minimum, 0)
  134. * @param subTo No user-interface to set
  135. */
  136. public void addBreak(int main, int subFrom, int subTo) {
  137. Integer key = main;
  138. Break region = _breakMap.get(key);
  139. if(region == null) {
  140. region = new Break(main, subFrom, subTo);
  141. _breakMap.put(key, region);
  142. _breaks.add(region);
  143. } else {
  144. region.main = main;
  145. region.subFrom = subFrom;
  146. region.subTo = subTo;
  147. }
  148. }
  149. /**
  150. * Removes the break indicated by the parameter
  151. * @param main (zero-based)
  152. */
  153. public final void removeBreak(int main) {
  154. Integer rowKey = main;
  155. Break region = _breakMap.get(rowKey);
  156. _breaks.remove(region);
  157. _breakMap.remove(rowKey);
  158. }
  159. /**
  160. * Retrieves the region at the row/column indicated
  161. * @param main FIXME: Document this!
  162. * @return The Break or null if no break exists at the row/col specified.
  163. */
  164. public final Break getBreak(int main) {
  165. Integer rowKey = main;
  166. return _breakMap.get(rowKey);
  167. }
  168. public final int[] getBreaks() {
  169. int count = getNumBreaks();
  170. if (count < 1) {
  171. return EMPTY_INT_ARRAY;
  172. }
  173. int[] result = new int[count];
  174. for (int i=0; i<count; i++) {
  175. Break breakItem = _breaks.get(i);
  176. result[i] = breakItem.main;
  177. }
  178. return result;
  179. }
  180. @Override
  181. public abstract PageBreakRecord copy();
  182. @Override
  183. public Map<String, Supplier<?>> getGenericProperties() {
  184. return GenericRecordUtil.getGenericProperties(
  185. "numBreaks", this::getNumBreaks,
  186. "breaks", () -> _breaks
  187. );
  188. }
  189. }