| 1 | /* TextComponent.java -- Widgets for entering text
|
|---|
| 2 | Copyright (C) 1999, 2002 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 | this.editable = true;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /*************************************************************************/
|
|---|
| 104 |
|
|---|
| 105 | /*
|
|---|
| 106 | * Instance Methods
|
|---|
| 107 | */
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * Returns the text in this component
|
|---|
| 111 | *
|
|---|
| 112 | * @return The text in this component.
|
|---|
| 113 | */
|
|---|
| 114 | public synchronized String
|
|---|
| 115 | getText()
|
|---|
| 116 | {
|
|---|
| 117 | TextComponentPeer tcp = (TextComponentPeer)getPeer();
|
|---|
| 118 | if (tcp != null)
|
|---|
| 119 | text = tcp.getText();
|
|---|
| 120 |
|
|---|
| 121 | return(text);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /*************************************************************************/
|
|---|
| 125 |
|
|---|
| 126 | /**
|
|---|
| 127 | * Sets the text in this component to the specified string.
|
|---|
| 128 | *
|
|---|
| 129 | * @param text The new text for this component.
|
|---|
| 130 | */
|
|---|
| 131 | public synchronized void
|
|---|
| 132 | setText(String text)
|
|---|
| 133 | {
|
|---|
| 134 | if (text == null)
|
|---|
| 135 | text = "";
|
|---|
| 136 |
|
|---|
| 137 | this.text = text;
|
|---|
| 138 |
|
|---|
| 139 | TextComponentPeer tcp = (TextComponentPeer)getPeer();
|
|---|
| 140 | if (tcp != null)
|
|---|
| 141 | tcp.setText(text);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /*************************************************************************/
|
|---|
| 145 |
|
|---|
| 146 | /**
|
|---|
| 147 | * Returns a string that contains the text that is currently selected.
|
|---|
| 148 | *
|
|---|
| 149 | * @return The currently selected text region.
|
|---|
| 150 | */
|
|---|
| 151 | public synchronized String
|
|---|
| 152 | getSelectedText()
|
|---|
| 153 | {
|
|---|
| 154 | String alltext = getText();
|
|---|
| 155 | int start = getSelectionStart();
|
|---|
| 156 | int end = getSelectionEnd();
|
|---|
| 157 |
|
|---|
| 158 | return(alltext.substring(start, end));
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /*************************************************************************/
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * Returns the starting position of the selected text region.
|
|---|
| 165 | * // FIXME: What is returned if there is no selected text?
|
|---|
| 166 | *
|
|---|
| 167 | * @return The starting position of the selected text region.
|
|---|
| 168 | */
|
|---|
| 169 | public synchronized int
|
|---|
| 170 | getSelectionStart()
|
|---|
| 171 | {
|
|---|
| 172 | TextComponentPeer tcp = (TextComponentPeer)getPeer();
|
|---|
| 173 | if (tcp != null)
|
|---|
| 174 | selectionStart = tcp.getSelectionStart();
|
|---|
| 175 |
|
|---|
| 176 | return(selectionStart);
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /*************************************************************************/
|
|---|
| 180 |
|
|---|
| 181 | /**
|
|---|
| 182 | * Sets the starting position of the selected region to the
|
|---|
| 183 | * specified value. If the specified value is out of range, then it
|
|---|
| 184 | * will be silently changed to the nearest legal value.
|
|---|
| 185 | *
|
|---|
| 186 | * @param selectionStart The new start position for selected text.
|
|---|
| 187 | */
|
|---|
| 188 | public synchronized void
|
|---|
| 189 | setSelectionStart(int selectionStart)
|
|---|
| 190 | {
|
|---|
| 191 | select(selectionStart, getSelectionEnd());
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /*************************************************************************/
|
|---|
| 195 |
|
|---|
| 196 | /**
|
|---|
| 197 | * Returns the ending position of the selected text region.
|
|---|
| 198 | * // FIXME: What is returned if there is no selected text.
|
|---|
| 199 | *
|
|---|
| 200 | * @return The ending position of the selected text region.
|
|---|
| 201 | */
|
|---|
| 202 | public synchronized int
|
|---|
| 203 | getSelectionEnd()
|
|---|
| 204 | {
|
|---|
|
|---|