use of org.apache.pivot.wtk.text.Node in project pivot by apache.
the class TextPane method paste.
public void paste() {
if (document == null) {
setDocument(new Document());
}
Manifest clipboardContent = Clipboard.getContent();
if (clipboardContent != null && clipboardContent.containsText()) {
// Paste the string representation of the content
String text = null;
try {
// Replace \r\n with just \n and plain \r with \n instead
// so we can deal with lines uniformly below.
text = clipboardContent.getText().replace("\r\n", "\n").replace("\r", "\n");
} catch (IOException exception) {
// No-op
}
if (text != null && text.length() > 0) {
// Insert the clipboard contents
int n = 0;
int start = selectionStart;
int tabWidth = ((TextPane.Skin) getSkin()).getTabWidth();
Node currentNode = document.getDescendantAt(start);
while (!(currentNode instanceof Paragraph)) {
currentNode = currentNode.getParent();
}
int paragraphStartOffset = start - currentNode.getDocumentOffset();
bulkOperation = true;
// If there is only a line fragment here, then just insert it
int eolIndex = text.indexOf('\n');
if (eolIndex < 0) {
if (expandTabs) {
n = insertWithTabSubstitution(text, start, paragraphStartOffset, tabWidth);
} else {
insertText(text, start);
n = text.length();
}
} else {
int textOffset = 0;
// Insert each line into place, with a new paragraph following
while (eolIndex >= 0) {
String fragment = text.substring(textOffset, eolIndex);
int len;
if (expandTabs) {
len = insertWithTabSubstitution(fragment, start, paragraphStartOffset, tabWidth);
} else {
insertText(fragment, start);
len = fragment.length();
}
insertParagraph();
n += len + 1;
start += len + 1;
paragraphStartOffset = 0;
textOffset = eolIndex + 1;
eolIndex = text.indexOf('\n', textOffset);
}
// Now deal with any leftover at end of string
if (textOffset < text.length()) {
String lastFragment = text.substring(textOffset);
if (expandTabs) {
n += insertWithTabSubstitution(lastFragment, start, paragraphStartOffset, tabWidth);
} else {
insertText(lastFragment, start);
n += lastFragment.length();
}
}
}
bulkOperation = false;
textPaneCharacterListeners.charactersInserted(this, start, n);
}
}
}
use of org.apache.pivot.wtk.text.Node in project pivot by apache.
the class TextPane method insertParagraph.
public void insertParagraph() {
checkDocumentExists();
if (selectionLength > 0) {
removeDocumentRange(selectionStart, selectionLength);
}
// Walk up the tree until we find a paragraph
Node descendant = document.getDescendantAt(selectionStart);
while (!(descendant instanceof Paragraph)) {
descendant = descendant.getParent();
}
// Split the paragraph at the insertion point
Paragraph leadingSegment = (Paragraph) descendant;
int offset = selectionStart - leadingSegment.getDocumentOffset();
int characterCount = leadingSegment.getCharacterCount() - offset;
Paragraph trailingSegment = (Paragraph) leadingSegment.removeRange(offset, characterCount);
Element parent = leadingSegment.getParent();
int index = parent.indexOf(leadingSegment);
parent.insert(trailingSegment, index + 1);
// Set the selection start to the character following the insertion
setSelection(selectionStart + 1, selectionLength);
}
use of org.apache.pivot.wtk.text.Node in project pivot by apache.
the class TextPane method addToText.
/**
* Add the text from the given element (and its children) to the given buffer,
* respecting the range of characters to be included.
*
* @param text The buffer we're building.
* @param element The current element in the document.
* @param includeSpan The range of text to be included (in document-relative
* coordinates).
*/
private void addToText(StringBuilder text, Element element, Span includeSpan) {
Span elementSpan = element.getDocumentSpan();
Span elementIntersection = elementSpan.intersect(includeSpan);
if (elementIntersection != null) {
for (Node node : element) {
if (node instanceof Element) {
addToText(text, (Element) node, includeSpan);
} else {
Span nodeSpan = node.getDocumentSpan();
Span nodeIntersection = nodeSpan.intersect(includeSpan);
if (nodeIntersection != null) {
Span currentSpan = nodeIntersection.offset(-nodeSpan.start);
if (node instanceof TextNode) {
text.append(((TextNode) node).getCharacters(currentSpan));
} else if (node instanceof ComponentNode) {
text.append(((ComponentNode) node).getCharacters(currentSpan));
}
// TODO: anything more that could/should be handled?
// lists for instance???
}
}
}
if (element instanceof Paragraph && elementIntersection.end == elementSpan.end) {
// TODO: unclear if this is included in the character count for a paragraph or not
// or what that means for the intersection range above
text.append('\n');
}
}
}
use of org.apache.pivot.wtk.text.Node in project pivot by apache.
the class TextPaneSkinElementView method attach.
@Override
protected void attach() {
super.attach();
Element element = (Element) getNode();
element.getElementListeners().add(this);
// Attach child node views
for (Node node : element) {
add(createNodeView(getTextPaneSkin(), node));
}
}
use of org.apache.pivot.wtk.text.Node in project pivot by apache.
the class TextPaneDemo method applyStyle.
private void applyStyle(Document document, Span selectionSpan, StyleApplicator styleApplicator) {
// I can't apply the styles while iterating over the tree, because I
// need to update the tree.
// So first collect a list of all the nodes in the tree.
List<Node> nodeList = new ArrayList<>();
collectNodes(document, nodeList);
final int selectionStart = textPane.getSelectionStart();
final int selectionLength = textPane.getSelectionLength();
for (Node node : nodeList) {
if (node instanceof TextSpan) {
TextSpan span = (TextSpan) node;
int documentOffset = node.getDocumentOffset();
int characterCount = node.getCharacterCount();
Span textSpan = new Span(documentOffset, documentOffset + characterCount - 1);
if (selectionSpan.intersects(textSpan)) {
applyStyleToSpanNode(selectionSpan, styleApplicator, span, characterCount, textSpan);
}
}
if (node instanceof org.apache.pivot.wtk.text.TextNode) {
org.apache.pivot.wtk.text.TextNode textNode = (org.apache.pivot.wtk.text.TextNode) node;
int documentOffset = node.getDocumentOffset();
int characterCount = node.getCharacterCount();
Span textSpan = new Span(documentOffset, documentOffset + characterCount - 1);
if (selectionSpan.intersects(textSpan)) {
applyStyleToTextNode(selectionSpan, styleApplicator, textNode, characterCount, textSpan);
}
}
}
// maintain the selected range
textPane.setSelection(selectionStart, selectionLength);
}
Aggregations