Search in sources :

Example 16 with MutableAttributeSet

use of javax.swing.text.MutableAttributeSet in project omegat by omegat-org.

the class Document3 method setAlignment.

/**
 * Set alignment for specified part of text.
 *
 * @param beginOffset
 *            begin offset
 * @param endOffset
 *            end offset
 * @param isRightAlignment
 *            false - left alignment, true - right alignment
 */
protected void setAlignment(int beginOffset, int endOffset, boolean isRightAlignment) {
    try {
        writeLock();
        DefaultDocumentEvent changes = new DefaultDocumentEvent(beginOffset, endOffset - beginOffset, DocumentEvent.EventType.CHANGE);
        Element root = getDefaultRootElement();
        int parBeg = root.getElementIndex(beginOffset);
        int parEnd = root.getElementIndex(endOffset - 1);
        for (int par = parBeg; par <= parEnd; par++) {
            Element el = root.getElement(par);
            MutableAttributeSet attr = (MutableAttributeSet) el.getAttributes();
            attr.addAttribute(StyleConstants.Alignment, isRightAlignment ? StyleConstants.ALIGN_RIGHT : StyleConstants.ALIGN_LEFT);
        }
        changes.end();
        fireChangedUpdate(changes);
    } finally {
        writeUnlock();
    }
}
Also used : MutableAttributeSet(javax.swing.text.MutableAttributeSet) Element(javax.swing.text.Element)

Example 17 with MutableAttributeSet

use of javax.swing.text.MutableAttributeSet in project omegat by omegat-org.

the class FontFallbackListener method getAttributes.

private AttributeSet getAttributes(Font font) {
    MutableAttributeSet attrs = new SimpleAttributeSet();
    StyleConstants.setFontFamily(attrs, font.getFamily());
    StyleConstants.setFontSize(attrs, defaultFont.getSize());
    return attrs;
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) MutableAttributeSet(javax.swing.text.MutableAttributeSet)

Example 18 with MutableAttributeSet

use of javax.swing.text.MutableAttributeSet in project opennars by opennars.

the class SwingText method print.

public void print(final Color color, final Color bgColor, CharSequence text, Action action) {
    MutableAttributeSet aset = getInputAttributes();
    StyleConstants.setForeground(aset, color);
    StyleConstants.setBackground(aset, bgColor != null ? bgColor : Color.BLACK);
    try {
        if (action == null) {
            doc.insertString(doc.getLength(), text.toString(), aset);
        } else {
            // http://stackoverflow.com/questions/16131811/clickable-text-in-a-jtextpane
            Style link = doc.addStyle(null, StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE));
            StyleConstants.setForeground(link, color);
            // StyleConstants.setUnderline(link, true);
            // StyleConstants.setBold(link, true);
            link.addAttribute("linkact", action);
            doc.insertString(doc.getLength(), text.toString(), link);
        }
    } catch (BadLocationException ex) {
        Logger.getLogger(SwingText.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : MutableAttributeSet(javax.swing.text.MutableAttributeSet) Style(javax.swing.text.Style) BadLocationException(javax.swing.text.BadLocationException)

Example 19 with MutableAttributeSet

use of javax.swing.text.MutableAttributeSet in project alfresco-repository by Alfresco.

the class HtmlMetadataExtracter method extractRaw.

@Override
protected Map<String, Serializable> extractRaw(ContentReader reader) throws Throwable {
    final Map<String, Serializable> rawProperties = newRawMap();
    HTMLEditorKit.ParserCallback callback = new HTMLEditorKit.ParserCallback() {

        StringBuffer title = null;

        boolean inHead = false;

        public void handleText(char[] data, int pos) {
            if (title != null) {
                title.append(data);
            }
        }

        public void handleComment(char[] data, int pos) {
        // Perhaps sniff for Office 9+ metadata in here?
        }

        public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
            if (HTML.Tag.HEAD.equals(t)) {
                inHead = true;
            } else if (HTML.Tag.TITLE.equals(t) && inHead) {
                title = new StringBuffer();
            } else
                handleSimpleTag(t, a, pos);
        }

        public void handleEndTag(HTML.Tag t, int pos) {
            if (HTML.Tag.HEAD.equals(t)) {
                inHead = false;
            } else if (HTML.Tag.TITLE.equals(t) && title != null) {
                putRawValue(KEY_TITLE, title.toString(), rawProperties);
                title = null;
            }
        }

        public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
            if (HTML.Tag.META.equals(t)) {
                Object nameO = a.getAttribute(HTML.Attribute.NAME);
                Object valueO = a.getAttribute(HTML.Attribute.CONTENT);
                if (nameO == null || valueO == null)
                    return;
                String name = nameO.toString();
                if (name.equalsIgnoreCase("creator") || name.equalsIgnoreCase("author") || name.equalsIgnoreCase("dc.creator")) {
                    putRawValue(KEY_AUTHOR, valueO.toString(), rawProperties);
                } else if (name.equalsIgnoreCase("description") || name.equalsIgnoreCase("dc.description")) {
                    putRawValue(KEY_DESCRIPTION, valueO.toString(), rawProperties);
                }
            }
        }

        public void handleError(String errorMsg, int pos) {
        }
    };
    String charsetGuess = "UTF-8";
    int tries = 0;
    while (tries < 3) {
        rawProperties.clear();
        Reader r = null;
        InputStream cis = null;
        try {
            cis = reader.getContentInputStream();
            // TODO: for now, use default charset; we should attempt to map from html meta-data
            r = new InputStreamReader(cis, charsetGuess);
            HTMLEditorKit.Parser parser = new ParserDelegator();
            parser.parse(r, callback, tries > 0);
            break;
        } catch (ChangedCharSetException ccse) {
            tries++;
            charsetGuess = ccse.getCharSetSpec();
            int begin = charsetGuess.indexOf("charset=");
            if (begin > 0)
                charsetGuess = charsetGuess.substring(begin + 8, charsetGuess.length());
            reader = reader.getReader();
        } finally {
            if (r != null)
                r.close();
            if (cis != null)
                cis.close();
        }
    }
    // Done
    return rawProperties;
}
Also used : Serializable(java.io.Serializable) ParserDelegator(javax.swing.text.html.parser.ParserDelegator) InputStreamReader(java.io.InputStreamReader) ChangedCharSetException(javax.swing.text.ChangedCharSetException) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ContentReader(org.alfresco.service.cmr.repository.ContentReader) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) MutableAttributeSet(javax.swing.text.MutableAttributeSet)

Aggregations

MutableAttributeSet (javax.swing.text.MutableAttributeSet)19 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)7 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)5 ParserDelegator (javax.swing.text.html.parser.ParserDelegator)5 IOException (java.io.IOException)3 Reader (java.io.Reader)3 BadLocationException (javax.swing.text.BadLocationException)3 HTML (javax.swing.text.html.HTML)3 Font (java.awt.Font)2 FileReader (java.io.FileReader)2 StringReader (java.io.StringReader)2 AttributeSet (javax.swing.text.AttributeSet)2 NotNull (org.jetbrains.annotations.NotNull)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1 Point (java.awt.Point)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 FocusAdapter (java.awt.event.FocusAdapter)1 FocusEvent (java.awt.event.FocusEvent)1 BufferedImage (java.awt.image.BufferedImage)1