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.

XSSFPatternFormatting.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.PatternFormatting;
  22. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
  23. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
  24. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
  25. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
  26. /**
  27. * @author Yegor Kozlov
  28. */
  29. public class XSSFPatternFormatting implements PatternFormatting {
  30. IndexedColorMap _colorMap;
  31. CTFill _fill;
  32. XSSFPatternFormatting(CTFill fill, IndexedColorMap colorMap) {
  33. _fill = fill;
  34. _colorMap = colorMap;
  35. }
  36. public XSSFColor getFillBackgroundColorColor() {
  37. if(!_fill.isSetPatternFill()) return null;
  38. return new XSSFColor(_fill.getPatternFill().getBgColor(), _colorMap);
  39. }
  40. public XSSFColor getFillForegroundColorColor() {
  41. if(!_fill.isSetPatternFill() || ! _fill.getPatternFill().isSetFgColor())
  42. return null;
  43. return new XSSFColor(_fill.getPatternFill().getFgColor(), _colorMap);
  44. }
  45. public short getFillPattern() {
  46. if(!_fill.isSetPatternFill() || !_fill.getPatternFill().isSetPatternType()) return NO_FILL;
  47. return (short)(_fill.getPatternFill().getPatternType().intValue() - 1);
  48. }
  49. public short getFillBackgroundColor() {
  50. XSSFColor color = getFillBackgroundColorColor();
  51. if (color == null) return 0;
  52. return color.getIndexed();
  53. }
  54. public short getFillForegroundColor() {
  55. XSSFColor color = getFillForegroundColorColor();
  56. if (color == null) return 0;
  57. return color.getIndexed();
  58. }
  59. public void setFillBackgroundColor(Color bg) {
  60. XSSFColor xcolor = XSSFColor.toXSSFColor(bg);
  61. if (xcolor == null) setFillBackgroundColor((CTColor)null);
  62. else setFillBackgroundColor(xcolor.getCTColor());
  63. }
  64. public void setFillBackgroundColor(short bg) {
  65. CTColor bgColor = CTColor.Factory.newInstance();
  66. bgColor.setIndexed(bg);
  67. setFillBackgroundColor(bgColor);
  68. }
  69. private void setFillBackgroundColor(CTColor color) {
  70. CTPatternFill ptrn = _fill.isSetPatternFill() ? _fill.getPatternFill() : _fill.addNewPatternFill();
  71. if (color == null) {
  72. ptrn.unsetBgColor();
  73. } else {
  74. ptrn.setBgColor(color);
  75. }
  76. }
  77. public void setFillForegroundColor(Color fg) {
  78. XSSFColor xcolor = XSSFColor.toXSSFColor(fg);
  79. if (xcolor == null) setFillForegroundColor((CTColor)null);
  80. else setFillForegroundColor(xcolor.getCTColor());
  81. }
  82. public void setFillForegroundColor(short fg) {
  83. CTColor fgColor = CTColor.Factory.newInstance();
  84. fgColor.setIndexed(fg);
  85. setFillForegroundColor(fgColor);
  86. }
  87. private void setFillForegroundColor(CTColor color) {
  88. CTPatternFill ptrn = _fill.isSetPatternFill() ? _fill.getPatternFill() : _fill.addNewPatternFill();
  89. if (color == null) {
  90. ptrn.unsetFgColor();
  91. } else {
  92. ptrn.setFgColor(color);
  93. }
  94. }
  95. public void setFillPattern(short fp){
  96. CTPatternFill ptrn = _fill.isSetPatternFill() ? _fill.getPatternFill() : _fill.addNewPatternFill();
  97. if(fp == NO_FILL) ptrn.unsetPatternType();
  98. else ptrn.setPatternType(STPatternType.Enum.forInt(fp + 1));
  99. }
  100. }