@Override
public boolean keyPressed(final Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
boolean consumed = false;
final TextPane textPane = (TextPane)getComponent();
Document document = textPane.getDocument();
Keyboard.Modifier commandModifier = Platform.getCommandModifier();
if (document != null) {
if (keyCode == Keyboard.KeyCode.ENTER
&& textPane.isEditable()) {
textPane.insertParagraph();
consumed = true;
} else if (keyCode == Keyboard.KeyCode.DELETE
&& textPane.isEditable()) {
textPane.delete(false);
consumed = true;
} else if (keyCode == Keyboard.KeyCode.BACKSPACE
&& textPane.isEditable()) {
textPane.delete(true);
consumed = true;
} else if (keyCode == Keyboard.KeyCode.LEFT) {
int selectionStart = textPane.getSelectionStart();
int selectionLength = textPane.getSelectionLength();
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
// Add the previous character to the selection
if (selectionStart > 0) {
selectionStart--;
selectionLength++;
}
} else if (Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
// Move the caret to the start of the next word to our left
if (selectionStart > 0) {
// 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--;
}
selectionLength = 0;
}
} 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) {
int selectionStart = textPane.getSelectionStart();
int selectionLength = textPane.getSelectionLength();
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
// Add the next character to the selection
if (selectionStart + selectionLength < document.getCharacterCount()) {
selectionLength++;
}
textPane.setSelection(selectionStart, selectionLength);
scrollCharacterToVisible(selectionStart + selectionLength);
} else if (Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
// 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 selectionStart = textPane.getSelectionStart();
int offset = getNextInsertionPoint(caretX, selectionStart, TextPane.ScrollDirection.UP);
if (offset == -1) {
offset = 0;
}
int selectionLength;
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
int selectionEnd = selectionStart + textPane.getSelectionLength() - 1;
selectionLength = selectionEnd - offset + 1;
} else {
selectionLength = 0;
}
textPane.setSelection(offset, selectionLength);
scrollCharacterToVisible(offset);
consumed = true;
} else if (keyCode == Keyboard.KeyCode.DOWN) {
int selectionStart = textPane.getSelectionStart();
int selectionLength = textPane.getSelectionLength();
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
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 {
// If the next character is a paragraph terminator and is not the
// 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 (Keyboard.isPressed(commandModifier)
&& keyCode == Keyboard.KeyCode.TAB
&& textPane.isEditable()) {
textPane.insert("\t");
showCaret(true);
consumed = true;
} else if (Keyboard.isPressed(commandModifier)) {
if (keyCode == Keyboard.KeyCode.A) {
textPane.setSelection(0, document.getCharacterCount());
consumed = true;
} else if (keyCode == Keyboard.KeyCode.X
&& textPane.isEditable()) {
textPane.cut();
consumed = true;
} else if (keyCode == Keyboard.KeyCode.C) {
textPane.copy();
consumed = true;
} else if (keyCode == Keyboard.KeyCode.V
&& textPane.isEditable()) {
textPane.paste();
consumed = true;
} else if (keyCode == Keyboard.KeyCode.Z
&& textPane.isEditable()) {
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
textPane.redo();
} else {
textPane.undo();
}
consumed = true;
}
} else if (keyCode == Keyboard.KeyCode.HOME) {
// Move the caret to the beginning of the text
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
textPane.setSelection(0, textPane.getSelectionStart());
} else {
textPane.setSelection(0, 0);
}
scrollCharacterToVisible(0);
consumed = true;
} else if (keyCode == Keyboard.KeyCode.END) {
// Move the caret to the end of the text
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
int selectionStart = textPane.getSelectionStart();
textPane.setSelection(selectionStart, textPane.getCharacterCount()
- selectionStart);
} else {
textPane.setSelection(textPane.getCharacterCount() - 1, 0);
}
scrollCharacterToVisible(textPane.getCharacterCount() - 1);
consumed = true;
} else if (keyCode == Keyboard.KeyCode.INSERT) {
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)
&& textPane.isEditable()) {
textPane.paste();
consumed = true;
}
} else {
consumed = super.keyPressed(component, keyCode, keyLocation);
}