use of org.apache.pivot.wtk.text.TextNode in project pivot by apache.
the class TextPaneDemo method applyStyleToTextNode.
private static void applyStyleToTextNode(Span selectionSpan, StyleApplicator styleApplicator, org.apache.pivot.wtk.text.TextNode textNode, int characterCount, Span textSpan) {
if (selectionSpan.contains(textSpan)) {
// if the text-node is contained wholly inside the selection, remove
// the text-node, replace it with a Span, and apply the style
Element parent = textNode.getParent();
TextSpan newSpanNode = new TextSpan();
newSpanNode.add(new TextNode(textNode.getText()));
styleApplicator.apply(newSpanNode);
int index = parent.remove(textNode);
parent.insert(newSpanNode, index);
} else if (selectionSpan.start <= textSpan.start) {
// if the selection covers the first part of the text-node, split
// off the first part of the text-node, and apply the style to it
int intersectionLength = selectionSpan.end - textSpan.start + 1;
String part1 = textNode.getSubstring(0, intersectionLength);
String part2 = textNode.getSubstring(intersectionLength, characterCount);
TextSpan newSpanNode = new TextSpan();
newSpanNode.add(new TextNode(part1));
styleApplicator.apply(newSpanNode);
Element parent = textNode.getParent();
int index = parent.remove(textNode);
parent.insert(newSpanNode, index);
parent.insert(new TextNode(part2), index + 1);
} else if (selectionSpan.end >= textSpan.end) {
// if the selection covers the last part of the text-node, split off
// the last part of the text-node, and apply the style to it
int intersectionStart = selectionSpan.start - textSpan.start;
String part1 = textNode.getSubstring(0, intersectionStart);
String part2 = textNode.getSubstring(intersectionStart, characterCount);
TextSpan newSpanNode = new TextSpan();
newSpanNode.add(new TextNode(part2));
styleApplicator.apply(newSpanNode);
Element parent = textNode.getParent();
int index = parent.remove(textNode);
parent.insert(new TextNode(part1), index);
parent.insert(newSpanNode, index + 1);
} else {
// if the selection covers an internal part of the text-node, split the
// text-node into 3 parts, and apply the style to the second part
int part2Start = selectionSpan.start - textSpan.start;
int part2End = selectionSpan.end - textSpan.start + 1;
String part1 = textNode.getSubstring(0, part2Start);
String part2 = textNode.getSubstring(part2Start, part2End);
String part3 = textNode.getSubstring(part2End, characterCount);
TextSpan newSpanNode = new TextSpan();
newSpanNode.add(new TextNode(part2));
styleApplicator.apply(newSpanNode);
Element parent = textNode.getParent();
int index = parent.remove(textNode);
parent.insert(new TextNode(part1), index);
parent.insert(newSpanNode, index + 1);
parent.insert(new TextNode(part3), index + 2);
}
}
use of org.apache.pivot.wtk.text.TextNode in project pivot by apache.
the class TextPaneDemo method dumpDocumentNode.
/**
* debugging tools
*/
private void dumpDocumentNode(Node node, PrintStream printStream, final int indent) {
for (int i = 0; i < indent; i++) {
printStream.append(" ");
}
printStream.append("<" + node.getClass().getSimpleName() + ">");
if (node instanceof TextNode) {
TextNode textNode = (TextNode) node;
String text = textNode.getText();
printStream.append(text);
printStream.append("</" + node.getClass().getSimpleName() + ">");
printStream.println();
} else {
printStream.println();
if (node instanceof Element) {
Element element = (Element) node;
for (Node childNode : element) {
dumpDocumentNode(childNode, printStream, indent + 1);
}
} else {
String text = node.toString();
printStream.append(text);
}
for (int i = 0; i < indent; i++) {
printStream.append(" ");
}
printStream.append("</" + node.getClass().getSimpleName() + ">");
printStream.println();
}
}
use of org.apache.pivot.wtk.text.TextNode in project pivot by apache.
the class TextPane method insertText.
public void insertText(String text, int index) {
Utils.checkNull(text, "text");
checkDocumentNull();
if (selectionLength > 0) {
delete(false);
}
if (document.getCharacterCount() == 0) {
// the document is currently empty
document.insert(new Paragraph(text), 0);
} else {
Node descendant = document.getDescendantAt(index);
int offset = index - descendant.getDocumentOffset();
if (descendant instanceof TextNode) {
// The caret is positioned within an existing text node
TextNode textNode = (TextNode) descendant;
textNode.insertText(text, offset);
} else if (descendant instanceof Paragraph) {
// The caret is positioned on the paragraph terminator
// so get to the bottom rightmost descendant and add there
Paragraph paragraph = (Paragraph) descendant;
Node node = getRightmostDescendant(paragraph);
if (node instanceof TextNode) {
// Insert the text into the existing node
TextNode textNode = (TextNode) node;
textNode.insertText(text, index - textNode.getDocumentOffset());
} else if (node instanceof Element) {
// Append a new text node
Element element = (Element) node;
element.add(new TextNode(text));
} else {
// The paragraph is currently empty
paragraph.add(new TextNode(text));
}
} else {
// The caret is positioned on a non-text character node; insert
// the text into the descendant's parent
Element parent = descendant.getParent();
int elemIndex = parent.indexOf(descendant);
parent.insert(new TextNode(text), elemIndex);
}
}
// Set the selection start to the character following the insertion
setSelection(index + text.length(), 0);
}
use of org.apache.pivot.wtk.text.TextNode in project pivot by apache.
the class TextPaneSkinTextNodeView method detach.
@Override
protected void detach() {
super.detach();
TextNode textNode = (TextNode) getNode();
textNode.getTextNodeListeners().remove(this);
}
use of org.apache.pivot.wtk.text.TextNode in project pivot by apache.
the class TextPaneSkinTextNodeView method attach.
@Override
protected void attach() {
super.attach();
TextNode textNode = (TextNode) getNode();
textNode.getTextNodeListeners().add(this);
}
Aggregations