Example usage for org.apache.poi.ss.util CellRangeUtil INSIDE

List of usage examples for org.apache.poi.ss.util CellRangeUtil INSIDE

Introduction

In this page you can find the example usage for org.apache.poi.ss.util CellRangeUtil INSIDE.

Prototype

int INSIDE

To view the source code for org.apache.poi.ss.util CellRangeUtil INSIDE.

Click Source Link

Document

first range is within the second range

Usage

From source file:com.vaadin.addon.spreadsheet.Spreadsheet.java

License:Open Source License

/**
 * Merges the given cells. See/*from  w  w w  .  ja v a 2 s . c om*/
 * {@link Sheet#addMergedRegion(CellRangeAddress)}.
 * <p>
 * If another existing merged region is completely inside the given range,
 * it is removed. If another existing region either encloses or overlaps the
 * given range, an error is thrown. See
 * {@link CellRangeUtil#intersect(CellRangeAddress, CellRangeAddress)}.
 * <p>
 * Note: POI doesn't seem to update the cells that are "removed" due to the
 * merge - the values for those cells still exist and continue being used in
 * possible formulas. If you need to make sure those values are removed,
 * just delete the cells before creating the merged region.
 * <p>
 * If the added region affects the currently selected cell, a new
 * {@link SelectionChangeEvent} is fired.
 * 
 * @param region
 *            The range of cells to merge
 * @throws IllegalArgumentException
 *             If the given region overlaps with or encloses another
 *             existing region within the sheet.
 */
public void addMergedRegion(CellRangeAddress region) throws IllegalArgumentException {
    final Sheet sheet = getActiveSheet();
    // need to check if there are merged regions already inside the given
    // range, otherwise very bad inconsistencies appear.
    int index = 0;
    while (index < sheet.getNumMergedRegions()) {
        CellRangeAddress existingRegion = sheet.getMergedRegion(index);
        int intersect = CellRangeUtil.intersect(region, existingRegion);
        if (intersect == CellRangeUtil.INSIDE) {
            deleteMergedRegion(index);
        } else if (intersect == CellRangeUtil.OVERLAP || intersect == CellRangeUtil.ENCLOSES) {
            throw new IllegalArgumentException("An existing region " + existingRegion + " "
                    + (intersect == CellRangeUtil.OVERLAP ? "overlaps " : "encloses ") + "the given region "
                    + region);
        } else {
            index++;
        }
    }
    createMergedRegionIntoSheet(region);
    selectionManager.mergedRegionAdded(region);
}