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.

CFHeaderBase.java 6.0KB

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