KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > text > TextInput


1 /*
2  * Copyright 2005 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
22 import java.io.Serializable JavaDoc;
23
24 /**
25  * Class to manage InputEvents as text processing. It maintains a current
26  * cursor position, and inserts all characters at that position. Home, End,
27  * cursor keys, delete and backspace are also interpreted correctly.
28  * @author Dusty Phillips [dusty@buchuki.com]
29  */

30 public class TextInput implements InputProcessor, Serializable JavaDoc {
31     
32     /**
33      * Set a SerialVersionUID to identify that later versions of the class can
34      * deserialize this one
35      */

36     static final long serialVersionUID = 20050328L;
37
38     /**
39      * Process a specific input event and apply it to the String.
40      */

41     public void processInput(EnsmerInputEvent event) {
42         if (!focused) {
43             return;
44         }
45         if (event instanceof KeyPressEvent) {
46             KeyEvent JavaDoc key = (KeyEvent JavaDoc) 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                     //beginLine is the index of \n or is -1. First char of line
64
//is beginLine + 1
65
if (beginLine == -1) {
66                         break;
67                     }
68                     int beginPreviousLine = text.lastIndexOf("\n", beginLine - 1);
69                     //beginPrevious is the index of \n, or is -1
70
int charsToCursor = cursor - beginLine;
71                     //charsToCursor is >=1 because lastIndexOf started at cursor - 1
72
int lengthPreviousLine = beginLine - beginPreviousLine;
73                     //lengthPreviousLine is >=1
74
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                     //endLine is >= cursor
83
if (endLine == -1) {
84                         break; //already on last line
85
}
86                     int endNextLine = text.indexOf("\n", endLine + 1);
87                     //endNextLine is > endLine or is -1
88
if (endNextLine == -1) {
89                         endNextLine = text.length();
90                     }
91                     //endNextLine is > endLine
92
int lengthNextLine = endNextLine - endLine;
93                     //lengthNextLine >= 1
94
int beginLine = text.lastIndexOf("\n", cursor-1);
95                     //beginLine is < cursor and may be -1
96
int charsToCursor = cursor - beginLine;
97                     //charsToCursor is >= 1
98
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     /**
142      * Get the current text value.
143      */

144     public String JavaDoc getText() {
145         return text.toString();
146     }
147         
148     /**
149      * Set the current text managed by the TextInput
150      *
151      * @param s the string to set the text to
152      */

153     public void setText(String JavaDoc s) {
154         text.replace(0, text.length(), s);
155         cursor = s.length();
156     }
157     
158     /**
159      * Get the current position of the cursor
160      *
161      * @return the position of the cursor in the string
162      */

163     public int getCursorPosition() {
164         return cursor;
165     }
166     
167     /**
168      * Set the current position of the cursor
169      *
170      * @param pos the new cursor position
171      * @throws IndexOutOfBoundsException if it is < 0 or > text.length
172      */

173     public void setCursorPosition(int pos) {
174         if (pos < 0 || pos > text.length()) {
175             throw new IndexOutOfBoundsException JavaDoc("cursor position must be within string boundaries");
176         }
177         cursor = pos;
178     }
179     
180     /**
181      * Set whether or not the TextInput can accept input
182      *
183      * @param focused if true, the TextInput can accept input, if false it cannot
184      */

185     public void setFocused(boolean focused) {
186         this.focused = focused;
187     }
188     
189     /**
190      * Determine if the TextInput is currently focused
191      *
192      * @return true if the TextInput is accepting input, false otherwise
193      */

194     public boolean isFocused() {
195         return focused;
196     }
197     
198     /**
199      * The StringBuffer that contains the actual text needing to be managed.
200      */

201     private StringBuffer JavaDoc text = new StringBuffer JavaDoc();
202     
203     /**
204      * The position of the 'cursor' (current area of input) in the StringBuffer.
205      * Set to 0 to insert at the beginning of the string, text.length() to append
206      * to the end, or anything in between to insert at that position.
207      */

208     private int cursor;
209     
210     /**
211      * True only if the text is accepting input.
212      */

213     private boolean focused = false;
214 }
215
Popular Tags