use of org.apache.pivot.wtk.Bounds in project pivot by apache.
the class TreeViewNodeEditor method beginEdit.
@Override
public void beginEdit(TreeView treeViewArgument, Path pathArgument) {
this.treeView = treeViewArgument;
this.path = pathArgument;
// Get the data being edited
List<?> treeData = treeViewArgument.getTreeData();
TreeNode treeNode = (TreeNode) Sequence.Tree.get(treeData, pathArgument);
String text = treeNode.getText();
textInput.setText(text != null ? text : "");
textInput.selectAll();
// Get the node bounds
Bounds nodeBounds = treeViewArgument.getNodeBounds(pathArgument);
int nodeIndent = treeViewArgument.getNodeIndent(pathArgument.getLength());
nodeBounds = new Bounds(nodeBounds.x + nodeIndent, nodeBounds.y, nodeBounds.width - nodeIndent, nodeBounds.height);
// Render the node data
TreeViewNodeRenderer nodeRenderer = (TreeViewNodeRenderer) treeViewArgument.getNodeRenderer();
nodeRenderer.render(treeNode, pathArgument, treeViewArgument.getRowIndex(pathArgument), treeViewArgument, false, false, TreeView.NodeCheckState.UNCHECKED, false, false);
nodeRenderer.setSize(nodeBounds.width, nodeBounds.height);
// Get the text bounds
Bounds textBounds = nodeRenderer.getTextBounds();
// Calculate the bounds of what is being edited
Insets padding = (Insets) textInput.getStyles().get(Style.padding);
Bounds editBounds = new Bounds(nodeBounds.x + textBounds.x - (padding.left + 1), nodeBounds.y, nodeBounds.width - textBounds.x + (padding.left + 1), nodeBounds.height);
// Scroll to make the node as visible as possible
treeViewArgument.scrollAreaToVisible(editBounds.x, editBounds.y, textBounds.width + padding.left + 1, editBounds.height);
// Constrain the bounds by what is visible through viewport ancestors
editBounds = treeViewArgument.getVisibleArea(editBounds);
Point location = treeViewArgument.mapPointToAncestor(treeViewArgument.getDisplay(), editBounds.x, editBounds.y);
textInput.setPreferredWidth(editBounds.width);
setLocation(location.x, location.y + (editBounds.height - getPreferredHeight(-1)) / 2);
// Open the editor
open(treeViewArgument.getWindow());
}
use of org.apache.pivot.wtk.Bounds in project pivot by apache.
the class TranslationDecorator method getBounds.
@Override
public Bounds getBounds(Component component) {
int width = component.getWidth();
int height = component.getHeight();
Bounds bounds = new Bounds(x, y, width, height);
if (clip) {
bounds = bounds.intersect(0, 0, width, height);
}
return bounds;
}
use of org.apache.pivot.wtk.Bounds in project pivot by apache.
the class ScrollPaneSkin method scrollLeftChanged.
@Override
public void scrollLeftChanged(Viewport viewport, int previousScrollLeft) {
// NOTE we don't invalidate the component here because we need only
// reposition the view and column header. Invalidating would yield
// the correct positioning, but it would do much more work than needed.
ScrollPane scrollPane = (ScrollPane) viewport;
Component view = scrollPane.getView();
Component rowHeader = scrollPane.getRowHeader();
Component columnHeader = scrollPane.getColumnHeader();
int rowHeaderWidth = 0;
if (rowHeader != null) {
rowHeaderWidth = rowHeader.getWidth();
}
int scrollLeft = scrollPane.getScrollLeft();
if (view != null && view.isShowing() && isOptimizeScrolling()) {
Bounds blitArea = view.getVisibleArea();
int blitX = blitArea.x + view.getX();
int blitY = blitArea.y + view.getY();
int blitWidth = blitArea.width;
int blitHeight = blitArea.height;
if (columnHeader != null) {
// Blit the column header as well
int columnHeaderHeight = columnHeader.getHeight();
blitY -= columnHeaderHeight;
blitHeight += columnHeaderHeight;
}
int deltaScrollLeft = scrollLeft - previousScrollLeft;
blitX += Math.max(deltaScrollLeft, 0);
blitWidth -= Math.abs(deltaScrollLeft);
Graphics2D graphics = scrollPane.getGraphics();
graphics.copyArea(blitX, blitY, blitWidth, blitHeight, -deltaScrollLeft, 0);
scrollPane.setConsumeRepaint(true);
try {
view.setLocation(rowHeaderWidth - scrollLeft, view.getY());
if (columnHeader != null) {
columnHeader.setLocation(rowHeaderWidth - scrollLeft, 0);
}
} finally {
scrollPane.setConsumeRepaint(false);
}
boolean repaintAllViewport = scrollPane.isRepaintAllViewport();
if (!repaintAllViewport) {
scrollPane.repaint(rowHeaderWidth + (deltaScrollLeft > 0 ? blitWidth : 0), blitY, Math.abs(deltaScrollLeft), blitHeight, true);
} else {
Bounds viewportBounds = getViewportBounds();
scrollPane.repaint(viewportBounds.x, viewportBounds.y, viewportBounds.width, viewportBounds.height, true);
}
} else {
if (view != null) {
view.setLocation(rowHeaderWidth - scrollLeft, view.getY());
}
if (columnHeader != null) {
columnHeader.setLocation(rowHeaderWidth - scrollLeft, 0);
}
}
if (scrollLeft >= 0 && scrollLeft <= getMaxScrollLeft()) {
horizontalScrollBar.setValue(scrollLeft);
}
}
use of org.apache.pivot.wtk.Bounds in project pivot by apache.
the class TextAreaSkin method scrollCharacterToVisible.
private void scrollCharacterToVisible(int index) {
Bounds characterBounds = getCharacterBounds(index);
if (characterBounds != null) {
TextArea textArea = (TextArea) getComponent();
textArea.scrollAreaToVisible(characterBounds.x, characterBounds.y, characterBounds.width, characterBounds.height);
}
}
use of org.apache.pivot.wtk.Bounds in project pivot by apache.
the class TextAreaSkin method keyPressed.
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
boolean consumed = false;
if (paragraphViews.getLength() > 0) {
TextArea textArea = (TextArea) getComponent();
boolean commandPressed = Keyboard.isPressed(Platform.getCommandModifier());
boolean wordNavPressed = Keyboard.isPressed(Platform.getWordNavigationModifier());
boolean shiftPressed = Keyboard.isPressed(Keyboard.Modifier.SHIFT);
boolean ctrlPressed = Keyboard.isPressed(Keyboard.Modifier.CTRL);
boolean metaPressed = Keyboard.isPressed(Keyboard.Modifier.META);
boolean isEditable = textArea.isEditable();
if (keyCode == Keyboard.KeyCode.ENTER && acceptsEnter && isEditable && Keyboard.getModifiers() == 0) {
int index = textArea.getSelectionStart();
textArea.removeText(index, textArea.getSelectionLength());
textArea.insertText("\n", index);
consumed = true;
} else if (keyCode == Keyboard.KeyCode.DELETE && isEditable) {
int index = textArea.getSelectionStart();
if (index < textArea.getCharacterCount()) {
int count = Math.max(textArea.getSelectionLength(), 1);
textArea.removeText(index, count);
anchor = -1;
consumed = true;
}
} else if (keyCode == Keyboard.KeyCode.BACKSPACE && isEditable) {
int index = textArea.getSelectionStart();
int count = textArea.getSelectionLength();
if (count == 0 && index > 0) {
textArea.removeText(index - 1, 1);
consumed = true;
} else {
textArea.removeText(index, count);
consumed = true;
}
anchor = -1;
} else if (keyCode == Keyboard.KeyCode.TAB && (acceptsTab != ctrlPressed) && isEditable) {
int selectionStart = textArea.getSelectionStart();
int selectionLength = textArea.getSelectionLength();
int rowOffset = textArea.getRowOffset(selectionStart);
int linePos = selectionStart - rowOffset;
StringBuilder tabBuilder = new StringBuilder(tabWidth);
for (int i = 0; i < tabWidth - (linePos % tabWidth); i++) {
tabBuilder.append(" ");
}
if (textArea.getCharacterCount() - selectionLength + tabWidth > textArea.getMaximumLength()) {
Toolkit.getDefaultToolkit().beep();
} else {
textArea.removeText(selectionStart, selectionLength);
textArea.insertText(tabBuilder, selectionStart);
}
showCaret(true);
consumed = true;
} else if (keyCode == Keyboard.KeyCode.HOME || (keyCode == Keyboard.KeyCode.LEFT && metaPressed)) {
int start;
int selectionStart = textArea.getSelectionStart();
int selectionLength = textArea.getSelectionLength();
if (commandPressed) {
// Move the caret to the beginning of the text
start = 0;
} else {
// Move the caret to the beginning of the line
start = getRowOffset(selectionStart);
}
if (shiftPressed) {
selectionLength += selectionStart - start;
} else {
selectionLength = 0;
}
if (selectionStart >= 0) {
textArea.setSelection(start, selectionLength);
scrollCharacterToVisible(start);
caretX = caret.x;
consumed = true;
}
} else if (keyCode == Keyboard.KeyCode.END || (keyCode == Keyboard.KeyCode.RIGHT && metaPressed)) {
int end;
int selectionStart = textArea.getSelectionStart();
int selectionLength = textArea.getSelectionLength();
int index = selectionStart + selectionLength;
if (commandPressed) {
// Move the caret to end of the text
end = textArea.getCharacterCount();
} else {
// Move the caret to the end of the line
int rowOffset = getRowOffset(index);
int rowLength = getRowLength(index);
end = rowOffset + rowLength;
}
if (shiftPressed) {
selectionLength += end - index;
} else {
selectionStart = end;
if (selectionStart < textArea.getCharacterCount() && textArea.getCharacterAt(selectionStart) != '\n') {
selectionStart--;
}
selectionLength = 0;
}
if (selectionStart + selectionLength <= textArea.getCharacterCount()) {
textArea.setSelection(selectionStart, selectionLength);
scrollCharacterToVisible(selectionStart + selectionLength);
caretX = caret.x;
if (selection != null) {
caretX += selection.getBounds2D().getWidth();
}
consumed = true;
}
} else if (keyCode == Keyboard.KeyCode.LEFT) {
int selectionStart = textArea.getSelectionStart();
int selectionLength = textArea.getSelectionLength();
if (wordNavPressed) {
// Move the caret to the start of the next word to the left
if (selectionStart > 0) {
// Skip over any space immediately to the left
int index = selectionStart;
while (index > 0 && Character.isWhitespace(textArea.getCharacterAt(index - 1))) {
index--;
}
// Skip over any word-letters to the left
while (index > 0 && !Character.isWhitespace(textArea.getCharacterAt(index - 1))) {
index--;
}
if (shiftPressed) {
selectionLength += selectionStart - index;
} else {
selectionLength = 0;
}
selectionStart = index;
}
} else if (shiftPressed) {
if (anchor != -1) {
if (selectionStart < anchor) {
if (selectionStart > 0) {
selectionStart--;
selectionLength++;
}
} else {
if (selectionLength > 0) {
selectionLength--;
} else {
selectionStart--;
selectionLength++;
}
}
} else {
// Add the previous character to the selection
anchor = selectionStart;
if (selectionStart > 0) {
selectionStart--;
selectionLength++;
}
}
} else {
// Move the caret back by one character
if (selectionLength == 0 && selectionStart > 0) {
selectionStart--;
}
// Clear the selection
anchor = -1;
selectionLength = 0;
}
if (selectionStart >= 0) {
textArea.setSelection(selectionStart, selectionLength);
scrollCharacterToVisible(selectionStart);
caretX = caret.x;
consumed = true;
}
} else if (keyCode == Keyboard.KeyCode.RIGHT) {
int selectionStart = textArea.getSelectionStart();
int selectionLength = textArea.getSelectionLength();
if (wordNavPressed) {
// Move the caret to the start of the next word to the right
if (selectionStart < textArea.getCharacterCount()) {
int index = selectionStart + selectionLength;
// Skip over any space immediately to the right
while (index < textArea.getCharacterCount() && Character.isWhitespace(textArea.getCharacterAt(index))) {
index++;
}
// Skip over any word-letters to the right
while (index < textArea.getCharacterCount() && !Character.isWhitespace(textArea.getCharacterAt(index))) {
index++;
}
if (shiftPressed) {
selectionLength = index - selectionStart;
} else {
selectionStart = index;
selectionLength = 0;
}
}
} else if (shiftPressed) {
if (anchor != -1) {
if (selectionStart < anchor) {
selectionStart++;
selectionLength--;
} else {
selectionLength++;
}
} else {
// Add the next character to the selection
anchor = selectionStart;
selectionLength++;
}
} else {
// Move the caret forward by one character
if (selectionLength == 0) {
selectionStart++;
} else {
selectionStart += selectionLength;
}
// Clear the selection
anchor = -1;
selectionLength = 0;
}
if (selectionStart + selectionLength <= textArea.getCharacterCount()) {
textArea.setSelection(selectionStart, selectionLength);
scrollCharacterToVisible(selectionStart + selectionLength);
caretX = caret.x;
if (selection != null) {
caretX += selection.getBounds2D().getWidth();
}
consumed = true;
}
} else if (keyCode == Keyboard.KeyCode.UP) {
int selectionStart = textArea.getSelectionStart();
int selectionLength = textArea.getSelectionLength();
int index = -1;
if (shiftPressed) {
if (anchor == -1) {
anchor = selectionStart;
index = getNextInsertionPoint(caretX, selectionStart, TextArea.ScrollDirection.UP);
if (index != -1) {
selectionLength = selectionStart - index;
}
} else {
if (selectionStart < anchor) {
// continue upwards
index = getNextInsertionPoint(caretX, selectionStart, TextArea.ScrollDirection.UP);
if (index != -1) {
selectionLength = selectionStart + selectionLength - index;
}
} else {
// reduce downward size
Bounds trailingSelectionBounds = getCharacterBounds(selectionStart + selectionLength - 1);
int x = trailingSelectionBounds.x + trailingSelectionBounds.width;
index = getNextInsertionPoint(x, selectionStart + selectionLength - 1, TextArea.ScrollDirection.UP);
if (index != -1) {
if (index < anchor) {
selectionLength = anchor - index;
} else {
selectionLength = index - selectionStart;
index = selectionStart;
}
}
}
}
} else {
index = getNextInsertionPoint(caretX, selectionStart, TextArea.ScrollDirection.UP);
if (index != -1) {
selectionLength = 0;
}
anchor = -1;
}
if (index != -1) {
textArea.setSelection(index, selectionLength);
scrollCharacterToVisible(index);
caretX = caret.x;
}
consumed = true;
} else if (keyCode == Keyboard.KeyCode.DOWN) {
int selectionStart = textArea.getSelectionStart();
int selectionLength = textArea.getSelectionLength();
if (shiftPressed) {
int from;
int x;
int index;
if (anchor == -1) {
anchor = selectionStart;
index = getNextInsertionPoint(caretX, selectionStart, TextArea.ScrollDirection.DOWN);
if (index != -1) {
selectionLength = index - selectionStart;
}
} else {
if (selectionStart < anchor) {
// Reducing upward size
// Get next insertion point from leading selection
// character
from = selectionStart;
x = caretX;
index = getNextInsertionPoint(x, from, TextArea.ScrollDirection.DOWN);
if (index != -1) {
if (index < anchor) {
selectionStart = index;
selectionLength = anchor - index;
} else {
selectionStart = anchor;
selectionLength = index - anchor;
}
textArea.setSelection(selectionStart, selectionLength);
scrollCharacterToVisible(selectionStart);
}
} else {
// Increasing downward size
// Get next insertion point from right edge of
// trailing selection
// character
from = selectionStart + selectionLength - 1;
Bounds trailingSelectionBounds = getCharacterBounds(from);
x = trailingSelectionBounds.x + trailingSelectionBounds.width;
index = getNextInsertionPoint(x, from, TextArea.ScrollDirection.DOWN);
if (index != -1) {
// the selection
if (index < textArea.getCharacterCount() - 1 && textArea.getCharacterAt(index) == '\n') {
index++;
}
textArea.setSelection(selectionStart, index - selectionStart);
scrollCharacterToVisible(index);
}
}
}
} else {
int from;
if (selectionLength == 0) {
// Get next insertion point from leading selection
// character
from = selectionStart;
} else {
// Get next insertion point from trailing selection
// character
from = selectionStart + selectionLength - 1;
}
int index = getNextInsertionPoint(caretX, from, TextArea.ScrollDirection.DOWN);
if (index != -1) {
textArea.setSelection(index, 0);
scrollCharacterToVisible(index);
caretX = caret.x;
}
anchor = -1;
}
consumed = true;
} else if (commandPressed) {
if (keyCode == Keyboard.KeyCode.A) {
textArea.setSelection(0, textArea.getCharacterCount());
consumed = true;
} else if (keyCode == Keyboard.KeyCode.X && isEditable) {
textArea.cut();
consumed = true;
} else if (keyCode == Keyboard.KeyCode.C) {
textArea.copy();
consumed = true;
} else if (keyCode == Keyboard.KeyCode.V && isEditable) {
textArea.paste();
consumed = true;
} else if (keyCode == Keyboard.KeyCode.Z && isEditable) {
if (!shiftPressed) {
textArea.undo();
}
consumed = true;
} else if (keyCode == Keyboard.KeyCode.TAB) {
// Only here if acceptsTab is false
consumed = super.keyPressed(component, keyCode, keyLocation);
}
} else if (keyCode == Keyboard.KeyCode.INSERT) {
if (shiftPressed && isEditable) {
textArea.paste();
consumed = true;
}
} else {
consumed = super.keyPressed(component, keyCode, keyLocation);
}
}
return consumed;
}
Aggregations