use of org.apache.pivot.wtk.TextPane 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;
}
}
}
use of org.apache.pivot.wtk.TextPane in project pivot by apache.
the class HyperlinkButtonTest method startup.
@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
frame = new Frame();
frame.setTitle("Hyperlink Button Test");
frame.setPreferredSize(480, 360);
HyperlinkButton button1 = new HyperlinkButton("http://pivot.apache.org");
HyperlinkButton button2 = new HyperlinkButton("Apache website", "http://apache.org");
TextPane textPane = new TextPane();
Document document = new Document();
TextNode text1 = new TextNode("Link to the Apache Pivot site: ");
TextNode text2 = new TextNode("Main Apache Software Foundation website: ");
ComponentNode compNode1 = new ComponentNode(button1);
ComponentNode compNode2 = new ComponentNode(button2);
Paragraph para1 = new Paragraph();
para1.add(text1);
document.add(para1);
document.add(compNode1);
Paragraph para2 = new Paragraph();
para2.add(text2);
document.add(para2);
document.add(compNode2);
ImageNode image1 = new ImageNode("/org/apache/pivot/tests/house.png");
document.add(image1);
textPane.setDocument(document);
frame.setContent(textPane);
frame.open(display);
}
use of org.apache.pivot.wtk.TextPane in project pivot by apache.
the class TextPaneSkin method mouseDown.
@Override
public boolean mouseDown(Component component, Mouse.Button button, int x, int y) {
boolean consumed = super.mouseDown(component, button, x, y);
if (button == Mouse.Button.LEFT) {
TextPane textPane = (TextPane) component;
anchor = getInsertionPoint(x, y);
if (anchor != -1) {
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
// Select the range
int selectionStart = textPane.getSelectionStart();
if (anchor > selectionStart) {
textPane.setSelection(selectionStart, anchor - selectionStart);
} else {
textPane.setSelection(anchor, selectionStart - anchor);
}
} else {
// Move the caret to the insertion point
textPane.setSelection(anchor, 0);
consumed = true;
}
}
caretX = caret.x;
// Set focus to the text input
textPane.requestFocus();
}
return consumed;
}
use of org.apache.pivot.wtk.TextPane in project pivot by apache.
the class TextPaneSkin method paint.
@Override
public void paint(Graphics2D graphics) {
super.paint(graphics);
TextPane textPane = getTextPane();
if (documentView != null) {
// Draw the selection highlight
if (selection != null) {
graphics.setColor(textPane.isFocused() && textPane.isEditable() ? selectionBackgroundColor : inactiveSelectionBackgroundColor);
graphics.fill(selection);
}
int width = getWidth();
int breakWidth;
if (wrapText) {
breakWidth = Math.max(width - margin.getWidth(), 0);
} else {
breakWidth = Integer.MAX_VALUE;
}
documentView.layout(breakWidth);
// Draw the document content
graphics.translate(margin.left, margin.top);
documentView.paint(graphics);
graphics.translate(-margin.left, -margin.top);
// Draw the caret
if (selection == null && caretOn && textPane.isFocused()) {
graphics.setColor(textPane.isEditable() ? color : inactiveColor);
graphics.fill(caret);
}
}
}
use of org.apache.pivot.wtk.TextPane in project pivot by apache.
the class TextPaneSkin method keyTyped.
@Override
public boolean keyTyped(final Component component, char character) {
boolean consumed = super.keyTyped(component, character);
final TextPane textPane = getTextPane();
if (textPane.isEditable()) {
Document document = textPane.getDocument();
if (document != null) {
// character as well as meta key presses
if (character > 0x1F && character != 0x7F && !Keyboard.isPressed(Keyboard.Modifier.META)) {
textPane.insert(character);
showCaret(true);
}
}
}
return consumed;
}
Aggregations