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.

XSSFColorScaleFormatting.java 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xssf.usermodel;
  20. import org.apache.poi.ss.usermodel.Color;
  21. import org.apache.poi.ss.usermodel.ColorScaleFormatting;
  22. import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold;
  23. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCfvo;
  24. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
  25. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColorScale;
  26. /**
  27. * High level representation for Color Scale / Color Gradient Formatting
  28. * component of Conditional Formatting settings
  29. */
  30. public class XSSFColorScaleFormatting implements ColorScaleFormatting {
  31. private CTColorScale _scale;
  32. private IndexedColorMap _indexedColorMap;
  33. /*package*/ XSSFColorScaleFormatting(CTColorScale scale, IndexedColorMap colorMap){
  34. _scale = scale;
  35. _indexedColorMap = colorMap;
  36. }
  37. public int getNumControlPoints() {
  38. return _scale.sizeOfCfvoArray();
  39. }
  40. public void setNumControlPoints(int num) {
  41. while (num < _scale.sizeOfCfvoArray()) {
  42. _scale.removeCfvo(_scale.sizeOfCfvoArray()-1);
  43. _scale.removeColor(_scale.sizeOfColorArray()-1);
  44. }
  45. while (num > _scale.sizeOfCfvoArray()) {
  46. _scale.addNewCfvo();
  47. _scale.addNewColor();
  48. }
  49. }
  50. public XSSFColor[] getColors() {
  51. CTColor[] ctcols = _scale.getColorArray();
  52. XSSFColor[] c = new XSSFColor[ctcols.length];
  53. for (int i=0; i<ctcols.length; i++) {
  54. c[i] = new XSSFColor(ctcols[i], _indexedColorMap);
  55. }
  56. return c;
  57. }
  58. public void setColors(Color[] colors) {
  59. CTColor[] ctcols = new CTColor[colors.length];
  60. for (int i=0; i<colors.length; i++) {
  61. ctcols[i] = ((XSSFColor)colors[i]).getCTColor();
  62. }
  63. _scale.setColorArray(ctcols);
  64. }
  65. public XSSFConditionalFormattingThreshold[] getThresholds() {
  66. CTCfvo[] cfvos = _scale.getCfvoArray();
  67. XSSFConditionalFormattingThreshold[] t =
  68. new XSSFConditionalFormattingThreshold[cfvos.length];
  69. for (int i=0; i<cfvos.length; i++) {
  70. t[i] = new XSSFConditionalFormattingThreshold(cfvos[i]);
  71. }
  72. return t;
  73. }
  74. public void setThresholds(ConditionalFormattingThreshold[] thresholds) {
  75. CTCfvo[] cfvos = new CTCfvo[thresholds.length];
  76. for (int i=0; i<thresholds.length; i++) {
  77. cfvos[i] = ((XSSFConditionalFormattingThreshold)thresholds[i]).getCTCfvo();
  78. }
  79. _scale.setCfvoArray(cfvos);
  80. }
  81. /**
  82. * @return color from scale
  83. */
  84. public XSSFColor createColor() {
  85. return new XSSFColor(_scale.addNewColor(), _indexedColorMap);
  86. }
  87. public XSSFConditionalFormattingThreshold createThreshold() {
  88. return new XSSFConditionalFormattingThreshold(_scale.addNewCfvo());
  89. }
  90. }