| 1 | /* TextComponent.java -- Widgets for entering text
|
|---|
| 2 | Copyright (C) 1999 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.awt;
|
|---|
| 40 |
|
|---|
| 41 | import java.awt.event.TextEvent;
|
|---|
| 42 | import java.awt.event.TextListener;
|
|---|
| 43 | import java.awt.peer.TextComponentPeer;
|
|---|
| 44 | import java.awt.peer.ComponentPeer;
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * This class provides common functionality for widgets than
|
|---|
| 48 | * contain text.
|
|---|
| 49 | *
|
|---|
| 50 | * @author Aaron M. Renn ([email protected])
|
|---|
| 51 | */
|
|---|
| 52 | public class TextComponent extends Component implements java.io.Serializable
|
|---|
| 53 | {
|
|---|
| 54 |
|
|---|
| 55 | /*
|
|---|
| 56 | * Static Variables
|
|---|
| 57 | */
|
|---|
| 58 |
|
|---|
| 59 | // Constant for serialization
|
|---|
| 60 | private static final long serialVersionUID = -2214773872412987419L;
|
|---|
| 61 |
|
|---|
| 62 | /*
|
|---|
| 63 | * Instance Variables
|
|---|
| 64 | */
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * @serial Indicates whether or not this component is editable.
|
|---|
| 68 | */
|
|---|
| 69 | private boolean editable;
|
|---|
| 70 |
|
|---|
| 71 | /**
|
|---|
| 72 | * @serial The starting position of the selected text region.
|
|---|
| 73 | */
|
|---|
| 74 | private int selectionStart;
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * @serial The ending position of the selected text region.
|
|---|
| 78 | */
|
|---|
| 79 | private int selectionEnd;
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * @serial The text in the component
|
|---|
| 83 | */
|
|---|
| 84 | private String text;
|
|---|
| 85 |
|
|---|
| 86 | /**
|
|---|
| 87 | * A list of listeners that will receive events from this object.
|
|---|
| 88 | */
|
|---|
| 89 | protected transient TextListener textListener;
|
|---|
| 90 |
|
|---|
| 91 | /*************************************************************************/
|
|---|
| 92 |
|
|---|
| 93 | /*
|
|---|
| 94 | * Constructors
|
|---|
| 95 | */
|
|---|
| 96 |
|
|---|
| 97 | TextComponent(String text)
|
|---|
| 98 | {
|
|---|
| 99 | this.text = text;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /*************************************************************************/
|
|---|
| 103 |
|
|---|
| 104 | /*
|
|---|
| 105 | * Instance Methods
|
|---|
| 106 | */
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * Returns the text in this component
|
|---|
| 110 | *
|
|---|
| 111 | * @return The text in this component.
|
|---|
| 112 | */
|
|---|
| 113 | public synchronized String
|
|---|
| 114 | getText()
|
|---|
| 115 | {
|
|---|
| 116 | TextComponentPeer tcp = (TextComponentPeer)getPeer();
|
|---|
| 117 | if (tcp != null)
|
|---|
| 118 | text = tcp.getText();
|
|---|
| 119 |
|
|---|
| 120 | return(text);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /*************************************************************************/
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | * Sets the text in this component to the specified string.
|
|---|
| 127 | *
|
|---|
| 128 | * @param text The new text for this component.
|
|---|
| 129 | */
|
|---|
| 130 | public synchronized void
|
|---|
| 131 | setText(String text)
|
|---|
| 132 | {
|
|---|
| 133 | if (text == null)
|
|---|
| 134 | text = "";
|
|---|
| 135 |
|
|---|
| 136 | this.text = text;
|
|---|
| 137 |
|
|---|
| 138 | TextComponentPeer tcp = (TextComponentPeer)getPeer();
|
|---|
| 139 | if (tcp != null)
|
|---|
| 140 | tcp.setText(text);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /*************************************************************************/
|
|---|
| 144 |
|
|---|
| 145 | /**
|
|---|
| 146 | * Returns a string that contains the text that is currently selected.
|
|---|
| 147 | *
|
|---|
| 148 | * @return The currently selected text region.
|
|---|
| 149 | */
|
|---|
| 150 | public synchronized String
|
|---|
| 151 | getSelectedText()
|
|---|
| 152 | {
|
|---|
| 153 | String alltext = getText();
|
|---|
| 154 | int start = getSelectionStart();
|
|---|
| 155 | int end = getSelectionEnd();
|
|---|
| 156 |
|
|---|
| 157 | return(alltext.substring(start, end));
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /*************************************************************************/
|
|---|
| 161 |
|
|---|
| 162 | /**
|
|---|
| 163 | * Returns the starting position of the selected text region.
|
|---|
| 164 | * // FIXME: What is returned if there is no selected text?
|
|---|
| 165 | *
|
|---|
| 166 | * @return The starting position of the selected text region.
|
|---|
| 167 | */
|
|---|
| 168 | public synchronized int
|
|---|
| 169 | getSelectionStart()
|
|---|
| 170 | {
|
|---|
| 171 | TextComponentPeer tcp = (TextComponentPeer)getPeer();
|
|---|
| 172 | if (tcp != null)
|
|---|
| 173 | selectionStart = tcp.getSelectionStart();
|
|---|
| 174 |
|
|---|
| 175 | return(selectionStart);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | /*************************************************************************/
|
|---|
| 179 |
|
|---|
| 180 | /**
|
|---|
| 181 | * Sets the starting position of the selected region to the
|
|---|
| 182 | * specified value. If the specified value is out of range, then it
|
|---|
| 183 | * will be silently changed to the nearest legal value.
|
|---|
| 184 | *
|
|---|
| 185 | * @param selectionStart The new start position for selected text.
|
|---|
| 186 | */
|
|---|
| 187 | public synchronized void
|
|---|
| 188 | setSelectionStart(int selectionStart)
|
|---|
| 189 | {
|
|---|
| 190 | select(selectionStart, getSelectionEnd());
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /*************************************************************************/
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 196 | * Returns the ending position of the selected text region.
|
|---|
| 197 | * // FIXME: What is returned if there is no selected text.
|
|---|
| 198 | *
|
|---|
| 199 | * @return The ending position of the selected text region.
|
|---|
| 200 | */
|
|---|
| 201 | public synchronized int
|
|---|
| 202 | getSelectionEnd()
|
|---|
| 203 | {
|
|---|
| 204 | TextComponentPeer tcp = (TextComponentPeer)getPeer();
|
|---|
| 205 | if (tcp != null)
|
|---|
| 206 | selectionEnd = tcp.getSelectionEnd();
|
|---|
| 207 |
|
|---|
| 208 | return(selectionEnd);
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /*************************************************************************/
|
|---|
| 212 |
|
|---|
| 213 | /**
|
|---|
| 214 | * Sets the ending position of the selected region to the
|
|---|
| 215 | * specified value. If the specified value is out of range, then it
|
|---|
| 216 | * will be silently changed to the nearest legal value.
|
|---|
| 217 | *
|
|---|
| 218 | * @param selectionEnd The new start position for selected text.
|
|---|
| 219 | */
|
|---|
| 220 | public synchronized void
|
|---|
| 221 | setSelectionEnd(int selectionEnd)
|
|---|
| 222 | {
|
|---|
| 223 | select(getSelectionStart(), selectionEnd);
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /*************************************************************************/
|
|---|
| 227 |
|
|---|
| 228 | /**
|
|---|
| 229 | * This method sets the selected text range to the text between the
|
|---|
| 230 | * specified start and end positions. Illegal values for these
|
|---|
| 231 | * positions are silently fixed.
|
|---|
| 232 | *
|
|---|
| 233 | * @param startSelection The new start position for the selected text.
|
|---|
| 234 | * @param endSelection The new end position for the selected text.
|
|---|
| 235 | */
|
|---|
| 236 | public synchronized void
|
|---|
| 237 | select(int selectionStart, int endSelection)
|
|---|
| 238 | {
|
|---|
| 239 | if (selectionStart < 0)
|
|---|
| 240 | selectionStart = 0;
|
|---|
| 241 |
|
|---|
| 242 | if (selectionStart > getText().length())
|
|---|
| 243 | selectionStart = text.length();
|
|---|
| 244 |
|
|---|
| 245 | if (selectionEnd > text.length())
|
|---|
| 246 | selectionEnd = text.length();
|
|---|
| 247 |
|
|---|
| 248 | if (selectionStart > getSelectionEnd())
|
|---|
| 249 | selectionStart = selectionEnd;
|
|---|
| 250 |
|
|---|
| 251 | this.selectionStart = selectionStart;
|
|---|
| 252 | this.selectionEnd = selectionEnd;
|
|---|
| 253 |
|
|---|
| 254 | TextComponentPeer tcp = (TextComponentPeer)getPeer();
|
|---|
| 255 | if (tcp != null)
|
|---|
| 256 | tcp.select(selectionStart, selectionEnd);
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | /*************************************************************************/
|
|---|
| 260 |
|
|---|
| 261 | /**
|
|---|
| 262 | * Selects all of the text in the component.
|
|---|
| 263 | */
|
|---|
| 264 | public synchronized void
|
|---|
| 265 | selectAll()
|
|---|
| 266 | {
|
|---|
| 267 | select(0, getText().length());
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | /*************************************************************************/
|
|---|
| 271 |
|
|---|
| 272 | /**
|
|---|
| 273 | * Returns the current caret position in the text.
|
|---|
| 274 | *
|
|---|
| 275 | * @return The caret position in the text.
|
|---|
| 276 | */
|
|---|
| 277 | public synchronized int
|
|---|
| 278 | getCaretPosition()
|
|---|
| 279 | {
|
|---|
| 280 | TextComponentPeer tcp = (TextComponentPeer)getPeer();
|
|---|
| 281 | if (tcp != null)
|
|---|
| 282 | return(tcp.getCaretPosition());
|
|---|
| 283 | else
|
|---|
| 284 | return(0);
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | /*************************************************************************/
|
|---|
| 288 |
|
|---|
| 289 | /**
|
|---|
| 290 | * Sets the caret position to the specified value.
|
|---|
| 291 | *
|
|---|
| 292 | * @param caretPosition The new caret position.
|
|---|
| 293 | */
|
|---|
| 294 | public synchronized void
|
|---|
| 295 | setCaretPosition(int caretPosition)
|
|---|
| 296 | {
|
|---|
| 297 | TextComponentPeer tcp = (TextComponentPeer)getPeer();
|
|---|
| 298 | if (tcp != null)
|
|---|
| 299 | tcp.setCaretPosition(caretPosition);
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | /*************************************************************************/
|
|---|
| 303 |
|
|---|
| 304 | /**
|
|---|
| 305 | * Tests whether or not this component's text can be edited.
|
|---|
| 306 | *
|
|---|
| 307 | * @return <code>true</code> if the text can be edited, <code>false</code>
|
|---|
| 308 | * otherwise.
|
|---|
| 309 | */
|
|---|
| 310 | public boolean
|
|---|
| 311 | isEditable()
|
|---|
| 312 | {
|
|---|
| 313 | return(editable);
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | /*************************************************************************/
|
|---|
| 317 |
|
|---|
| 318 | /**
|
|---|
| 319 | * Sets whether or not this component's text can be edited.
|
|---|
| 320 | *
|
|---|
| 321 | * @param editable <code>true</code> to enable editing of the text,
|
|---|
| 322 | * <code>false</code> to disable it.
|
|---|
| 323 | */
|
|---|
| 324 | public synchronized void
|
|---|
| 325 | setEditable(boolean editable)
|
|---|
| 326 | {
|
|---|
| 327 | this.editable = editable;
|
|---|
| 328 |
|
|---|
| 329 | TextComponentPeer tcp = (TextComponentPeer)getPeer();
|
|---|
| 330 | if (tcp != null)
|
|---|
| 331 | tcp.setEditable(editable);
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | /*************************************************************************/
|
|---|
| 335 |
|
|---|
| 336 | /**
|
|---|
| 337 | * Notifies the component that it should destroy its native peer.
|
|---|
| 338 | */
|
|---|
| 339 | public void
|
|---|
| 340 | removeNotify()
|
|---|
| 341 | {
|
|---|
| 342 | super.removeNotify();
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | /*************************************************************************/
|
|---|
| 346 |
|
|---|
| 347 | /**
|
|---|
| 348 | * Adds a new listener to the list of text listeners for this
|
|---|
| 349 | * component.
|
|---|
| 350 | *
|
|---|
| 351 | * @param listener The listener to be added.
|
|---|
| 352 | */
|
|---|
| 353 | public synchronized void
|
|---|
| 354 | addTextListener(TextListener listener)
|
|---|
| 355 | {
|
|---|
| 356 | textListener = AWTEventMulticaster.add(textListener, listener);
|
|---|
| 357 |
|
|---|
| 358 | enableEvents(AWTEvent.TEXT_EVENT_MASK);
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | /*************************************************************************/
|
|---|
| 362 |
|
|---|
| 363 | /**
|
|---|
| 364 | * Removes the specified listener from the list of listeners
|
|---|
| 365 | * for this component.
|
|---|
| 366 | *
|
|---|
| 367 | * @param listener The listener to remove.
|
|---|
| 368 | */
|
|---|
| 369 | public synchronized void
|
|---|
| 370 | removeTextListener(TextListener listener)
|
|---|
| 371 | {
|
|---|
| 372 | textListener = AWTEventMulticaster.remove(textListener, listener);
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | /*************************************************************************/
|
|---|
| 376 |
|
|---|
| 377 | /**
|
|---|
| 378 | * Processes the specified event for this component. Text events are
|
|---|
| 379 | * processed by calling the <code>processTextEvent()</code> method.
|
|---|
| 380 | * All other events are passed to the superclass method.
|
|---|
| 381 | *
|
|---|
| 382 | * @param event The event to process.
|
|---|
| 383 | */
|
|---|
| 384 | protected void
|
|---|
| 385 | processEvent(AWTEvent event)
|
|---|
| 386 | {
|
|---|
| 387 | if (event instanceof TextEvent)
|
|---|
| 388 | processTextEvent((TextEvent)event);
|
|---|
| 389 | else
|
|---|
| 390 | super.processEvent(event);
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | /*************************************************************************/
|
|---|
| 394 |
|
|---|
| 395 | /**
|
|---|
| 396 | * Processes the specified text event by dispatching it to any listeners
|
|---|
| 397 | * that are registered. Note that this method will only be called
|
|---|
| 398 | * if text event's are enabled. This will be true if there are any
|
|---|
| 399 | * registered listeners, or if the event has been specifically
|
|---|
| 400 | * enabled using <code>enableEvents()</code>.
|
|---|
| 401 | *
|
|---|
| 402 | * @param event The text event to process.
|
|---|
| 403 | */
|
|---|
| 404 | protected void
|
|---|
| 405 | processTextEvent(TextEvent event)
|
|---|
| 406 | {
|
|---|
| 407 | if (textListener != null)
|
|---|
| 408 | textListener.textValueChanged(event);
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | /*************************************************************************/
|
|---|
| 412 |
|
|---|
| 413 | /**
|
|---|
| 414 | * Returns a debugging string.
|
|---|
| 415 | *
|
|---|
| 416 | * @return A debugging string.
|
|---|
| 417 | */
|
|---|
| 418 | protected String
|
|---|
| 419 | paramString()
|
|---|
| 420 | {
|
|---|
| 421 | return(getClass().getName() + "(text=" + getText() + ")");
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | } // class TextComponent
|
|---|
| 425 |
|
|---|