use of org.apache.pivot.wtk.text.Document in project pivot by apache.
the class TextPaneSkin method keyPressed.
@Override
public boolean keyPressed(final Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
boolean consumed = false;
final TextPane textPane = getTextPane();
Document document = textPane.getDocument();
int selectionStart = textPane.getSelectionStart();
int selectionLength = textPane.getSelectionLength();
boolean commandPressed = Keyboard.isPressed(Platform.getCommandModifier());
boolean wordNavPressed = Keyboard.isPressed(Platform.getWordNavigationModifier());
boolean shiftPressed = Keyboard.isPressed(Keyboard.Modifier.SHIFT);
boolean metaPressed = Keyboard.isPressed(Keyboard.Modifier.META);
boolean isEditable = textPane.isEditable();
if (document != null) {
if (keyCode == Keyboard.KeyCode.ENTER && isEditable) {
textPane.insertParagraph();
consumed = true;
} else if (keyCode == Keyboard.KeyCode.DELETE && isEditable) {
textPane.delete(false);
consumed = true;
} else if (keyCode == Keyboard.KeyCode.BACKSPACE && isEditable) {
textPane.delete(true);
consumed = true;
} else if (keyCode == Keyboard.KeyCode.HOME || (keyCode == Keyboard.KeyCode.LEFT && metaPressed)) {
int start;
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(document, selectionStart);
}
if (shiftPressed) {
// TODO: if last direction was left, then extend further left
// but if right, then reverse selection from the pivot point
selectionLength += selectionStart - start;
} else {
selectionLength = 0;
}
if (start >= 0) {
textPane.setSelection(start, selectionLength);
scrollCharacterToVisible(start);
consumed = true;
}
} else if (keyCode == Keyboard.KeyCode.END || (keyCode == Keyboard.KeyCode.RIGHT && metaPressed)) {
int end;
int index = selectionStart + selectionLength;
if (commandPressed) {
// Move the caret to end of the text
end = textPane.getCharacterCount() - 1;
} else {
// Move the caret to the end of the line
int rowOffset = getRowOffset(document, index);
int rowLength = getRowLength(document, index);
end = rowOffset + rowLength;
}
if (shiftPressed) {
// TODO: if last direction was right, then extend further right
// but if left, then reverse selection from the pivot point
selectionLength += end - index;
} else {
selectionStart = end;
selectionLength = 0;
}
if (selectionStart + selectionLength <= textPane.getCharacterCount()) {
textPane.setSelection(selectionStart, selectionLength);
scrollCharacterToVisible(selectionStart + selectionLength);
consumed = true;
}
} else if (keyCode == Keyboard.KeyCode.LEFT) {
if (wordNavPressed) {
// Move the caret to the start of the next word to our left
if (selectionStart > 0) {
int originalStart = selectionStart;
// first, skip over any space immediately to our left
while (selectionStart > 0 && Character.isWhitespace(document.getCharacterAt(selectionStart - 1))) {
selectionStart--;
}
// then, skip over any word-letters to our left
while (selectionStart > 0 && !Character.isWhitespace(document.getCharacterAt(selectionStart - 1))) {
selectionStart--;
}
if (shiftPressed) {
selectionLength += (originalStart - selectionStart);
} else {
selectionLength = 0;
}
}
} else if (shiftPressed) {
// Add the previous character to the selection
if (selectionStart > 0) {
selectionStart--;
selectionLength++;
}
} else {
// Clear the selection and move the caret back by one character
if (selectionLength == 0 && selectionStart > 0) {
selectionStart--;
}
selectionLength = 0;
}
textPane.setSelection(selectionStart, selectionLength);
scrollCharacterToVisible(selectionStart);
caretX = caret.x;
consumed = true;
} else if (keyCode == Keyboard.KeyCode.RIGHT) {
if (shiftPressed) {
// Add the next character to the selection
if (selectionStart + selectionLength < document.getCharacterCount()) {
selectionLength++;
}
textPane.setSelection(selectionStart, selectionLength);
scrollCharacterToVisible(selectionStart + selectionLength);
} else if (wordNavPressed) {
// Move the caret to the start of the next word to our right
if (selectionStart < document.getCharacterCount()) {
// first, skip over any word-letters to our right
while (selectionStart < document.getCharacterCount() - 1 && !Character.isWhitespace(document.getCharacterAt(selectionStart))) {
selectionStart++;
}
// then, skip over any space immediately to our right
while (selectionStart < document.getCharacterCount() - 1 && Character.isWhitespace(document.getCharacterAt(selectionStart))) {
selectionStart++;
}
textPane.setSelection(selectionStart, 0);
scrollCharacterToVisible(selectionStart);
caretX = caret.x;
}
} else {
// Clear the selection and move the caret forward by one character
if (selectionLength > 0) {
selectionStart += selectionLength - 1;
}
if (selectionStart < document.getCharacterCount() - 1) {
selectionStart++;
}
textPane.setSelection(selectionStart, 0);
scrollCharacterToVisible(selectionStart);
caretX = caret.x;
}
consumed = true;
} else if (keyCode == Keyboard.KeyCode.UP) {
int offset = getNextInsertionPoint(caretX, selectionStart, TextPane.ScrollDirection.UP);
if (offset == -1) {
offset = 0;
}
if (shiftPressed) {
selectionLength = selectionStart + selectionLength - offset;
} else {
selectionLength = 0;
}
textPane.setSelection(offset, selectionLength);
scrollCharacterToVisible(offset);
consumed = true;
} else if (keyCode == Keyboard.KeyCode.DOWN) {
if (shiftPressed) {
int from;
int x;
if (selectionLength == 0) {
// Get next insertion point from leading selection character
from = selectionStart;
x = caretX;
} else {
// Get next insertion point from right edge of trailing
// selection character
from = selectionStart + selectionLength - 1;
Bounds trailingSelectionBounds = getCharacterBounds(from);
x = trailingSelectionBounds.x + trailingSelectionBounds.width;
}
int offset = getNextInsertionPoint(x, from, TextPane.ScrollDirection.DOWN);
if (offset == -1) {
offset = documentView.getCharacterCount() - 1;
} else {
// final terminator character, increment the selection
if (document.getCharacterAt(offset) == '\n' && offset < documentView.getCharacterCount() - 1) {
offset++;
}
}
textPane.setSelection(selectionStart, offset - selectionStart);
scrollCharacterToVisible(offset);
} 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 offset = getNextInsertionPoint(caretX, from, TextPane.ScrollDirection.DOWN);
if (offset == -1) {
offset = documentView.getCharacterCount() - 1;
}
textPane.setSelection(offset, 0);
scrollCharacterToVisible(offset);
}
consumed = true;
} else if (keyCode == Keyboard.KeyCode.TAB && (acceptsTab != Keyboard.isPressed(Keyboard.Modifier.CTRL)) && isEditable) {
if (textPane.getExpandTabs()) {
int linePos = selectionStart - getRowOffset(document, selectionStart);
StringBuilder tabBuilder = new StringBuilder(tabWidth);
for (int i = 0; i < tabWidth - (linePos % tabWidth); i++) {
tabBuilder.append(" ");
}
textPane.insert(tabBuilder.toString());
} else {
textPane.insert("\t");
}
showCaret(true);
consumed = true;
} else if (keyCode == Keyboard.KeyCode.INSERT) {
if (shiftPressed && isEditable) {
textPane.paste();
consumed = true;
}
} else if (commandPressed) {
if (keyCode == Keyboard.KeyCode.A) {
textPane.setSelection(0, document.getCharacterCount());
consumed = true;
} else if (keyCode == Keyboard.KeyCode.X && isEditable) {
textPane.cut();
consumed = true;
} else if (keyCode == Keyboard.KeyCode.C) {
textPane.copy();
consumed = true;
} else if (keyCode == Keyboard.KeyCode.V && isEditable) {
textPane.paste();
consumed = true;
} else if (keyCode == Keyboard.KeyCode.Z && isEditable) {
if (shiftPressed) {
textPane.redo();
} else {
textPane.undo();
}
consumed = true;
}
} else {
consumed = super.keyPressed(component, keyCode, keyLocation);
}
}
return consumed;
}
Aggregations