1 16 17 package com.buchuki.ensmer.text; 18 19 import com.buchuki.ensmer.input.InputProcessor; 20 import com.buchuki.ensmer.input.event.*; 21 import java.awt.event.KeyEvent ; 22 import java.io.Serializable ; 23 24 30 public class TextInput implements InputProcessor, Serializable { 31 32 36 static final long serialVersionUID = 20050328L; 37 38 41 public void processInput(EnsmerInputEvent event) { 42 if (!focused) { 43 return; 44 } 45 if (event instanceof KeyPressEvent) { 46 KeyEvent key = (KeyEvent ) event.getInputEvent(); 47 int keycode = key.getKeyCode(); 48 switch(keycode) { 49 case KeyEvent.VK_LEFT : { 50 if (cursor > 0) { 51 cursor--; 52 } 53 break; 54 } 55 case KeyEvent.VK_RIGHT : { 56 if (cursor < text.length()) { 57 cursor++; 58 } 59 break; 60 } 61 case KeyEvent.VK_UP : { 62 int beginLine = text.lastIndexOf("\n", cursor-1); 63 if (beginLine == -1) { 66 break; 67 } 68 int beginPreviousLine = text.lastIndexOf("\n", beginLine - 1); 69 int charsToCursor = cursor - beginLine; 71 int lengthPreviousLine = beginLine - beginPreviousLine; 73 if (charsToCursor > lengthPreviousLine) { 75 charsToCursor = lengthPreviousLine; 76 } 77 cursor = beginPreviousLine + charsToCursor; 78 break; 79 } 80 case KeyEvent.VK_DOWN : { 81 int endLine = text.indexOf("\n", cursor); 82 if (endLine == -1) { 84 break; } 86 int endNextLine = text.indexOf("\n", endLine + 1); 87 if (endNextLine == -1) { 89 endNextLine = text.length(); 90 } 91 int lengthNextLine = endNextLine - endLine; 93 int beginLine = text.lastIndexOf("\n", cursor-1); 95 int charsToCursor = cursor - beginLine; 97 if (charsToCursor > lengthNextLine) { 99 charsToCursor = lengthNextLine; 100 } 101 cursor = endLine + charsToCursor; 102 break; 103 } 104 case KeyEvent.VK_HOME : { 105 int beginLine = text.lastIndexOf("\n", cursor-1); 106 cursor = beginLine + 1; 107 break; 108 } 109 case KeyEvent.VK_END : { 110 int endLine = text.indexOf("\n", cursor); 111 if (endLine == -1) { 112 cursor = text.length(); 113 } 114 else { 115 cursor = endLine; 116 } 117 break; 118 } 119 case KeyEvent.VK_BACK_SPACE : { 120 if (cursor > 0) { 121 text.deleteCharAt(--cursor); 122 } 123 break; 124 } 125 case KeyEvent.VK_DELETE : { 126 if (cursor < text.length()) { 127 text.deleteCharAt(cursor); 128 } 129 break; 130 } 131 default : { 132 if (key.getKeyChar() != KeyEvent.CHAR_UNDEFINED) { 133 text.insert(cursor, key.getKeyChar()); 134 cursor++; 135 } 136 } 137 } 138 } 139 } 140 141 144 public String getText() { 145 return text.toString(); 146 } 147 148 153 public void setText(String s) { 154 text.replace(0, text.length(), s); 155 cursor = s.length(); 156 } 157 158 163 public int getCursorPosition() { 164 return cursor; 165 } 166 167 173 public void setCursorPosition(int pos) { 174 if (pos < 0 || pos > text.length()) { 175 throw new IndexOutOfBoundsException ("cursor position must be within string boundaries"); 176 } 177 cursor = pos; 178 } 179 180 185 public void setFocused(boolean focused) { 186 this.focused = focused; 187 } 188 189 194 public boolean isFocused() { 195 return focused; 196 } 197 198 201 private StringBuffer text = new StringBuffer (); 202 203 208 private int cursor; 209 210 213 private boolean focused = false; 214 } 215 | Popular Tags |