1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
package beans.list;
import beans.tables.employe.Employes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
public class EmpDataList extends SortableList
{
private DataModel data;
private DataModel columnHeaders;
private static final int SORT_ASCENDING = 1;
private static final int SORT_DESCENDING = -1;
public EmpDataList()
{
super(null);
// create header info
List headerList = new ArrayList();
headerList.add(new ColumnHeader("Matricule","100",false));
headerList.add(new ColumnHeader("Nom","200",false));
headerList.add(new ColumnHeader("Prenom","300",false));
headerList.add(new ColumnHeader("Service","300",false));
headerList.add(new ColumnHeader("Statut","300",false));
columnHeaders = new ListDataModel(headerList);
data = new ListDataModel(new Employes().getEmployes());
}
//==========================================================================
// Getters
//==========================================================================
public DataModel getData()
{
sort(getSort(), isAscending());
return data;
}
void setData(DataModel datamodel)
{
System.out.println("preserved datamodel updated");
// just here to see if the datamodel is updated if preservedatamodel=true
}
public DataModel getColumnHeaders()
{
return columnHeaders;
}
//==========================================================================
// Public Methods
//==========================================================================
public Object getColumnValue()
{
Object columnValue = null;
if (data.isRowAvailable() && columnHeaders.isRowAvailable())
{
columnValue = ((List)data.getRowData()).get(columnHeaders.getRowIndex());
}
return columnValue;
}
public void setColumnValue(Object value)
{
if (data.isRowAvailable() && columnHeaders.isRowAvailable())
{
((List)data.getRowData()).set(columnHeaders.getRowIndex(), value);
}
}
public String getColumnWidth()
{
String columnWidth = null;
if (data.isRowAvailable() && columnHeaders.isRowAvailable())
{
columnWidth = ((ColumnHeader)columnHeaders.getRowData()).getWidth();
}
return columnWidth;
}
public boolean isValueModifiable()
{
boolean valueModifiable = false;
if (data.isRowAvailable() && columnHeaders.isRowAvailable())
{
valueModifiable = ((ColumnHeader)columnHeaders.getRowData()).isEditable();
}
return valueModifiable;
}
//==========================================================================
// Protected Methods
//==========================================================================
protected boolean isDefaultAscending(String sortColumn)
{
return true;
}
protected void sort(final String column, final boolean ascending)
{
if (column != null)
{
int columnIndex = getColumnIndex(column);
int direction = (ascending) ? SORT_ASCENDING : SORT_DESCENDING;
sort(columnIndex, direction);
}
}
protected void sort(final int columnIndex, final int direction)
{
Comparator comparator = new Comparator()
{
public int compare(Object o1, Object o2)
{
int result = 0;
Object column1 = ((List)o1).get(columnIndex);
Object column2 = ((List)o2).get(columnIndex);
if (column1 == null && column2 != null)
result = -1;
else if (column1 == null && column2 == null)
result = 0;
else if (column1 != null && column2 == null)
result = 1;
else
result = ((Comparable)column1).compareTo(column2) * direction;
return result;
}
};
Collections.sort((List)data.getWrappedData(), comparator);
}
//==========================================================================
// Private Methods
//==========================================================================
private int getColumnIndex(final String columnName)
{
int columnIndex = -1;
List headers = (List) columnHeaders.getWrappedData();
for (int i=0;i<headers.size() && columnIndex==-1;i++)
{
ColumnHeader header = (ColumnHeader) headers.get(i);
if (header.getLabel().equals(columnName))
{
columnIndex = i;
}
}
return columnIndex;
}
} |
Partager