Java Code Examples for javax.swing.text.Element#getEndOffset()

The following examples show how to use javax.swing.text.Element#getEndOffset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinkUtils.java    From desktopclient-java with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
    WebTextPane textPane = (WebTextPane) e.getComponent();
    StyledDocument doc = textPane.getStyledDocument();
    Element elem = doc.getCharacterElement(textPane.viewToModel(e.getPoint()));
    if (!elem.getAttributes().isDefined(URL_ATT_NAME))
        // not a link
        return;

    int len = elem.getEndOffset() - elem.getStartOffset();
    final String url;
    try {
        url = doc.getText(elem.getStartOffset(), len);
    } catch (BadLocationException ex) {
        LOGGER.log(Level.WARNING, "can't get URL", ex);
        return;
    }

    Runnable run = new Runnable() {
        @Override
        public void run() {
            WebUtils.browseSiteSafely(fixProto(url));
        }
    };
    new Thread(run, "Link Browser").start();
}
 
Example 2
Source File: ActionUtils.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
/**
 * If the selection is multi lined, then the full lines are selected,
 * otherwise, nothing is done.
 * @param target
 * @return true if the selection is multi-line, or a whole line
 */
public static boolean selectLines(JTextComponent target) {
	if (target.getSelectionStart() == target.getSelectionEnd()) {
		return false;
	}
	PlainDocument pDoc = (PlainDocument) target.getDocument();
	Element es = pDoc.getParagraphElement(target.getSelectionStart());
	// if more than one line is selected, we need to subtract one from the end
	// so that we do not select the line with the caret and no selection in it
	Element ee = pDoc.getParagraphElement(target.getSelectionEnd() - 1);
	if (es.equals(ee) && ee.getEndOffset() != target.getSelectionEnd()) {
		return false;
	}
	int start = es.getStartOffset();
	int end = ee.getEndOffset();
	target.select(start, end - 1);
	return true;
}
 
Example 3
Source File: DocumentUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Get the paragraph element for the given document.
 *
 * @param doc non-null document instance.
 * @param offset offset in the document >=0
 * @return paragraph element containing the given offset.
 */
public static Element getParagraphElement(Document doc, int offset) {
    Element paragraph;
    if (doc instanceof StyledDocument) {
        paragraph = ((StyledDocument)doc).getParagraphElement(offset);
    } else {
        Element rootElem = doc.getDefaultRootElement();
        int index = rootElem.getElementIndex(offset);
        paragraph = rootElem.getElement(index);
        if ((offset < paragraph.getStartOffset()) || (offset >= paragraph.getEndOffset())) {
            paragraph = null;
        }
    }
    return paragraph;
}
 
Example 4
Source File: CAccessibleText.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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() };
}
 
Example 5
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
protected void addHighlight(Element element, boolean isBlock) {
  Highlighter highlighter = editorPane.getHighlighter();
  int start = element.getStartOffset();
  int lf = isBlock ? 1 : 0;
  int end = element.getEndOffset() - lf; // lf???, setDrawsLayeredHighlights(false) bug???
  try {
    highlighter.addHighlight(start, end, HIGHLIGHT);
  } catch (BadLocationException ex) {
    // should never happen
    RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
    wrap.initCause(ex);
    throw wrap;
  }
}
 
Example 6
Source File: bug6857057.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
bug6857057() {
    Element elem = new StubBranchElement(" G L Y P H V");
    GlyphView view = new GlyphView(elem);
    float pos = elem.getStartOffset();
    float len = elem.getEndOffset() - pos;
    int res = view.getBreakWeight(View.X_AXIS, pos, len);
    if (res != View.ExcellentBreakWeight) {
        throw new RuntimeException("breakWeight != ExcellentBreakWeight");
    }
}
 
Example 7
Source File: SyntaxDocument.java    From binnavi with Apache License 2.0 5 votes vote down vote up
private String getLine(final String content, final int offset) {
  final int line = rootElement.getElementIndex(offset);
  final Element lineElement = rootElement.getElement(line);
  final int start = lineElement.getStartOffset();
  final int end = lineElement.getEndOffset();
  return content.substring(start, end - 1);
}
 
Example 8
Source File: CAccessibleText.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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() };
}
 
Example 9
Source File: CAccessibleText.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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() };
}
 
Example 10
Source File: bug6857057.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
bug6857057() {
    Element elem = new StubBranchElement(" G L Y P H V");
    GlyphView view = new GlyphView(elem);
    float pos = elem.getStartOffset();
    float len = elem.getEndOffset() - pos;
    int res = view.getBreakWeight(View.X_AXIS, pos, len);
    if (res != View.ExcellentBreakWeight) {
        throw new RuntimeException("breakWeight != ExcellentBreakWeight");
    }
}
 
Example 11
Source File: bug6857057.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
bug6857057() {
    Element elem = new StubBranchElement(" G L Y P H V");
    GlyphView view = new GlyphView(elem);
    float pos = elem.getStartOffset();
    float len = elem.getEndOffset() - pos;
    int res = view.getBreakWeight(View.X_AXIS, pos, len);
    if (res != View.ExcellentBreakWeight) {
        throw new RuntimeException("breakWeight != ExcellentBreakWeight");
    }
}
 
Example 12
Source File: CAccessibleText.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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() };
}
 
Example 13
Source File: ElementTreePanel.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
public ElementTreePanel(JTextComponent editor) {
    this.editor = editor;

    Document document = editor.getDocument();

    // Create the tree.
    treeModel = new ElementTreeModel(document);
    tree = new JTree(treeModel) {

        @Override
        public String convertValueToText(Object value, boolean selected,
                boolean expanded, boolean leaf,
                int row, boolean hasFocus) {
            // Should only happen for the root
            if (!(value instanceof Element)) {
                return value.toString();
            }

            Element e = (Element) value;
            AttributeSet as = e.getAttributes().copyAttributes();
            String asString;

            if (as != null) {
                StringBuilder retBuffer = new StringBuilder("[");
                Enumeration names = as.getAttributeNames();

                while (names.hasMoreElements()) {
                    Object nextName = names.nextElement();

                    if (nextName != StyleConstants.ResolveAttribute) {
                        retBuffer.append(" ");
                        retBuffer.append(nextName);
                        retBuffer.append("=");
                        retBuffer.append(as.getAttribute(nextName));
                    }
                }
                retBuffer.append(" ]");
                asString = retBuffer.toString();
            } else {
                asString = "[ ]";
            }

            if (e.isLeaf()) {
                return e.getName() + " [" + e.getStartOffset() + ", " + e.
                        getEndOffset() + "] Attributes: " + asString;
            }
            return e.getName() + " [" + e.getStartOffset() + ", " + e.
                    getEndOffset() + "] Attributes: " + asString;
        }
    };
    tree.addTreeSelectionListener(this);
    tree.setDragEnabled(true);
    // Don't show the root, it is fake.
    tree.setRootVisible(false);
    // Since the display value of every node after the insertion point
    // changes every time the text changes and we don't generate a change
    // event for all those nodes the display value can become off.
    // This can be seen as '...' instead of the complete string value.
    // This is a temporary workaround, increase the needed size by 15,
    // hoping that will be enough.
    tree.setCellRenderer(new DefaultTreeCellRenderer() {

        @Override
        public Dimension getPreferredSize() {
            Dimension retValue = super.getPreferredSize();
            if (retValue != null) {
                retValue.width += 15;
            }
            return retValue;
        }
    });
    // become a listener on the document to update the tree.
    document.addDocumentListener(this);

    // become a PropertyChangeListener to know when the Document has
    // changed.
    editor.addPropertyChangeListener(this);

    // Become a CaretListener
    editor.addCaretListener(this);

    // configure the panel and frame containing it.
    setLayout(new BorderLayout());
    add(new JScrollPane(tree), BorderLayout.CENTER);

    // Add a label above tree to describe what is being shown
    JLabel label = new JLabel("Elements that make up the current document",
            SwingConstants.CENTER);

    label.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label, BorderLayout.NORTH);

    setPreferredSize(new Dimension(400, 400));
}
 
Example 14
Source File: ElementTreePanel.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
public ElementTreePanel(JTextComponent editor) {
    this.editor = editor;

    Document document = editor.getDocument();

    // Create the tree.
    treeModel = new ElementTreeModel(document);
    tree = new JTree(treeModel) {

        @Override
        public String convertValueToText(Object value, boolean selected,
                boolean expanded, boolean leaf,
                int row, boolean hasFocus) {
            // Should only happen for the root
            if (!(value instanceof Element)) {
                return value.toString();
            }

            Element e = (Element) value;
            AttributeSet as = e.getAttributes().copyAttributes();
            String asString;

            if (as != null) {
                StringBuilder retBuffer = new StringBuilder("[");
                Enumeration names = as.getAttributeNames();

                while (names.hasMoreElements()) {
                    Object nextName = names.nextElement();

                    if (nextName != StyleConstants.ResolveAttribute) {
                        retBuffer.append(" ");
                        retBuffer.append(nextName);
                        retBuffer.append("=");
                        retBuffer.append(as.getAttribute(nextName));
                    }
                }
                retBuffer.append(" ]");
                asString = retBuffer.toString();
            } else {
                asString = "[ ]";
            }

            if (e.isLeaf()) {
                return e.getName() + " [" + e.getStartOffset() + ", " + e.
                        getEndOffset() + "] Attributes: " + asString;
            }
            return e.getName() + " [" + e.getStartOffset() + ", " + e.
                    getEndOffset() + "] Attributes: " + asString;
        }
    };
    tree.addTreeSelectionListener(this);
    tree.setDragEnabled(true);
    // Don't show the root, it is fake.
    tree.setRootVisible(false);
    // Since the display value of every node after the insertion point
    // changes every time the text changes and we don't generate a change
    // event for all those nodes the display value can become off.
    // This can be seen as '...' instead of the complete string value.
    // This is a temporary workaround, increase the needed size by 15,
    // hoping that will be enough.
    tree.setCellRenderer(new DefaultTreeCellRenderer() {

        @Override
        public Dimension getPreferredSize() {
            Dimension retValue = super.getPreferredSize();
            if (retValue != null) {
                retValue.width += 15;
            }
            return retValue;
        }
    });
    // become a listener on the document to update the tree.
    document.addDocumentListener(this);

    // become a PropertyChangeListener to know when the Document has
    // changed.
    editor.addPropertyChangeListener(this);

    // Become a CaretListener
    editor.addCaretListener(this);

    // configure the panel and frame containing it.
    setLayout(new BorderLayout());
    add(new JScrollPane(tree), BorderLayout.CENTER);

    // Add a label above tree to describe what is being shown
    JLabel label = new JLabel("Elements that make up the current document",
            SwingConstants.CENTER);

    label.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label, BorderLayout.NORTH);

    setPreferredSize(new Dimension(400, 400));
}
 
Example 15
Source File: ToolTipAnnotation.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static String getIdentifier (
    JPDADebugger debugger,
    StyledDocument doc,
    JEditorPane ep,
    int offset,
    boolean[] isFunctionPtr
) {
    // do always evaluation if the tooltip is invoked on a text selection
    String t = null;
    if ( (ep.getSelectionStart () <= offset) &&
         (offset <= ep.getSelectionEnd ())
    ) {
        t = ep.getSelectedText ();
    }
    if (t != null) {
        return t;
    }
    int line = NbDocument.findLineNumber (
        doc,
        offset
    );
    int col = NbDocument.findLineColumn (
        doc,
        offset
    );
    try {
        Element lineElem =
            NbDocument.findLineRootElement (doc).
            getElement (line);

        if (lineElem == null) {
            return null;
        }
        int lineStartOffset = lineElem.getStartOffset ();
        int lineLen = lineElem.getEndOffset() - lineStartOffset;
        t = doc.getText (lineStartOffset, lineLen);
        int identStart = col;
        while (identStart > 0 &&
            (Character.isJavaIdentifierPart (
                t.charAt (identStart - 1)
            ) ||
            (t.charAt (identStart - 1) == '.'))) {
            identStart--;
        }
        int identEnd = col;
        while (identEnd < lineLen &&
               Character.isJavaIdentifierPart(t.charAt(identEnd))
        ) {
            identEnd++;
        }

        if (identStart == identEnd) {
            return null;
        }

        String ident = t.substring (identStart, identEnd);
        //if (JS_KEYWORDS.contains(ident)) {
            // JS keyword => Do not show anything
        //    return null;
        //}

        while (identEnd < lineLen &&
               Character.isWhitespace(t.charAt(identEnd))
        ) {
            identEnd++;
        }
        if (identEnd < lineLen && t.charAt(identEnd) == '(') {
            // We're at a function call
            isFunctionPtr[0] = true;
        }
        return ident;
    } catch (BadLocationException e) {
        return null;
    }
}
 
Example 16
Source File: ElementTreePanel.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Messaged when the selection in the editor has changed. Will update
 * the selection in the tree.
 */
public void caretUpdate(CaretEvent e) {
    if (!updatingSelection) {
        int selBegin = Math.min(e.getDot(), e.getMark());
        int end = Math.max(e.getDot(), e.getMark());
        List<TreePath> paths = new ArrayList<TreePath>();
        TreeModel model = getTreeModel();
        Object root = model.getRoot();
        int rootCount = model.getChildCount(root);

        // Build an array of all the paths to all the character elements
        // in the selection.
        for (int counter = 0; counter < rootCount; counter++) {
            int start = selBegin;

            while (start <= end) {
                TreePath path = getPathForIndex(start, root,
                        (Element) model.getChild(root, counter));
                Element charElement = (Element) path.getLastPathComponent();

                paths.add(path);
                if (start >= charElement.getEndOffset()) {
                    start++;
                } else {
                    start = charElement.getEndOffset();
                }
            }
        }

        // If a path was found, select it (them).
        int numPaths = paths.size();

        if (numPaths > 0) {
            TreePath[] pathArray = new TreePath[numPaths];

            paths.toArray(pathArray);
            updatingSelection = true;
            try {
                getTree().setSelectionPaths(pathArray);
                getTree().scrollPathToVisible(pathArray[0]);
            } finally {
                updatingSelection = false;
            }
        }
    }
}
 
Example 17
Source File: ElementTreePanel.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Messaged when the selection in the editor has changed. Will update
 * the selection in the tree.
 */
public void caretUpdate(CaretEvent e) {
    if (!updatingSelection) {
        int selBegin = Math.min(e.getDot(), e.getMark());
        int end = Math.max(e.getDot(), e.getMark());
        List<TreePath> paths = new ArrayList<TreePath>();
        TreeModel model = getTreeModel();
        Object root = model.getRoot();
        int rootCount = model.getChildCount(root);

        // Build an array of all the paths to all the character elements
        // in the selection.
        for (int counter = 0; counter < rootCount; counter++) {
            int start = selBegin;

            while (start <= end) {
                TreePath path = getPathForIndex(start, root,
                        (Element) model.getChild(root, counter));
                Element charElement = (Element) path.getLastPathComponent();

                paths.add(path);
                if (start >= charElement.getEndOffset()) {
                    start++;
                } else {
                    start = charElement.getEndOffset();
                }
            }
        }

        // If a path was found, select it (them).
        int numPaths = paths.size();

        if (numPaths > 0) {
            TreePath[] pathArray = new TreePath[numPaths];

            paths.toArray(pathArray);
            updatingSelection = true;
            try {
                getTree().setSelectionPaths(pathArray);
                getTree().scrollPathToVisible(pathArray[0]);
            } finally {
                updatingSelection = false;
            }
        }
    }
}
 
Example 18
Source File: ElementTreePanel.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Messaged when the selection in the editor has changed. Will update
 * the selection in the tree.
 */
public void caretUpdate(CaretEvent e) {
    if (!updatingSelection) {
        int selBegin = Math.min(e.getDot(), e.getMark());
        int end = Math.max(e.getDot(), e.getMark());
        List<TreePath> paths = new ArrayList<TreePath>();
        TreeModel model = getTreeModel();
        Object root = model.getRoot();
        int rootCount = model.getChildCount(root);

        // Build an array of all the paths to all the character elements
        // in the selection.
        for (int counter = 0; counter < rootCount; counter++) {
            int start = selBegin;

            while (start <= end) {
                TreePath path = getPathForIndex(start, root,
                        (Element) model.getChild(root, counter));
                Element charElement = (Element) path.getLastPathComponent();

                paths.add(path);
                if (start >= charElement.getEndOffset()) {
                    start++;
                } else {
                    start = charElement.getEndOffset();
                }
            }
        }

        // If a path was found, select it (them).
        int numPaths = paths.size();

        if (numPaths > 0) {
            TreePath[] pathArray = new TreePath[numPaths];

            paths.toArray(pathArray);
            updatingSelection = true;
            try {
                getTree().setSelectionPaths(pathArray);
                getTree().scrollPathToVisible(pathArray[0]);
            } finally {
                updatingSelection = false;
            }
        }
    }
}
 
Example 19
Source File: ElementTreePanel.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Messaged when the selection in the editor has changed. Will update
 * the selection in the tree.
 */
public void caretUpdate(CaretEvent e) {
    if (!updatingSelection) {
        int selBegin = Math.min(e.getDot(), e.getMark());
        int end = Math.max(e.getDot(), e.getMark());
        List<TreePath> paths = new ArrayList<TreePath>();
        TreeModel model = getTreeModel();
        Object root = model.getRoot();
        int rootCount = model.getChildCount(root);

        // Build an array of all the paths to all the character elements
        // in the selection.
        for (int counter = 0; counter < rootCount; counter++) {
            int start = selBegin;

            while (start <= end) {
                TreePath path = getPathForIndex(start, root,
                        (Element) model.getChild(root, counter));
                Element charElement = (Element) path.getLastPathComponent();

                paths.add(path);
                if (start >= charElement.getEndOffset()) {
                    start++;
                } else {
                    start = charElement.getEndOffset();
                }
            }
        }

        // If a path was found, select it (them).
        int numPaths = paths.size();

        if (numPaths > 0) {
            TreePath[] pathArray = new TreePath[numPaths];

            paths.toArray(pathArray);
            updatingSelection = true;
            try {
                getTree().setSelectionPaths(pathArray);
                getTree().scrollPathToVisible(pathArray[0]);
            } finally {
                updatingSelection = false;
            }
        }
    }
}
 
Example 20
Source File: GuardUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
static String toString(Element el) {
    return el.toString() + "{el s:" + el.getStartOffset() + ", e:" + el.getEndOffset() + "}";
}