1 7 package java.awt; 8 9 import java.awt.event.InputEvent ; 10 import java.awt.event.KeyEvent ; 11 import java.awt.peer.TextAreaPeer; 12 import java.io.ObjectOutputStream ; 13 import java.io.ObjectInputStream ; 14 import java.io.IOException ; 15 import java.util.Set ; 16 import java.util.TreeSet ; 17 import javax.accessibility.*; 18 19 39 public class TextArea extends TextComponent { 40 41 50 int rows; 51 52 63 int columns; 64 65 private static final String base = "text"; 66 private static int nameCounter = 0; 67 68 72 public static final int SCROLLBARS_BOTH = 0; 73 74 78 public static final int SCROLLBARS_VERTICAL_ONLY = 1; 79 80 84 public static final int SCROLLBARS_HORIZONTAL_ONLY = 2; 85 86 90 public static final int SCROLLBARS_NONE = 3; 91 92 103 private int scrollbarVisibility; 104 105 109 private static Set forwardTraversalKeys, backwardTraversalKeys; 110 111 114 private static final long serialVersionUID = 3692302836626095722L; 115 116 119 private static native void initIDs(); 120 121 static { 122 123 Toolkit.loadLibraries(); 124 if (!GraphicsEnvironment.isHeadless()) { 125 initIDs(); 126 } 127 forwardTraversalKeys = KeyboardFocusManager.initFocusTraversalKeysSet( 128 "ctrl TAB", 129 new TreeSet ()); 130 backwardTraversalKeys = KeyboardFocusManager.initFocusTraversalKeysSet( 131 "ctrl shift TAB", 132 new TreeSet ()); 133 } 134 135 144 public TextArea() throws HeadlessException { 145 this("", 0, 0, SCROLLBARS_BOTH); 146 } 147 148 160 public TextArea(String text) throws HeadlessException { 161 this(text, 0, 0, SCROLLBARS_BOTH); 162 } 163 164 178 public TextArea(int rows, int columns) throws HeadlessException { 179 this("", rows, columns, SCROLLBARS_BOTH); 180 } 181 182 199 public TextArea(String text, int rows, int columns) 200 throws HeadlessException { 201 this(text, rows, columns, SCROLLBARS_BOTH); 202 } 203 204 239 public TextArea(String text, int rows, int columns, int scrollbars) 240 throws HeadlessException { 241 super(text); 242 243 this.rows = (rows >= 0) ? rows : 0; 244 this.columns = (columns >= 0) ? columns : 0; 245 246 if (scrollbars >= SCROLLBARS_BOTH && scrollbars <= SCROLLBARS_NONE) { 247 this.scrollbarVisibility = scrollbars; 248 } else { 249 this.scrollbarVisibility = SCROLLBARS_BOTH; 250 } 251 252 setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, 253 forwardTraversalKeys); 254 setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, 255 backwardTraversalKeys); 256 } 257 258 262 String constructComponentName() { 263 synchronized (getClass()) { 264 return base + nameCounter++; 265 } 266 } 267 268 273 public void addNotify() { 274 synchronized (getTreeLock()) { 275 if (peer == null) 276 peer = getToolkit().createTextArea(this); 277 super.addNotify(); 278 } 279 } 280 281 295 public void insert(String str, int pos) { 296 insertText(str, pos); 297 } 298 299 303 @Deprecated 304 public synchronized void insertText(String str, int pos) { 305 TextAreaPeer peer = (TextAreaPeer)this.peer; 306 if (peer != null) { 307 peer.insertText(str, pos); 308 } else { 309 text = text.substring(0, pos) + str + text.substring(pos); 310 } 311 } 312 313 323 public void append(String str) { 324 appendText(str); 325 } 326 327 331 @Deprecated 332 public synchronized void appendText(String str) { 333 if (peer != null) { 334 insertText(str, getText().length()); 335 } else { 336 text = text + str; 337 } 338 } 339 340 359 public void replaceRange(String str, int start, int end) { 360 replaceText(str, start, end); 361 } 362 363 367 @Deprecated 368 public synchronized void replaceText(String str, int start, int end) { 369 TextAreaPeer peer = (TextAreaPeer)this.peer; 370 if (peer != null) { 371 peer.replaceText(str, start, end); 372 } else { 373 text = text.substring(0, start) + str + text.substring(end); 374 } 375 } 376 377 384 public int getRows() { 385 return rows; 386 } 387 388 398 public void setRows(int rows) { 399 int oldVal = this.rows; 400 if (rows < 0) { 401 throw new IllegalArgumentException ("rows less than zero."); 402 } 403 if (rows != oldVal) { 404 this.rows = rows; 405 invalidate(); 406 } 407 } 408 409 415 public int getColumns() { 416 return columns; 417 } 418 419 429 public void setColumns(int columns) { 430 int oldVal = this.columns; 431 if (columns < 0) { 432 throw new IllegalArgumentException ("columns less than zero."); 433 } 434 if (columns != oldVal) { 435 this.columns = columns; 436 invalidate(); 437 } 438 } 439 440 457 public int getScrollbarVisibility() { 458 return scrollbarVisibility; 459 } 460 461 462 473 public Dimension getPreferredSize(int rows, int columns) { 474 return preferredSize(rows, columns); 475 } 476 477 481 @Deprecated 482 public Dimension preferredSize(int rows, int columns) { 483 synchronized (getTreeLock()) { 484 TextAreaPeer peer = (TextAreaPeer)this.peer; 485 return (peer != null) ? 486 peer.preferredSize(rows, columns) : 487 super.preferredSize(); 488 } 489 } 490 491 497 public Dimension getPreferredSize() { 498 return preferredSize(); 499 } 500 501 505 @Deprecated 506 public Dimension preferredSize() { 507 synchronized (getTreeLock()) { 508 return ((rows > 0) && (columns > 0)) ? 509 preferredSize(rows, columns) : 510 super.preferredSize(); 511 } 512 } 513 514 525 public Dimension getMinimumSize(int rows, int columns) { 526 return minimumSize(rows, columns); 527 } 528 529 533 @Deprecated 534 public Dimension minimumSize(int rows, int columns) { 535 synchronized (getTreeLock()) { 536 TextAreaPeer peer = (TextAreaPeer)this.peer; 537 return (peer != null) ? 538 peer.minimumSize(rows, columns) : 539 super.minimumSize(); 540 } 541 } 542 543 549 public Dimension getMinimumSize() { 550 return minimumSize(); 551 } 552 553 557 @Deprecated 558 public Dimension minimumSize() { 559 synchronized (getTreeLock()) { 560 return ((rows > 0) && (columns > 0)) ? 561 minimumSize(rows, columns) : 562 super.minimumSize(); 563 } 564 } 565 566 575 protected String paramString() { 576 String sbVisStr; 577 switch (scrollbarVisibility) { 578 case SCROLLBARS_BOTH: 579 sbVisStr = "both"; 580 break; 581 case SCROLLBARS_VERTICAL_ONLY: 582 sbVisStr = "vertical-only"; 583 break; 584 case SCROLLBARS_HORIZONTAL_ONLY: 585 sbVisStr = "horizontal-only"; 586 break; 587 case SCROLLBARS_NONE: 588 sbVisStr = "none"; 589 break; 590 default: 591 sbVisStr = "invalid display policy"; 592 } 593 594 return super.paramString() + ",rows=" + rows + 595 ",columns=" + columns + 596 ",scrollbarVisibility=" + sbVisStr; 597 } 598 599 600 603 608 private int textAreaSerializedDataVersion = 2; 609 610 617 private void readObject(ObjectInputStream s) 618 throws ClassNotFoundException , IOException , HeadlessException 619 { 620 s.defaultReadObject(); 622 623 if (columns < 0) { 626 columns = 0; 627 } 628 if (rows < 0) { 629 rows = 0; 630 } 631 632 if ((scrollbarVisibility < SCROLLBARS_BOTH) || 633 (scrollbarVisibility > SCROLLBARS_NONE)) { 634 this.scrollbarVisibility = SCROLLBARS_BOTH; 635 } 636 637 if (textAreaSerializedDataVersion < 2) { 638 setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, 639 forwardTraversalKeys); 640 setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, 641 backwardTraversalKeys); 642 } 643 } 644 645 646 650 651 661 public AccessibleContext getAccessibleContext() { 662 if (accessibleContext == null) { 663 accessibleContext = new AccessibleAWTTextArea(); 664 } 665 return accessibleContext; 666 } 667 668 673 protected class AccessibleAWTTextArea extends AccessibleAWTTextComponent 674 { 675 678 private static final long serialVersionUID = 3472827823632144419L; 679 680 687 public AccessibleStateSet getAccessibleStateSet() { 688 AccessibleStateSet states = super.getAccessibleStateSet(); 689 states.add(AccessibleState.MULTI_LINE); 690 return states; 691 } 692 } 693 694 695 } 696 | Popular Tags |