PageRenderTime 26ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/gosu/TextInput.java

http://jgosu.googlecode.com/
Java | 148 lines | 100 code | 43 blank | 5 comment | 31 complexity | 222f0c80f9b50bf05d7f281c19a64378 MD5 | raw file
  1package gosu;
  2
  3import java.awt.event.KeyEvent;
  4import java.awt.event.InputEvent;
  5import static java.awt.event.KeyEvent.*;
  6
  7
  8public class TextInput {
  9  private String _text;
 10  private int _caretPos;
 11  private int _selectionStart;
 12
 13  public TextInput() {
 14    _text = "";
 15  }
 16
 17
 18  public String getText() { return _text; }
 19
 20
 21  public void setText(String text) {
 22    _text = text;
 23    _caretPos = _selectionStart = _text.length();
 24  }
 25
 26
 27  public int getCaretPos() { return _caretPos; }
 28
 29
 30  public int getSelectionStart() { return _selectionStart; }
 31
 32
 33  protected boolean feedEvent(InputEvent ie) {
 34    if (!(ie instanceof KeyEvent)) return false;
 35
 36    KeyEvent event = (KeyEvent) ie;
 37
 38    char c = event.getKeyChar();
 39
 40    if (c != CHAR_UNDEFINED && c >= 32 && c != 127) {
 41      // If text is selected, it will get overwritten
 42      int min = Math.min(_caretPos, _selectionStart);
 43      int max = Math.max(_caretPos, _selectionStart);
 44
 45      _text = _text.substring(0, min) + c + _text.substring(max);
 46
 47      _caretPos = _selectionStart = min + 1;
 48
 49      return true;
 50    }
 51
 52    switch (event.getKeyCode()) {
 53      case VK_LEFT:
 54        moveLeft(event);
 55        return true;
 56
 57      case VK_RIGHT:
 58        moveRight(event);
 59        return true;
 60
 61      case VK_HOME:
 62        _caretPos = 0;
 63        if (!event.isShiftDown()) _selectionStart = _caretPos;
 64        return true;
 65
 66      case VK_END:
 67        _caretPos = _text.length();
 68        if (!event.isShiftDown()) _selectionStart = _caretPos;
 69        return true;
 70
 71      case VK_BACK_SPACE:
 72        if (_caretPos != _selectionStart) {
 73          deleteSelection();
 74        } else {
 75          int oldCaret = _caretPos;
 76          moveLeft(event); // move left either a character or a word
 77          _text = _text.substring(0, _caretPos) + _text.substring(oldCaret);
 78          _selectionStart = _caretPos;
 79        }
 80        return true;
 81
 82      case VK_DELETE:
 83        if (_caretPos != _selectionStart) {
 84          deleteSelection();
 85        } else {
 86          int oldCaret = _caretPos;
 87          moveRight(event); // move right either a character or a word
 88          _text = _text.substring(0, oldCaret) + _text.substring(_caretPos);
 89          _selectionStart = _caretPos = oldCaret;
 90        }
 91        return true;
 92
 93    }
 94    return false;
 95  }
 96
 97
 98  private void moveLeft(KeyEvent event) {
 99    if (event.isControlDown()) {
100      // move left a word
101
102      while (_caretPos > 0 && Character.isWhitespace(_text.charAt(_caretPos - 1))) {
103        _caretPos--;
104      }
105
106      while (_caretPos > 0 && !Character.isWhitespace(_text.charAt(_caretPos - 1))) {
107        _caretPos--;
108      }
109      
110    } else {
111      // move left a character
112      if (_caretPos > 0) _caretPos--;
113    }
114
115    if (!event.isShiftDown()) _selectionStart = _caretPos;
116  }
117
118
119  private void moveRight(KeyEvent event) {
120    if (event.isControlDown()) {
121      // move right a word
122
123      while (_caretPos < _text.length() && Character.isWhitespace(_text.charAt(_caretPos))) {
124        _caretPos++;
125      }
126
127      while (_caretPos < _text.length() && !Character.isWhitespace(_text.charAt(_caretPos))) {
128        _caretPos++;
129      }
130
131    } else {
132      // move right a character
133      if (_caretPos < _text.length()) _caretPos++;
134    }
135
136    if (!event.isShiftDown()) _selectionStart = _caretPos;
137  }
138
139
140  private void deleteSelection() {
141    int min = Math.min(_caretPos, _selectionStart);
142    int max = Math.max(_caretPos, _selectionStart);
143
144    _text = _text.substring(0, min) + _text.substring(max);
145
146    _caretPos = _selectionStart = min;
147  }
148}