use of org.apache.pivot.wtk.text.Node in project pivot by apache.
the class TextPaneDemo method collectNodes.
private void collectNodes(org.apache.pivot.wtk.text.Node node, List<Node> nodeList) {
// don't worry about the text-nodes that are children of Span nodes.
if (node instanceof TextSpan) {
return;
}
if (node instanceof org.apache.pivot.wtk.text.Element) {
Element element = (Element) node;
for (Node child : element) {
nodeList.add(child);
collectNodes(child, nodeList);
}
}
}
use of org.apache.pivot.wtk.text.Node in project pivot by apache.
the class TextPaneDemo method applyAlignmentStyle.
private void applyAlignmentStyle(HorizontalAlignment horizontalAlignment) {
Node node = textPane.getDocument().getDescendantAt(textPane.getSelectionStart());
while (node != null && !(node instanceof Paragraph)) {
node = node.getParent();
}
if (node != null) {
Paragraph paragraph = (Paragraph) node;
paragraph.setHorizontalAlignment(horizontalAlignment);
}
}
use of org.apache.pivot.wtk.text.Node 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.Node in project pivot by apache.
the class TextPaneDemo method applyStyleToSpanNode.
private static void applyStyleToSpanNode(Span selectionSpan, StyleApplicator styleApplicator, TextSpan spanNode, int characterCount, Span textSpan) {
if (selectionSpan.contains(textSpan)) {
// if the span-node is contained wholly inside the
// selection, apply the style
styleApplicator.apply(spanNode);
} else if (selectionSpan.start <= textSpan.start) {
// if the selection covers the first part of the span-node, split
// off the first part of the span-node, and apply the style to it
int intersectionLength = selectionSpan.end - textSpan.start + 1;
TextSpan node1 = spanNode.getRange(0, intersectionLength);
styleApplicator.apply(node1);
Node node2 = spanNode.getRange(intersectionLength, characterCount - intersectionLength);
Element parent = spanNode.getParent();
int index = parent.remove(spanNode);
parent.insert(node1, index);
parent.insert(node2, index + 1);
} else if (selectionSpan.end >= textSpan.end) {
// if the selection covers the last part of the span-node, split off
// the last part of the span-node, and apply the style to it
int intersectionStart = selectionSpan.start - textSpan.start;
TextSpan part1 = spanNode.getRange(0, intersectionStart);
TextSpan part2 = spanNode.getRange(intersectionStart, characterCount - intersectionStart);
styleApplicator.apply(part2);
Element parent = spanNode.getParent();
int index = parent.remove(spanNode);
parent.insert(part1, index);
parent.insert(part2, index + 1);
} else {
// if the selection covers an internal part of the span-node, split the
// span-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;
TextSpan part1 = spanNode.getRange(0, part2Start);
TextSpan part2 = spanNode.getRange(part2Start, part2End - part2Start);
TextSpan part3 = spanNode.getRange(part2End, characterCount - part2End);
styleApplicator.apply(part2);
Element parent = spanNode.getParent();
int index = parent.remove(spanNode);
parent.insert(part1, index);
parent.insert(part2, index + 1);
parent.insert(part3, index + 2);
}
}
use of org.apache.pivot.wtk.text.Node 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);
}
Aggregations