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;
}
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();
}
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);
}
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());
}
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);
}
Aggregations