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.

MergeCellsRecord.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.ss.util.CellRangeAddress;
  17. import org.apache.poi.ss.util.CellRangeAddressList;
  18. import org.apache.poi.util.LittleEndianOutput;
  19. /**
  20. * Title: Merged Cells Record (0x00E5)
  21. * <br/>
  22. * Description: Optional record defining a square area of cells to "merged" into one cell. <br>
  23. * @author Andrew C. Oliver (acoliver at apache dot org)
  24. */
  25. public final class MergeCellsRecord extends StandardRecord {
  26. public final static short sid = 0x00E5;
  27. /** sometimes the regions array is shared with other MergedCellsRecords */
  28. private CellRangeAddress[] _regions;
  29. private final int _startIndex;
  30. private final int _numberOfRegions;
  31. public MergeCellsRecord(CellRangeAddress[] regions, int startIndex, int numberOfRegions) {
  32. _regions = regions;
  33. _startIndex = startIndex;
  34. _numberOfRegions = numberOfRegions;
  35. }
  36. /**
  37. * Constructs a MergedCellsRecord and sets its fields appropriately
  38. * @param in the RecordInputstream to read the record from
  39. */
  40. public MergeCellsRecord(RecordInputStream in) {
  41. int nRegions = in.readUShort();
  42. CellRangeAddress[] cras = new CellRangeAddress[nRegions];
  43. for (int i = 0; i < nRegions; i++) {
  44. cras[i] = new CellRangeAddress(in);
  45. }
  46. _numberOfRegions = nRegions;
  47. _startIndex = 0;
  48. _regions = cras;
  49. }
  50. /**
  51. * get the number of merged areas. If this drops down to 0 you should just go
  52. * ahead and delete the record.
  53. * @return number of areas
  54. */
  55. public short getNumAreas() {
  56. return (short)_numberOfRegions;
  57. }
  58. /**
  59. * @return MergedRegion at the given index representing the area that is Merged (r1,c1 - r2,c2)
  60. */
  61. public CellRangeAddress getAreaAt(int index) {
  62. return _regions[_startIndex + index];
  63. }
  64. protected int getDataSize() {
  65. return CellRangeAddressList.getEncodedSize(_numberOfRegions);
  66. }
  67. public short getSid() {
  68. return sid;
  69. }
  70. public void serialize(LittleEndianOutput out) {
  71. int nItems = _numberOfRegions;
  72. out.writeShort(nItems);
  73. for (int i = 0; i < _numberOfRegions; i++) {
  74. _regions[_startIndex + i].serialize(out);
  75. }
  76. }
  77. public String toString() {
  78. StringBuffer retval = new StringBuffer();
  79. retval.append("[MERGEDCELLS]").append("\n");
  80. retval.append(" .numregions =").append(getNumAreas()).append("\n");
  81. for (int k = 0; k < _numberOfRegions; k++) {
  82. CellRangeAddress r = _regions[_startIndex + k];
  83. retval.append(" .rowfrom =").append(r.getFirstRow()).append("\n");
  84. retval.append(" .rowto =").append(r.getLastRow()).append("\n");
  85. retval.append(" .colfrom =").append(r.getFirstColumn()).append("\n");
  86. retval.append(" .colto =").append(r.getLastColumn()).append("\n");
  87. }
  88. retval.append("[MERGEDCELLS]").append("\n");
  89. return retval.toString();
  90. }
  91. public Object clone() {
  92. int nRegions = _numberOfRegions;
  93. CellRangeAddress[] clonedRegions = new CellRangeAddress[nRegions];
  94. for (int i = 0; i < clonedRegions.length; i++) {
  95. clonedRegions[i] = _regions[_startIndex + i].copy();
  96. }
  97. return new MergeCellsRecord(clonedRegions, 0, nRegions);
  98. }
  99. }