Example usage for java.text AttributedString addAttribute

List of usage examples for java.text AttributedString addAttribute

Introduction

In this page you can find the example usage for java.text AttributedString addAttribute.

Prototype

public void addAttribute(Attribute attribute, Object value, int beginIndex, int endIndex) 

Source Link

Document

Adds an attribute to a subrange of the string.

Usage

From source file:net.sf.jasperreports.engine.export.HtmlExporter.java

private void addSearchAttributes(JRStyledText styledText, JRPrintText textElement) {
    ReportContext reportContext = getReportContext();
    if (reportContext != null) {
        SpansInfo spansInfo = (SpansInfo) reportContext
                .getParameterValue("net.sf.jasperreports.search.term.highlighter");
        PrintElementId pei = PrintElementId.forElement(textElement);

        if (spansInfo != null && spansInfo.hasHitTermsInfo(pei.toString())) {
            List<HitTermInfo> hitTermInfos = JRCloneUtils.cloneList(spansInfo.getHitTermsInfo(pei.toString()));

            short[] lineBreakOffsets = textElement.getLineBreakOffsets();
            if (lineBreakOffsets != null && lineBreakOffsets.length > 0) {
                int sz = lineBreakOffsets.length;
                for (HitTermInfo ti : hitTermInfos) {
                    for (int i = 0; i < sz; i++) {
                        if (lineBreakOffsets[i] <= ti.getStart()) {
                            ti.setStart(ti.getStart() + 1);
                            ti.setEnd(ti.getEnd() + 1);
                        } else {
                            break;
                        }/*from   www  . j a v  a 2s .c o m*/
                    }
                }
            }

            AttributedString attributedString = styledText.getAttributedString();
            for (int i = 0, ln = hitTermInfos.size(); i < ln; i = i + spansInfo.getTermsPerQuery()) {
                attributedString.addAttribute(JRTextAttribute.SEARCH_HIGHLIGHT, Color.yellow,
                        hitTermInfos.get(i).getStart(),
                        hitTermInfos.get(i + spansInfo.getTermsPerQuery() - 1).getEnd());
            }
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("No ReportContext to hold search data!");
        }
    }
}

From source file:org.gitools.ui.app.heatmap.drawer.AbstractHeatmapDrawer.java

protected static void paintCell(Decoration decoration, Color gridColor, int gridSize, int offsetX, int offsetY,
        int width, int height, Graphics2D g, Rectangle box) {

    int y = box.y + offsetY;
    int x = box.x + offsetX;

    g.setColor(decoration.getBgColor());
    g.fillRect(x, y, width, height);//from w ww.ja  v a2s  .  c  o  m

    g.setColor(gridColor);
    g.fillRect(x, y + height, width, gridSize);

    String text = decoration.getFormatedValue();
    if (!StringUtils.isEmpty(text)) {

        Font font = g.getFont();

        boolean isRotated = decoration.isRotate();

        int fontHeight = (int) font.getSize2D();

        if (fontHeight <= (isRotated ? width : height)) {

            int textWidth = (int) g.getFontMetrics().getStringBounds(text, g).getWidth();
            //TODO: textWidth depends on SuperScript

            if (textWidth < (isRotated ? height : width)) {

                int leftMargin = ((width - textWidth) / 2) + 1;
                int bottomMargin = ((height - fontHeight) / 2) + 1;

                if (isRotated) {
                    leftMargin = ((width - fontHeight) / 2) + 1;
                    bottomMargin = height - (((height - textWidth) / 2));
                }

                g.setColor(Colors.bestForegroundColor(decoration.getBgColor()));
                if (text.matches("[0-9\\.]+e-?[0-9]+")) {
                    int e_pos = text.indexOf("e") + 3;
                    text = text.replaceAll("e(-?[0-9]+)", "10$1");
                    int superscriptEnd = text.length();
                    AttributedString attText = new AttributedString(text);
                    attText.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, e_pos,
                            superscriptEnd);
                    if (isRotated) {
                        g.rotate(radianAngle90);
                        g.drawString(attText.getIterator(), y + height - bottomMargin, -x - leftMargin - 1);
                        g.rotate(-radianAngle90);
                    } else {
                        g.drawString(attText.getIterator(), x + leftMargin, y + height - bottomMargin);
                    }
                } else {

                    if (isRotated) {
                        g.rotate(radianAngle90);
                        g.drawString(text, y + height - bottomMargin, -x - leftMargin - 1);

                        if ("CoCA-08".equals(text)) {
                            System.out.println("x = " + x + " leftMargin = " + leftMargin + " width = " + width
                                    + " fontHeight = " + fontHeight);
                        }

                        g.rotate(-radianAngle90);
                    } else {
                        g.drawString(text, x + leftMargin, y + height - bottomMargin);
                    }
                }
            }
        }
    }

}

From source file:org.zkoss.poi.ss.util.SheetUtil.java

/**
 * Copy text attributes from the supplied Font to Java2D AttributedString
 *//*from  ww  w  .  j  av a  2 s  . c om*/
private static void copyAttributes(Font font, AttributedString str, int startIdx, int endIdx) {
    str.addAttribute(TextAttribute.FAMILY, font.getFontName(), startIdx, endIdx);
    str.addAttribute(TextAttribute.SIZE, (float) font.getFontHeightInPoints());
    if (font.getBoldweight() == Font.BOLDWEIGHT_BOLD)
        str.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, startIdx, endIdx);
    if (font.getItalic())
        str.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, startIdx, endIdx);
    if (font.getUnderline() == Font.U_SINGLE)
        str.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, startIdx, endIdx);
}