1 16 package javax.faces.model; 17 18 22 public class ArrayDataModel extends DataModel 23 { 24 private int _rowIndex = -1; 26 private Object [] _data; 27 28 public ArrayDataModel() 30 { 31 super(); 32 } 33 34 public ArrayDataModel(Object [] array) 35 { 36 if (array == null) throw new NullPointerException ("array"); 37 setWrappedData(array); 38 } 39 40 public int getRowCount() 42 { 43 if (_data == null) 44 { 45 return -1; 46 } 47 return _data.length; 48 } 49 50 public Object getRowData() 51 { 52 if (_data == null) 53 { 54 return null; 55 } 56 if (!isRowAvailable()) 57 { 58 throw new IllegalArgumentException ("row is unavailable"); 59 } 60 return _data[_rowIndex]; 61 } 62 63 public int getRowIndex() 64 { 65 return _rowIndex; 66 } 67 68 public Object getWrappedData() 69 { 70 return _data; 71 } 72 73 public boolean isRowAvailable() 74 { 75 if (_data == null) 76 { 77 return false; 78 } 79 return _rowIndex >= 0 && _rowIndex < _data.length; 80 } 81 82 public void setRowIndex(int rowIndex) 83 { 84 if (rowIndex < -1) 85 { 86 throw new IllegalArgumentException ("illegal rowIndex " + rowIndex); 87 } 88 int oldRowIndex = _rowIndex; 89 _rowIndex = rowIndex; 90 if (_data != null && oldRowIndex != _rowIndex) 91 { 92 Object data = isRowAvailable() ? getRowData() : null; 93 DataModelEvent event = new DataModelEvent(this, _rowIndex, data); 94 DataModelListener[] listeners = getDataModelListeners(); 95 for (int i = 0; i < listeners.length; i++) 96 { 97 listeners[i].rowSelected(event); 98 } 99 } 100 } 101 102 public void setWrappedData(Object data) 103 { 104 _data = (Object [])data; 105 int rowIndex = _data != null ? 0 : -1; 106 setRowIndex(rowIndex); 107 } 108 109 } 110 | Popular Tags |