1 /***
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions are
4 * met :
5 *
6 * . Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 *
9 * . Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * . The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $Id: TextBox.java 381 2005-12-30 07:44:33Z mat007 $
29 */
30
31 package palmed.edit;
32
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.OutputStream;
36 import palmed.edit.scrollbar.HorizontalScrollbar;
37 import palmed.edit.scrollbar.VerticalScrollbar;
38 import palmed.edit.text.IText;
39 import palmed.edit.text.Text;
40 import palmed.edit.util.Coordinate;
41 import palmed.edit.view.IView;
42 import palmed.edit.view.Viewport;
43 import palmed.ui.Container;
44
45 /***
46 * This class implements a custom text box component.
47 *
48 * @author Mathieu Champlon
49 * @version $Revision: 381 $ $Date: 2005-12-30 16:44:33 +0900 (ven., 30 déc. 2005) $
50 */
51 public final class TextBox extends Container implements IView, ITextBox
52 {
53 /***
54 * The size of each text chunk in bytes.
55 */
56
57 /***
58 * The value of the 'enter' key.
59 */
60 private static final int KEY_ENTER = 10;
61 /***
62 * The value of the 'backspace' key.
63 */
64 private static final int KEY_BACKSPACE = -11;
65 /***
66 // * The value of the 'tab' key.
67 // */
68
69 /***
70 * The text.
71 */
72 private final IText text_;
73 /***
74 * The text view omponent.
75 */
76 private final TextArea textArea_;
77 /***
78 * The viewport.
79 */
80 private final Viewport viewport_;
81 /***
82 * The vertical scrollbar.
83 */
84 private final VerticalScrollbar verticalScrollbar_;
85 /***
86 * The horizontal scrollbar.
87 */
88 private final HorizontalScrollbar horizontalScrollbar_;
89 /***
90 * The current modification status.
91 */
92 private boolean hasBeenModified_;
93 /***
94 * Create a text box.
95 */
96 public TextBox()
97 {
98 text_ = new Text();
99
100 viewport_ = new Viewport( new Content( text_ ) );
101 textArea_ = new TextArea( viewport_ );
102 verticalScrollbar_ = new VerticalScrollbar( viewport_ );
103 horizontalScrollbar_ = new HorizontalScrollbar( viewport_ );
104 final ModificationIndicator indicator = new ModificationIndicator();
105 text_.register( indicator );
106 viewport_.register( this );
107 add( textArea_ );
108 add( verticalScrollbar_ );
109 add( horizontalScrollbar_ );
110 add( indicator );
111 }
112
113 /***
114 * {@inheritDoc}
115 */
116 protected void keyPressed( final int keyCode )
117 {
118 switch( getGameAction( keyCode ) )
119 {
120 case UP :
121 viewport_.scroll( 0, -1 );
122 break;
123 case DOWN :
124 viewport_.scroll( 0, 1 );
125 break;
126 case LEFT :
127 viewport_.scroll( -1, 0 );
128 break;
129 case RIGHT :
130 viewport_.scroll( 1, 0 );
131 break;
132 case FIRE :
133 case KEY_NUM0 :
134 case KEY_NUM1 :
135 case KEY_NUM2 :
136 case KEY_NUM3 :
137 case KEY_NUM4 :
138 case KEY_NUM5 :
139 case KEY_NUM6 :
140 case KEY_NUM7 :
141 case KEY_NUM8 :
142 case KEY_STAR :
143 case KEY_POUND :
144 break;
145 default :
146 switch( keyCode )
147 {
148 case KEY_BACKSPACE :
149 viewport_.backspace();
150 break;
151 case KEY_ENTER :
152 viewport_.enter();
153 break;
154 default :
155 viewport_.type( keyCode );
156 }
157 break;
158 }
159 }
160
161 /***
162 * {@inheritDoc}
163 */
164 protected void keyRepeated( final int keyCode )
165 {
166 keyPressed( keyCode );
167 }
168
169 /***
170 * {@inheritDoc}
171 */
172 public void update( final int columns, final int lines )
173 {
174 verticalScrollbar_.update( textArea_.getLines(), lines );
175 horizontalScrollbar_.update( textArea_.getColumns(), columns );
176 repaint();
177 }
178
179 /***
180 * {@inheritDoc}
181 */
182 public void update( final Coordinate position )
183 {
184 textArea_.update( position );
185 repaint();
186 }
187
188 /***
189 * {@inheritDoc}
190 */
191 public void unmarshall( final InputStream stream ) throws IOException
192 {
193 viewport_.unmarshall( stream );
194 }
195
196 /***
197 * {@inheritDoc}
198 */
199 public void marshall( final OutputStream stream ) throws IOException
200 {
201 viewport_.marshall( stream );
202 }
203
204 /***
205 * {@inheritDoc}
206 */
207 public void read( final InputStream stream ) throws IOException
208 {
209 text_.read( stream );
210 }
211
212 /***
213 * {@inheritDoc}
214 */
215 public void write( final OutputStream stream ) throws IOException
216 {
217 text_.write( stream );
218 }
219
220 /***
221 * {@inheritDoc}
222 */
223 public boolean hasBeenModified()
224 {
225 return hasBeenModified_;
226 }
227
228 /***
229 * {@inheritDoc}
230 */
231 public void clear()
232 {
233 viewport_.clear();
234 }
235
236 /***
237 * {@inheritDoc}
238 */
239 public void copy( final OutputStream stream ) throws IOException
240 {
241 viewport_.copy( stream );
242 }
243
244 /***
245 * {@inheritDoc}
246 */
247 public void cut( final OutputStream stream ) throws IOException
248 {
249 viewport_.cut( stream );
250 }
251
252 /***
253 * {@inheritDoc}
254 */
255 public void paste( final InputStream stream ) throws IOException
256 {
257 viewport_.paste( stream );
258 }
259
260 /***
261 * {@inheritDoc}
262 */
263 public void modified( final boolean status )
264 {
265 hasBeenModified_ = status;
266 repaint();
267 }
268
269 /***
270 * {@inheritDoc}
271 */
272 public void delete()
273 {
274 text_.delete();
275 }
276
277 /***
278 * {@inheritDoc}
279 */
280 public void undo()
281 {
282 viewport_.undo();
283 }
284
285 /***
286 * {@inheritDoc}
287 */
288 public void redo()
289 {
290 viewport_.redo();
291 }
292
293 /***
294 * {@inheritDoc}
295 */
296 public void setLineSeparator( final String separator )
297 {
298 text_.setLineSeparator( separator );
299 }
300
301 /***
302 * {@inheritDoc}
303 */
304 public void setFont( final String name )
305 {
306 textArea_.setFont( name );
307 repaint();
308 }
309 }