1 7 package javax.swing; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import javax.swing.text.*; 12 import javax.swing.plaf.*; 13 import javax.accessibility.*; 14 15 import java.util.Collections ; 16 import java.util.Set ; 17 import java.util.StringTokenizer ; 18 import java.util.TreeSet ; 19 20 import java.io.ObjectOutputStream ; 21 import java.io.ObjectInputStream ; 22 import java.io.IOException ; 23 24 105 public class JTextArea extends JTextComponent { 106 107 111 private static final String uiClassID = "TextAreaUI"; 112 113 117 public JTextArea() { 118 this(null, null, 0, 0); 119 } 120 121 127 public JTextArea(String text) { 128 this(null, text, 0, 0); 129 } 130 131 141 public JTextArea(int rows, int columns) { 142 this(null, null, rows, columns); 143 } 144 145 155 public JTextArea(String text, int rows, int columns) { 156 this(null, text, rows, columns); 157 } 158 159 165 public JTextArea(Document doc) { 166 this(doc, null, 0, 0); 167 } 168 169 181 public JTextArea(Document doc, String text, int rows, int columns) { 182 super(); 183 this.rows = rows; 184 this.columns = columns; 185 if (doc == null) { 186 doc = createDefaultModel(); 187 } 188 setDocument(doc); 189 if (text != null) { 190 setText(text); 191 select(0, 0); 192 } 193 if (rows < 0) { 194 throw new IllegalArgumentException ("rows: " + rows); 195 } 196 if (columns < 0) { 197 throw new IllegalArgumentException ("columns: " + columns); 198 } 199 LookAndFeel.installProperty(this, 200 "focusTraversalKeysForward", 201 JComponent. 202 getManagingFocusForwardTraversalKeys()); 203 LookAndFeel.installProperty(this, 204 "focusTraversalKeysBackward", 205 JComponent. 206 getManagingFocusBackwardTraversalKeys()); 207 } 208 209 216 public String getUIClassID() { 217 return uiClassID; 218 } 219 220 227 protected Document createDefaultModel() { 228 return new PlainDocument(); 229 } 230 231 244 public void setTabSize(int size) { 245 Document doc = getDocument(); 246 if (doc != null) { 247 int old = getTabSize(); 248 doc.putProperty(PlainDocument.tabSizeAttribute, new Integer (size)); 249 firePropertyChange("tabSize", old, size); 250 } 251 } 252 253 259 public int getTabSize() { 260 int size = 8; 261 Document doc = getDocument(); 262 if (doc != null) { 263 Integer i = (Integer ) doc.getProperty(PlainDocument.tabSizeAttribute); 264 if (i != null) { 265 size = i.intValue(); 266 } 267 } 268 return size; 269 } 270 271 286 public void setLineWrap(boolean wrap) { 287 boolean old = this.wrap; 288 this.wrap = wrap; 289 firePropertyChange("lineWrap", old, wrap); 290 } 291 292 300 public boolean getLineWrap() { 301 return wrap; 302 } 303 304 320 public void setWrapStyleWord(boolean word) { 321 boolean old = this.word; 322 this.word = word; 323 firePropertyChange("wrapStyleWord", old, word); 324 } 325 326 337 public boolean getWrapStyleWord() { 338 return word; 339 } 340 341 350 public int getLineOfOffset(int offset) throws BadLocationException { 351 Document doc = getDocument(); 352 if (offset < 0) { 353 throw new BadLocationException("Can't translate offset to line", -1); 354 } else if (offset > doc.getLength()) { 355 throw new BadLocationException("Can't translate offset to line", doc.getLength()+1); 356 } else { 357 Element map = getDocument().getDefaultRootElement(); 358 return map.getElementIndex(offset); 359 } 360 } 361 362 367 public int getLineCount() { 368 Element map = getDocument().getDefaultRootElement(); 369 return map.getElementCount(); 370 } 371 372 382 public int getLineStartOffset(int line) throws BadLocationException { 383 int lineCount = getLineCount(); 384 if (line < 0) { 385 throw new BadLocationException("Negative line", -1); 386 } else if (line >= lineCount) { 387 throw new BadLocationException("No such line", getDocument().getLength()+1); 388 } else { 389 Element map = getDocument().getDefaultRootElement(); 390 Element lineElem = map.getElement(line); 391 return lineElem.getStartOffset(); 392 } 393 } 394 395 405 public int getLineEndOffset(int line) throws BadLocationException { 406 int lineCount = getLineCount(); 407 if (line < 0) { 408 throw new BadLocationException("Negative line", -1); 409 } else if (line >= lineCount) { 410 throw new BadLocationException("No such line", getDocument().getLength()+1); 411 } else { 412 Element map = getDocument().getDefaultRootElement(); 413 Element lineElem = map.getElement(line); 414 int endOffset = lineElem.getEndOffset(); 415 return ((line == lineCount - 1) ? (endOffset - 1) : endOffset); 417 } 418 } 419 420 422 438 public void insert(String str, int pos) { 439 Document doc = getDocument(); 440 if (doc != null) { 441 try { 442 doc.insertString(pos, str, null); 443 } catch (BadLocationException e) { 444 throw new IllegalArgumentException (e.getMessage()); 445 } 446 } 447 } 448 449 461 public void append(String str) { 462 Document doc = getDocument(); 463 if (doc != null) { 464 try { 465 doc.insertString(doc.getLength(), str, null); 466 } catch (BadLocationException e) { 467 } 468 } 469 } 470 471 489 public void replaceRange(String str, int start, int end) { 490 if (end < start) { 491 throw new IllegalArgumentException ("end before start"); 492 } 493 Document doc = getDocument(); 494 if (doc != null) { 495 try { 496 if (doc instanceof AbstractDocument) { 497 ((AbstractDocument)doc).replace(start, end - start, str, 498 null); 499 } 500 else { 501 doc.remove(start, end - start); 502 doc.insertString(start, str, null); 503 } 504 } catch (BadLocationException e) { 505 throw new IllegalArgumentException (e.getMessage()); 506 } 507 } 508 } 509 510 515 public int getRows() { 516 return rows; 517 } 518 519 529 public void setRows(int rows) { 530 int oldVal = this.rows; 531 if (rows < 0) { 532 throw new IllegalArgumentException ("rows less than zero."); 533 } 534 if (rows != oldVal) { 535 this.rows = rows; 536 invalidate(); 537 } 538 } 539 540 546 protected int getRowHeight() { 547 if (rowHeight == 0) { 548 FontMetrics metrics = getFontMetrics(getFont()); 549 rowHeight = metrics.getHeight(); 550 } 551 return rowHeight; 552 } 553 554 559 public int getColumns() { 560 return columns; 561 } 562 563 573 public void setColumns(int columns) { 574 int oldVal = this.columns; 575 if (columns < 0) { 576 throw new IllegalArgumentException ("columns less than zero."); 577 } 578 if (columns != oldVal) { 579 this.columns = columns; 580 invalidate(); 581 } 582 } 583 584 594 protected int getColumnWidth() { 595 if (columnWidth == 0) { 596 FontMetrics metrics = getFontMetrics(getFont()); 597 columnWidth = metrics.charWidth('m'); 598 } 599 return columnWidth; 600 } 601 602 604 611 public Dimension getPreferredSize() { 612 Dimension d = super.getPreferredSize(); 613 d = (d == null) ? new Dimension(400,400) : d; 614 Insets insets = getInsets(); 615 616 if (columns != 0) { 617 d.width = Math.max(d.width, columns * getColumnWidth() + 618 insets.left + insets.right); 619 } 620 if (rows != 0) { 621 d.height = Math.max(d.height, rows * getRowHeight() + 622 insets.top + insets.bottom); 623 } 624 return d; 625 } 626 627 633 public void setFont(Font f) { 634 super.setFont(f); 635 rowHeight = 0; 636 columnWidth = 0; 637 } 638 639 640 649 protected String paramString() { 650 String wrapString = (wrap ? 651 "true" : "false"); 652 String wordString = (word ? 653 "true" : "false"); 654 655 return super.paramString() + 656 ",colums=" + columns + 657 ",columWidth=" + columnWidth + 658 ",rows=" + rows + 659 ",rowHeight=" + rowHeight + 660 ",word=" + wordString + 661 ",wrap=" + wrapString; 662 } 663 664 666 675 public boolean getScrollableTracksViewportWidth() { 676 return (wrap) ? true : super.getScrollableTracksViewportWidth(); 677 } 678 679 688 public Dimension getPreferredScrollableViewportSize() { 689 Dimension size = super.getPreferredScrollableViewportSize(); 690 size = (size == null) ? new Dimension(400,400) : size; 691 size.width = (columns == 0) ? size.width : columns * getColumnWidth(); 692 size.height = (rows == 0) ? size.height : rows * getRowHeight(); 693 return size; 694 } 695 696 717 public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { 718 switch (orientation) { 719 case SwingConstants.VERTICAL: 720 return getRowHeight(); 721 case SwingConstants.HORIZONTAL: 722 return getColumnWidth(); 723 default: 724 throw new IllegalArgumentException ("Invalid orientation: " + orientation); 725 } 726 } 727 728 732 private void writeObject(ObjectOutputStream s) throws IOException { 733 s.defaultWriteObject(); 734 if (getUIClassID().equals(uiClassID)) { 735 byte count = JComponent.getWriteObjCounter(this); 736 JComponent.setWriteObjCounter(this, --count); 737 if (count == 0 && ui != null) { 738 ui.installUI(this); 739 } 740 } 741 } 742 743 747 748 757 public AccessibleContext getAccessibleContext() { 758 if (accessibleContext == null) { 759 accessibleContext = new AccessibleJTextArea(); 760 } 761 return accessibleContext; 762 } 763 764 779 protected class AccessibleJTextArea extends AccessibleJTextComponent { 780 781 788 public AccessibleStateSet getAccessibleStateSet() { 789 AccessibleStateSet states = super.getAccessibleStateSet(); 790 states.add(AccessibleState.MULTI_LINE); 791 return states; 792 } 793 } 794 795 797 private int rows; 798 private int columns; 799 private int columnWidth; 800 private int rowHeight; 801 private boolean wrap; 802 private boolean word; 803 804 } 805 | Popular Tags |