選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SelectionRecord.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 java.util.stream.Stream;
  19. import org.apache.poi.hssf.util.CellRangeAddress8Bit;
  20. import org.apache.poi.util.GenericRecordUtil;
  21. import org.apache.poi.util.LittleEndianOutput;
  22. /**
  23. * Shows the user's selection on the sheet for write set num refs to 0
  24. */
  25. public final class SelectionRecord extends StandardRecord {
  26. public static final short sid = 0x001D;
  27. private byte field_1_pane;
  28. private int field_2_row_active_cell;
  29. private int field_3_col_active_cell;
  30. private int field_4_active_cell_ref_index;
  31. private CellRangeAddress8Bit[] field_6_refs;
  32. public SelectionRecord(SelectionRecord other) {
  33. super(other);
  34. field_1_pane = other.field_1_pane;
  35. field_2_row_active_cell = other.field_2_row_active_cell;
  36. field_3_col_active_cell = other.field_3_col_active_cell;
  37. field_4_active_cell_ref_index = other.field_4_active_cell_ref_index;
  38. field_6_refs = (other.field_6_refs == null) ? null
  39. : Stream.of(other.field_6_refs).map(CellRangeAddress8Bit::copy).toArray(CellRangeAddress8Bit[]::new);
  40. }
  41. /**
  42. * Creates a default selection record (cell A1, in pane ID 3)
  43. *
  44. * @param activeCellRow the active cells row index
  45. * @param activeCellCol the active cells column index
  46. */
  47. public SelectionRecord(int activeCellRow, int activeCellCol) {
  48. field_1_pane = 3; // pane id 3 is always present. see OOO sec 5.75 'PANE'
  49. field_2_row_active_cell = activeCellRow;
  50. field_3_col_active_cell = activeCellCol;
  51. field_4_active_cell_ref_index = 0;
  52. field_6_refs = new CellRangeAddress8Bit[] {
  53. new CellRangeAddress8Bit(activeCellRow, activeCellRow, activeCellCol, activeCellCol),
  54. };
  55. }
  56. public SelectionRecord(RecordInputStream in) {
  57. field_1_pane = in.readByte();
  58. field_2_row_active_cell = in.readUShort();
  59. field_3_col_active_cell = in.readShort();
  60. field_4_active_cell_ref_index = in.readShort();
  61. int field_5_num_refs = in.readUShort();
  62. field_6_refs = new CellRangeAddress8Bit[field_5_num_refs];
  63. for (int i = 0; i < field_6_refs.length; i++) {
  64. field_6_refs[i] = new CellRangeAddress8Bit(in);
  65. }
  66. }
  67. /**
  68. * set which window pane this is for
  69. * @param pane the window pane
  70. */
  71. public void setPane(byte pane) {
  72. field_1_pane = pane;
  73. }
  74. /**
  75. * set the active cell's row
  76. * @param row number of active cell
  77. */
  78. public void setActiveCellRow(int row) {
  79. field_2_row_active_cell = row;
  80. resetField6();
  81. }
  82. /**
  83. * set the active cell's col
  84. * @param col number of active cell
  85. */
  86. public void setActiveCellCol(short col) {
  87. field_3_col_active_cell = col;
  88. resetField6();
  89. }
  90. private void resetField6() {
  91. // this is necessary in Excel to actually make Workbook.setActiveCell() take effect
  92. field_6_refs = new CellRangeAddress8Bit[] {
  93. new CellRangeAddress8Bit(field_2_row_active_cell, field_2_row_active_cell, field_3_col_active_cell, field_3_col_active_cell),
  94. };
  95. }
  96. /**
  97. * set the active cell's reference number
  98. * @param ref number of active cell
  99. */
  100. public void setActiveCellRef(short ref) {
  101. field_4_active_cell_ref_index = ref;
  102. }
  103. /**
  104. * @return the pane ID which window pane this is for
  105. */
  106. public byte getPane() {
  107. return field_1_pane;
  108. }
  109. /**
  110. * get the active cell's row
  111. * @return row number of active cell
  112. */
  113. public int getActiveCellRow() {
  114. return field_2_row_active_cell;
  115. }
  116. /**
  117. * get the active cell's col
  118. * @return col number of active cell
  119. */
  120. public int getActiveCellCol() {
  121. return field_3_col_active_cell;
  122. }
  123. /**
  124. * get the active cell's reference number
  125. * @return ref number of active cell
  126. */
  127. public int getActiveCellRef() {
  128. return field_4_active_cell_ref_index;
  129. }
  130. @Override
  131. protected int getDataSize() {
  132. return 9 // 1 byte + 4 shorts
  133. + CellRangeAddress8Bit.getEncodedSize(field_6_refs.length);
  134. }
  135. @Override
  136. public void serialize(LittleEndianOutput out) {
  137. out.writeByte(getPane());
  138. out.writeShort(getActiveCellRow());
  139. out.writeShort(getActiveCellCol());
  140. out.writeShort(getActiveCellRef());
  141. int nRefs = field_6_refs.length;
  142. out.writeShort(nRefs);
  143. for (CellRangeAddress8Bit field_6_ref : field_6_refs) {
  144. field_6_ref.serialize(out);
  145. }
  146. }
  147. @Override
  148. public short getSid() {
  149. return sid;
  150. }
  151. @Override
  152. public SelectionRecord copy() {
  153. return new SelectionRecord(this);
  154. }
  155. @Override
  156. public HSSFRecordTypes getGenericRecordType() {
  157. return HSSFRecordTypes.SELECTION;
  158. }
  159. @Override
  160. public Map<String, Supplier<?>> getGenericProperties() {
  161. return GenericRecordUtil.getGenericProperties(
  162. "pane", this::getPane,
  163. "activeCellRow", this::getActiveCellRow,
  164. "activeCellCol", this::getActiveCellCol,
  165. "activeCellRef", this::getActiveCellRef,
  166. "refs", () -> field_6_refs
  167. );
  168. }
  169. }