Example usage for org.apache.poi.ss.usermodel FontFormatting isBold

List of usage examples for org.apache.poi.ss.usermodel FontFormatting isBold

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel FontFormatting isBold.

Prototype

boolean isBold();

Source Link

Document

get whether the font weight is set to bold or not

Usage

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

/**
 * Creates the necessary CSS rules and runs evaluations on all affected
 * cells.//from   w  w  w . j a  va2  s .  c  om
 */
public void createConditionalFormatterRules() {

    // make sure old styles are cleared
    if (cellToIndex != null) {
        for (String key : cellToIndex.keySet()) {
            int col = SpreadsheetUtil.getColumnIndexFromKey(key) - 1;
            int row = SpreadsheetUtil.getRowFromKey(key) - 1;
            Cell cell = spreadsheet.getCell(row, col);
            if (cell != null) {
                spreadsheet.markCellAsUpdated(cell, true);
            }
        }
    }

    cellToIndex.clear();
    topBorders.clear();
    leftBorders.clear();
    spreadsheet.getState().conditionalFormattingStyles = new HashMap<Integer, String>();

    SheetConditionalFormatting cfs = spreadsheet.getActiveSheet().getSheetConditionalFormatting();

    if (cfs instanceof HSSFSheetConditionalFormatting) {
        // disable formatting for HSSF, since formulas are read incorrectly
        // and we would return incorrect results.
        return;
    }

    for (int i = 0; i < cfs.getNumConditionalFormattings(); i++) {
        ConditionalFormatting cf = cfs.getConditionalFormattingAt(i);

        List<XSSFConditionalFormattingRule> cfRuleList = getOrderedRuleList(cf);

        // rules are listen bottom up, but we want top down so that we can
        // stop when we need to. Rule indexes follow original order, because
        // that is the order CSS is applied on client side.
        for (int ruleIndex = cf.getNumberOfRules() - 1; ruleIndex >= 0; ruleIndex--) {

            ConditionalFormattingRule rule = cfRuleList.get(ruleIndex);

            // first formatting object gets 0-999, second 1000-1999...
            // should be enough.
            int cssIndex = i * 1000000 + ruleIndex * 1000;

            // build style

            // TODO: some of this code will override all old values on each
            // iteration. POI API will return the default value for nulls,
            // which is not what we want.

            StringBuilder css = new StringBuilder();

            FontFormatting fontFormatting = rule.getFontFormatting();

            if (fontFormatting != null) {
                String fontColorCSS = colorConverter.getFontColorCSS(rule);
                if (fontColorCSS != null) {
                    css.append("color:" + fontColorCSS);
                }

                // we can't have both underline and line-through in the same
                // DIV element, so use the first one that matches.

                // HSSF might return 255 for 'none'...
                if (fontFormatting.getUnderlineType() != FontFormatting.U_NONE
                        && fontFormatting.getUnderlineType() != 255) {
                    css.append("text-decoration: underline;");
                }
                if (hasStrikeThrough(fontFormatting)) {
                    css.append("text-decoration: line-through;");
                }

                if (fontFormatting.getFontHeight() != -1) {
                    // POI returns height in 1/20th points, convert
                    int fontHeight = fontFormatting.getFontHeight() / 20;
                    css.append("font-size:" + fontHeight + "pt;");
                }

                // excel has a setting for bold italic, otherwise bold
                // overrides
                // italic and vice versa
                if (fontFormatting.isItalic() && fontFormatting.isBold()) {
                    css.append("font-style: italic;");
                    css.append("font-weight: bold;");
                } else if (fontFormatting.isItalic()) {
                    css.append("font-style: italic;");
                    css.append("font-weight: initial;");
                } else if (fontFormatting.isBold()) {
                    css.append("font-style: normal;");
                    css.append("font-weight: bold;");
                }
            }

            PatternFormatting patternFormatting = rule.getPatternFormatting();
            if (patternFormatting != null) {
                String colorCSS = colorConverter.getBackgroundColorCSS(rule);

                if (colorCSS != null) {
                    css.append("background-color:" + colorCSS);
                }
            }

            cssIndex = addBorderFormatting(cf, rule, css, cssIndex);

            spreadsheet.getState().conditionalFormattingStyles.put(cssIndex, css.toString());

            // check actual cells
            runCellMatcher(cf, rule, cssIndex);

            // stop here if defined in rules
            if (stopHere(rule)) {
                break;
            }
        }

    }
}