use of org.apache.pivot.wtk.TextPane in project pivot by apache.
the class TextPaneSkin method getCaretRectangle.
private Rectangle getCaretRectangle(TextHitInfo textCaret) {
TextPane textPane = getTextPane();
AttributedStringCharacterIterator composedText = textPane.getComposedText();
// Special case that tweaks the bounds at the end of line so that the entire
// composed text width isn't added in here.... (yeah, I know it's ugly...)
int selectionStart = textPane.getSelectionStart();
this.doingCaretCalculations = true;
Bounds selectionStartBounds = getCharacterBounds(selectionStart);
this.doingCaretCalculations = false;
// without the "doingCaretCalculations" flag to get back something non-null
if (selectionStartBounds == null) {
selectionStartBounds = getCharacterBounds(selectionStart);
org.apache.pivot.util.Console.logMethod("****", "null selection bounds: selectionStart=%1$d, updated bounds=%2$s", selectionStart, selectionStartBounds);
}
return GraphicsUtilities.getCaretRectangle(textCaret, composedText, selectionStartBounds.x, selectionStartBounds.y);
}
use of org.apache.pivot.wtk.TextPane in project pivot by apache.
the class TextPaneSkin method install.
@Override
public void install(Component component) {
super.install(component);
TextPane textPane = (TextPane) component;
textPane.getTextPaneListeners().add(this);
textPane.getTextPaneSelectionListeners().add(this);
textPane.setCursor(Cursor.TEXT);
Document document = textPane.getDocument();
if (document != null) {
documentView = (TextPaneSkinDocumentView) TextPaneSkinNodeView.createNodeView(this, document);
documentView.attach();
updateSelection();
}
}
use of org.apache.pivot.wtk.TextPane in project pivot by apache.
the class TextPaneSkin method mouseMove.
@Override
public boolean mouseMove(Component component, int x, int y) {
boolean consumed = super.mouseMove(component, x, y);
if (Mouse.getCapturer() == component) {
TextPane textPane = getTextPane();
Bounds visibleArea = textPane.getVisibleArea();
visibleArea = new Bounds(visibleArea.x, visibleArea.y, visibleArea.width, visibleArea.height);
if (y >= visibleArea.y && y < visibleArea.y + visibleArea.height) {
// Stop the scroll selection timer
if (scheduledScrollSelectionCallback != null) {
scheduledScrollSelectionCallback.cancel();
scheduledScrollSelectionCallback = null;
}
scrollDirection = null;
int offset = getInsertionPoint(x, y);
if (offset != -1) {
// Select the range
if (offset > anchor) {
textPane.setSelection(anchor, offset - anchor);
} else {
textPane.setSelection(offset, anchor - offset);
}
}
} else {
if (scheduledScrollSelectionCallback == null) {
scrollDirection = (y < visibleArea.y) ? TextPane.ScrollDirection.UP : TextPane.ScrollDirection.DOWN;
scheduledScrollSelectionCallback = ApplicationContext.runAndScheduleRecurringCallback(scrollSelectionCallback, SCROLL_RATE);
}
}
mouseX = x;
} else {
if (Mouse.isPressed(Mouse.Button.LEFT) && Mouse.getCapturer() == null && anchor != -1) {
// Capture the mouse so we can select text
Mouse.capture(component);
}
}
return consumed;
}
use of org.apache.pivot.wtk.TextPane in project pivot by apache.
the class TextPaneSkin method mouseClick.
@Override
public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
boolean consumed = super.mouseClick(component, button, x, y, count);
TextPane textPane = (TextPane) component;
Document document = textPane.getDocument();
if (button == Mouse.Button.LEFT) {
int index = getInsertionPoint(x, y);
if (index != -1) {
if (count == 2) {
CharSpan charSpan = CharUtils.selectWord(getRowCharacters(document, index), index);
if (charSpan != null) {
textPane.setSelection(charSpan);
}
} else if (count == 3) {
textPane.setSelection(getRowOffset(document, index), getRowLength(document, index));
}
}
}
return consumed;
}
use of org.apache.pivot.wtk.TextPane in project pivot by apache.
the class TextPaneSkinTextNodeView method paint.
@Override
public void paint(Graphics2D graphics) {
if (textLayout != null) {
TextPane textPane = (TextPane) getTextPaneSkin().getComponent();
FontRenderContext fontRenderContext = Platform.getFontRenderContext();
Font effectiveFont = getEffectiveFont();
LineMetrics lm = effectiveFont.getLineMetrics("", fontRenderContext);
float ascent = lm.getAscent() + lm.getLeading();
graphics.setFont(effectiveFont);
int selectionStart = textPane.getSelectionStart();
int selectionLength = textPane.getSelectionLength();
Span selectionRange = new Span(selectionStart, selectionStart + selectionLength - 1);
int documentOffset = getDocumentOffset();
Span characterRange = new Span(documentOffset, documentOffset + getCharacterCount() - 1);
int width = getWidth();
int height = getHeight();
if (selectionLength > 0 && characterRange.intersects(selectionRange)) {
// Determine the selection bounds
int x0;
if (selectionRange.start > characterRange.start) {
Bounds leadingSelectionBounds = getCharacterBounds(selectionRange.start - documentOffset);
x0 = leadingSelectionBounds.x;
} else {
x0 = 0;
}
int x1;
if (selectionRange.end < characterRange.end) {
Bounds trailingSelectionBounds = getCharacterBounds(selectionRange.end - documentOffset);
x1 = trailingSelectionBounds.x + trailingSelectionBounds.width;
} else {
x1 = width;
}
int selectionWidth = x1 - x0;
Rectangle selection = new Rectangle(x0, 0, selectionWidth, height);
// Paint the unselected text
Area unselectedArea = new Area();
unselectedArea.add(new Area(new Rectangle(0, 0, width, height)));
unselectedArea.subtract(new Area(selection));
Graphics2D textGraphics = (Graphics2D) graphics.create();
textGraphics.setColor(getEffectiveForegroundColor());
textGraphics.clip(unselectedArea);
textLayout.draw(textGraphics, 0, ascent);
textGraphics.dispose();
// Paint the selection
Color selectionColor;
if (textPane.isFocused()) {
selectionColor = getTextPaneSkin().getSelectionColor();
} else {
selectionColor = getTextPaneSkin().getInactiveSelectionColor();
}
Graphics2D selectedTextGraphics = (Graphics2D) graphics.create();
selectedTextGraphics.setColor(textPane.isFocused() && textPane.isEditable() ? selectionColor : getTextPaneSkin().getInactiveSelectionColor());
selectedTextGraphics.clip(selection.getBounds());
textLayout.draw(selectedTextGraphics, 0, ascent);
selectedTextGraphics.dispose();
} else {
// Draw the text
graphics.setColor(getEffectiveForegroundColor());
textLayout.draw(graphics, 0, ascent);
}
}
}
Aggregations