Example usage for javax.swing.text StyleConstants isBold

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

Introduction

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

Prototype

public static boolean isBold(AttributeSet a) 

Source Link

Document

Checks whether the bold attribute is set.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JTextPane textPane = new JTextPane();

    JButton button = new JButton("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel.setLayout(new BorderLayout());
    panel.setPreferredSize(new Dimension(200, 200));

    panel.add(textPane, BorderLayout.CENTER);
    panel.add(button, BorderLayout.SOUTH);
    textPane.addStyle("myStyle", null);

    button.addActionListener(e -> {//from  w  ww  . j  a  v a  2  s  . co m
        StyledDocument doc = textPane.getStyledDocument();
        int start = textPane.getSelectionStart();
        int end = textPane.getSelectionEnd();
        if (start == end) {
            return;
        }
        if (start > end) {
            int life = start;
            start = end;
            end = life;
        }
        Style style = textPane.getStyle("myStyle");

        if (StyleConstants.isBold(style)) {
            StyleConstants.setBold(style, false);
        } else {
            StyleConstants.setBold(style, true);
        }
        doc.setCharacterAttributes(start, end - start, style, false);
    });

    frame.add(panel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

private boolean isInputAttributeBold() {
    AttributeSet attSet = ((HTMLEditorKit) jep.getEditorKit()).getInputAttributes();
    return StyleConstants.isBold(attSet);
}

From source file:Main.java

private void changeStyle() {
    StyledDocument doc = (StyledDocument) textPane.getDocument();
    int selectionEnd = textPane.getSelectionEnd();
    int selectionStart = textPane.getSelectionStart();
    if (selectionStart == selectionEnd) {
        return;//from  ww  w .  j  a  v a  2  s.  co  m
    }
    Element element = doc.getCharacterElement(selectionStart);
    AttributeSet as = element.getAttributes();

    MutableAttributeSet asNew = new SimpleAttributeSet(as.copyAttributes());
    StyleConstants.setBold(asNew, !StyleConstants.isBold(as));
    doc.setCharacterAttributes(selectionStart, textPane.getSelectedText().length(), asNew, true);
    String text = (StyleConstants.isBold(as) ? "Cancel Bold" : "Bold");
    btnStyle.setText(text);
}

From source file:MainClass.java

public void actionPerformed(ActionEvent e) {
    JEditorPane editor = getEditor(e);
    if (editor != null) {
        StyledEditorKit kit = getStyledEditorKit(editor);
        MutableAttributeSet attr = kit.getInputAttributes();
        boolean bold = (StyleConstants.isBold(attr)) ? false : true;
        SimpleAttributeSet sas = new SimpleAttributeSet();
        StyleConstants.setBold(sas, bold);
        setCharacterAttributes(editor, sas, false);

    }//from w w w  .j av  a2s . c o  m
}

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());
    }/*from w ww  .j  ava2s.c  o 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);/*w w  w.  j a v a  2 s  .  com*/
}

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));
    }//from w  w  w  .  jav  a2 s  . 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:net.java.sip.communicator.impl.gui.main.chat.ChatWritePanel.java

/**
 * Enables the bold style/*ww w  .j  a  va 2  s . c o m*/
 * @param b TRUE enable - FALSE disable
 */
public void setBoldStyleEnable(boolean b) {
    StyledEditorKit editorKit = (StyledEditorKit) editorPane.getEditorKit();
    MutableAttributeSet attr = editorKit.getInputAttributes();

    if (b && !StyleConstants.isBold(attr)) {
        setStyleConstant(new HTMLEditorKit.BoldAction(), StyleConstants.Bold);
    }
}

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));
    }//from  ww  w . j a  va2  s. c om

    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;
}

From source file:pl.otros.logview.gui.message.pattern.PropertyPatternMessageColorizerTest.java

License:asdf

@Test
public void testColorize() throws BadLocationException {
    String message = "a55(a)a";
    Collection<MessageFragmentStyle> colorize = colorizer.colorize(message);
    assertEquals(2, colorize.size());//  w w w  .j  a  v  a 2 s  .  co  m
    ArrayList<MessageFragmentStyle> list = new ArrayList<MessageFragmentStyle>(colorize);
    MessageFragmentStyle messageFragmentStyle = list.get(0);
    MessageFragmentStyle messageFragmentStyle1 = list.get(1);

    assertEquals(messageFragmentStyle.isReplace(), false);
    assertEquals(messageFragmentStyle.getOffset(), 0);
    assertEquals(messageFragmentStyle.getLength(), message.length());
    Style style = messageFragmentStyle.getStyle();
    assertEquals(StyleConstants.getBackground(style), new Color(0, 255, 255));
    assertEquals(StyleConstants.getForeground(style), new Color(255, 255, 0));
    assertEquals((Boolean) StyleConstants.isBold(style), Boolean.FALSE);
    assertEquals((Boolean) StyleConstants.isItalic(style), Boolean.TRUE);

    assertEquals(messageFragmentStyle1.isReplace(), false);
    assertEquals(messageFragmentStyle1.getOffset(), 1);
    assertEquals(messageFragmentStyle1.getLength(), 5);
    Style style1 = messageFragmentStyle1.getStyle();
    assertEquals(StyleConstants.getBackground(style1), new Color(0, 0, 255));
    assertEquals(StyleConstants.getForeground(style1), new Color(255, 0, 0));
    assertEquals((Boolean) StyleConstants.isBold(style1), Boolean.TRUE);
    assertEquals((Boolean) StyleConstants.isItalic(style1), Boolean.FALSE);

}