001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.uilib.list.structure;
016:
017: import java.io.Serializable;
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.araneaframework.uilib.list.structure.order.MultiFieldOrder;
026:
027: public class BaseListStructure implements Serializable {
028:
029: private static final long serialVersionUID = 1L;
030:
031: private static final Log log = LogFactory
032: .getLog(BaseListStructure.class);
033:
034: /**
035: * Map of {@link ListField}s where the column Ids are the keys and columns
036: * are the values.
037: */
038: protected Map fields = new HashMap();
039:
040: /**
041: * List of {@link ListField}s.
042: */
043: protected List fieldList = new ArrayList();
044:
045: /**
046: * {@link ListOrder} information.
047: */
048: protected ListOrder order;
049:
050: /**
051: * {@link ListFilter} information.
052: */
053: protected ListFilter filter;
054:
055: /**
056: * Returns {@link ListField}s.
057: *
058: * @return {@link ListField}s.
059: */
060: public Map getFields() {
061: return this .fields;
062: }
063:
064: /**
065: * Returns {@link ListField}s.
066: *
067: * @return {@link ListField}s.
068: */
069: public List getFieldList() {
070: return this .fieldList;
071: }
072:
073: /**
074: * Returns {@link ListField}.
075: *
076: * @param id
077: * {@link ListField}identifier.
078: * @return {@link ListField}.
079: */
080: public ListField getField(String id) {
081: return (ListField) this .fields.get(id);
082: }
083:
084: /**
085: * Adds a {@link ListField}.
086: *
087: * @param column
088: * {@link ListField}.
089: */
090: public void addField(ListField column) {
091: this .fields.put(column.getId(), column);
092: this .fieldList.add(column);
093: }
094:
095: /**
096: * Clears the {@link ListField}s
097: */
098: public void clearFields() {
099: this .fields = new HashMap();
100: this .fieldList = new ArrayList();
101: }
102:
103: /**
104: * Returns the {@link ListOrder}.
105: *
106: * @return the {@link ListOrder}.
107: */
108: public ListOrder getListOrder() {
109: return this .order;
110: }
111:
112: /**
113: * Sets the {@link ListOrder}.
114: * @param order
115: * the {@link ListOrder}.
116: */
117: public void setListOrder(ListOrder order) {
118: this .order = order;
119: }
120:
121: /**
122: * Returns the {@link ListFilter}.
123: *
124: * @return the {@link ListFilter}.
125: */
126: public ListFilter getListFilter() {
127: return this .filter;
128: }
129:
130: /**
131: * Saves the {@link ListFilter}.
132: *
133: * @param filter
134: * the {@link ListFilter}.
135: */
136: public void setListFilter(ListFilter filter) {
137: this .filter = filter;
138: }
139:
140: /**
141: * Returns view model.
142: *
143: * @return view model.
144: */
145: public ViewModel getViewModel() {
146: return new ViewModel();
147: }
148:
149: /**
150: * View Model.
151: *
152: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
153: */
154: public class ViewModel implements Serializable {
155:
156: private static final long serialVersionUID = 1L;
157:
158: protected Map columns = new HashMap();
159: protected List columnList = new ArrayList();
160: protected Map columnOrders = new HashMap();
161:
162: /**
163: * Takes a snapshot of outer class state.
164: */
165: protected ViewModel() {
166: MultiFieldOrder multiOrder = getListOrder() instanceof MultiFieldOrder ? (MultiFieldOrder) getListOrder()
167: : null;
168:
169: for (Iterator i = BaseListStructure.this .fieldList
170: .iterator(); i.hasNext();) {
171: ListField.ViewModel currentColumn = ((ListField) i
172: .next()).getViewModel();
173: boolean isOrdered = multiOrder != null
174: && multiOrder.isFiedOrdered(currentColumn
175: .getId());
176:
177: this .columnList.add(currentColumn);
178: this .columns.put(currentColumn.getId(), currentColumn);
179: this .columnOrders.put(currentColumn.getId(),
180: isOrdered ? Boolean.TRUE : Boolean.FALSE);
181: }
182: }
183:
184: /**
185: * @return Returns the columnList.
186: */
187: public List getColumnList() {
188: return this .columnList;
189: }
190:
191: /**
192: * @return Returns the columns.
193: */
194: public Map getColumns() {
195: return this .columns;
196: }
197:
198: /**
199: * Returns <code>true</code> if the column can be ordered.
200: *
201: * @param column
202: * the column name.
203: * @return Returns <code>true</code> if the column can be ordered.
204: */
205: public boolean isColumnOrdered(String column) {
206: return ((Boolean) this.columnOrders.get(column))
207: .booleanValue();
208: }
209: }
210: }
|