Example usage for org.apache.poi.xssf.usermodel XSSFConditionalFormatting getNumberOfRules

List of usage examples for org.apache.poi.xssf.usermodel XSSFConditionalFormatting getNumberOfRules

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFConditionalFormatting getNumberOfRules.

Prototype

@Override
public int getNumberOfRules() 

Source Link

Usage

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

/**
 * Excel uses a field called 'priority' to re-order rules. Just calling
 * {@link XSSFConditionalFormatting#getRule(int)} will result in wrong
 * order. So, instead, get the list and reorder it according to the priority
 * field.//from  w  ww .ja  v  a  2  s . com
 *
 * @return The list of conditional formatting rules in reverse order (same
 *         order Excel processes them).
 */
private List<XSSFConditionalFormattingRule> getOrderedRuleList(ConditionalFormatting cf) {

    // get the list
    XSSFConditionalFormatting xcf = (XSSFConditionalFormatting) cf;
    List<XSSFConditionalFormattingRule> rules = new ArrayList<XSSFConditionalFormattingRule>();
    for (int i = 0; i < xcf.getNumberOfRules(); i++) {
        rules.add(xcf.getRule(i));
    }

    // reorder with hidden field
    Collections.sort(rules, new Comparator<XSSFConditionalFormattingRule>() {

        @Override
        public int compare(XSSFConditionalFormattingRule o1, XSSFConditionalFormattingRule o2) {

            CTCfRule object = (CTCfRule) getFieldValWithReflection(o1, "_cfRule");
            CTCfRule object2 = (CTCfRule) getFieldValWithReflection(o2, "_cfRule");

            if (object != null && object2 != null) {
                // reverse order
                return object2.getPriority() - object.getPriority();
            }

            return 0;
        }
    });

    return rules;
}