home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161a.iso / handson / files / copyjava.exe / com / sun / java / swing / JTextArea.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  6.2 KB  |  282 lines

  1. package com.sun.java.swing;
  2.  
  3. import com.sun.java.accessibility.AccessibleContext;
  4. import com.sun.java.swing.text.AttributeSet;
  5. import com.sun.java.swing.text.BadLocationException;
  6. import com.sun.java.swing.text.Document;
  7. import com.sun.java.swing.text.Element;
  8. import com.sun.java.swing.text.JTextComponent;
  9. import com.sun.java.swing.text.PlainDocument;
  10. import java.awt.Component;
  11. import java.awt.Container;
  12. import java.awt.Dimension;
  13. import java.awt.Font;
  14. import java.awt.FontMetrics;
  15. import java.awt.Rectangle;
  16.  
  17. public class JTextArea extends JTextComponent {
  18.    private int rows;
  19.    private int columns;
  20.    private int columnWidth;
  21.    private int rowHeight;
  22.    private boolean wrap;
  23.    private static final String base = "text";
  24.    private static int nameCounter;
  25.  
  26.    public JTextArea() {
  27.       this((Document)null, (String)null, 0, 0);
  28.    }
  29.  
  30.    public JTextArea(String var1) {
  31.       this((Document)null, var1, 0, 0);
  32.    }
  33.  
  34.    public JTextArea(int var1, int var2) {
  35.       this((Document)null, (String)null, var1, var2);
  36.    }
  37.  
  38.    public JTextArea(String var1, int var2, int var3) {
  39.       this((Document)null, var1, var2, var3);
  40.    }
  41.  
  42.    public JTextArea(Document var1) {
  43.       this(var1, (String)null, 0, 0);
  44.    }
  45.  
  46.    public JTextArea(Document var1, String var2, int var3, int var4) {
  47.       ((Component)this).setName("text" + nameCounter++);
  48.       this.rows = var3;
  49.       this.columns = var4;
  50.       if (var1 == null) {
  51.          var1 = this.createDefaultModel();
  52.       }
  53.  
  54.       ((JTextComponent)this).setDocument(var1);
  55.       if (var2 != null) {
  56.          ((JTextComponent)this).setText(var2);
  57.       }
  58.  
  59.    }
  60.  
  61.    public String getUIClassID() {
  62.       return "TextAreaUI";
  63.    }
  64.  
  65.    protected Document createDefaultModel() {
  66.       return new PlainDocument();
  67.    }
  68.  
  69.    public void setTabSize(int var1) {
  70.       Document var2 = ((JTextComponent)this).getDocument();
  71.       if (var2 != null) {
  72.          int var3 = this.getTabSize();
  73.          var2.putProperty("tabSize", new Integer(var1));
  74.          ((JComponent)this).firePropertyChange("TabSize", var3, var1);
  75.       }
  76.  
  77.    }
  78.  
  79.    public int getTabSize() {
  80.       int var1 = 8;
  81.       Document var2 = ((JTextComponent)this).getDocument();
  82.       if (var2 != null) {
  83.          Integer var3 = (Integer)var2.getProperty("tabSize");
  84.          if (var3 != null) {
  85.             var1 = var3;
  86.          }
  87.       }
  88.  
  89.       return var1;
  90.    }
  91.  
  92.    public void setLineWrap(boolean var1) {
  93.       boolean var2 = this.wrap;
  94.       this.wrap = var1;
  95.       ((JComponent)this).firePropertyChange("LineWrap", var2, var1);
  96.    }
  97.  
  98.    public boolean getLineWrap() {
  99.       return this.wrap;
  100.    }
  101.  
  102.    public int getLineOfOffset(int var1) throws BadLocationException {
  103.       Document var2 = ((JTextComponent)this).getDocument();
  104.       if (var1 < 0) {
  105.          throw new BadLocationException("Can't translate offset to line", -1);
  106.       } else if (var1 > var2.getLength()) {
  107.          throw new BadLocationException("Can't translate offset to line", var2.getLength() + 1);
  108.       } else {
  109.          Element var3 = ((JTextComponent)this).getDocument().getDefaultRootElement();
  110.          return var3.getElementIndex(var1);
  111.       }
  112.    }
  113.  
  114.    public int getLineCount() {
  115.       Element var1 = ((JTextComponent)this).getDocument().getDefaultRootElement();
  116.       return var1.getElementCount();
  117.    }
  118.  
  119.    public int getLineStartOffset(int var1) throws BadLocationException {
  120.       Element var2 = ((JTextComponent)this).getDocument().getDefaultRootElement();
  121.       if (var1 < 0) {
  122.          throw new BadLocationException("Negative line", -1);
  123.       } else if (var1 >= var2.getElementCount()) {
  124.          throw new BadLocationException("No such line", ((JTextComponent)this).getDocument().getLength() + 1);
  125.       } else {
  126.          Element var3 = var2.getElement(var1);
  127.          return var3.getStartOffset();
  128.       }
  129.    }
  130.  
  131.    public int getLineEndOffset(int var1) throws BadLocationException {
  132.       Element var2 = ((JTextComponent)this).getDocument().getDefaultRootElement();
  133.       if (var1 < 0) {
  134.          throw new BadLocationException("Negative line", -1);
  135.       } else if (var1 >= var2.getElementCount()) {
  136.          throw new BadLocationException("No such line", ((JTextComponent)this).getDocument().getLength() + 1);
  137.       } else {
  138.          Element var3 = var2.getElement(var1);
  139.          return var3.getEndOffset();
  140.       }
  141.    }
  142.  
  143.    public void insert(String var1, int var2) {
  144.       Document var3 = ((JTextComponent)this).getDocument();
  145.       if (var3 != null) {
  146.          try {
  147.             var3.insertString(var2, var1, (AttributeSet)null);
  148.          } catch (BadLocationException var5) {
  149.             throw new IllegalArgumentException(((Throwable)var5).getMessage());
  150.          }
  151.       }
  152.    }
  153.  
  154.    public void append(String var1) {
  155.       Document var2 = ((JTextComponent)this).getDocument();
  156.       if (var2 != null) {
  157.          try {
  158.             var2.insertString(var2.getLength(), var1, (AttributeSet)null);
  159.          } catch (BadLocationException var3) {
  160.          }
  161.       }
  162.    }
  163.  
  164.    public void replaceRange(String var1, int var2, int var3) {
  165.       if (var3 < var2) {
  166.          throw new IllegalArgumentException("end before start");
  167.       } else {
  168.          Document var4 = ((JTextComponent)this).getDocument();
  169.          if (var4 != null) {
  170.             try {
  171.                var4.remove(var2, var3 - var2);
  172.                var4.insertString(var2, var1, (AttributeSet)null);
  173.             } catch (BadLocationException var6) {
  174.                throw new IllegalArgumentException(((Throwable)var6).getMessage());
  175.             }
  176.          }
  177.       }
  178.    }
  179.  
  180.    public boolean isManagingFocus() {
  181.       return true;
  182.    }
  183.  
  184.    public int getRows() {
  185.       return this.rows;
  186.    }
  187.  
  188.    public void setRows(int var1) {
  189.       int var2 = this.rows;
  190.       if (var1 < 0) {
  191.          throw new IllegalArgumentException("rows less than zero.");
  192.       } else {
  193.          if (var1 != var2) {
  194.             this.rows = var1;
  195.             ((Container)this).invalidate();
  196.          }
  197.  
  198.       }
  199.    }
  200.  
  201.    protected int getRowHeight() {
  202.       if (this.rowHeight == 0) {
  203.          FontMetrics var1 = ((Component)this).getFontMetrics(((Component)this).getFont());
  204.          this.rowHeight = var1.getHeight();
  205.       }
  206.  
  207.       return this.rowHeight;
  208.    }
  209.  
  210.    public int getColumns() {
  211.       return this.columns;
  212.    }
  213.  
  214.    public void setColumns(int var1) {
  215.       int var2 = this.columns;
  216.       if (var1 < 0) {
  217.          throw new IllegalArgumentException("columns less than zero.");
  218.       } else {
  219.          if (var1 != var2) {
  220.             this.columns = var1;
  221.             ((Container)this).invalidate();
  222.          }
  223.  
  224.       }
  225.    }
  226.  
  227.    protected int getColumnWidth() {
  228.       if (this.columnWidth == 0) {
  229.          FontMetrics var1 = ((Component)this).getFontMetrics(((Component)this).getFont());
  230.          this.columnWidth = var1.charWidth('m');
  231.       }
  232.  
  233.       return this.columnWidth;
  234.    }
  235.  
  236.    public Dimension getMinimumSize() {
  237.       return this.columns == 0 && this.rows == 0 ? super.getMinimumSize() : ((JComponent)this).getPreferredSize();
  238.    }
  239.  
  240.    public void setFont(Font var1) {
  241.       super.setFont(var1);
  242.       this.rowHeight = 0;
  243.       this.columnWidth = 0;
  244.       ((JComponent)this).revalidate();
  245.    }
  246.  
  247.    protected String paramString() {
  248.       return super.paramString() + ",rows=" + this.rows + ",columns=" + this.columns;
  249.    }
  250.  
  251.    public boolean getScrollableTracksViewportWidth() {
  252.       return this.wrap;
  253.    }
  254.  
  255.    public Dimension getPreferredScrollableViewportSize() {
  256.       Dimension var1 = super.getPreferredScrollableViewportSize();
  257.       var1 = var1 == null ? new Dimension(400, 400) : var1;
  258.       var1.width = this.columns == 0 ? var1.width : this.columns * this.getColumnWidth();
  259.       var1.height = this.rows == 0 ? var1.height : this.rows * this.getRowHeight();
  260.       return var1;
  261.    }
  262.  
  263.    public int getScrollableUnitIncrement(Rectangle var1, int var2, int var3) {
  264.       switch (var2) {
  265.          case 0:
  266.             return this.getColumnWidth();
  267.          case 1:
  268.             return this.getRowHeight();
  269.          default:
  270.             throw new IllegalArgumentException("Invalid orientation: " + var2);
  271.       }
  272.    }
  273.  
  274.    public AccessibleContext getAccessibleContext() {
  275.       if (super.accessibleContext == null) {
  276.          super.accessibleContext = new AccessibleJTextArea(this);
  277.       }
  278.  
  279.       return super.accessibleContext;
  280.    }
  281. }
  282.