64 * A <code>null</code> value is the same as "". 65 * 66 * @serial 67 * @see #setText(String) 68 * @see #getText() 69 */ 70 String text; 71 72 /** 73 * A boolean indicating whether or not this 74 * <code>TextComponent</code> is editable. 75 * It will be <code>true</code> if the text component 76 * is editable and <code>false</code> if not. 77 * 78 * @serial 79 * @see #isEditable() 80 */ 81 boolean editable = true; 82 83 /** 84 * The selection refers to the selected text, and the 85 * <code>selectionStart</code> is the start position 86 * of the selected text. 87 * 88 * @serial 89 * @see #getSelectionStart() 90 * @see #setSelectionStart(int) 91 */ 92 int selectionStart; 93 94 /** 95 * The selection refers to the selected text, and the 96 * <code>selectionEnd</code> 97 * is the end position of the selected text. 98 * 99 * @serial 100 * @see #getSelectionEnd() 101 * @see #setSelectionEnd(int) 102 */ 103 int selectionEnd; 119 120 /** 121 * Constructs a new text component initialized with the 122 * specified text. Sets the value of the cursor to 123 * <code>Cursor.TEXT_CURSOR</code>. 124 * @param text the text to be displayed; if 125 * <code>text</code> is <code>null</code>, the empty 126 * string <code>""</code> will be displayed 127 * @exception HeadlessException if 128 * <code>GraphicsEnvironment.isHeadless</code> 129 * returns true 130 * @see java.awt.GraphicsEnvironment#isHeadless 131 * @see java.awt.Cursor 132 */ 133 TextComponent(String text) throws HeadlessException { 134 GraphicsEnvironment.checkHeadless(); 135 this.text = (text != null) ? text : ""; 136 setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); 137 } 138 139 private void enableInputMethodsIfNecessary() { 140 if (checkForEnableIM) { 141 checkForEnableIM = false; 142 try { 143 Toolkit toolkit = Toolkit.getDefaultToolkit(); 144 boolean shouldEnable = false; 145 if (toolkit instanceof InputMethodSupport) { 146 shouldEnable = ((InputMethodSupport)toolkit) 147 .enableInputMethodsForTextComponent(); 148 } 149 enableInputMethods(shouldEnable); 150 } catch (Exception e) { 151 // if something bad happens, just don't enable input methods 152 } 153 } 154 } 155 156 /** 157 * Enables or disables input method support for this text component. If input 158 * method support is enabled and the text component also processes key events, 215 text = peer.getText(); 216 selectionStart = peer.getSelectionStart(); 217 selectionEnd = peer.getSelectionEnd(); 218 } 219 super.removeNotify(); 220 } 221 } 222 223 /** 224 * Sets the text that is presented by this 225 * text component to be the specified text. 226 * @param t the new text; 227 * if this parameter is <code>null</code> then 228 * the text is set to the empty string "" 229 * @see java.awt.TextComponent#getText 230 */ 231 public synchronized void setText(String t) { 232 boolean skipTextEvent = (text == null || text.isEmpty()) 233 && (t == null || t.isEmpty()); 234 text = (t != null) ? t : ""; 235 TextComponentPeer peer = (TextComponentPeer)this.peer; 236 // Please note that we do not want to post an event 237 // if TextArea.setText() or TextField.setText() replaces an empty text 238 // by an empty text, that is, if component's text remains unchanged. 239 if (peer != null && !skipTextEvent) { 240 peer.setText(text); 241 } 242 } 243 244 /** 245 * Returns the text that is presented by this text component. 246 * By default, this is an empty string. 247 * 248 * @return the value of this <code>TextComponent</code> 249 * @see java.awt.TextComponent#setText 250 */ 251 public synchronized String getText() { 252 TextComponentPeer peer = (TextComponentPeer)this.peer; 253 if (peer != null) { 254 text = peer.getText(); | 64 * A <code>null</code> value is the same as "". 65 * 66 * @serial 67 * @see #setText(String) 68 * @see #getText() 69 */ 70 String text; 71 72 /** 73 * A boolean indicating whether or not this 74 * <code>TextComponent</code> is editable. 75 * It will be <code>true</code> if the text component 76 * is editable and <code>false</code> if not. 77 * 78 * @serial 79 * @see #isEditable() 80 */ 81 boolean editable = true; 82 83 /** 84 * A boolean indicating whether EOL characters present in the input 85 * text should be replaced by a space character. 86 * This variable is set to <code>true</code> for <code>TextField</code>. 87 * If set to <code>true</code> EOL will be replaced with space. 88 * If set to <code>false</code> text remains unchanged. 89 */ 90 boolean replaceEOL; 91 92 /** 93 * The selection refers to the selected text, and the 94 * <code>selectionStart</code> is the start position 95 * of the selected text. 96 * 97 * @serial 98 * @see #getSelectionStart() 99 * @see #setSelectionStart(int) 100 */ 101 int selectionStart; 102 103 /** 104 * The selection refers to the selected text, and the 105 * <code>selectionEnd</code> 106 * is the end position of the selected text. 107 * 108 * @serial 109 * @see #getSelectionEnd() 110 * @see #setSelectionEnd(int) 111 */ 112 int selectionEnd; 128 129 /** 130 * Constructs a new text component initialized with the 131 * specified text. Sets the value of the cursor to 132 * <code>Cursor.TEXT_CURSOR</code>. 133 * @param text the text to be displayed; if 134 * <code>text</code> is <code>null</code>, the empty 135 * string <code>""</code> will be displayed 136 * @exception HeadlessException if 137 * <code>GraphicsEnvironment.isHeadless</code> 138 * returns true 139 * @see java.awt.GraphicsEnvironment#isHeadless 140 * @see java.awt.Cursor 141 */ 142 TextComponent(String text) throws HeadlessException { 143 GraphicsEnvironment.checkHeadless(); 144 this.text = (text != null) ? text : ""; 145 setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); 146 } 147 148 /** 149 * Constructs a new text component. Replaces EOL character from the input 150 * text with space character if the flag replaceEOL is <code>true</code> 151 * and also sets the value of the cursor to <code>Cursor.TEXT_CURSOR</code>. 152 * @param text the text to be displayed; if 153 * <code>text</code> is <code>null</code>, the empty 154 * string <code>""</code> will be displayed 155 * @param replaceEOL boolean flag to decide whether to replace 156 * EOL or not. 157 * @exception HeadlessException if 158 * <code>GraphicsEnvironment.isHeadless</code> 159 * returns true 160 * @see java.awt.GraphicsEnvironment#isHeadless 161 * @see java.awt.Cursor 162 */ 163 TextComponent(String text, boolean replaceEOL) throws HeadlessException { 164 GraphicsEnvironment.checkHeadless(); 165 this.text = (text != null) ? text : ""; 166 if (replaceEOL) { 167 this.replaceEOL = replaceEOL; 168 if (this.text != "") { 169 replaceEOL(); 170 } 171 } 172 setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); 173 } 174 175 /** 176 * Replaces EOL characters from the text variable with a space character. 177 */ 178 private void replaceEOL() { 179 String[] strEOLs = {System.getProperty("line.separator"), "\n"}; 180 for (String eol : strEOLs) { 181 if (text.contains(eol)) { 182 text = text.replace(eol, " "); 183 } 184 } 185 } 186 187 private void enableInputMethodsIfNecessary() { 188 if (checkForEnableIM) { 189 checkForEnableIM = false; 190 try { 191 Toolkit toolkit = Toolkit.getDefaultToolkit(); 192 boolean shouldEnable = false; 193 if (toolkit instanceof InputMethodSupport) { 194 shouldEnable = ((InputMethodSupport)toolkit) 195 .enableInputMethodsForTextComponent(); 196 } 197 enableInputMethods(shouldEnable); 198 } catch (Exception e) { 199 // if something bad happens, just don't enable input methods 200 } 201 } 202 } 203 204 /** 205 * Enables or disables input method support for this text component. If input 206 * method support is enabled and the text component also processes key events, 263 text = peer.getText(); 264 selectionStart = peer.getSelectionStart(); 265 selectionEnd = peer.getSelectionEnd(); 266 } 267 super.removeNotify(); 268 } 269 } 270 271 /** 272 * Sets the text that is presented by this 273 * text component to be the specified text. 274 * @param t the new text; 275 * if this parameter is <code>null</code> then 276 * the text is set to the empty string "" 277 * @see java.awt.TextComponent#getText 278 */ 279 public synchronized void setText(String t) { 280 boolean skipTextEvent = (text == null || text.isEmpty()) 281 && (t == null || t.isEmpty()); 282 text = (t != null) ? t : ""; 283 if (replaceEOL && text != "") { 284 replaceEOL(); 285 } 286 TextComponentPeer peer = (TextComponentPeer)this.peer; 287 // Please note that we do not want to post an event 288 // if TextArea.setText() or TextField.setText() replaces an empty text 289 // by an empty text, that is, if component's text remains unchanged. 290 if (peer != null && !skipTextEvent) { 291 peer.setText(text); 292 } 293 } 294 295 /** 296 * Returns the text that is presented by this text component. 297 * By default, this is an empty string. 298 * 299 * @return the value of this <code>TextComponent</code> 300 * @see java.awt.TextComponent#setText 301 */ 302 public synchronized String getText() { 303 TextComponentPeer peer = (TextComponentPeer)this.peer; 304 if (peer != null) { 305 text = peer.getText(); |