Example usage for javax.swing.text StyleConstants getFontFamily

List of usage examples for javax.swing.text StyleConstants getFontFamily

Introduction

In this page you can find the example usage for javax.swing.text StyleConstants getFontFamily.

Prototype

public static String getFontFamily(AttributeSet a) 

Source Link

Document

Gets the font family setting from the attribute list.

Usage

From source file:FontPicker.java

public void actionPerformed(ActionEvent ae) {
    // Check the name of the font
    if (!StyleConstants.getFontFamily(attributes).equals(fontName.getSelectedItem())) {
        StyleConstants.setFontFamily(attributes, (String) fontName.getSelectedItem());
    }//  ww  w.ja v a 2s.co m
    // Check the font size (no error checking yet)
    if (StyleConstants.getFontSize(attributes) != Integer.parseInt(fontSize.getText())) {
        StyleConstants.setFontSize(attributes, Integer.parseInt(fontSize.getText()));
    }
    // Check to see if the font should be bold
    if (StyleConstants.isBold(attributes) != fontBold.isSelected()) {
        StyleConstants.setBold(attributes, fontBold.isSelected());
    }
    // Check to see if the font should be italic
    if (StyleConstants.isItalic(attributes) != fontItalic.isSelected()) {
        StyleConstants.setItalic(attributes, fontItalic.isSelected());
    }
    // and update our preview label
    updatePreviewFont();
}

From source file:FontPicker.java

protected void updatePreviewFont() {
    String name = StyleConstants.getFontFamily(attributes);
    boolean bold = StyleConstants.isBold(attributes);
    boolean ital = StyleConstants.isItalic(attributes);
    int size = StyleConstants.getFontSize(attributes);

    //Bold and italic don't work properly in beta 4.
    Font f = new Font(name, (bold ? Font.BOLD : 0) + (ital ? Font.ITALIC : 0), size);
    previewLabel.setFont(f);/*from   w w w .ja va2  s.c o  m*/
}

From source file:MainClass.java

public void actionPerformed(ActionEvent e) {
    JTextPane editor = (JTextPane) getEditor(e);
    int p0 = editor.getSelectionStart();
    StyledDocument doc = getStyledDocument(editor);
    Element paragraph = doc.getCharacterElement(p0);
    AttributeSet as = paragraph.getAttributes();

    family = StyleConstants.getFontFamily(as);
    fontSize = StyleConstants.getFontSize(as);

    formatText = new JDialog(new JFrame(), "Font and Size", true);
    formatText.getContentPane().setLayout(new BorderLayout());

    JPanel choosers = new JPanel();
    choosers.setLayout(new GridLayout(2, 1));

    JPanel fontFamilyPanel = new JPanel();
    fontFamilyPanel.add(new JLabel("Font"));

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String[] fontNames = ge.getAvailableFontFamilyNames();

    fontFamilyChooser = new JComboBox();
    for (int i = 0; i < fontNames.length; i++) {
        fontFamilyChooser.addItem(fontNames[i]);
    }//w  w w . j av  a 2 s  .  co m
    fontFamilyChooser.setSelectedItem(family);
    fontFamilyPanel.add(fontFamilyChooser);
    choosers.add(fontFamilyPanel);

    JPanel fontSizePanel = new JPanel();
    fontSizePanel.add(new JLabel("Size"));
    fontSizeChooser = new JComboBox();
    fontSizeChooser.setEditable(true);
    fontSizeChooser.addItem(new Float(4));
    fontSizeChooser.addItem(new Float(8));
    fontSizeChooser.addItem(new Float(12));
    fontSizeChooser.addItem(new Float(16));
    fontSizeChooser.addItem(new Float(20));
    fontSizeChooser.addItem(new Float(24));
    fontSizeChooser.setSelectedItem(new Float(fontSize));
    fontSizePanel.add(fontSizeChooser);
    choosers.add(fontSizePanel);

    JButton ok = new JButton("OK");
    ok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            accept = true;
            formatText.dispose();
            family = (String) fontFamilyChooser.getSelectedItem();
            fontSize = Float.parseFloat(fontSizeChooser.getSelectedItem().toString());
        }
    });

    JButton cancel = new JButton("Cancel");
    cancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            formatText.dispose();
        }
    });

    JPanel buttons = new JPanel();
    buttons.add(ok);
    buttons.add(cancel);
    formatText.getContentPane().add(choosers, BorderLayout.CENTER);
    formatText.getContentPane().add(buttons, BorderLayout.SOUTH);
    formatText.pack();
    formatText.setVisible(true);

    MutableAttributeSet attr = null;
    if (editor != null && accept) {
        attr = new SimpleAttributeSet();
        StyleConstants.setFontFamily(attr, family);
        StyleConstants.setFontSize(attr, (int) fontSize);
        setCharacterAttributes(editor, attr, false);
    }

}

From source file:net.sf.jasperreports.engine.util.JEditorPaneHtmlMarkupProcessor.java

@Override
protected Map<Attribute, Object> getAttributes(AttributeSet attrSet) {
    Map<Attribute, Object> attrMap = new HashMap<Attribute, Object>();
    if (attrSet.isDefined(StyleConstants.FontFamily)) {
        attrMap.put(TextAttribute.FAMILY, StyleConstants.getFontFamily(attrSet));
    }/*w ww. j a v a  2s.  c  o m*/

    if (attrSet.isDefined(StyleConstants.Bold)) {
        attrMap.put(TextAttribute.WEIGHT,
                StyleConstants.isBold(attrSet) ? TextAttribute.WEIGHT_BOLD : TextAttribute.WEIGHT_REGULAR);
    }

    if (attrSet.isDefined(StyleConstants.Italic)) {
        attrMap.put(TextAttribute.POSTURE, StyleConstants.isItalic(attrSet) ? TextAttribute.POSTURE_OBLIQUE
                : TextAttribute.POSTURE_REGULAR);
    }

    if (attrSet.isDefined(StyleConstants.Underline)) {
        attrMap.put(TextAttribute.UNDERLINE,
                StyleConstants.isUnderline(attrSet) ? TextAttribute.UNDERLINE_ON : null);
    }

    if (attrSet.isDefined(StyleConstants.StrikeThrough)) {
        attrMap.put(TextAttribute.STRIKETHROUGH,
                StyleConstants.isStrikeThrough(attrSet) ? TextAttribute.STRIKETHROUGH_ON : null);
    }

    if (attrSet.isDefined(StyleConstants.FontSize)) {
        attrMap.put(TextAttribute.SIZE, StyleConstants.getFontSize(attrSet));
    }

    if (attrSet.isDefined(StyleConstants.Foreground)) {
        attrMap.put(TextAttribute.FOREGROUND, StyleConstants.getForeground(attrSet));
    }

    if (attrSet.isDefined(StyleConstants.Background)) {
        attrMap.put(TextAttribute.BACKGROUND, StyleConstants.getBackground(attrSet));
    }

    //FIXME: why StyleConstants.isSuperscript(attrSet) does return false
    if (attrSet.isDefined(StyleConstants.Superscript) && !StyleConstants.isSubscript(attrSet)) {
        attrMap.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER);
    }

    if (attrSet.isDefined(StyleConstants.Subscript) && StyleConstants.isSubscript(attrSet)) {
        attrMap.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB);
    }

    return attrMap;
}

From source file:pl.otros.logview.gui.message.html.ExportToHtml.java

protected Map<String, String> styleToCssMap(MessageFragmentStyle mfs) {
    Style style = mfs.getStyle();
    HashMap<String, Object> attributes = new HashMap<String, Object>();
    Enumeration<?> attributeNames = style.getAttributeNames();
    while (attributeNames.hasMoreElements()) {
        Object nextElement = attributeNames.nextElement();
        attributes.put(nextElement.toString(), style.getAttribute(nextElement));
    }/*  ww  w.j a  v  a  2  s  . c  o  m*/

    Map<String, String> cssMap = new HashMap<String, String>();

    if (attributes.get("family") != null) {
        cssMap.put("font-family", String.format("'%s'", StyleConstants.getFontFamily(style)));
    }

    if (attributes.get("size") != null) {
        cssMap.put("font-size", Integer.toString(StyleConstants.getFontSize(style)));
    }

    if (attributes.get("foreground") != null) {
        cssMap.put("color", colorToHex(StyleConstants.getForeground(style)));
    }

    if (attributes.get("background") != null) {
        cssMap.put("background-color", colorToHex(StyleConstants.getBackground(style)));
    }

    if (attributes.get("bold") != null) {
        cssMap.put("font-weight", StyleConstants.isBold(style) ? "bold" : "normal");
    }

    if (attributes.get("italic") != null) {
        cssMap.put("font-style", StyleConstants.isItalic(style) ? "italic" : "normal");
    }

    if (attributes.get("underline") != null) {
        cssMap.put("text-decoration", StyleConstants.isItalic(style) ? "underline" : "none");
    }

    return cssMap;
}