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.

IconMultiStateFormatting.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.cf;
  16. import java.util.Map;
  17. import java.util.function.Supplier;
  18. import java.util.stream.Stream;
  19. import org.apache.logging.log4j.LogManager;
  20. import org.apache.logging.log4j.Logger;
  21. import org.apache.poi.common.Duplicatable;
  22. import org.apache.poi.common.usermodel.GenericRecord;
  23. import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;
  24. import org.apache.poi.util.BitField;
  25. import org.apache.poi.util.BitFieldFactory;
  26. import org.apache.poi.util.GenericRecordJsonWriter;
  27. import org.apache.poi.util.GenericRecordUtil;
  28. import org.apache.poi.util.LittleEndianInput;
  29. import org.apache.poi.util.LittleEndianOutput;
  30. import static org.apache.logging.log4j.util.Unbox.box;
  31. /**
  32. * Icon / Multi-State Conditional Formatting Rule Record.
  33. */
  34. public final class IconMultiStateFormatting implements Duplicatable, GenericRecord {
  35. private static final Logger LOG = LogManager.getLogger(IconMultiStateFormatting.class);
  36. private static BitField ICON_ONLY = BitFieldFactory.getInstance(0x01);
  37. private static BitField REVERSED = BitFieldFactory.getInstance(0x04);
  38. private IconSet iconSet;
  39. private byte options;
  40. private Threshold[] thresholds;
  41. public IconMultiStateFormatting() {
  42. iconSet = IconSet.GYR_3_TRAFFIC_LIGHTS;
  43. options = 0;
  44. thresholds = new Threshold[iconSet.num];
  45. }
  46. public IconMultiStateFormatting(IconMultiStateFormatting other) {
  47. iconSet = other.iconSet;
  48. options = other.options;
  49. if (other.thresholds != null) {
  50. thresholds = Stream.of(other.thresholds).map(Threshold::copy).toArray(Threshold[]::new);
  51. }
  52. }
  53. public IconMultiStateFormatting(LittleEndianInput in) {
  54. in.readShort(); // Ignored
  55. in.readByte(); // Reserved
  56. int num = in.readByte();
  57. int set = in.readByte();
  58. iconSet = IconSet.byId(set);
  59. if (iconSet.num != num) {
  60. LOG.atWarn().log("Inconsistent Icon Set definition, found {} but defined as {} entries", iconSet, box(num));
  61. }
  62. options = in.readByte();
  63. thresholds = new Threshold[iconSet.num];
  64. for (int i=0; i<thresholds.length; i++) {
  65. thresholds[i] = new IconMultiStateThreshold(in);
  66. }
  67. }
  68. public IconSet getIconSet() {
  69. return iconSet;
  70. }
  71. public void setIconSet(IconSet set) {
  72. this.iconSet = set;
  73. }
  74. public Threshold[] getThresholds() {
  75. return thresholds;
  76. }
  77. public void setThresholds(Threshold[] thresholds) {
  78. this.thresholds = (thresholds == null) ? null : thresholds.clone();
  79. }
  80. public boolean isIconOnly() {
  81. return ICON_ONLY.isSet(options);
  82. }
  83. public void setIconOnly(boolean only) {
  84. options = ICON_ONLY.setByteBoolean(options, only);
  85. }
  86. public boolean isReversed() {
  87. return REVERSED.isSet(options);
  88. }
  89. public void setReversed(boolean rev) {
  90. options = REVERSED.setByteBoolean(options, rev);
  91. }
  92. @Override
  93. public Map<String, Supplier<?>> getGenericProperties() {
  94. return GenericRecordUtil.getGenericProperties(
  95. "iconSet", this::getIconSet,
  96. "iconOnly", this::isIconOnly,
  97. "reversed", this::isReversed,
  98. "thresholds", this::getThresholds
  99. );
  100. }
  101. public String toString() {
  102. return GenericRecordJsonWriter.marshal(this);
  103. }
  104. @Override
  105. public IconMultiStateFormatting copy() {
  106. return new IconMultiStateFormatting(this);
  107. }
  108. public int getDataLength() {
  109. int len = 6;
  110. for (Threshold t : thresholds) {
  111. len += t.getDataLength();
  112. }
  113. return len;
  114. }
  115. public void serialize(LittleEndianOutput out) {
  116. out.writeShort(0);
  117. out.writeByte(0);
  118. out.writeByte(iconSet.num);
  119. out.writeByte(iconSet.id);
  120. out.writeByte(options);
  121. for (Threshold t : thresholds) {
  122. t.serialize(out);
  123. }
  124. }
  125. }