Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CFHeaderBase.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.ss.util.CellRangeAddress;
  19. import org.apache.poi.ss.util.CellRangeAddressList;
  20. import org.apache.poi.ss.util.CellRangeUtil;
  21. import org.apache.poi.util.GenericRecordUtil;
  22. import org.apache.poi.util.LittleEndianOutput;
  23. /**
  24. * Parent of Conditional Formatting Header records,
  25. * {@link CFHeaderRecord} and {@link CFHeader12Record}.
  26. */
  27. public abstract class CFHeaderBase extends StandardRecord {
  28. private int field_1_numcf;
  29. private int field_2_need_recalculation_and_id;
  30. private CellRangeAddress field_3_enclosing_cell_range;
  31. private CellRangeAddressList field_4_cell_ranges;
  32. protected CFHeaderBase() {}
  33. protected CFHeaderBase(CFHeaderBase other) {
  34. super(other);
  35. field_1_numcf = other.field_1_numcf;
  36. field_2_need_recalculation_and_id = other.field_2_need_recalculation_and_id;
  37. field_3_enclosing_cell_range = other.field_3_enclosing_cell_range.copy();
  38. field_4_cell_ranges = other.field_4_cell_ranges.copy();
  39. }
  40. protected CFHeaderBase(CellRangeAddress[] regions, int nRules) {
  41. CellRangeAddress[] mergeCellRanges = CellRangeUtil.mergeCellRanges(regions);
  42. setCellRanges(mergeCellRanges);
  43. field_1_numcf = nRules;
  44. }
  45. protected void createEmpty() {
  46. field_3_enclosing_cell_range = new CellRangeAddress(0, 0, 0, 0);
  47. field_4_cell_ranges = new CellRangeAddressList();
  48. }
  49. protected void read(RecordInputStream in) {
  50. field_1_numcf = in.readShort();
  51. field_2_need_recalculation_and_id = in.readShort();
  52. field_3_enclosing_cell_range = new CellRangeAddress(in);
  53. field_4_cell_ranges = new CellRangeAddressList(in);
  54. }
  55. public int getNumberOfConditionalFormats() {
  56. return field_1_numcf;
  57. }
  58. public void setNumberOfConditionalFormats(int n) {
  59. field_1_numcf=n;
  60. }
  61. public boolean getNeedRecalculation() {
  62. // Held on the 1st bit
  63. return (field_2_need_recalculation_and_id & 1) == 1;
  64. }
  65. public void setNeedRecalculation(boolean b) {
  66. // held on the first bit
  67. if (b == getNeedRecalculation()) {
  68. return;
  69. }
  70. if (b) {
  71. field_2_need_recalculation_and_id++;
  72. } else {
  73. field_2_need_recalculation_and_id--;
  74. }
  75. }
  76. public int getID() {
  77. // Remaining 15 bits of field 2
  78. return field_2_need_recalculation_and_id>>1;
  79. }
  80. public void setID(int id) {
  81. // Remaining 15 bits of field 2
  82. boolean needsRecalc = getNeedRecalculation();
  83. field_2_need_recalculation_and_id = (id<<1);
  84. if (needsRecalc) {
  85. field_2_need_recalculation_and_id++;
  86. }
  87. }
  88. public CellRangeAddress getEnclosingCellRange() {
  89. return field_3_enclosing_cell_range;
  90. }
  91. public void setEnclosingCellRange(CellRangeAddress cr) {
  92. field_3_enclosing_cell_range = cr;
  93. }
  94. /**
  95. * Set cell ranges list to a single cell range and
  96. * modify the enclosing cell range accordingly.
  97. * @param cellRanges - list of CellRange objects
  98. */
  99. public void setCellRanges(CellRangeAddress[] cellRanges) {
  100. if(cellRanges == null) {
  101. throw new IllegalArgumentException("cellRanges must not be null");
  102. }
  103. CellRangeAddressList cral = new CellRangeAddressList();
  104. CellRangeAddress enclosingRange = null;
  105. for (CellRangeAddress cr : cellRanges) {
  106. enclosingRange = CellRangeUtil.createEnclosingCellRange(cr, enclosingRange);
  107. cral.addCellRangeAddress(cr);
  108. }
  109. field_3_enclosing_cell_range = enclosingRange;
  110. field_4_cell_ranges = cral;
  111. }
  112. public CellRangeAddress[] getCellRanges() {
  113. return field_4_cell_ranges.getCellRangeAddresses();
  114. }
  115. protected abstract String getRecordName();
  116. protected int getDataSize() {
  117. return 4 // 2 short fields
  118. + CellRangeAddress.ENCODED_SIZE
  119. + field_4_cell_ranges.getSize();
  120. }
  121. public void serialize(LittleEndianOutput out) {
  122. out.writeShort(field_1_numcf);
  123. out.writeShort(field_2_need_recalculation_and_id);
  124. field_3_enclosing_cell_range.serialize(out);
  125. field_4_cell_ranges.serialize(out);
  126. }
  127. @Override
  128. public abstract CFHeaderBase copy();
  129. @Override
  130. public Map<String, Supplier<?>> getGenericProperties() {
  131. return GenericRecordUtil.getGenericProperties(
  132. "id", this::getID,
  133. "numCF", this::getNumberOfConditionalFormats,
  134. "needRecalculationAndId", this::getNeedRecalculation,
  135. "enclosingCellRange", this::getEnclosingCellRange,
  136. "cfRanges", this::getCellRanges
  137. );
  138. }
  139. }