home *** CD-ROM | disk | FTP | other *** search
- package java.awt;
-
- import java.awt.peer.ListPeer;
- import java.util.Vector;
-
- public class List extends Component {
- Vector items;
- int rows;
- boolean multipleSelections;
- int[] selected;
- int visibleIndex;
-
- public List() {
- this(0, false);
- }
-
- public List(int rows, boolean multipleSelections) {
- this.items = new Vector();
- this.multipleSelections = false;
- this.selected = new int[0];
- this.visibleIndex = -1;
- this.rows = rows;
- this.multipleSelections = multipleSelections;
- }
-
- public synchronized void addNotify() {
- super.peer = ((Component)this).getToolkit().createList(this);
- super.addNotify();
- this.visibleIndex = -1;
- }
-
- public synchronized void removeNotify() {
- if (super.peer != null) {
- ListPeer peer = (ListPeer)super.peer;
- this.selected = peer.getSelectedIndexes();
- }
-
- super.removeNotify();
- }
-
- public int countItems() {
- return this.items.size();
- }
-
- public String getItem(int index) {
- return (String)this.items.elementAt(index);
- }
-
- public synchronized void addItem(String item) {
- this.addItem(item, -1);
- }
-
- public synchronized void addItem(String item, int index) {
- if (index < -1 || index >= this.items.size()) {
- index = -1;
- }
-
- if (index == -1) {
- this.items.addElement(item);
- } else {
- this.items.insertElementAt(item, index);
- }
-
- ListPeer peer = (ListPeer)super.peer;
- if (peer != null) {
- peer.addItem(item, index);
- }
-
- }
-
- public synchronized void replaceItem(String newValue, int index) {
- this.delItem(index);
- this.addItem(newValue, index);
- }
-
- public synchronized void clear() {
- if (super.peer != null) {
- ((ListPeer)super.peer).clear();
- }
-
- this.items = new Vector();
- this.selected = new int[0];
- }
-
- public synchronized void delItem(int position) {
- this.delItems(position, position);
- }
-
- public synchronized void delItems(int start, int end) {
- for(int i = end; i >= start; --i) {
- this.items.removeElementAt(i);
- }
-
- if (super.peer != null) {
- ((ListPeer)super.peer).delItems(start, end);
- }
-
- }
-
- public synchronized int getSelectedIndex() {
- int[] sel = this.getSelectedIndexes();
- return sel.length == 1 ? sel[0] : -1;
- }
-
- public synchronized int[] getSelectedIndexes() {
- ListPeer peer = (ListPeer)super.peer;
- if (peer != null) {
- this.selected = peer.getSelectedIndexes();
- }
-
- return this.selected;
- }
-
- public synchronized String getSelectedItem() {
- int index = this.getSelectedIndex();
- return index < 0 ? null : this.getItem(index);
- }
-
- public synchronized String[] getSelectedItems() {
- int[] sel = this.getSelectedIndexes();
- String[] str = new String[sel.length];
-
- for(int i = 0; i < sel.length; ++i) {
- str[i] = this.getItem(sel[i]);
- }
-
- return str;
- }
-
- public synchronized void select(int index) {
- ListPeer peer = (ListPeer)super.peer;
- if (peer != null) {
- peer.select(index);
- } else {
- for(int i = 0; i < this.selected.length; ++i) {
- if (this.selected[i] == index) {
- return;
- }
- }
-
- if (!this.multipleSelections) {
- this.selected = new int[1];
- this.selected[0] = index;
- } else {
- int[] newsel = new int[this.selected.length + 1];
- System.arraycopy(this.selected, 0, newsel, 0, this.selected.length);
- newsel[this.selected.length] = index;
- this.selected = newsel;
- }
- }
- }
-
- public synchronized void deselect(int index) {
- ListPeer peer = (ListPeer)super.peer;
- if (peer != null) {
- peer.deselect(index);
- }
-
- for(int i = 0; i < this.selected.length; ++i) {
- if (this.selected[i] == index) {
- int[] newsel = new int[this.selected.length - 1];
- System.arraycopy(this.selected, 0, newsel, 0, i);
- System.arraycopy(this.selected, i + 1, newsel, i, this.selected.length - (i + 1));
- this.selected = newsel;
- return;
- }
- }
-
- }
-
- public synchronized boolean isSelected(int index) {
- int[] sel = this.getSelectedIndexes();
-
- for(int i = 0; i < sel.length; ++i) {
- if (sel[i] == index) {
- return true;
- }
- }
-
- return false;
- }
-
- public int getRows() {
- return this.rows;
- }
-
- public boolean allowsMultipleSelections() {
- return this.multipleSelections;
- }
-
- public void setMultipleSelections(boolean v) {
- if (v != this.multipleSelections) {
- this.multipleSelections = v;
- ListPeer peer = (ListPeer)super.peer;
- if (peer != null) {
- peer.setMultipleSelections(v);
- }
- }
-
- }
-
- public int getVisibleIndex() {
- return this.visibleIndex;
- }
-
- public void makeVisible(int index) {
- ListPeer peer = (ListPeer)super.peer;
- this.visibleIndex = index;
- if (peer != null) {
- peer.makeVisible(index);
- }
-
- }
-
- public Dimension preferredSize(int rows) {
- ListPeer peer = (ListPeer)super.peer;
- return peer != null ? peer.preferredSize(rows) : super.preferredSize();
- }
-
- public Dimension preferredSize() {
- return this.rows > 0 ? this.preferredSize(this.rows) : super.preferredSize();
- }
-
- public Dimension minimumSize(int rows) {
- ListPeer peer = (ListPeer)super.peer;
- return peer != null ? peer.minimumSize(rows) : super.minimumSize();
- }
-
- public Dimension minimumSize() {
- return this.rows > 0 ? this.minimumSize(this.rows) : super.minimumSize();
- }
-
- protected String paramString() {
- return super.paramString() + ",selected=" + this.getSelectedItem();
- }
- }
-