Search in sources :

Example 26 with TextAttribute

use of java.awt.font.TextAttribute in project Zong by Xenoage.

the class TextLayoutTools method createAttributesMap.

/**
 * Creates a Map with the attributes representing
 * the given style for an AttibutedString.
 */
private static Map<TextAttribute, Object> createAttributesMap(FormattedTextStyle style) {
    Map<TextAttribute, Object> ret = new HashMap<>();
    FontInfo fontInfo = notNull(style.getFont(), FontInfo.defaultValue);
    // font name
    Font font = toAwtFont(fontInfo);
    ret.put(TextAttribute.FAMILY, font.getFamily());
    // font size
    ret.put(TextAttribute.SIZE, font.getSize2D());
    // color
    ret.put(TextAttribute.FOREGROUND, toAwtColor(style.getColor()));
    // bold
    FontStyle fontStyle = fontInfo.getStyle();
    ret.put(TextAttribute.WEIGHT, (fontStyle.isSet(FontStyle.Bold) ? TextAttribute.WEIGHT_BOLD : TextAttribute.WEIGHT_REGULAR));
    // italic
    ret.put(TextAttribute.POSTURE, (fontStyle.isSet(FontStyle.Italic) ? TextAttribute.POSTURE_OBLIQUE : TextAttribute.POSTURE_REGULAR));
    // underline
    ret.put(TextAttribute.UNDERLINE, (fontStyle.isSet(FontStyle.Underline) ? TextAttribute.UNDERLINE_ON : null));
    // superscript
    switch(style.getSuperscript()) {
        case Super:
            ret.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER);
            break;
        case Sub:
            ret.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB);
            break;
        default:
            ret.put(TextAttribute.SUPERSCRIPT, null);
    }
    return ret;
}
Also used : FontStyle(com.xenoage.utils.font.FontStyle) HashMap(java.util.HashMap) TextAttribute(java.awt.font.TextAttribute) FontInfo(com.xenoage.utils.font.FontInfo) AwtFontUtils.toAwtFont(com.xenoage.utils.jse.font.AwtFontUtils.toAwtFont)

Example 27 with TextAttribute

use of java.awt.font.TextAttribute in project jsql-injection by ron190.

the class AbstractRadioLink method setUnderlined.

/**
 * Change font of radio label to underline.
 */
public final void setUnderlined() {
    Font font = this.getFont();
    Map<TextAttribute, Object> attributes = new HashMap<>(font.getAttributes());
    attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DOTTED);
    attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
    this.setFont(font.deriveFont(attributes));
}
Also used : HashMap(java.util.HashMap) TextAttribute(java.awt.font.TextAttribute) Font(java.awt.Font)

Example 28 with TextAttribute

use of java.awt.font.TextAttribute in project cuba by cuba-platform.

the class SwingXTableSettings method loadFontPreferences.

protected void loadFontPreferences(Element element) {
    // load font preferences
    String fontFamily = element.attributeValue("fontFamily");
    String fontSize = element.attributeValue("fontSize");
    String fontStyle = element.attributeValue("fontStyle");
    String fontUnderline = element.attributeValue("fontUnderline");
    if (!StringUtils.isBlank(fontFamily) && !StringUtils.isBlank(fontSize) && !StringUtils.isBlank(fontUnderline) && !StringUtils.isBlank(fontStyle)) {
        try {
            int size = Integer.parseInt(fontSize);
            int style = Integer.parseInt(fontStyle);
            String[] availableFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
            int fontIndex = Arrays.asList(availableFonts).indexOf(fontFamily);
            if (fontIndex < 0) {
                log.debug("Unsupported font family, font settings not loaded");
                return;
            }
            Configuration configuration = AppBeans.get(Configuration.NAME);
            DesktopConfig desktopConfig = configuration.getConfig(DesktopConfig.class);
            int sizeIndex = desktopConfig.getAvailableFontSizes().indexOf(size);
            if (sizeIndex < 0) {
                log.debug("Unsupported font size, font settings not loaded");
                return;
            }
            Boolean underline = BooleanUtils.toBooleanObject(fontUnderline);
            @SuppressWarnings("MagicConstant") Font font = new Font(fontFamily, style, size);
            if (underline != null && Boolean.TRUE.equals(underline)) {
                Map<TextAttribute, Integer> attributes = new HashMap<>();
                attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
                font = font.deriveFont(attributes);
            }
            table.setFont(font);
        } catch (NumberFormatException ex) {
            log.debug("Broken font definition in user setting");
        }
    }
}
Also used : Configuration(com.haulmont.cuba.core.global.Configuration) TextAttribute(java.awt.font.TextAttribute) DesktopConfig(com.haulmont.cuba.desktop.DesktopConfig)

Example 29 with TextAttribute

use of java.awt.font.TextAttribute in project cuba by cuba-platform.

the class FontDialog method initUI.

private void initUI() {
    Configuration configuration = AppBeans.get(Configuration.NAME);
    DesktopConfig desktopConfig = configuration.getConfig(DesktopConfig.class);
    setIconImage(null);
    setIconImages(null);
    setPreferredSize(new Dimension(400, 220));
    setSize(new Dimension(400, 220));
    setMinimumSize(new Dimension(380, 200));
    JPanel contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout(0, 5));
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    // font properties panel
    JPanel fontPrefsPanel = new JPanel();
    fontPrefsPanel.setLayout(new BoxLayout(fontPrefsPanel, BoxLayout.X_AXIS));
    fontFamilyBox = new JComboBox();
    fontFamilyBox.setPreferredSize(new Dimension(160, -1));
    String[] availableFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
    fontFamilyBox.setModel(new DefaultComboBoxModel<>(availableFonts));
    fontSizeBox = new JComboBox();
    fontSizeBox.setPreferredSize(new Dimension(60, -1));
    fontSizeBox.setMaximumSize(new Dimension(60, Integer.MAX_VALUE));
    fontSizeBox.setMinimumSize(new Dimension(60, 0));
    fontSizeBox.setModel(new ListComboBoxModel<>(desktopConfig.getAvailableFontSizes()));
    DesktopResources resources = App.getInstance().getResources();
    boldToggle = new JToggleButton(resources.getIcon("font/bold.png"));
    italicToggle = new JToggleButton(resources.getIcon("font/italic.png"));
    underlineToggle = new JToggleButton(resources.getIcon("font/underline.png"));
    fontPrefsPanel.add(fontFamilyBox);
    fontPrefsPanel.add(fontSizeBox);
    fontPrefsPanel.add(boldToggle);
    fontPrefsPanel.add(italicToggle);
    fontPrefsPanel.add(underlineToggle);
    if (editFont != null) {
        fontFamilyBox.setSelectedItem(editFont.getFamily());
        fontSizeBox.setSelectedItem(editFont.getSize());
        // toggle buttons
        Map<TextAttribute, ?> attributes = editFont.getAttributes();
        boldToggle.setSelected((editFont.getStyle() & Font.BOLD) == Font.BOLD);
        italicToggle.setSelected((editFont.getStyle() & Font.ITALIC) == Font.ITALIC);
        underlineToggle.setSelected(attributes.get(TextAttribute.UNDERLINE) == TextAttribute.UNDERLINE_ON);
    } else {
        fontFamilyBox.setSelectedIndex(0);
        fontSizeBox.setSelectedIndex(0);
    }
    initListeners();
    contentPane.add(fontPrefsPanel, BorderLayout.NORTH);
    // preview panel
    JPanel previewPanel = new JPanel();
    previewPanel.setLayout(new GridBagLayout());
    previewPanel.setPreferredSize(new Dimension(-1, 120));
    previewPanel.setMinimumSize(new Dimension(0, 120));
    previewPanel.setSize(-1, 120);
    previewLabel = new JLabel("ABCDEFG abcdefg");
    previewPanel.add(previewLabel);
    previewLabel.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
    if (editFont != null)
        previewLabel.setFont(editFont);
    CollapsiblePanel groupBox = new CollapsiblePanel(previewPanel);
    groupBox.setCollapsible(false);
    groupBox.setCaption(messages.getMessage(getClass(), "FontDialog.preview"));
    contentPane.add(groupBox, BorderLayout.CENTER);
    // buttons panel
    JPanel buttonsPanel = new JPanel();
    buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
    JButton okBtn = new JButton(new AbstractAction(messages.getMessage(getClass(), "actions.Ok"), resources.getIcon("icons/ok.png")) {

        @Override
        public void actionPerformed(ActionEvent e) {
            result = compileFont();
            closeDialog();
        }
    });
    okBtn.setPreferredSize(new Dimension(0, DesktopComponentsHelper.BUTTON_HEIGHT));
    JButton cancelBtn = new JButton(new AbstractAction(messages.getMessage(getClass(), "actions.Cancel"), resources.getIcon("icons/cancel.png")) {

        @Override
        public void actionPerformed(ActionEvent e) {
            closeDialog();
        }
    });
    cancelBtn.setPreferredSize(new Dimension(0, DesktopComponentsHelper.BUTTON_HEIGHT));
    buttonsPanel.add(okBtn);
    buttonsPanel.add(cancelBtn);
    contentPane.add(buttonsPanel, BorderLayout.SOUTH);
    initToolTips();
    setContentPane(contentPane);
    pack();
    applyLocation();
}
Also used : Configuration(com.haulmont.cuba.core.global.Configuration) TextAttribute(java.awt.font.TextAttribute) DesktopConfig(com.haulmont.cuba.desktop.DesktopConfig) CollapsiblePanel(com.haulmont.cuba.desktop.sys.vcl.CollapsiblePanel) DesktopResources(com.haulmont.cuba.desktop.DesktopResources) EmptyBorder(javax.swing.border.EmptyBorder)

Example 30 with TextAttribute

use of java.awt.font.TextAttribute in project mudmap2 by Neop.

the class MapPainterDefault method drawText.

/**
 * Draw place tile text
 *
 * @param g graphics to draw on
 * @param x x coordinate of text area (within tile borders)
 * @param y y coordinate of text area (within tile borders)
 * @param width text area width
 * @param height text area height
 * @param top text for the top part
 * @param flags flags line
 * @param exits exits line
 */
private void drawText(Graphics g, int x, int y, int width, int height, List<String> top, String flags, String exits) {
    FontMetrics fm = g.getFontMetrics();
    final int lineHeight = fm.getMaxAscent();
    // maximum number of lines
    final int maxLines = (int) Math.floor((height - fm.getDescent()) / lineHeight);
    // max number of lines for the top part
    final int topLines = maxLines - ((!flags.isEmpty() || !exits.isEmpty()) && maxLines > 1 ? 1 : 0);
    // reformat lines
    LinkedList<String> linesTop = new LinkedList<>();
    for (String topText : top) {
        LinkedList<String> fitLineLength = fitLineLength(topText, fm, width, topLines);
        linesTop.addAll(fitLineLength);
        if (linesTop.size() >= topLines)
            break;
    }
    // draw top lines
    for (int i = 0; i < topLines && i < linesTop.size(); ++i) {
        g.drawString(linesTop.get(i), x, y + (i + 1) * lineHeight);
    }
    if (maxLines > 1) {
        if (fm.stringWidth(flags + exits) < width) {
            // draw flags
            g.drawString(flags, x, y + height - fm.getDescent());
        }
        // change font for exits
        Font orig = g.getFont();
        // derive font: increase font size and decrease character spacing
        Map<TextAttribute, Object> attributes = new HashMap<>();
        attributes.put(TextAttribute.SIZE, 17);
        attributes.put(TextAttribute.TRACKING, 0.0);
        g.setFont(orig.deriveFont(attributes));
        FontMetrics fm2 = g.getFontMetrics();
        int exitsWidth = fm2.stringWidth(exits);
        // draw exit string
        g.drawString(exits, x + width - exitsWidth, y + height - fm2.getDescent());
        g.setFont(orig);
    }
}
Also used : HashMap(java.util.HashMap) FontMetrics(java.awt.FontMetrics) TextAttribute(java.awt.font.TextAttribute) GradientPaint(java.awt.GradientPaint) LinkedList(java.util.LinkedList) Font(java.awt.Font)

Aggregations

TextAttribute (java.awt.font.TextAttribute)31 Font (java.awt.Font)22 HashMap (java.util.HashMap)22 Configuration (com.haulmont.cuba.core.global.Configuration)2 DesktopConfig (com.haulmont.cuba.desktop.DesktopConfig)2 FontMetrics (java.awt.FontMetrics)2 Paint (java.awt.Paint)2 MouseEvent (java.awt.event.MouseEvent)2 AffineTransform (java.awt.geom.AffineTransform)2 Hashtable (java.util.Hashtable)2 DesktopResources (com.haulmont.cuba.desktop.DesktopResources)1 CollapsiblePanel (com.haulmont.cuba.desktop.sys.vcl.CollapsiblePanel)1 WebColors (com.revolsys.awt.WebColors)1 Geometry (com.revolsys.geometry.model.Geometry)1 SwingUtil (com.revolsys.swing.SwingUtil)1 ClipboardUtil (com.revolsys.swing.dnd.ClipboardUtil)1 AbstractRecordLayer (com.revolsys.swing.map.layer.record.AbstractRecordLayer)1 LayerRecord (com.revolsys.swing.map.layer.record.LayerRecord)1 LayerRecordMenu (com.revolsys.swing.map.layer.record.LayerRecordMenu)1 RecordLayerTableModel (com.revolsys.swing.map.layer.record.table.model.RecordLayerTableModel)1