home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 16DCDA6 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  7.2 KB  |  243 lines

  1. package com.sun.java.swing;
  2.  
  3. import com.sun.java.accessibility.AccessibleContext;
  4. import com.sun.java.swing.event.EventListenerList;
  5. import com.sun.java.swing.text.Document;
  6. import com.sun.java.swing.text.JTextComponent;
  7. import com.sun.java.swing.text.PlainDocument;
  8. import com.sun.java.swing.text.TextAction;
  9. import java.awt.Component;
  10. import java.awt.Container;
  11. import java.awt.Dimension;
  12. import java.awt.Font;
  13. import java.awt.FontMetrics;
  14. import java.awt.Rectangle;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.ActionListener;
  17.  
  18. public class JTextField extends JTextComponent implements SwingConstants {
  19.    public static final String notifyAction = "notify-field-accept";
  20.    private BoundedRangeModel visibility;
  21.    private int horizontalAlignment;
  22.    private int columns;
  23.    private int columnWidth;
  24.    private String command;
  25.    private static final Action[] defaultActions = new Action[]{new NotifyAction()};
  26.    static Class class$java$awt$event$ActionListener;
  27.  
  28.    public JTextField() {
  29.       this((Document)null, (String)null, 0);
  30.    }
  31.  
  32.    public JTextField(int columns) {
  33.       this((Document)null, (String)null, columns);
  34.    }
  35.  
  36.    public JTextField(Document doc, String text, int columns) {
  37.       this.horizontalAlignment = 2;
  38.       if (columns < 0) {
  39.          throw new IllegalArgumentException("columns less than zero.");
  40.       } else {
  41.          this.visibility = new DefaultBoundedRangeModel();
  42.          this.visibility.addChangeListener(new ScrollRepainter(this));
  43.          this.columns = columns;
  44.          if (doc == null) {
  45.             doc = this.createDefaultModel();
  46.          }
  47.  
  48.          ((JTextComponent)this).setDocument(doc);
  49.          if (text != null) {
  50.             ((JTextComponent)this).setText(text);
  51.          }
  52.  
  53.       }
  54.    }
  55.  
  56.    public JTextField(String text) {
  57.       this((Document)null, text, 0);
  58.    }
  59.  
  60.    public JTextField(String text, int columns) {
  61.       this((Document)null, text, columns);
  62.    }
  63.  
  64.    public synchronized void addActionListener(ActionListener l) {
  65.       EventListenerList var10000 = super.listenerList;
  66.       Class var10001 = class$java$awt$event$ActionListener;
  67.       if (var10001 == null) {
  68.          try {
  69.             var10001 = Class.forName("java.awt.event.ActionListener");
  70.          } catch (ClassNotFoundException var2) {
  71.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  72.          }
  73.  
  74.          class$java$awt$event$ActionListener = var10001;
  75.       }
  76.  
  77.       var10000.add(var10001, l);
  78.    }
  79.  
  80.    protected Document createDefaultModel() {
  81.       return new PlainDocument();
  82.    }
  83.  
  84.    protected void fireActionPerformed() {
  85.       Object[] listeners = super.listenerList.getListenerList();
  86.       ActionEvent e = new ActionEvent(this, 1001, this.command != null ? this.command : ((JTextComponent)this).getText());
  87.  
  88.       for(int i = listeners.length - 2; i >= 0; i -= 2) {
  89.          Object var10000 = listeners[i];
  90.          Class var10001 = class$java$awt$event$ActionListener;
  91.          if (var10001 == null) {
  92.             try {
  93.                var10001 = Class.forName("java.awt.event.ActionListener");
  94.             } catch (ClassNotFoundException var4) {
  95.                throw new NoClassDefFoundError(((Throwable)var4).getMessage());
  96.             }
  97.  
  98.             class$java$awt$event$ActionListener = var10001;
  99.          }
  100.  
  101.          if (var10000 == var10001) {
  102.             ((ActionListener)listeners[i + 1]).actionPerformed(e);
  103.          }
  104.       }
  105.  
  106.    }
  107.  
  108.    public AccessibleContext getAccessibleContext() {
  109.       if (super.accessibleContext == null) {
  110.          super.accessibleContext = new AccessibleJTextField(this);
  111.       }
  112.  
  113.       return super.accessibleContext;
  114.    }
  115.  
  116.    public Action[] getActions() {
  117.       return TextAction.augmentList(super.getActions(), defaultActions);
  118.    }
  119.  
  120.    public int getColumns() {
  121.       return this.columns;
  122.    }
  123.  
  124.    protected int getColumnWidth() {
  125.       if (this.columnWidth == 0) {
  126.          FontMetrics metrics = ((Component)this).getFontMetrics(((Component)this).getFont());
  127.          this.columnWidth = metrics.charWidth('m');
  128.       }
  129.  
  130.       return this.columnWidth;
  131.    }
  132.  
  133.    public int getHorizontalAlignment() {
  134.       return this.horizontalAlignment;
  135.    }
  136.  
  137.    public BoundedRangeModel getHorizontalVisibility() {
  138.       return this.visibility;
  139.    }
  140.  
  141.    public Dimension getMinimumSize() {
  142.       return this.getPreferredSize();
  143.    }
  144.  
  145.    public Dimension getPreferredSize() {
  146.       synchronized(((Component)this).getTreeLock()) {
  147.          Dimension size = super.getPreferredSize();
  148.          if (this.columns != 0) {
  149.             size.width = this.columns * this.getColumnWidth();
  150.          }
  151.  
  152.          return size;
  153.       }
  154.    }
  155.  
  156.    public int getScrollOffset() {
  157.       return this.visibility.getValue();
  158.    }
  159.  
  160.    public String getUIClassID() {
  161.       return "TextFieldUI";
  162.    }
  163.  
  164.    public boolean isValidateRoot() {
  165.       return true;
  166.    }
  167.  
  168.    protected String paramString() {
  169.       return super.paramString() + ",columns=" + this.columns + ",command=" + this.command;
  170.    }
  171.  
  172.    public void postActionEvent() {
  173.       this.fireActionPerformed();
  174.    }
  175.  
  176.    public synchronized void removeActionListener(ActionListener l) {
  177.       EventListenerList var10000 = super.listenerList;
  178.       Class var10001 = class$java$awt$event$ActionListener;
  179.       if (var10001 == null) {
  180.          try {
  181.             var10001 = Class.forName("java.awt.event.ActionListener");
  182.          } catch (ClassNotFoundException var2) {
  183.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  184.          }
  185.  
  186.          class$java$awt$event$ActionListener = var10001;
  187.       }
  188.  
  189.       var10000.remove(var10001, l);
  190.    }
  191.  
  192.    public void scrollRectToVisible(Rectangle r) {
  193.       int x = r.x + this.visibility.getValue();
  194.       if (x < this.visibility.getValue()) {
  195.          this.visibility.setValue(x - 2);
  196.       } else if (x > this.visibility.getValue() + this.visibility.getExtent()) {
  197.          this.visibility.setValue(x - this.visibility.getExtent() + 2);
  198.       }
  199.  
  200.    }
  201.  
  202.    public void setActionCommand(String command) {
  203.       this.command = command;
  204.    }
  205.  
  206.    public void setColumns(int columns) {
  207.       int oldVal = this.columns;
  208.       if (columns < 0) {
  209.          throw new IllegalArgumentException("columns less than zero.");
  210.       } else {
  211.          if (columns != oldVal) {
  212.             this.columns = columns;
  213.             ((Container)this).invalidate();
  214.          }
  215.  
  216.       }
  217.    }
  218.  
  219.    public void setFont(Font f) {
  220.       super.setFont(f);
  221.       this.columnWidth = 0;
  222.       ((JComponent)this).revalidate();
  223.    }
  224.  
  225.    public void setHorizontalAlignment(int alignment) {
  226.       if (alignment != this.horizontalAlignment) {
  227.          int oldValue = this.horizontalAlignment;
  228.          if (alignment != 2 && alignment != 0 && alignment != 4) {
  229.             throw new IllegalArgumentException("horizontalAlignment");
  230.          } else {
  231.             this.horizontalAlignment = alignment;
  232.             ((JComponent)this).firePropertyChange("horizontalAlignment", oldValue, this.horizontalAlignment);
  233.             ((Container)this).invalidate();
  234.             ((Component)this).repaint();
  235.          }
  236.       }
  237.    }
  238.  
  239.    public void setScrollOffset(int scrollOffset) {
  240.       this.visibility.setValue(scrollOffset);
  241.    }
  242. }
  243.