use of org.apache.pivot.wtk.text.Element 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.Element 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.Element 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.Element in project pivot by apache.
the class TextPaneSkinElementView method detach.
@Override
protected void detach() {
Element element = (Element) getNode();
element.getElementListeners().remove(this);
// Detach child node views
for (TextPaneSkinNodeView nodeView : this) {
nodeView.detach();
}
super.detach();
}
use of org.apache.pivot.wtk.text.Element in project pivot by apache.
the class TextPaneSkinTextNodeView method childLayout.
@Override
protected void childLayout(int breakWidth) {
TextNode textNode = (TextNode) getNode();
textLayout = null;
Font effectiveFont = getEffectiveFont();
FontRenderContext fontRenderContext = Platform.getFontRenderContext();
TextPane textPane = (TextPane) getTextPaneSkin().getComponent();
AttributedStringCharacterIterator composedText = textPane.getComposedText();
Element parent = textNode.getParent();
// The calculations below really need to know if we're at the end of the paragraph
// when composing text, so make sure we ignore intervening span or other nodes, but
// also update the offset relative to the paragraph in the process.
int relStart = start;
if (parent != null && !(parent instanceof Paragraph)) {
relStart += parent.getOffset();
parent = parent.getParent();
}
int parentCount = parent == null ? 0 : parent.getCharacterCount();
int selectionStart = textPane.getSelectionStart();
int selectionLength = textPane.getSelectionLength();
int documentOffset = textNode.getDocumentOffset();
int charCount = textNode.getCharacterCount();
boolean composedIntersects = false;
if (composedText != null) {
int composedTextBegin = composedText.getBeginIndex();
int composedTextEnd = composedText.getEndIndex();
int composedTextLength = composedTextEnd - composedTextBegin;
/* exclusive - inclusive, so no +1 needed */
// If this text node is at the end of a paragraph, increase the span by 1 for the newline
Span ourSpan;
if (parent instanceof Paragraph && charCount + relStart == parentCount - 1) {
ourSpan = new Span(documentOffset + start, documentOffset + charCount);
} else {
ourSpan = new Span(documentOffset + start, documentOffset + charCount - 1);
}
// The "composed span" just encompasses the start position, because this is "phantom" text, so it exists between any two other "real" text positions.
Span composedSpan = new Span(selectionStart + composedTextBegin);
composedIntersects = composedSpan.intersects(ourSpan);
}
if (charCount == 0 && !composedIntersects) {
Dimensions charSize = GraphicsUtilities.getAverageCharacterSize(effectiveFont);
setSize(0, charSize.height);
length = 0;
next = null;
} else {
AttributedCharacterIterator text = null;
boolean underlined = getEffectiveUnderline();
boolean struckthrough = getEffectiveStrikethrough();
if (composedText != null && composedIntersects) {
int composedPos = selectionStart - documentOffset;
if (composedPos == 0) {
if (charCount - start == 0) {
text = composedText;
} else {
AttributedStringCharacterIterator fullText = getCharIterator(textNode, start, charCount, effectiveFont);
// Note: only apply the underline and strikethrough to our text, not the composed text
fullText.addUnderlineAttribute(underlined);
fullText.addStrikethroughAttribute(struckthrough);
text = new CompositeIterator(composedText, fullText);
}
} else if (composedPos == charCount) {
// Composed text is at the end
AttributedStringCharacterIterator fullText = getCharIterator(textNode, start, charCount, effectiveFont);
// Note: only apply the underline and strikethrough to our text, not the composed text
fullText.addUnderlineAttribute(underlined);
fullText.addStrikethroughAttribute(struckthrough);
text = new CompositeIterator(fullText, composedText);
} else {
// Composed text is somewhere in the middle
AttributedStringCharacterIterator leadingText = getCharIterator(textNode, start, composedPos, effectiveFont);
leadingText.addUnderlineAttribute(underlined);
leadingText.addStrikethroughAttribute(struckthrough);
AttributedStringCharacterIterator trailingText = getCharIterator(textNode, composedPos, charCount, effectiveFont);
trailingText.addUnderlineAttribute(underlined);
trailingText.addStrikethroughAttribute(struckthrough);
text = new CompositeIterator(leadingText, composedText, trailingText);
}
} else {
AttributedStringCharacterIterator fullText = getCharIterator(textNode, start, charCount, effectiveFont);
fullText.addUnderlineAttribute(underlined);
fullText.addStrikethroughAttribute(struckthrough);
text = fullText;
}
if (getTextPaneSkin().getWrapText()) {
LineBreakMeasurer measurer = new LineBreakMeasurer(text, fontRenderContext);
float wrappingWidth = (float) breakWidth;
textLayout = measurer.nextLayout(wrappingWidth);
length = textLayout.getCharacterCount();
Dimensions size = getTextSize(textLayout);
float advance = textLayout.getAdvance();
setSize(size);
if (start + measurer.getPosition() < textNode.getCharacterCount()) {
next = new TextPaneSkinTextNodeView(getTextPaneSkin(), textNode, start + measurer.getPosition());
next.setParent(getParent());
} else {
next = null;
}
} else {
// Not wrapping the text, then the width is of the whole thing
textLayout = new TextLayout(text, fontRenderContext);
length = textLayout.getCharacterCount();
Dimensions size = getTextSize(textLayout);
float advance = textLayout.getAdvance();
setSize(size);
// set to null in case this node used to be broken across multiple,
// but is no longer
next = null;
}
}
}
Aggregations