home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / awt / List.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  5.1 KB  |  237 lines

  1. package java.awt;
  2.  
  3. import java.awt.peer.ListPeer;
  4. import java.util.Vector;
  5.  
  6. public class List extends Component {
  7.    Vector items;
  8.    int rows;
  9.    boolean multipleSelections;
  10.    int[] selected;
  11.    int visibleIndex;
  12.  
  13.    public List() {
  14.       this(0, false);
  15.    }
  16.  
  17.    public List(int rows, boolean multipleSelections) {
  18.       this.items = new Vector();
  19.       this.multipleSelections = false;
  20.       this.selected = new int[0];
  21.       this.visibleIndex = -1;
  22.       this.rows = rows;
  23.       this.multipleSelections = multipleSelections;
  24.    }
  25.  
  26.    public synchronized void addNotify() {
  27.       super.peer = ((Component)this).getToolkit().createList(this);
  28.       super.addNotify();
  29.       this.visibleIndex = -1;
  30.    }
  31.  
  32.    public synchronized void removeNotify() {
  33.       if (super.peer != null) {
  34.          ListPeer peer = (ListPeer)super.peer;
  35.          this.selected = peer.getSelectedIndexes();
  36.       }
  37.  
  38.       super.removeNotify();
  39.    }
  40.  
  41.    public int countItems() {
  42.       return this.items.size();
  43.    }
  44.  
  45.    public String getItem(int index) {
  46.       return (String)this.items.elementAt(index);
  47.    }
  48.  
  49.    public synchronized void addItem(String item) {
  50.       this.addItem(item, -1);
  51.    }
  52.  
  53.    public synchronized void addItem(String item, int index) {
  54.       if (index < -1 || index >= this.items.size()) {
  55.          index = -1;
  56.       }
  57.  
  58.       if (index == -1) {
  59.          this.items.addElement(item);
  60.       } else {
  61.          this.items.insertElementAt(item, index);
  62.       }
  63.  
  64.       ListPeer peer = (ListPeer)super.peer;
  65.       if (peer != null) {
  66.          peer.addItem(item, index);
  67.       }
  68.  
  69.    }
  70.  
  71.    public synchronized void replaceItem(String newValue, int index) {
  72.       this.delItem(index);
  73.       this.addItem(newValue, index);
  74.    }
  75.  
  76.    public synchronized void clear() {
  77.       if (super.peer != null) {
  78.          ((ListPeer)super.peer).clear();
  79.       }
  80.  
  81.       this.items = new Vector();
  82.       this.selected = new int[0];
  83.    }
  84.  
  85.    public synchronized void delItem(int position) {
  86.       this.delItems(position, position);
  87.    }
  88.  
  89.    public synchronized void delItems(int start, int end) {
  90.       for(int i = end; i >= start; --i) {
  91.          this.items.removeElementAt(i);
  92.       }
  93.  
  94.       if (super.peer != null) {
  95.          ((ListPeer)super.peer).delItems(start, end);
  96.       }
  97.  
  98.    }
  99.  
  100.    public synchronized int getSelectedIndex() {
  101.       int[] sel = this.getSelectedIndexes();
  102.       return sel.length == 1 ? sel[0] : -1;
  103.    }
  104.  
  105.    public synchronized int[] getSelectedIndexes() {
  106.       ListPeer peer = (ListPeer)super.peer;
  107.       if (peer != null) {
  108.          this.selected = peer.getSelectedIndexes();
  109.       }
  110.  
  111.       return this.selected;
  112.    }
  113.  
  114.    public synchronized String getSelectedItem() {
  115.       int index = this.getSelectedIndex();
  116.       return index < 0 ? null : this.getItem(index);
  117.    }
  118.  
  119.    public synchronized String[] getSelectedItems() {
  120.       int[] sel = this.getSelectedIndexes();
  121.       String[] str = new String[sel.length];
  122.  
  123.       for(int i = 0; i < sel.length; ++i) {
  124.          str[i] = this.getItem(sel[i]);
  125.       }
  126.  
  127.       return str;
  128.    }
  129.  
  130.    public synchronized void select(int index) {
  131.       ListPeer peer = (ListPeer)super.peer;
  132.       if (peer != null) {
  133.          peer.select(index);
  134.       } else {
  135.          for(int i = 0; i < this.selected.length; ++i) {
  136.             if (this.selected[i] == index) {
  137.                return;
  138.             }
  139.          }
  140.  
  141.          if (!this.multipleSelections) {
  142.             this.selected = new int[1];
  143.             this.selected[0] = index;
  144.          } else {
  145.             int[] newsel = new int[this.selected.length + 1];
  146.             System.arraycopy(this.selected, 0, newsel, 0, this.selected.length);
  147.             newsel[this.selected.length] = index;
  148.             this.selected = newsel;
  149.          }
  150.       }
  151.    }
  152.  
  153.    public synchronized void deselect(int index) {
  154.       ListPeer peer = (ListPeer)super.peer;
  155.       if (peer != null) {
  156.          peer.deselect(index);
  157.       }
  158.  
  159.       for(int i = 0; i < this.selected.length; ++i) {
  160.          if (this.selected[i] == index) {
  161.             int[] newsel = new int[this.selected.length - 1];
  162.             System.arraycopy(this.selected, 0, newsel, 0, i);
  163.             System.arraycopy(this.selected, i + 1, newsel, i, this.selected.length - (i + 1));
  164.             this.selected = newsel;
  165.             return;
  166.          }
  167.       }
  168.  
  169.    }
  170.  
  171.    public synchronized boolean isSelected(int index) {
  172.       int[] sel = this.getSelectedIndexes();
  173.  
  174.       for(int i = 0; i < sel.length; ++i) {
  175.          if (sel[i] == index) {
  176.             return true;
  177.          }
  178.       }
  179.  
  180.       return false;
  181.    }
  182.  
  183.    public int getRows() {
  184.       return this.rows;
  185.    }
  186.  
  187.    public boolean allowsMultipleSelections() {
  188.       return this.multipleSelections;
  189.    }
  190.  
  191.    public void setMultipleSelections(boolean v) {
  192.       if (v != this.multipleSelections) {
  193.          this.multipleSelections = v;
  194.          ListPeer peer = (ListPeer)super.peer;
  195.          if (peer != null) {
  196.             peer.setMultipleSelections(v);
  197.          }
  198.       }
  199.  
  200.    }
  201.  
  202.    public int getVisibleIndex() {
  203.       return this.visibleIndex;
  204.    }
  205.  
  206.    public void makeVisible(int index) {
  207.       ListPeer peer = (ListPeer)super.peer;
  208.       this.visibleIndex = index;
  209.       if (peer != null) {
  210.          peer.makeVisible(index);
  211.       }
  212.  
  213.    }
  214.  
  215.    public Dimension preferredSize(int rows) {
  216.       ListPeer peer = (ListPeer)super.peer;
  217.       return peer != null ? peer.preferredSize(rows) : super.preferredSize();
  218.    }
  219.  
  220.    public Dimension preferredSize() {
  221.       return this.rows > 0 ? this.preferredSize(this.rows) : super.preferredSize();
  222.    }
  223.  
  224.    public Dimension minimumSize(int rows) {
  225.       ListPeer peer = (ListPeer)super.peer;
  226.       return peer != null ? peer.minimumSize(rows) : super.minimumSize();
  227.    }
  228.  
  229.    public Dimension minimumSize() {
  230.       return this.rows > 0 ? this.minimumSize(this.rows) : super.minimumSize();
  231.    }
  232.  
  233.    protected String paramString() {
  234.       return super.paramString() + ",selected=" + this.getSelectedItem();
  235.    }
  236. }
  237.