/src/gosu/TextInput.java

http://jgosu.googlecode.com/ · Java · 148 lines · 100 code · 43 blank · 5 comment · 31 complexity · 222f0c80f9b50bf05d7f281c19a64378 MD5 · raw file

  1. package gosu;
  2. import java.awt.event.KeyEvent;
  3. import java.awt.event.InputEvent;
  4. import static java.awt.event.KeyEvent.*;
  5. public class TextInput {
  6. private String _text;
  7. private int _caretPos;
  8. private int _selectionStart;
  9. public TextInput() {
  10. _text = "";
  11. }
  12. public String getText() { return _text; }
  13. public void setText(String text) {
  14. _text = text;
  15. _caretPos = _selectionStart = _text.length();
  16. }
  17. public int getCaretPos() { return _caretPos; }
  18. public int getSelectionStart() { return _selectionStart; }
  19. protected boolean feedEvent(InputEvent ie) {
  20. if (!(ie instanceof KeyEvent)) return false;
  21. KeyEvent event = (KeyEvent) ie;
  22. char c = event.getKeyChar();
  23. if (c != CHAR_UNDEFINED && c >= 32 && c != 127) {
  24. // If text is selected, it will get overwritten
  25. int min = Math.min(_caretPos, _selectionStart);
  26. int max = Math.max(_caretPos, _selectionStart);
  27. _text = _text.substring(0, min) + c + _text.substring(max);
  28. _caretPos = _selectionStart = min + 1;
  29. return true;
  30. }
  31. switch (event.getKeyCode()) {
  32. case VK_LEFT:
  33. moveLeft(event);
  34. return true;
  35. case VK_RIGHT:
  36. moveRight(event);
  37. return true;
  38. case VK_HOME:
  39. _caretPos = 0;
  40. if (!event.isShiftDown()) _selectionStart = _caretPos;
  41. return true;
  42. case VK_END:
  43. _caretPos = _text.length();
  44. if (!event.isShiftDown()) _selectionStart = _caretPos;
  45. return true;
  46. case VK_BACK_SPACE:
  47. if (_caretPos != _selectionStart) {
  48. deleteSelection();
  49. } else {
  50. int oldCaret = _caretPos;
  51. moveLeft(event); // move left either a character or a word
  52. _text = _text.substring(0, _caretPos) + _text.substring(oldCaret);
  53. _selectionStart = _caretPos;
  54. }
  55. return true;
  56. case VK_DELETE:
  57. if (_caretPos != _selectionStart) {
  58. deleteSelection();
  59. } else {
  60. int oldCaret = _caretPos;
  61. moveRight(event); // move right either a character or a word
  62. _text = _text.substring(0, oldCaret) + _text.substring(_caretPos);
  63. _selectionStart = _caretPos = oldCaret;
  64. }
  65. return true;
  66. }
  67. return false;
  68. }
  69. private void moveLeft(KeyEvent event) {
  70. if (event.isControlDown()) {
  71. // move left a word
  72. while (_caretPos > 0 && Character.isWhitespace(_text.charAt(_caretPos - 1))) {
  73. _caretPos--;
  74. }
  75. while (_caretPos > 0 && !Character.isWhitespace(_text.charAt(_caretPos - 1))) {
  76. _caretPos--;
  77. }
  78. } else {
  79. // move left a character
  80. if (_caretPos > 0) _caretPos--;
  81. }
  82. if (!event.isShiftDown()) _selectionStart = _caretPos;
  83. }
  84. private void moveRight(KeyEvent event) {
  85. if (event.isControlDown()) {
  86. // move right a word
  87. while (_caretPos < _text.length() && Character.isWhitespace(_text.charAt(_caretPos))) {
  88. _caretPos++;
  89. }
  90. while (_caretPos < _text.length() && !Character.isWhitespace(_text.charAt(_caretPos))) {
  91. _caretPos++;
  92. }
  93. } else {
  94. // move right a character
  95. if (_caretPos < _text.length()) _caretPos++;
  96. }
  97. if (!event.isShiftDown()) _selectionStart = _caretPos;
  98. }
  99. private void deleteSelection() {
  100. int min = Math.min(_caretPos, _selectionStart);
  101. int max = Math.max(_caretPos, _selectionStart);
  102. _text = _text.substring(0, min) + _text.substring(max);
  103. _caretPos = _selectionStart = min;
  104. }
  105. }