1 16 package org.apache.myfaces.examples.crosstable; 17 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import javax.faces.model.DataModel; 25 import javax.faces.model.ListDataModel; 26 27 import org.apache.myfaces.examples.listexample.SimpleCountry; 28 import org.apache.myfaces.examples.listexample.SimpleCountryList; 29 30 34 public class DataBean extends SimpleCountryList 35 { 36 private DataModel mColumns; 37 private DataModel mCountryDataModel; 38 private Map mValueMap = new HashMap (); 39 private boolean mEditValues; 40 private String mColumnLabel; 41 42 45 public DataBean() 46 { 47 super(); 48 } 49 50 public boolean isEditValues() 51 { 52 return mEditValues; 53 } 54 55 public String editValues() 56 { 57 mEditValues = true; 58 return null; 59 } 60 61 public String saveValues() 62 { 63 mEditValues = false; 64 return null; 65 } 66 67 public String addColumn() 68 { 69 if (mColumnLabel != null) 70 { 71 List columns = (List ) getColumnDataModel().getWrappedData(); 72 columns.add(mColumnLabel); 73 } 74 return null; 75 } 76 77 public String removeColumn() 78 { 79 if (mColumns != null && mColumns.isRowAvailable()) 80 { 81 Object column = mColumns.getRowData(); 82 List columns = (List ) getColumnDataModel().getWrappedData(); 83 columns.remove(column); 84 } 85 return null; 86 } 87 88 public String getColumnLabel() 89 { 90 return mColumnLabel; 91 } 92 93 public void setColumnLabel(String label) 94 { 95 mColumnLabel = label; 96 } 97 98 public DataModel getCountryDataModel() 99 { 100 if (mCountryDataModel == null) 101 { 102 mCountryDataModel = new ListDataModel(getCountries()); 103 } 104 return mCountryDataModel; 105 } 106 107 public DataModel getColumnDataModel() 108 { 109 if (mColumns == null) 110 { 111 String [] result = new String [] {"2002", "2003", "2004"}; 112 mColumns = new ListDataModel(new ArrayList (Arrays.asList(result))); 113 } 114 return mColumns; 115 } 116 117 public String getColumnValue() 118 { 119 DataModel countryDataModel = getCountryDataModel(); 120 if (countryDataModel.isRowAvailable()) 121 { 122 SimpleCountry row = (SimpleCountry) countryDataModel.getRowData(); 123 DataModel columnDataModel = getColumnDataModel(); 124 if (columnDataModel.isRowAvailable()) 125 { 126 Object column = columnDataModel.getRowData(); 127 Object key = new RowColumnKey(new Long (row.getId()), column); 128 if (!mValueMap.containsKey(key)) 129 { 130 String randomValue = String.valueOf((int) (Math.random() * 5000) + 5000); 132 mValueMap.put(key, randomValue); 133 } 134 return (String ) mValueMap.get(key); 135 } 136 } 137 return null; 138 } 139 140 public void setColumnValue(String value) 141 { 142 DataModel countryDataModel = getCountryDataModel(); 143 if (countryDataModel.isRowAvailable()) 144 { 145 SimpleCountry row = (SimpleCountry) countryDataModel.getRowData(); 146 DataModel columnDataModel = getColumnDataModel(); 147 if (columnDataModel.isRowAvailable()) 148 { 149 Object column = columnDataModel.getRowData(); 150 Object key = new RowColumnKey(new Long (row.getId()), column); 151 mValueMap.put(key, value); 152 } 153 } 154 } 155 156 private class RowColumnKey 157 { 158 private final Object mRow; 159 private final Object mColumn; 160 161 165 public RowColumnKey(Object row, Object column) 166 { 167 mRow = row; 168 mColumn = column; 169 } 170 171 174 public boolean equals(Object obj) 175 { 176 if (obj == null) 177 { 178 return false; 179 } 180 if (obj == this) 181 { 182 return true; 183 } 184 if (obj instanceof RowColumnKey) 185 { 186 RowColumnKey other = (RowColumnKey) obj; 187 return other.mRow.equals(mRow) && other.mColumn.equals(mColumn); 188 } 189 return super.equals(obj); 190 } 191 192 195 public int hashCode() 196 { 197 return (37 * 3 + mRow.hashCode()) * (37 * 3 + mColumn.hashCode()); 198 } 199 200 203 public String toString() 204 { 205 return mRow.toString() + "," + mColumn.toString(); 206 } 207 } 208 } 209 210 216 | Popular Tags |