Search in sources :

Example 16 with Element

use of javax.swing.text.Element in project Botnak by Gocnak.

the class ListenerURL method mouseReleased.

@Override
public void mouseReleased(MouseEvent e) {
    JTextPane editor = (JTextPane) e.getSource();
    Point pt = new Point(e.getX(), e.getY());
    int pos = editor.viewToModel(pt);
    if (pos >= 0) {
        Document doc = editor.getDocument();
        if (doc instanceof DefaultStyledDocument) {
            DefaultStyledDocument hdoc = (DefaultStyledDocument) doc;
            Element el = hdoc.getCharacterElement(pos);
            AttributeSet a = el.getAttributes();
            String href = (String) a.getAttribute(HTML.Attribute.HREF);
            if (href != null) {
                Utils.openWebPage(href);
            }
        }
    }
}
Also used : AttributeSet(javax.swing.text.AttributeSet) Element(javax.swing.text.Element) DefaultStyledDocument(javax.swing.text.DefaultStyledDocument) Document(javax.swing.text.Document) DefaultStyledDocument(javax.swing.text.DefaultStyledDocument)

Example 17 with Element

use of javax.swing.text.Element in project adempiere by adempiere.

the class HTMLRenderer method get.

/**
	 * 	Get View from HTML String
	 *	@param html html string
	 *	@return renderer view
	 */
public static HTMLRenderer get(String html) {
    HTMLEditorKit kit = new HTMLEditorKit();
    HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
    try {
        doc.remove(0, doc.getLength());
        Reader r = new StringReader(html);
        kit.read(r, doc, 0);
    } catch (Exception e) {
        log.log(Level.SEVERE, "", e);
    }
    //	Create Renderer
    Element element = doc.getDefaultRootElement();
    ViewFactory factory = kit.getViewFactory();
    //	Y_AXIS is main
    View view = factory.create(element);
    HTMLRenderer renderer = new HTMLRenderer(factory, view);
    renderer.preferenceChanged(null, true, true);
    return renderer;
}
Also used : HTMLDocument(javax.swing.text.html.HTMLDocument) Element(javax.swing.text.Element) ViewFactory(javax.swing.text.ViewFactory) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) View(javax.swing.text.View) BadLocationException(javax.swing.text.BadLocationException)

Example 18 with Element

use of javax.swing.text.Element in project jdk8u_jdk by JetBrains.

the class CAccessibleText method getLineNumberForIndex.

static int getLineNumberForIndex(final Accessible a, int index) {
    final Accessible sa = CAccessible.getSwingAccessible(a);
    if (!(sa instanceof JTextComponent))
        return -1;
    final JTextComponent jc = (JTextComponent) sa;
    final Element root = jc.getDocument().getDefaultRootElement();
    // treat -1 special, returns the current caret position
    if (index == -1)
        index = jc.getCaretPosition();
    // Determine line number (can be -1)
    return root.getElementIndex(index);
}
Also used : Element(javax.swing.text.Element) JTextComponent(javax.swing.text.JTextComponent) Accessible(javax.accessibility.Accessible)

Example 19 with Element

use of javax.swing.text.Element in project jdk8u_jdk by JetBrains.

the class CAccessibleText method getRangeForLine.

static int[] getRangeForLine(final Accessible a, final int lineIndex) {
    Accessible sa = CAccessible.getSwingAccessible(a);
    if (!(sa instanceof JTextComponent))
        return null;
    final JTextComponent jc = (JTextComponent) sa;
    final Element root = jc.getDocument().getDefaultRootElement();
    final Element line = root.getElement(lineIndex);
    if (line == null)
        return null;
    return new int[] { line.getStartOffset(), line.getEndOffset() };
}
Also used : Element(javax.swing.text.Element) JTextComponent(javax.swing.text.JTextComponent) Accessible(javax.accessibility.Accessible)

Example 20 with Element

use of javax.swing.text.Element in project binnavi by google.

the class SyntaxDocument method highlightLinesAfter.

/*
   * Highlight lines to start or end delimiter
   */
private void highlightLinesAfter(final String content, final int line) {
    final int offset = rootElement.getElement(line).getEndOffset();
    // Start/End delimiter not found, nothing to do
    int startDelimiter = indexOf(content, getStartDelimiter(), offset);
    int endDelimiter = indexOf(content, getEndDelimiter(), offset);
    if (startDelimiter < 0) {
        startDelimiter = content.length();
    }
    if (endDelimiter < 0) {
        endDelimiter = content.length();
    }
    final int delimiter = Math.min(startDelimiter, endDelimiter);
    if (delimiter < offset) {
        return;
    }
    // Start/End delimiter found, reapply highlighting
    final int endLine = rootElement.getElementIndex(delimiter);
    for (int i = line + 1; i < endLine; i++) {
        final Element branch = rootElement.getElement(i);
        final Element leaf = doc.getCharacterElement(branch.getStartOffset());
        final AttributeSet as = leaf.getAttributes();
        if (as.isEqual(comment)) {
            applyHighlighting(content, i);
        }
    }
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) AttributeSet(javax.swing.text.AttributeSet) MutableAttributeSet(javax.swing.text.MutableAttributeSet) Element(javax.swing.text.Element)

Aggregations

Element (javax.swing.text.Element)40 BadLocationException (javax.swing.text.BadLocationException)15 Point (java.awt.Point)13 AttributeSet (javax.swing.text.AttributeSet)6 HTMLDocument (javax.swing.text.html.HTMLDocument)6 FontMetrics (java.awt.FontMetrics)4 Dimension (java.awt.Dimension)3 IOException (java.io.IOException)3 Document (javax.swing.text.Document)3 TreePath (javax.swing.tree.TreePath)3 ExtendedHTMLDocument (gmgen.gui.ExtendedHTMLDocument)2 Font (java.awt.Font)2 Insets (java.awt.Insets)2 Rectangle (java.awt.Rectangle)2 Accessible (javax.accessibility.Accessible)2 JTextComponent (javax.swing.text.JTextComponent)2 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)2 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)2 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1