1 24 package org.riotfamily.riot.list; 25 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.riotfamily.riot.dao.Order; 31 import org.riotfamily.riot.dao.RiotDao; 32 import org.riotfamily.riot.list.command.Command; 33 import org.springframework.util.StringUtils; 34 35 38 public class ListConfig { 39 40 private static final String DEFAULT_ID_PROPERTY = "id"; 41 42 private String id; 43 44 private RiotDao dao; 45 46 private List columnConfigs = new ArrayList (); 47 48 private List commands = new ArrayList (); 49 50 private ArrayList columnCommands = new ArrayList (); 51 52 private List formCommands; 53 54 private String [] defaultCommandIds; 55 56 private String idProperty; 57 58 private String rowStyleProperty; 59 60 private int pageSize = 15; 61 62 private String filterFormId; 63 64 private Order defaultOrder; 65 66 private String [] searchProperties; 67 68 public String getId() { 69 return id; 70 } 71 72 public void setId(String id) { 73 this.id = id; 74 } 75 76 public String getIdProperty() { 77 return idProperty != null ? idProperty : DEFAULT_ID_PROPERTY; 78 } 79 80 public void setIdProperty(String idProperty) { 81 this.idProperty = idProperty; 82 } 83 84 public String getRowStyleProperty() { 85 return this.rowStyleProperty; 86 } 87 88 public void setRowStyleProperty(String rowStyleProperty) { 89 this.rowStyleProperty = rowStyleProperty; 90 } 91 92 public void setDao(RiotDao dao) { 93 this.dao = dao; 94 } 95 96 public RiotDao getDao() { 97 return dao; 98 } 99 100 public int getPageSize() { 101 return pageSize; 102 } 103 104 public void setPageSize(int pageSize) { 105 this.pageSize = pageSize; 106 } 107 108 public void addColumnConfig(ColumnConfig column) { 109 columnConfigs.add(column); 110 } 111 112 public List getColumnConfigs() { 113 return columnConfigs; 114 } 115 116 public ColumnConfig getColumnConfig(int index) { 117 return (ColumnConfig) columnConfigs.get(index); 118 } 119 120 public ColumnConfig getColumnConfig(String property) { 121 property = property.trim(); 122 Iterator it = columnConfigs.iterator(); 123 while (it.hasNext()) { 124 ColumnConfig columnConfig = (ColumnConfig) it.next(); 125 if (property.equals(columnConfig.getProperty())) { 126 return columnConfig; 127 } 128 } 129 return null; 130 } 131 132 public void addCommand(Command command) { 133 commands.add(command); 134 } 135 136 public List getCommands() { 137 return commands; 138 } 139 140 public Class getItemClass() { 141 return dao.getEntityClass(); 142 } 143 144 public String getFirstProperty() { 145 if (!columnConfigs.isEmpty()) { 146 ColumnConfig columnConfig = (ColumnConfig) columnConfigs.get(0); 147 return columnConfig.getProperty(); 148 } 149 return null; 150 } 151 152 public String getFirstSortableProperty() { 153 Iterator it = columnConfigs.iterator(); 154 while (it.hasNext()) { 155 ColumnConfig columnConfig = (ColumnConfig) it.next(); 156 if (columnConfig.getProperty() != null 157 && columnConfig.isSortable()) { 158 return columnConfig.getProperty(); 159 } 160 } 161 return null; 162 } 163 164 public void addColumnCommand(Command command) { 165 columnCommands.add(command); 166 } 167 168 public List getColumnCommands() { 169 return columnCommands; 170 } 171 172 public List getFormCommands() { 173 if (formCommands == null) { 174 formCommands = new ArrayList (); 175 Iterator it = columnCommands.iterator(); 176 while (it.hasNext()) { 177 Command command = (Command) it.next(); 178 if (command.isShowOnForm()) { 179 formCommands.add(command); 180 } 181 } 182 } 183 return formCommands; 184 } 185 186 public Command getFirstColumnCommand() { 187 if (!columnCommands.isEmpty()) { 188 return (Command) columnCommands.get(0); 189 } 190 return null; 191 } 192 193 public String getFilterFormId() { 194 return filterFormId; 195 } 196 197 public void setFilterFormId(String filterForm) { 198 this.filterFormId = filterForm; 199 } 200 201 public void setDefaultCommandId(String defaultCommandId) { 202 setDefaultCommandIds(new String [] {defaultCommandId}); 203 } 204 205 public void setDefaultCommandIds(String [] defaultCommandIds) { 206 this.defaultCommandIds = defaultCommandIds; 207 } 208 209 public String [] getDefaultCommandIds() { 210 if (defaultCommandIds == null) { 211 Command command = getFirstColumnCommand(); 212 if (command != null) { 213 defaultCommandIds = new String [] {command.getId()}; 214 } 215 } 216 return defaultCommandIds; 217 } 218 219 public void setOrderBy(String orderBy) { 220 int i = orderBy.indexOf(' '); 221 if (i != -1) { 222 String property = orderBy.substring(0, i); 223 boolean asc = !orderBy.endsWith(" desc"); 224 ColumnConfig col = getColumnConfig(property); 225 boolean caseSensitive = (col == null) ? true : col.isCaseSensitive(); 226 defaultOrder = new Order(property, asc, caseSensitive); 227 } 228 else { 229 ColumnConfig col = getColumnConfig(orderBy); 230 boolean caseSensitive = (col == null) ? true : col.isCaseSensitive(); 231 defaultOrder = new Order(orderBy, true, caseSensitive); 232 } 233 } 234 235 public Order getDefaultOrder() { 236 if (defaultOrder == null) { 237 String property = getFirstSortableProperty(); 238 if (property != null) { 239 ColumnConfig col = getColumnConfig(property); 240 boolean caseSensitive = (col == null) ? true : col.isCaseSensitive(); 241 defaultOrder = new Order(property, true, caseSensitive); 242 } 243 } 244 return defaultOrder; 245 } 246 247 public String [] getSearchProperties() { 248 return this.searchProperties; 249 } 250 251 public void setSearchProperties(String [] searchProperties) { 252 this.searchProperties = searchProperties; 253 } 254 255 public void setSearch(String search) { 256 setSearchProperties(StringUtils.tokenizeToStringArray( 257 search, " ,\t\r\n")); 258 } 259 260 } 261 | Popular Tags |