001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.riot.list;
025:
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import org.riotfamily.riot.dao.Order;
031: import org.riotfamily.riot.dao.RiotDao;
032: import org.riotfamily.riot.list.command.Command;
033: import org.springframework.util.StringUtils;
034:
035: /**
036: *
037: */
038: public class ListConfig {
039:
040: private static final String DEFAULT_ID_PROPERTY = "id";
041:
042: private String id;
043:
044: private RiotDao dao;
045:
046: private List columnConfigs = new ArrayList();
047:
048: private List commands = new ArrayList();
049:
050: private ArrayList columnCommands = new ArrayList();
051:
052: private List formCommands;
053:
054: private String[] defaultCommandIds;
055:
056: private String idProperty;
057:
058: private String rowStyleProperty;
059:
060: private int pageSize = 15;
061:
062: private String filterFormId;
063:
064: private Order defaultOrder;
065:
066: private String[] searchProperties;
067:
068: public String getId() {
069: return id;
070: }
071:
072: public void setId(String id) {
073: this .id = id;
074: }
075:
076: public String getIdProperty() {
077: return idProperty != null ? idProperty : DEFAULT_ID_PROPERTY;
078: }
079:
080: public void setIdProperty(String idProperty) {
081: this .idProperty = idProperty;
082: }
083:
084: public String getRowStyleProperty() {
085: return this .rowStyleProperty;
086: }
087:
088: public void setRowStyleProperty(String rowStyleProperty) {
089: this .rowStyleProperty = rowStyleProperty;
090: }
091:
092: public void setDao(RiotDao dao) {
093: this .dao = dao;
094: }
095:
096: public RiotDao getDao() {
097: return dao;
098: }
099:
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
147: .get(0);
148: return columnConfig.getProperty();
149: }
150: return null;
151: }
152:
153: public String getFirstSortableProperty() {
154: Iterator it = columnConfigs.iterator();
155: while (it.hasNext()) {
156: ColumnConfig columnConfig = (ColumnConfig) it.next();
157: if (columnConfig.getProperty() != null
158: && columnConfig.isSortable()) {
159: return columnConfig.getProperty();
160: }
161: }
162: return null;
163: }
164:
165: public void addColumnCommand(Command command) {
166: columnCommands.add(command);
167: }
168:
169: public List getColumnCommands() {
170: return columnCommands;
171: }
172:
173: public List getFormCommands() {
174: if (formCommands == null) {
175: formCommands = new ArrayList();
176: Iterator it = columnCommands.iterator();
177: while (it.hasNext()) {
178: Command command = (Command) it.next();
179: if (command.isShowOnForm()) {
180: formCommands.add(command);
181: }
182: }
183: }
184: return formCommands;
185: }
186:
187: public Command getFirstColumnCommand() {
188: if (!columnCommands.isEmpty()) {
189: return (Command) columnCommands.get(0);
190: }
191: return null;
192: }
193:
194: public String getFilterFormId() {
195: return filterFormId;
196: }
197:
198: public void setFilterFormId(String filterForm) {
199: this .filterFormId = filterForm;
200: }
201:
202: public void setDefaultCommandId(String defaultCommandId) {
203: setDefaultCommandIds(new String[] { defaultCommandId });
204: }
205:
206: public void setDefaultCommandIds(String[] defaultCommandIds) {
207: this .defaultCommandIds = defaultCommandIds;
208: }
209:
210: public String[] getDefaultCommandIds() {
211: if (defaultCommandIds == null) {
212: Command command = getFirstColumnCommand();
213: if (command != null) {
214: defaultCommandIds = new String[] { command.getId() };
215: }
216: }
217: return defaultCommandIds;
218: }
219:
220: public void setOrderBy(String orderBy) {
221: int i = orderBy.indexOf(' ');
222: if (i != -1) {
223: String property = orderBy.substring(0, i);
224: boolean asc = !orderBy.endsWith(" desc");
225: ColumnConfig col = getColumnConfig(property);
226: boolean caseSensitive = (col == null) ? true : col
227: .isCaseSensitive();
228: defaultOrder = new Order(property, asc, caseSensitive);
229: } else {
230: ColumnConfig col = getColumnConfig(orderBy);
231: boolean caseSensitive = (col == null) ? true : col
232: .isCaseSensitive();
233: defaultOrder = new Order(orderBy, true, caseSensitive);
234: }
235: }
236:
237: public Order getDefaultOrder() {
238: if (defaultOrder == null) {
239: String property = getFirstSortableProperty();
240: if (property != null) {
241: ColumnConfig col = getColumnConfig(property);
242: boolean caseSensitive = (col == null) ? true : col
243: .isCaseSensitive();
244: defaultOrder = new Order(property, true, caseSensitive);
245: }
246: }
247: return defaultOrder;
248: }
249:
250: public String[] getSearchProperties() {
251: return this .searchProperties;
252: }
253:
254: public void setSearchProperties(String[] searchProperties) {
255: this .searchProperties = searchProperties;
256: }
257:
258: public void setSearch(String search) {
259: setSearchProperties(StringUtils.tokenizeToStringArray(search,
260: " ,\t\r\n"));
261: }
262:
263: }
|