You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by jm...@apache.org on 2016/10/05 02:23:52 UTC
svn commit: r1763338 [1/2] - in /poi:
site/src/documentation/content/xdocs/spreadsheet/
trunk/src/examples/src/org/apache/poi/ss/examples/
trunk/src/java/org/apache/poi/ss/usermodel/
trunk/src/java/org/apache/poi/ss/util/ trunk/src/ooxml/testcases/org/...
Author: jmarkmurphy
Date: Wed Oct 5 02:23:52 2016
New Revision: 1763338
URL: http://svn.apache.org/viewvc?rev=1763338&view=rev
Log:
Add PropertyTemplate for drawing Borders (58787)
Added:
poi/trunk/src/examples/src/org/apache/poi/ss/examples/DrawingBorders.java (with props)
poi/trunk/src/java/org/apache/poi/ss/usermodel/BorderExtent.java (with props)
poi/trunk/src/java/org/apache/poi/ss/util/PropertyTemplate.java (with props)
poi/trunk/src/ooxml/testcases/org/apache/poi/ss/util/TestXSSFPropertyTemplate.java (with props)
poi/trunk/src/testcases/org/apache/poi/ss/util/TestPropertyTemplate.java (with props)
Modified:
poi/site/src/documentation/content/xdocs/spreadsheet/quick-guide.xml
poi/trunk/src/java/org/apache/poi/ss/util/CellAddress.java
Modified: poi/site/src/documentation/content/xdocs/spreadsheet/quick-guide.xml
URL: http://svn.apache.org/viewvc/poi/site/src/documentation/content/xdocs/spreadsheet/quick-guide.xml?rev=1763338&r1=1763337&r2=1763338&view=diff
==============================================================================
--- poi/site/src/documentation/content/xdocs/spreadsheet/quick-guide.xml (original)
+++ poi/site/src/documentation/content/xdocs/spreadsheet/quick-guide.xml Wed Oct 5 02:23:52 2016
@@ -75,6 +75,7 @@
<li><link href="#ConditionalFormatting">Conditional Formatting</link></li>
<li><link href="#Hiding">Hiding and Un-Hiding Rows</link></li>
<li><link href="#CellProperties">Setting Cell Properties</link></li>
+ <li><link href="#DrawingBorders">Drawing Borders</link></li>
<li><link href="#PivotTable">Create a Pivot Table</link></li>
<li><link href="#RichText">Cells with multiple styles</link></li>
</ul>
@@ -2213,6 +2214,82 @@ the data to populate another drop down l
exist, it is added. This method will not remove CellStyle properties.
</p>
</section>
+ <anchor id="DrawingBorders"/>
+ <section>
+ <title>Drawing Borders</title>
+ <p>
+ In Excel, you can apply a set of borders on an entire workbook region at the press of a button. The PropertyTemplate
+ object simulates this with methods and constants defined to allow drawing top, bottom, left, right, horizontal,
+ vertical, inside, outside, or all borders around a range of cells. Additional methods allow for applying colors
+ to the borders.
+ </p>
+ <p>
+ It works like this: you create a PropertyTemplate object which is a container for the borders you wish to apply to a
+ sheet. Then you add borders and colors to the PropertyTemplate, and finally apply it to whichever sheets you need
+ that set of borders on. You can create multiple PropertyTemplate objects and apply them to a single sheet, or you can
+ apply the same PropertyTemplate object to multiple sheets. It is just like a preprinted form.
+ </p>
+ <p>
+ Enums:
+ </p>
+ <dl>
+ <dt>BorderStyle</dt>
+ <dd>
+ Defines the look of the border, is it thick or thin, solid or dashed, single or double.
+ This enum replaces the CellStyle.BORDER_XXXXX constants which have been deprecated. The PropertyTemplate will not
+ support the older style BORDER_XXXXX constants. A special value of BorderStyle.NONE will remove the border from
+ a Cell once it is applied.
+ </dd>
+ <dt>BorderExtent</dt>
+ <dd>
+ Describes the portion of the region that the BorderStyle will apply to. For example, TOP, BOTTOM, INSIDE, or OUTSIDE.
+ A special value of BorderExtent.NONE will remove the border from the PropertyTemplate. When the template is applied,
+ no change will be made to a cell border where no border properties exist in the PropertyTemplate.
+ </dd>
+ </dl>
+ <source>
+ // draw borders (three 3x3 grids)
+ PropertyTemplate pt = new PropertyTemplate();
+ // #1) these borders will all be medium in default color
+ pt.drawBorders(new CellRangeAddress(1, 3, 1, 3),
+ BorderStyle.MEDIUM, PropertyTemplate.Extent.ALL);
+ // #2) these cells will have medium outside borders and thin inside borders
+ pt.drawBorders(new CellRangeAddress(5, 7, 1, 3),
+ BorderStyle.MEDIUM, PropertyTemplate.Extent.OUTSIDE);
+ pt.drawBorders(new CellRangeAddress(5, 7, 1, 3), BorderStyle.THIN,
+ PropertyTemplate.Extent.INSIDE);
+ // #3) these cells will all be medium weight with different colors for the
+ // outside, inside horizontal, and inside vertical borders. The center
+ // cell will have no borders.
+ pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),
+ BorderStyle.MEDIUM, IndexedColors.RED.getIndex(),
+ PropertyTemplate.Extent.OUTSIDE);
+ pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),
+ BorderStyle.MEDIUM, IndexedColors.BLUE.getIndex(),
+ PropertyTemplate.Extent.INSIDE_VERTICAL);
+ pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),
+ BorderStyle.MEDIUM, IndexedColors.GREEN.getIndex(),
+ PropertyTemplate.Extent.INSIDE_HORIZONTAL);
+ pt.drawBorders(new CellRangeAddress(10, 10, 2, 2),
+ BorderStyle.NONE,
+ PropertyTemplate.Extent.ALL);
+
+ // apply borders to sheet
+ Workbook wb = new XSSFWorkbook();
+ Sheet sh = wb.createSheet("Sheet1");
+ pt.applyBorders(sh);
+ </source>
+ <p>
+ NOTE: The last pt.drawBorders() call removes the borders from the range by using BorderStyle.NONE. Like
+ setCellStyleProperties, the applyBorders method merges the properties of a cell style, so existing borders
+ are changed only if they are replaced by something else, or removed only if they are replaced by
+ BorderStyle.NONE. To remove a color from a border, use IndexedColor.AUTOMATIC.getIndex().
+ </p>
+ <p>Additionally, to remove a border or color from the PropertyTemplate object, use Extent.NONE.</p>
+ <p>
+ This does not work with diagonal borders yet.
+ </p>
+ </section>
<anchor id="PivotTable"/>
<section><title>Creating a Pivot Table</title>
<p>
Added: poi/trunk/src/examples/src/org/apache/poi/ss/examples/DrawingBorders.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/ss/examples/DrawingBorders.java?rev=1763338&view=auto
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/ss/examples/DrawingBorders.java (added)
+++ poi/trunk/src/examples/src/org/apache/poi/ss/examples/DrawingBorders.java Wed Oct 5 02:23:52 2016
@@ -0,0 +1,112 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ */
+
+package org.apache.poi.ss.examples;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.BorderExtent;
+import org.apache.poi.ss.usermodel.BorderStyle;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.ss.util.PropertyTemplate;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+/**
+ * Excel Border Drawing - examples
+ *
+ * <p>
+ * Partly based on the code snippets from
+ * org.apache.poi.ss.examples.ConditionalFormats
+ * </p>
+ */
+public class DrawingBorders {
+
+ public static void main(String[] args) throws IOException {
+ Workbook wb;
+
+ if (args.length > 0 && args[0].equals("-xls")) {
+ wb = new HSSFWorkbook();
+ } else {
+ wb = new XSSFWorkbook();
+ }
+
+ // add a sheet, and put some values into it
+ Sheet sh1 = wb.createSheet("Sheet1");
+ Row r = sh1.createRow(0);
+ Cell c = r.createCell(1);
+ c.setCellValue("All Borders Medium Width");
+ r = sh1.createRow(4);
+ c = r.createCell(1);
+ c.setCellValue("Medium Outside / Thin Inside Borders");
+ r = sh1.createRow(8);
+ c = r.createCell(1);
+ c.setCellValue("Colored Borders");
+
+ // draw borders (three 3x3 grids)
+ PropertyTemplate pt = new PropertyTemplate();
+ // #1) these borders will all be medium in default color
+ pt.drawBorders(new CellRangeAddress(1, 3, 1, 3),
+ BorderStyle.MEDIUM, BorderExtent.ALL);
+ // #2) these cells will have medium outside borders and thin inside borders
+ pt.drawBorders(new CellRangeAddress(5, 7, 1, 3),
+ BorderStyle.MEDIUM, BorderExtent.OUTSIDE);
+ pt.drawBorders(new CellRangeAddress(5, 7, 1, 3), BorderStyle.THIN,
+ BorderExtent.INSIDE);
+ // #3) these cells will all be medium weight with different colors for the
+ // outside, inside horizontal, and inside vertical borders. The center
+ // cell will have no borders.
+ pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),
+ BorderStyle.MEDIUM, IndexedColors.RED.getIndex(),
+ BorderExtent.OUTSIDE);
+ pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),
+ BorderStyle.MEDIUM, IndexedColors.BLUE.getIndex(),
+ BorderExtent.INSIDE_VERTICAL);
+ pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),
+ BorderStyle.MEDIUM, IndexedColors.GREEN.getIndex(),
+ BorderExtent.INSIDE_HORIZONTAL);
+ pt.drawBorders(new CellRangeAddress(10, 10, 2, 2),
+ BorderStyle.NONE,
+ BorderExtent.ALL);
+
+ // apply borders to sheet
+ pt.applyBorders(sh1);
+
+ // add another sheet and apply the borders to it
+ Sheet sh2 = wb.createSheet("Sheet2");
+ pt.applyBorders(sh2);
+
+ // Write the output to a file
+ String file = "db-poi.xls";
+ if (wb instanceof XSSFWorkbook)
+ file += "x";
+ FileOutputStream out = new FileOutputStream(file);
+ wb.write(out);
+ out.close();
+ wb.close();
+ System.out.println("Generated: " + file);
+ }
+
+}
Propchange: poi/trunk/src/examples/src/org/apache/poi/ss/examples/DrawingBorders.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: poi/trunk/src/examples/src/org/apache/poi/ss/examples/DrawingBorders.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: poi/trunk/src/java/org/apache/poi/ss/usermodel/BorderExtent.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/BorderExtent.java?rev=1763338&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/BorderExtent.java (added)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/BorderExtent.java Wed Oct 5 02:23:52 2016
@@ -0,0 +1,105 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.usermodel;
+
+/**
+ * The enumeration value indicating which borders to draw in a Property Template
+ */
+public enum BorderExtent {
+ /**
+ * No properties defined. This can be used to remove existing properties
+ * from the PropertyTemplate.
+ */
+ NONE,
+
+ /**
+ * All borders, that is top, bottom, left and right, including interior
+ * borders for the range. Does not include diagonals which are different
+ * and not implemented here.
+ */
+ ALL,
+
+ /**
+ * All inside borders. This is top, bottom, left, and right borders, but
+ * restricted to the interior borders for the range. For a range of one
+ * cell, this will produce no borders.
+ */
+ INSIDE,
+
+ /**
+ * All outside borders. That is top, bottom, left and right borders that
+ * bound the range only.
+ */
+ OUTSIDE,
+
+ /**
+ * This is just the top border for the range. No interior borders will
+ * be produced.
+ */
+ TOP,
+
+ /**
+ * This is just the bottom border for the range. No interior borders
+ * will be produced.
+ */
+ BOTTOM,
+
+ /**
+ * This is just the left border for the range, no interior borders will
+ * be produced.
+ */
+ LEFT,
+
+ /**
+ * This is just the right border for the range, no interior borders will
+ * be produced.
+ */
+ RIGHT,
+
+ /**
+ * This is all horizontal borders for the range, including interior and
+ * outside borders.
+ */
+ HORIZONTAL,
+
+ /**
+ * This is just the interior horizontal borders for the range.
+ */
+ INSIDE_HORIZONTAL,
+
+ /**
+ * This is just the outside horizontal borders for the range.
+ */
+ OUTSIDE_HORIZONTAL,
+
+ /**
+ * This is all vertical borders for the range, including interior and
+ * outside borders.
+ */
+ VERTICAL,
+
+ /**
+ * This is just the interior vertical borders for the range.
+ */
+ INSIDE_VERTICAL,
+
+ /**
+ * This is just the outside vertical borders for the range.
+ */
+ OUTSIDE_VERTICAL
+}
Propchange: poi/trunk/src/java/org/apache/poi/ss/usermodel/BorderExtent.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: poi/trunk/src/java/org/apache/poi/ss/usermodel/BorderExtent.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: poi/trunk/src/java/org/apache/poi/ss/util/CellAddress.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/util/CellAddress.java?rev=1763338&r1=1763337&r2=1763338&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/util/CellAddress.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/util/CellAddress.java Wed Oct 5 02:23:52 2016
@@ -88,6 +88,15 @@ public class CellAddress implements Comp
}
/**
+ * Create a new CellAddress object
+ *
+ * @param address a CellAddress
+ */
+ public CellAddress(CellAddress address) {
+ this(address.getRow(), address.getColumn());
+ }
+
+ /**
* Create a new CellAddress object.
*
* @param cell the Cell to get the location of
Added: poi/trunk/src/java/org/apache/poi/ss/util/PropertyTemplate.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/util/PropertyTemplate.java?rev=1763338&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/util/PropertyTemplate.java (added)
+++ poi/trunk/src/java/org/apache/poi/ss/util/PropertyTemplate.java Wed Oct 5 02:23:52 2016
@@ -0,0 +1,966 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.util;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.SpreadsheetVersion;
+import org.apache.poi.ss.usermodel.BorderExtent;
+import org.apache.poi.ss.usermodel.BorderStyle;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+
+/**
+ * <p>
+ * A {@link PropertyTemplate} is a template that can be applied to any sheet in
+ * a project. It contains all the border type and color attributes needed to
+ * draw all the borders for a single sheet. That template can be applied to any
+ * sheet in any workbook.
+ *
+ * This class requires the full spreadsheet to be in memory so {@link SWorkbook}
+ * Spreadsheets are not supported. The same {@link PropertyTemplate} can,
+ * however, be applied to both
+ * {@link HSSFWorkbook}, and XSSFWorkbook objects if
+ * necessary. Portions of the border that fall outside the max range of the
+ * {@link HSSFWorkbook} sheet are ignored.
+ * </p>
+ *
+ * <p>
+ * This would replace {@link RegionUtil}.
+ * </p>
+ */
+public final class PropertyTemplate {
+
+ /**
+ * This is a list of cell properties for one shot application to a range of
+ * cells at a later time.
+ */
+ private Map<CellAddress, Map<String, Object>> _propertyTemplate;
+
+ /**
+ * Create a PropertyTemplate object
+ */
+ public PropertyTemplate() {
+ _propertyTemplate = new HashMap<CellAddress, Map<String, Object>>();
+ }
+
+ /**
+ * Create a PropertyTemplate object from another PropertyTemplate
+ *
+ * @param template a PropertyTemplate object
+ */
+ public PropertyTemplate(PropertyTemplate template) {
+ this();
+ for(Map.Entry<CellAddress, Map<String, Object>> entry : template.getTemplate().entrySet()) {
+ _propertyTemplate.put(new CellAddress(entry.getKey()), cloneCellProperties(entry.getValue()));
+ }
+ }
+
+ private Map<CellAddress,Map<String, Object>> getTemplate() {
+ return _propertyTemplate;
+ }
+
+ private static Map<String, Object> cloneCellProperties(Map<String, Object> properties) {
+ Map<String, Object> newProperties = new HashMap<String, Object>();
+ for(Map.Entry<String, Object> entry : properties.entrySet()) {
+ newProperties.put(entry.getKey(), entry.getValue());
+ }
+ return newProperties;
+ }
+
+ /**
+ * Draws a group of cell borders for a cell range. The borders are not
+ * applied to the cells at this time, just the template is drawn. To apply
+ * the drawn borders to a sheet, use {@link #applyBorders}.
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. {@link BorderStyle}.
+ * @param extent
+ * - {@link BorderExtent} of the borders to be
+ * applied.
+ */
+ public void drawBorders(CellRangeAddress range, BorderStyle borderType,
+ BorderExtent extent) {
+ switch (extent) {
+ case NONE:
+ removeBorders(range);
+ break;
+ case ALL:
+ drawHorizontalBorders(range, borderType, BorderExtent.ALL);
+ drawVerticalBorders(range, borderType, BorderExtent.ALL);
+ break;
+ case INSIDE:
+ drawHorizontalBorders(range, borderType, BorderExtent.INSIDE);
+ drawVerticalBorders(range, borderType, BorderExtent.INSIDE);
+ break;
+ case OUTSIDE:
+ drawOutsideBorders(range, borderType, BorderExtent.ALL);
+ break;
+ case TOP:
+ drawTopBorder(range, borderType);
+ break;
+ case BOTTOM:
+ drawBottomBorder(range, borderType);
+ break;
+ case LEFT:
+ drawLeftBorder(range, borderType);
+ break;
+ case RIGHT:
+ drawRightBorder(range, borderType);
+ break;
+ case HORIZONTAL:
+ drawHorizontalBorders(range, borderType, BorderExtent.ALL);
+ break;
+ case INSIDE_HORIZONTAL:
+ drawHorizontalBorders(range, borderType, BorderExtent.INSIDE);
+ break;
+ case OUTSIDE_HORIZONTAL:
+ drawOutsideBorders(range, borderType, BorderExtent.HORIZONTAL);
+ break;
+ case VERTICAL:
+ drawVerticalBorders(range, borderType, BorderExtent.ALL);
+ break;
+ case INSIDE_VERTICAL:
+ drawVerticalBorders(range, borderType, BorderExtent.INSIDE);
+ break;
+ case OUTSIDE_VERTICAL:
+ drawOutsideBorders(range, borderType, BorderExtent.VERTICAL);
+ break;
+ }
+ }
+
+ /**
+ * Draws a group of cell borders for a cell range. The borders are not
+ * applied to the cells at this time, just the template is drawn. To apply
+ * the drawn borders to a sheet, use {@link #applyBorders}.
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. {@link BorderStyle}.
+ * @param color
+ * - Color index from {@link IndexedColors} used to draw the
+ * borders.
+ * @param extent
+ * - {@link BorderExtent} of the borders to be
+ * applied.
+ */
+ public void drawBorders(CellRangeAddress range, BorderStyle borderType,
+ short color, BorderExtent extent) {
+ drawBorders(range, borderType, extent);
+ if (borderType != BorderStyle.NONE) {
+ drawBorderColors(range, color, extent);
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the top border for a range of cells
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. {@link BorderStyle}.
+ */
+ private void drawTopBorder(CellRangeAddress range, BorderStyle borderType) {
+ int row = range.getFirstRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstCol; i <= lastCol; i++) {
+ addProperty(row, i, CellUtil.BORDER_TOP, borderType);
+ if (borderType == BorderStyle.NONE && row > 0) {
+ addProperty(row - 1, i, CellUtil.BORDER_BOTTOM, borderType);
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the bottom border for a range of cells
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. {@link BorderStyle}.
+ */
+ private void drawBottomBorder(CellRangeAddress range,
+ BorderStyle borderType) {
+ int row = range.getLastRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstCol; i <= lastCol; i++) {
+ addProperty(row, i, CellUtil.BORDER_BOTTOM, borderType);
+ if (borderType == BorderStyle.NONE
+ && row < SpreadsheetVersion.EXCEL2007.getMaxRows() - 1) {
+ addProperty(row + 1, i, CellUtil.BORDER_TOP, borderType);
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the left border for a range of cells
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. {@link BorderStyle}.
+ */
+ private void drawLeftBorder(CellRangeAddress range,
+ BorderStyle borderType) {
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int col = range.getFirstColumn();
+ for (int i = firstRow; i <= lastRow; i++) {
+ addProperty(i, col, CellUtil.BORDER_LEFT, borderType);
+ if (borderType == BorderStyle.NONE && col > 0) {
+ addProperty(i, col - 1, CellUtil.BORDER_RIGHT, borderType);
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the right border for a range of cells
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. {@link BorderStyle}.
+ */
+ private void drawRightBorder(CellRangeAddress range,
+ BorderStyle borderType) {
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int col = range.getLastColumn();
+ for (int i = firstRow; i <= lastRow; i++) {
+ addProperty(i, col, CellUtil.BORDER_RIGHT, borderType);
+ if (borderType == BorderStyle.NONE
+ && col < SpreadsheetVersion.EXCEL2007.getMaxColumns() - 1) {
+ addProperty(i, col + 1, CellUtil.BORDER_LEFT, borderType);
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the outside borders for a range of cells.
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. {@link BorderStyle}.
+ * @param extent
+ * - {@link BorderExtent} of the borders to be
+ * applied. Valid Values are:
+ * <ul>
+ * <li>BorderExtent.ALL</li>
+ * <li>BorderExtent.HORIZONTAL</li>
+ * <li>BorderExtent.VERTICAL</li>
+ * </ul>
+ */
+ private void drawOutsideBorders(CellRangeAddress range,
+ BorderStyle borderType, BorderExtent extent) {
+ switch (extent) {
+ case ALL:
+ case HORIZONTAL:
+ case VERTICAL:
+ if (extent == BorderExtent.ALL || extent == BorderExtent.HORIZONTAL) {
+ drawTopBorder(range, borderType);
+ drawBottomBorder(range, borderType);
+ }
+ if (extent == BorderExtent.ALL || extent == BorderExtent.VERTICAL) {
+ drawLeftBorder(range, borderType);
+ drawRightBorder(range, borderType);
+ }
+ break;
+ default:
+ throw new IllegalArgumentException(
+ "Unsupported PropertyTemplate.Extent, valid Extents are ALL, HORIZONTAL, and VERTICAL");
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the horizontal borders for a range of cells.
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. {@link BorderStyle}.
+ * @param extent
+ * - {@link BorderExtent} of the borders to be
+ * applied. Valid Values are:
+ * <ul>
+ * <li>BorderExtent.ALL</li>
+ * <li>BorderExtent.INSIDE</li>
+ * </ul>
+ */
+ private void drawHorizontalBorders(CellRangeAddress range,
+ BorderStyle borderType, BorderExtent extent) {
+ switch (extent) {
+ case ALL:
+ case INSIDE:
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstRow; i <= lastRow; i++) {
+ CellRangeAddress row = new CellRangeAddress(i, i, firstCol,
+ lastCol);
+ if (extent == BorderExtent.ALL || i > firstRow) {
+ drawTopBorder(row, borderType);
+ }
+ if (extent == BorderExtent.ALL || i < lastRow) {
+ drawBottomBorder(row, borderType);
+ }
+ }
+ break;
+ default:
+ throw new IllegalArgumentException(
+ "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the vertical borders for a range of cells.
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. {@link BorderStyle}.
+ * @param extent
+ * - {@link BorderExtent} of the borders to be
+ * applied. Valid Values are:
+ * <ul>
+ * <li>BorderExtent.ALL</li>
+ * <li>BorderExtent.INSIDE</li>
+ * </ul>
+ */
+ private void drawVerticalBorders(CellRangeAddress range,
+ BorderStyle borderType, BorderExtent extent) {
+ switch (extent) {
+ case ALL:
+ case INSIDE:
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstCol; i <= lastCol; i++) {
+ CellRangeAddress row = new CellRangeAddress(firstRow, lastRow,
+ i, i);
+ if (extent == BorderExtent.ALL || i > firstCol) {
+ drawLeftBorder(row, borderType);
+ }
+ if (extent == BorderExtent.ALL || i < lastCol) {
+ drawRightBorder(row, borderType);
+ }
+ }
+ break;
+ default:
+ throw new IllegalArgumentException(
+ "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");
+ }
+ }
+
+ /**
+ * Removes all border properties from this {@link PropertyTemplate} for the
+ * specified range.
+ *
+ * @parm range - {@link CellRangeAddress} range of cells to remove borders.
+ */
+ private void removeBorders(CellRangeAddress range) {
+ Set<String> properties = new HashSet<String>();
+ properties.add(CellUtil.BORDER_TOP);
+ properties.add(CellUtil.BORDER_BOTTOM);
+ properties.add(CellUtil.BORDER_LEFT);
+ properties.add(CellUtil.BORDER_RIGHT);
+ for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) {
+ for (int col = range.getFirstColumn(); col <= range
+ .getLastColumn(); col++) {
+ removeProperties(row, col, properties);
+ }
+ }
+ removeBorderColors(range);
+ }
+
+ /**
+ * Applies the drawn borders to a Sheet. The borders that are applied are
+ * the ones that have been drawn by the {@link #drawBorders} and
+ * {@link #drawBorderColors} methods.
+ *
+ * @param sheet
+ * - {@link Sheet} on which to apply borders
+ */
+ public void applyBorders(Sheet sheet) {
+ Workbook wb = sheet.getWorkbook();
+ for (Map.Entry<CellAddress, Map<String, Object>> entry : _propertyTemplate
+ .entrySet()) {
+ CellAddress cellAddress = entry.getKey();
+ if (cellAddress.getRow() < wb.getSpreadsheetVersion().getMaxRows()
+ && cellAddress.getColumn() < wb.getSpreadsheetVersion()
+ .getMaxColumns()) {
+ Map<String, Object> properties = entry.getValue();
+ Row row = CellUtil.getRow(cellAddress.getRow(), sheet);
+ Cell cell = CellUtil.getCell(row, cellAddress.getColumn());
+ CellUtil.setCellStyleProperties(cell, properties);
+ }
+ }
+ }
+
+ /**
+ * Sets the color for a group of cell borders for a cell range. The borders
+ * are not applied to the cells at this time, just the template is drawn. If
+ * the borders do not exist, a BORDER_THIN border is used. To apply the
+ * drawn borders to a sheet, use {@link #applyBorders}.
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which colors are
+ * set.
+ * @param color
+ * - Color index from {@link IndexedColors} used to draw the
+ * borders.
+ * @param extent
+ * - {@link BorderExtent} of the borders for which
+ * colors are set.
+ */
+ public void drawBorderColors(CellRangeAddress range, short color,
+ BorderExtent extent) {
+ switch (extent) {
+ case NONE:
+ removeBorderColors(range);
+ break;
+ case ALL:
+ drawHorizontalBorderColors(range, color, BorderExtent.ALL);
+ drawVerticalBorderColors(range, color, BorderExtent.ALL);
+ break;
+ case INSIDE:
+ drawHorizontalBorderColors(range, color, BorderExtent.INSIDE);
+ drawVerticalBorderColors(range, color, BorderExtent.INSIDE);
+ break;
+ case OUTSIDE:
+ drawOutsideBorderColors(range, color, BorderExtent.ALL);
+ break;
+ case TOP:
+ drawTopBorderColor(range, color);
+ break;
+ case BOTTOM:
+ drawBottomBorderColor(range, color);
+ break;
+ case LEFT:
+ drawLeftBorderColor(range, color);
+ break;
+ case RIGHT:
+ drawRightBorderColor(range, color);
+ break;
+ case HORIZONTAL:
+ drawHorizontalBorderColors(range, color, BorderExtent.ALL);
+ break;
+ case INSIDE_HORIZONTAL:
+ drawHorizontalBorderColors(range, color, BorderExtent.INSIDE);
+ break;
+ case OUTSIDE_HORIZONTAL:
+ drawOutsideBorderColors(range, color, BorderExtent.HORIZONTAL);
+ break;
+ case VERTICAL:
+ drawVerticalBorderColors(range, color, BorderExtent.ALL);
+ break;
+ case INSIDE_VERTICAL:
+ drawVerticalBorderColors(range, color, BorderExtent.INSIDE);
+ break;
+ case OUTSIDE_VERTICAL:
+ drawOutsideBorderColors(range, color, BorderExtent.VERTICAL);
+ break;
+ }
+ }
+
+ /**
+ * <p>
+ * Sets the color of the top border for a range of cells.
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which colors are
+ * set.
+ * @param color
+ * - Color index from {@link IndexedColors} used to draw the
+ * borders.
+ */
+ private void drawTopBorderColor(CellRangeAddress range, short color) {
+ int row = range.getFirstRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstCol; i <= lastCol; i++) {
+ if (getBorderStyle(row, i,
+ CellUtil.BORDER_TOP) == BorderStyle.NONE) {
+ drawTopBorder(new CellRangeAddress(row, row, i, i),
+ BorderStyle.THIN);
+ }
+ addProperty(row, i, CellUtil.TOP_BORDER_COLOR, color);
+ }
+ }
+
+ /**
+ * <p>
+ * Sets the color of the bottom border for a range of cells.
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which colors are
+ * set.
+ * @param color
+ * - Color index from {@link IndexedColors} used to draw the
+ * borders.
+ */
+ private void drawBottomBorderColor(CellRangeAddress range, short color) {
+ int row = range.getLastRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstCol; i <= lastCol; i++) {
+ if (getBorderStyle(row, i,
+ CellUtil.BORDER_BOTTOM) == BorderStyle.NONE) {
+ drawBottomBorder(new CellRangeAddress(row, row, i, i),
+ BorderStyle.THIN);
+ }
+ addProperty(row, i, CellUtil.BOTTOM_BORDER_COLOR, color);
+ }
+ }
+
+ /**
+ * <p>
+ * Sets the color of the left border for a range of cells.
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which colors are
+ * set.
+ * @param color
+ * - Color index from {@link IndexedColors} used to draw the
+ * borders.
+ */
+ private void drawLeftBorderColor(CellRangeAddress range, short color) {
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int col = range.getFirstColumn();
+ for (int i = firstRow; i <= lastRow; i++) {
+ if (getBorderStyle(i, col,
+ CellUtil.BORDER_LEFT) == BorderStyle.NONE) {
+ drawLeftBorder(new CellRangeAddress(i, i, col, col),
+ BorderStyle.THIN);
+ }
+ addProperty(i, col, CellUtil.LEFT_BORDER_COLOR, color);
+ }
+ }
+
+ /**
+ * <p>
+ * Sets the color of the right border for a range of cells. If the border is
+ * not drawn, it defaults to BORDER_THIN
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which colors are
+ * set.
+ * @param color
+ * - Color index from {@link IndexedColors} used to draw the
+ * borders.
+ */
+ private void drawRightBorderColor(CellRangeAddress range, short color) {
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int col = range.getLastColumn();
+ for (int i = firstRow; i <= lastRow; i++) {
+ if (getBorderStyle(i, col,
+ CellUtil.BORDER_RIGHT) == BorderStyle.NONE) {
+ drawRightBorder(new CellRangeAddress(i, i, col, col),
+ BorderStyle.THIN);
+ }
+ addProperty(i, col, CellUtil.RIGHT_BORDER_COLOR, color);
+ }
+ }
+
+ /**
+ * <p>
+ * Sets the color of the outside borders for a range of cells.
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which colors are
+ * set.
+ * @param color
+ * - Color index from {@link IndexedColors} used to draw the
+ * borders.
+ * @param extent
+ * - {@link BorderExtent} of the borders for which
+ * colors are set. Valid Values are:
+ * <ul>
+ * <li>BorderExtent.ALL</li>
+ * <li>BorderExtent.HORIZONTAL</li>
+ * <li>BorderExtent.VERTICAL</li>
+ * </ul>
+ */
+ private void drawOutsideBorderColors(CellRangeAddress range, short color,
+ BorderExtent extent) {
+ switch (extent) {
+ case ALL:
+ case HORIZONTAL:
+ case VERTICAL:
+ if (extent == BorderExtent.ALL || extent == BorderExtent.HORIZONTAL) {
+ drawTopBorderColor(range, color);
+ drawBottomBorderColor(range, color);
+ }
+ if (extent == BorderExtent.ALL || extent == BorderExtent.VERTICAL) {
+ drawLeftBorderColor(range, color);
+ drawRightBorderColor(range, color);
+ }
+ break;
+ default:
+ throw new IllegalArgumentException(
+ "Unsupported PropertyTemplate.Extent, valid Extents are ALL, HORIZONTAL, and VERTICAL");
+ }
+ }
+
+ /**
+ * <p>
+ * Sets the color of the horizontal borders for a range of cells.
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which colors are
+ * set.
+ * @param color
+ * - Color index from {@link IndexedColors} used to draw the
+ * borders.
+ * @param extent
+ * - {@link BorderExtent} of the borders for which
+ * colors are set. Valid Values are:
+ * <ul>
+ * <li>BorderExtent.ALL</li>
+ * <li>BorderExtent.INSIDE</li>
+ * </ul>
+ */
+ private void drawHorizontalBorderColors(CellRangeAddress range, short color,
+ BorderExtent extent) {
+ switch (extent) {
+ case ALL:
+ case INSIDE:
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstRow; i <= lastRow; i++) {
+ CellRangeAddress row = new CellRangeAddress(i, i, firstCol,
+ lastCol);
+ if (extent == BorderExtent.ALL || i > firstRow) {
+ drawTopBorderColor(row, color);
+ }
+ if (extent == BorderExtent.ALL || i < lastRow) {
+ drawBottomBorderColor(row, color);
+ }
+ }
+ break;
+ default:
+ throw new IllegalArgumentException(
+ "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");
+ }
+ }
+
+ /**
+ * <p>
+ * Sets the color of the vertical borders for a range of cells.
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which colors are
+ * set.
+ * @param color
+ * - Color index from {@link IndexedColors} used to draw the
+ * borders.
+ * @param extent
+ * - {@link BorderExtent} of the borders for which
+ * colors are set. Valid Values are:
+ * <ul>
+ * <li>BorderExtent.ALL</li>
+ * <li>BorderExtent.INSIDE</li>
+ * </ul>
+ */
+ private void drawVerticalBorderColors(CellRangeAddress range, short color,
+ BorderExtent extent) {
+ switch (extent) {
+ case ALL:
+ case INSIDE:
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstCol; i <= lastCol; i++) {
+ CellRangeAddress row = new CellRangeAddress(firstRow, lastRow,
+ i, i);
+ if (extent == BorderExtent.ALL || i > firstCol) {
+ drawLeftBorderColor(row, color);
+ }
+ if (extent == BorderExtent.ALL || i < lastCol) {
+ drawRightBorderColor(row, color);
+ }
+ }
+ break;
+ default:
+ throw new IllegalArgumentException(
+ "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");
+ }
+ }
+
+ /**
+ * Removes all border properties from this {@link PropertyTemplate} for the
+ * specified range.
+ *
+ * @parm range - {@link CellRangeAddress} range of cells to remove borders.
+ */
+ private void removeBorderColors(CellRangeAddress range) {
+ Set<String> properties = new HashSet<String>();
+ properties.add(CellUtil.TOP_BORDER_COLOR);
+ properties.add(CellUtil.BOTTOM_BORDER_COLOR);
+ properties.add(CellUtil.LEFT_BORDER_COLOR);
+ properties.add(CellUtil.RIGHT_BORDER_COLOR);
+ for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) {
+ for (int col = range.getFirstColumn(); col <= range
+ .getLastColumn(); col++) {
+ removeProperties(row, col, properties);
+ }
+ }
+ }
+
+ /**
+ * Adds a property to this {@link PropertyTemplate} for a given cell
+ *
+ * @param row
+ * @param col
+ * @param property
+ * @param value
+ */
+ private void addProperty(int row, int col, String property, short value) {
+ addProperty(row, col, property, Short.valueOf(value));
+ }
+
+ /**
+ * Adds a property to this {@link PropertyTemplate} for a given cell
+ *
+ * @param row
+ * @param col
+ * @param property
+ * @param value
+ */
+ private void addProperty(int row, int col, String property, Object value) {
+ CellAddress cell = new CellAddress(row, col);
+ Map<String, Object> cellProperties = _propertyTemplate.get(cell);
+ if (cellProperties == null) {
+ cellProperties = new HashMap<String, Object>();
+ }
+ cellProperties.put(property, value);
+ _propertyTemplate.put(cell, cellProperties);
+ }
+
+ /**
+ * Removes a set of properties from this {@link PropertyTemplate} for a
+ * given cell
+ *
+ * @param row
+ * @param col
+ * @param properties
+ */
+ private void removeProperties(int row, int col, Set<String> properties) {
+ CellAddress cell = new CellAddress(row, col);
+ Map<String, Object> cellProperties = _propertyTemplate.get(cell);
+ if (cellProperties != null) {
+ cellProperties.keySet().removeAll(properties);
+ if (cellProperties.isEmpty()) {
+ _propertyTemplate.remove(cell);
+ } else {
+ _propertyTemplate.put(cell, cellProperties);
+ }
+ }
+ }
+
+ /**
+ * Retrieves the number of borders assigned to a cell
+ *
+ * @param cell
+ */
+ public int getNumBorders(CellAddress cell) {
+ Map<String, Object> cellProperties = _propertyTemplate.get(cell);
+ if (cellProperties == null) {
+ return 0;
+ }
+
+ int count = 0;
+ for (String property : cellProperties.keySet()) {
+ if (property.equals(CellUtil.BORDER_TOP))
+ count += 1;
+ if (property.equals(CellUtil.BORDER_BOTTOM))
+ count += 1;
+ if (property.equals(CellUtil.BORDER_LEFT))
+ count += 1;
+ if (property.equals(CellUtil.BORDER_RIGHT))
+ count += 1;
+ }
+ return count;
+ }
+
+ /**
+ * Retrieves the number of borders assigned to a cell
+ *
+ * @param row
+ * @param col
+ */
+ public int getNumBorders(int row, int col) {
+ return getNumBorders(new CellAddress(row, col));
+ }
+
+ /**
+ * Retrieves the number of border colors assigned to a cell
+ *
+ * @param cell
+ */
+ public int getNumBorderColors(CellAddress cell) {
+ Map<String, Object> cellProperties = _propertyTemplate.get(cell);
+ if (cellProperties == null) {
+ return 0;
+ }
+
+ int count = 0;
+ for (String property : cellProperties.keySet()) {
+ if (property.equals(CellUtil.TOP_BORDER_COLOR))
+ count += 1;
+ if (property.equals(CellUtil.BOTTOM_BORDER_COLOR))
+ count += 1;
+ if (property.equals(CellUtil.LEFT_BORDER_COLOR))
+ count += 1;
+ if (property.equals(CellUtil.RIGHT_BORDER_COLOR))
+ count += 1;
+ }
+ return count;
+ }
+
+ /**
+ * Retrieves the number of border colors assigned to a cell
+ *
+ * @param row
+ * @param col
+ */
+ public int getNumBorderColors(int row, int col) {
+ return getNumBorderColors(new CellAddress(row, col));
+ }
+
+ /**
+ * Retrieves the border style for a given cell
+ *
+ * @param cell
+ * @param property
+ */
+ public BorderStyle getBorderStyle(CellAddress cell, String property) {
+ BorderStyle value = BorderStyle.NONE;
+ Map<String, Object> cellProperties = _propertyTemplate.get(cell);
+ if (cellProperties != null) {
+ Object obj = cellProperties.get(property);
+ if (obj instanceof BorderStyle) {
+ value = (BorderStyle) obj;
+ }
+ }
+ return value;
+ }
+
+ /**
+ * Retrieves the border style for a given cell
+ *
+ * @param row
+ * @param col
+ * @param property
+ */
+ public BorderStyle getBorderStyle(int row, int col, String property) {
+ return getBorderStyle(new CellAddress(row, col), property);
+ }
+
+ /**
+ * Retrieves the border style for a given cell
+ *
+ * @param cell
+ * @param property
+ */
+ public short getTemplateProperty(CellAddress cell, String property) {
+ short value = 0;
+ Map<String, Object> cellProperties = _propertyTemplate.get(cell);
+ if (cellProperties != null) {
+ Object obj = cellProperties.get(property);
+ if (obj != null) {
+ value = getShort(obj);
+ }
+ }
+ return value;
+ }
+
+ /**
+ * Retrieves the border style for a given cell
+ *
+ * @param row
+ * @param col
+ * @param property
+ */
+ public short getTemplateProperty(int row, int col, String property) {
+ return getTemplateProperty(new CellAddress(row, col), property);
+ }
+
+ /**
+ * Converts a Short object to a short value or 0 if the object is not a
+ * Short
+ *
+ * @param value
+ * @return
+ */
+ private static short getShort(Object value) {
+ if (value instanceof Short) {
+ return ((Short) value).shortValue();
+ }
+ return 0;
+ }
+}
\ No newline at end of file
Propchange: poi/trunk/src/java/org/apache/poi/ss/util/PropertyTemplate.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: poi/trunk/src/java/org/apache/poi/ss/util/PropertyTemplate.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: poi/trunk/src/ooxml/testcases/org/apache/poi/ss/util/TestXSSFPropertyTemplate.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/ss/util/TestXSSFPropertyTemplate.java?rev=1763338&view=auto
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/ss/util/TestXSSFPropertyTemplate.java (added)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/ss/util/TestXSSFPropertyTemplate.java Wed Oct 5 02:23:52 2016
@@ -0,0 +1,136 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotSame;
+
+import java.io.IOException;
+
+import org.apache.poi.ss.usermodel.BorderExtent;
+import org.apache.poi.ss.usermodel.BorderStyle;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.junit.Test;
+
+public class TestXSSFPropertyTemplate {
+
+ @Test
+ public void applyBorders() throws IOException {
+ CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2);
+ CellRangeAddress b2 = new CellRangeAddress(1, 1, 1, 1);
+ PropertyTemplate pt = new PropertyTemplate();
+ Workbook wb = new XSSFWorkbook();
+ Sheet sheet = wb.createSheet();
+
+ pt.drawBorders(a1c3, BorderStyle.THIN, IndexedColors.RED.getIndex(), BorderExtent.ALL);
+ pt.applyBorders(sheet);
+
+ for (Row row: sheet) {
+ for (Cell cell: row) {
+ CellStyle cs = cell.getCellStyle();
+ assertEquals(BorderStyle.THIN, cs.getBorderTopEnum());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getTopBorderColor());
+ assertEquals(BorderStyle.THIN, cs.getBorderBottomEnum());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getBottomBorderColor());
+ assertEquals(BorderStyle.THIN, cs.getBorderLeftEnum());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getLeftBorderColor());
+ assertEquals(BorderStyle.THIN, cs.getBorderRightEnum());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getRightBorderColor());
+ }
+ }
+
+ pt.drawBorders(b2, BorderStyle.NONE, BorderExtent.ALL);
+ pt.applyBorders(sheet);
+
+ for (Row row: sheet) {
+ for (Cell cell: row) {
+ CellStyle cs = cell.getCellStyle();
+ if (cell.getColumnIndex() != 1 || row.getRowNum() == 0) {
+ assertEquals(BorderStyle.THIN, cs.getBorderTopEnum());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getTopBorderColor());
+ } else {
+ assertEquals(BorderStyle.NONE, cs.getBorderTopEnum());
+ }
+ if (cell.getColumnIndex() != 1 || row.getRowNum() == 2) {
+ assertEquals(BorderStyle.THIN, cs.getBorderBottomEnum());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getBottomBorderColor());
+ } else {
+ assertEquals(BorderStyle.NONE, cs.getBorderBottomEnum());
+ }
+ if (cell.getColumnIndex() == 0 || row.getRowNum() != 1) {
+ assertEquals(BorderStyle.THIN, cs.getBorderLeftEnum());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getLeftBorderColor());
+ } else {
+ assertEquals(BorderStyle.NONE, cs.getBorderLeftEnum());
+ }
+ if (cell.getColumnIndex() == 2 || row.getRowNum() != 1) {
+ assertEquals(BorderStyle.THIN, cs.getBorderRightEnum());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getRightBorderColor());
+ } else {
+ assertEquals(BorderStyle.NONE, cs.getBorderRightEnum());
+ }
+ }
+ }
+
+ wb.close();
+ }
+
+ @Test
+ public void clonePropertyTemplate() throws IOException {
+ CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2);
+ PropertyTemplate pt = new PropertyTemplate();
+ pt.drawBorders(a1c3, BorderStyle.MEDIUM, IndexedColors.RED.getIndex(), BorderExtent.ALL);
+ PropertyTemplate pt2 = new PropertyTemplate(pt);
+ assertNotSame(pt2, pt);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(4, pt2.getNumBorderColors(i, j));
+ assertEquals(4, pt2.getNumBorderColors(i, j));
+ }
+ }
+
+ CellRangeAddress b2 = new CellRangeAddress(1,1,1,1);
+ pt2.drawBorders(b2, BorderStyle.THIN, BorderExtent.ALL);
+
+ Workbook wb = new XSSFWorkbook();
+ Sheet sheet = wb.createSheet();
+ pt.applyBorders(sheet);
+
+ for (Row row : sheet) {
+ for (Cell cell : row) {
+ CellStyle cs = cell.getCellStyle();
+ assertEquals(BorderStyle.MEDIUM, cs.getBorderTopEnum());
+ assertEquals(BorderStyle.MEDIUM, cs.getBorderBottomEnum());
+ assertEquals(BorderStyle.MEDIUM, cs.getBorderLeftEnum());
+ assertEquals(BorderStyle.MEDIUM, cs.getBorderRightEnum());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getTopBorderColor());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getBottomBorderColor());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getLeftBorderColor());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getRightBorderColor());
+ }
+ }
+
+ wb.close();
+ }
+}
\ No newline at end of file
Propchange: poi/trunk/src/ooxml/testcases/org/apache/poi/ss/util/TestXSSFPropertyTemplate.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: poi/trunk/src/ooxml/testcases/org/apache/poi/ss/util/TestXSSFPropertyTemplate.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org