001    package net.databinder.components;
002    
003    import java.awt.Color;
004    import java.awt.Font;
005    import java.awt.font.TextAttribute;
006    import java.text.AttributedString;
007    
008    import org.apache.wicket.model.IModel;
009    
010    /**
011     * Uses specified fonts for bold and italic text. By default bold and italic fonts will be derived
012     * from the RenderedLabel base font, but if that font is loaded from a resource
013     * these must be as well. Links are attributed as underlined plain-weight text in Color.BLUE.
014     * @author Nathan Hamblen
015     */
016    public class FontFormattedRenderedLabel extends RenderedLabel {
017            private Font italicFont = getFont().deriveFont(Font.ITALIC);
018            private Font boldFont = getFont().deriveFont(Font.BOLD);
019    
020            public FontFormattedRenderedLabel(String id) {
021                    super(id);
022            }
023            
024            public FontFormattedRenderedLabel(String id, IModel model) {
025                    super(id, model);
026            }
027    
028            public FontFormattedRenderedLabel(String id, boolean shareResource) {
029                    super(id, shareResource);
030            }
031            
032            public FontFormattedRenderedLabel(String id, IModel model, boolean shareResource) {
033                    super(id, model, shareResource);
034            }
035            
036            public static void loadSharedResources(String text, Font font, Font boldFont, Font italicFont, Color color, Color backgroundColor, Integer maxWidth) {
037                    loadSharedResources(new FontFormattedRenderedImageResource(), text, font, boldFont, italicFont, color, backgroundColor, maxWidth);
038            }
039    
040            protected static void loadSharedResources(FontFormattedRenderedImageResource res, String text, Font font, Font boldFont, Font italicFont, Color color, Color backgroundColor, Integer maxWidth) {
041                    res.boldFont = boldFont;
042                    res.italicFont = italicFont;
043                    RenderedLabel.loadSharedResources(res, text, font, color, backgroundColor, maxWidth);
044            }
045            
046            protected FontFormattedRenderedImageResource newRenderedTextImageResource(boolean isShared) {
047                    FontFormattedRenderedImageResource res = new FontFormattedRenderedImageResource();
048                    res.setCacheable(isShared);
049                    res.setState(this);
050                    return res;
051            }
052    
053    
054    
055            protected static class FontFormattedRenderedImageResource extends FormattedRenderedTextImageResource {
056                    protected Font boldFont, italicFont;
057    
058                    @Override
059                    public void setState(RenderedLabel label) {
060                            FontFormattedRenderedLabel ffLabel = (FontFormattedRenderedLabel) label;
061                            boldFont = ffLabel.getBoldFont();
062                            italicFont = ffLabel.getItalicFont();
063                            super.setState(label);
064                    }
065            
066                    @Override
067                    void attributeBold(AttributedString string, int start, int end) {
068                            string.addAttribute(TextAttribute.FONT, boldFont, start, end);
069                    }
070                    @Override
071                    void attributeItalic(AttributedString string, int start, int end) {
072                            string.addAttribute(TextAttribute.FONT, italicFont, start, end);
073                    }
074                    /** Renders as underlined plain-weight text in Color.BLUE; override for other attributes. */
075                    @Override
076                    void attributeLink(AttributedString string, int start, int end) {
077                            string.addAttribute(TextAttribute.UNDERLINE,TextAttribute.UNDERLINE_ON, start, end); 
078                            string.addAttribute(TextAttribute.FOREGROUND,Color.BLUE, start, end); 
079                    }
080            }
081            
082            public Font getItalicFont() {
083                    return italicFont;
084            }
085    
086            public void setItalicFont(Font italicFont) {
087                    this.italicFont = italicFont;
088            }
089    
090            public Font getBoldFont() {
091                    return boldFont;
092            }
093    
094            public void setBoldFont(Font boldFont) {
095                    this.boldFont = boldFont;
096            }
097    }