| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ArrayDataModel |
|
| 2.4444444444444446;2.444 |
| 1 | /* | |
| 2 | * Copyright 2004-2011 the Seasar Foundation and the Others. | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
| 13 | * either express or implied. See the License for the specific language | |
| 14 | * governing permissions and limitations under the License. | |
| 15 | */ | |
| 16 | package javax.faces.model; | |
| 17 | ||
| 18 | public class ArrayDataModel extends DataModel { | |
| 19 | ||
| 20 | 38 | private Object[] array = null; |
| 21 | ||
| 22 | 38 | private int index = -1; |
| 23 | ||
| 24 | public ArrayDataModel() { | |
| 25 | 6 | this(null); |
| 26 | 6 | } |
| 27 | ||
| 28 | public ArrayDataModel(Object[] array) { | |
| 29 | 38 | super(); |
| 30 | 38 | setWrappedData(array); |
| 31 | 38 | } |
| 32 | ||
| 33 | public int getRowCount() { | |
| 34 | 15 | return (array != null) ? array.length : -1; |
| 35 | } | |
| 36 | ||
| 37 | public Object getRowData() { | |
| 38 | 140 | if (array == null) { |
| 39 | 1 | return null; |
| 40 | } | |
| 41 | ||
| 42 | 139 | if (!isRowAvailable()) { |
| 43 | 1 | throw new IllegalArgumentException(); |
| 44 | } | |
| 45 | ||
| 46 | 138 | return array[index]; |
| 47 | } | |
| 48 | ||
| 49 | public int getRowIndex() { | |
| 50 | 3 | return index; |
| 51 | } | |
| 52 | ||
| 53 | public Object getWrappedData() { | |
| 54 | 2 | return array; |
| 55 | } | |
| 56 | ||
| 57 | public boolean isRowAvailable() { | |
| 58 | 373 | if (array == null) { |
| 59 | 0 | return false; |
| 60 | } | |
| 61 | 373 | return (index >= 0 && index < array.length); |
| 62 | } | |
| 63 | ||
| 64 | public void setWrappedData(Object data) { | |
| 65 | 40 | array = (Object[]) data; |
| 66 | 40 | int index = (data != null) ? 0 : -1; |
| 67 | 40 | setRowIndex(index); |
| 68 | 40 | } |
| 69 | ||
| 70 | public void setRowIndex(int rowIndex) { | |
| 71 | 148 | if (rowIndex < -1) { |
| 72 | 1 | throw new IllegalArgumentException(); |
| 73 | } | |
| 74 | ||
| 75 | 147 | int oldIndex = index; |
| 76 | 147 | index = rowIndex; |
| 77 | ||
| 78 | 147 | if (array == null) { |
| 79 | 7 | return; |
| 80 | } | |
| 81 | ||
| 82 | 140 | DataModelListener[] listeners = getDataModelListeners(); |
| 83 | 140 | if ((oldIndex != index) && (listeners != null)) { |
| 84 | 126 | Object rowData = null; |
| 85 | 126 | if (isRowAvailable()) { |
| 86 | 97 | rowData = getRowData(); |
| 87 | } | |
| 88 | ||
| 89 | 126 | DataModelEvent event = new DataModelEvent(this, index, rowData); |
| 90 | 129 | for (int i = 0; i < listeners.length; i++) { |
| 91 | 3 | listeners[i].rowSelected(event); |
| 92 | } | |
| 93 | } | |
| 94 | ||
| 95 | 140 | } |
| 96 | } |