Search in sources :

Example 46 with StyledDocument

use of javax.swing.text.StyledDocument in project chatty by chatty.

the class LinkController method getElement.

public static Element getElement(MouseEvent e) {
    JTextPane text = (JTextPane) e.getSource();
    Point mouseLocation = new Point(e.getX(), e.getY());
    int pos = text.viewToModel(mouseLocation);
    if (pos >= 0) {
        /**
         * Check if the found element is actually located where the mouse is
         * pointing, and if not try the previous element.
         *
         * This is a fix to make detection of emotes more reliable. The
         * viewToModel() method apparently searches for the closest element
         * to the given position, disregarding the size of the elements,
         * which means on the right side of an emote the next (non-emote)
         * element is nearer.
         *
         * See also:
         * https://stackoverflow.com/questions/24036650/detecting-image-on-current-mouse-position-only-works-on-part-of-image
         */
        try {
            Rectangle rect = text.modelToView(pos);
            if (e.getX() < rect.x && e.getY() < rect.y + rect.height && pos > 0) {
                pos--;
            }
        } catch (BadLocationException ex) {
        }
        StyledDocument doc = text.getStyledDocument();
        Element element = doc.getCharacterElement(pos);
        return element;
    }
    return null;
}
Also used : JTextPane(javax.swing.JTextPane) Element(javax.swing.text.Element) Rectangle(java.awt.Rectangle) StyledDocument(javax.swing.text.StyledDocument) Point(java.awt.Point) Point(java.awt.Point) BadLocationException(javax.swing.text.BadLocationException)

Example 47 with StyledDocument

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

the class FontFallbackListener method doStyling.

private void doStyling(Document document, final int offset, final int length) {
    if (!Preferences.isPreference(Preferences.FONT_FALLBACK)) {
        return;
    }
    if (!(document instanceof StyledDocument)) {
        return;
    }
    final StyledDocument doc = (StyledDocument) document;
    new SwingWorker<Void, StyleRun>() {

        @Override
        protected Void doInBackground() throws Exception {
            Segment seg = new Segment();
            seg.setPartialReturn(true);
            int nleft = length;
            int offs = offset;
            try {
                while (nleft > 0) {
                    doc.getText(offs, nleft, seg);
                    int i = seg.getBeginIndex();
                    while ((i = defaultFont.canDisplayUpTo(seg, i, seg.getEndIndex())) != -1) {
                        int cp = Character.codePointAt(seg, i - seg.getBeginIndex());
                        int start = i;
                        i += Character.charCount(cp);
                        Font font = FontFallbackManager.getCapableFont(cp);
                        if (font == null) {
                            continue;
                        }
                        // Look ahead to try to group as many characters as possible into this run.
                        for (int cpn, ccn, j = i; j < seg.getEndIndex(); j += ccn) {
                            cpn = Character.codePointAt(seg, j - seg.getBeginIndex());
                            ccn = Character.charCount(cpn);
                            if (!defaultFont.canDisplay(cpn) && font.canDisplay(cpn)) {
                                i += ccn;
                            } else {
                                break;
                            }
                        }
                        publish(new StyleRun(start, i - start, getAttributes(font)));
                    }
                    nleft -= seg.count;
                    offs += seg.count;
                }
            } catch (BadLocationException ex) {
            // Ignore
            }
            return null;
        }

        protected void process(List<StyleRun> chunks) {
            for (StyleRun chunk : chunks) {
                doc.setCharacterAttributes(chunk.start, chunk.length, chunk.attrs, false);
            }
        }
    }.execute();
}
Also used : StyledDocument(javax.swing.text.StyledDocument) BadLocationException(javax.swing.text.BadLocationException) Segment(javax.swing.text.Segment) Font(java.awt.Font) BadLocationException(javax.swing.text.BadLocationException)

Example 48 with StyledDocument

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

the class MatchesTextArea method onProjectClose.

@Override
protected void onProjectClose() {
    clear();
    setText(EXPLANATION);
    // We clean the ATTRIBUTE_SELECTED style set by the last displayed match
    StyledDocument doc = (StyledDocument) getDocument();
    doc.setCharacterAttributes(0, doc.getLength(), ATTRIBUTES_EMPTY, true);
}
Also used : StyledDocument(javax.swing.text.StyledDocument)

Example 49 with StyledDocument

use of javax.swing.text.StyledDocument in project triplea by triplea-game.

the class ChatPanelTest method testTrim.

@Test
public void testTrim() throws Exception {
    final StyledDocument doc = new DefaultStyledDocument();
    final StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < 10; i++) {
        buffer.append("\n");
    }
    doc.insertString(0, buffer.toString(), null);
    ChatMessagePanel.trimLines(doc, 20);
    assertEquals(10, doc.getLength());
    ChatMessagePanel.trimLines(doc, 10);
    assertEquals(10, doc.getLength());
    ChatMessagePanel.trimLines(doc, 5);
    assertEquals(5, doc.getLength());
    ChatMessagePanel.trimLines(doc, 1);
    assertEquals(1, doc.getLength());
}
Also used : DefaultStyledDocument(javax.swing.text.DefaultStyledDocument) StyledDocument(javax.swing.text.StyledDocument) DefaultStyledDocument(javax.swing.text.DefaultStyledDocument) Test(org.junit.jupiter.api.Test)

Example 50 with StyledDocument

use of javax.swing.text.StyledDocument in project opt4j by felixreimann.

the class Opt4JAbout method startup.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.opt4j.config.gui.Startupable#startup()
	 */
@Override
public void startup() {
    Container content = this;
    content.setLayout(new BorderLayout());
    JLabel logoLabel = new JLabel(Icons.getIcon("img/top_logo.png"));
    logoLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    JPanel logo = new JPanel(new BorderLayout());
    logo.setBackground(Color.WHITE);
    logo.add(logoLabel);
    content.add(logo, BorderLayout.PAGE_START);
    JTextPane license = new JTextPane();
    license.setEditable(false);
    final JScrollPane licenseScroll = new JScrollPane(license);
    licenseScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    StyledDocument doc = license.getStyledDocument();
    Style regular = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
    try {
        doc.insertString(doc.getLength(), LICENSE_TEXT, regular);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
    license.setPreferredSize(new Dimension(360, 100));
    content.add(licenseScroll, BorderLayout.CENTER);
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            licenseScroll.getVerticalScrollBar().setValue(0);
        }
    });
    JPanel footer = new JPanel(new BorderLayout());
    footer.setBackground(Color.WHITE);
    // Add Copyright & Credits
    String copyright = "<html>Build " + Opt4J.getDateISO() + " <br /> Version " + Opt4J.getVersion() + "   \u00a9 Opt4J.org 2007</html>";
    JLabel copyrightLabel = new JLabel(copyright);
    copyrightLabel.setHorizontalAlignment(SwingConstants.CENTER);
    copyrightLabel.setVerticalAlignment(SwingConstants.BOTTOM);
    copyrightLabel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
    footer.add(copyrightLabel, BorderLayout.EAST);
    String credits = "<html><p>Credits:<br />";
    for (String author : AUTHORS) {
        credits += author + "<br/>";
    }
    credits += "</p></html>";
    JLabel creditsLabel = new JLabel(credits);
    creditsLabel.setHorizontalAlignment(SwingConstants.CENTER);
    creditsLabel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
    footer.add(creditsLabel, BorderLayout.WEST);
    content.add(footer, BorderLayout.PAGE_END);
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) StyledDocument(javax.swing.text.StyledDocument) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) JTextPane(javax.swing.JTextPane) Container(java.awt.Container) BorderLayout(java.awt.BorderLayout) Style(javax.swing.text.Style) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

StyledDocument (javax.swing.text.StyledDocument)53 BadLocationException (javax.swing.text.BadLocationException)24 Style (javax.swing.text.Style)16 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)11 Point (java.awt.Point)8 JLabel (javax.swing.JLabel)4 JTextPane (javax.swing.JTextPane)4 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)4 Font (java.awt.Font)3 ArrayList (java.util.ArrayList)3 Matcher (java.util.regex.Matcher)3 ImageIcon (javax.swing.ImageIcon)3 JPanel (javax.swing.JPanel)3 PersistentArrayMap (clojure.lang.PersistentArrayMap)2 BorderLayout (java.awt.BorderLayout)2 Dimension (java.awt.Dimension)2 Rectangle (java.awt.Rectangle)2 IOException (java.io.IOException)2 Pattern (java.util.regex.Pattern)2 Element (javax.swing.text.Element)2