StyleApplicator styleApplicator, org.apache.pivot.wtk.text.TextNode textNode,
int characterCount, org.apache.pivot.wtk.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();
org.apache.pivot.wtk.text.Span newSpanNode = new org.apache.pivot.wtk.text.Span();
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;
String part1 = textNode.getSubstring(0, intersectionLength);
String part2 = textNode.getSubstring(intersectionLength, characterCount);
org.apache.pivot.wtk.text.Span newSpanNode = new org.apache.pivot.wtk.text.Span();
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);
org.apache.pivot.wtk.text.Span newSpanNode = new org.apache.pivot.wtk.text.Span();
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);
org.apache.pivot.wtk.text.Span newSpanNode = new org.apache.pivot.wtk.text.Span();
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);
}
}