home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / swingall.jar / javax / swing / JTextArea.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-07-15  |  7.1 KB  |  327 lines

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