下面列出了javax.swing.text.Element#getEndOffset ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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();
}
/**
* 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;
}
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() };
}
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() };
}
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");
}
}
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");
}
}
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() };
}
/**
* 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;
}
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);
}
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");
}
}
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;
}
}
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() };
}
static String toString(Element el) {
return el.toString() + "{el s:" + el.getStartOffset() + ", e:" + el.getEndOffset() + "}";
}
/**
* 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;
}
}
}
}
@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));
}
@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));
}
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;
}
}
/**
* 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;
}
}
}
}
/**
* 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;
}
}
}
}
/**
* 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;
}
}
}
}