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.util;
016:
017: import java.io.Serializable;
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.ListIterator;
022: import org.apache.commons.collections.Transformer;
023: import org.araneaframework.backend.util.BeanMapper;
024: import org.araneaframework.core.Assert;
025: import org.araneaframework.uilib.support.DisplayItem;
026:
027: /**
028: * Represents the items put into {@link org.araneaframework.uilib.form.control.SelectControl}or
029: * {@link org.araneaframework.uilib.form.control.MultiSelectControl}.
030: *
031: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
032: */
033: public class DisplayItemUtil implements java.io.Serializable {
034:
035: //*********************************************************************
036: //* PUBLIC METHODS
037: //*********************************************************************
038:
039: /**
040: * Creates {@link DisplayItem}s corresponding to beans in <code>beanCollection</code> and adds
041: * these to provided <code>displayItemContainer</code>.
042: *
043: * @param displayItemContainer the container for created {@link DisplayItem}s
044: * @param beanCollection <code>Collection</code> of beans, may not contain <code>null</code>.
045: * @param valueName the name of the bean field corresponding to the (submitted) value of the select item.
046: * @param displayStringName the name of the bean field corresponding to the displayed string (label) of the select item.
047: */
048: public static void addItemsFromBeanCollection(
049: DisplayItemContainer displayItemContainer,
050: Collection beanCollection, String valueName,
051: String displayStringName) {
052: Assert.notNullParam(displayItemContainer,
053: "displayItemContainer");
054: Assert.noNullElementsParam(beanCollection, "beanCollection");
055: Assert.notEmptyParam(valueName, "valueName");
056: Assert.notEmptyParam(displayStringName, "displayStringName");
057:
058: if (beanCollection.size() == 0)
059: return;
060: BeanMapper beanMapper = new BeanMapper(beanCollection
061: .iterator().next().getClass());
062:
063: Transformer valueTransformer = new BeanToPropertyValueTransformer(
064: beanMapper, valueName);
065: Transformer displayTransformer = new BeanToPropertyValueTransformer(
066: beanMapper, displayStringName);
067: addItemsFromBeanCollection(displayItemContainer,
068: beanCollection, valueTransformer, displayTransformer);
069: }
070:
071: /**
072: * Creates {@link DisplayItem}s corresponding to beans in <code>beanCollection</code> and adds
073: * these to provided <code>displayItemContainer</code>.
074: *
075: * @param displayItemContainer the container for created {@link DisplayItem}s
076: * @param beanCollection <code>Collection</code> of beans, may not contain <code>null</code>.
077: * @param valueName the name of the bean field corresponding to the (submitted) value of the select item.
078: * @param displayTransformer Transformer producing label (displayString) from a bean
079: *
080: * @since 1.1
081: */
082: public static void addItemsFromBeanCollection(
083: DisplayItemContainer displayItemContainer,
084: Collection beanCollection, String valueName,
085: Transformer displayTransformer) {
086: Assert.notNullParam(displayItemContainer,
087: "displayItemContainer");
088: Assert.noNullElementsParam(beanCollection, "beanCollection");
089: Assert.notEmptyParam(valueName, "valueName");
090: Assert.notNullParam(displayTransformer, "displayTransformer");
091:
092: if (beanCollection.size() == 0)
093: return;
094: BeanMapper beanMapper = new BeanMapper(beanCollection
095: .iterator().next().getClass());
096: Transformer valueTransformer = new BeanToPropertyValueTransformer(
097: beanMapper, valueName);
098: addItemsFromBeanCollection(displayItemContainer,
099: beanCollection, valueTransformer, displayTransformer);
100: }
101:
102: /**
103: * Creates {@link DisplayItem}s corresponding to beans in <code>beanCollection</code> and adds
104: * these to provided <code>displayItemContainer</code>.
105: *
106: * @param displayItemContainer the container for created {@link DisplayItem}s
107: * @param beanCollection <code>Collection</code> of beans, may not contain <code>null</code>.
108: * @param valueName the name of the bean field corresponding to the (submitted) value of the select item.
109: * @param displayStringName the name of the bean field corresponding to the displayed string (label) of the select item.
110: * @param valueTransformer Transformer producing value ({@link DisplayItem#getValue()}) from a bean.
111: *
112: * @since 1.1
113: */
114: public static void addItemsFromBeanCollection(
115: DisplayItemContainer displayItemContainer,
116: Collection beanCollection, Transformer valueTransformer,
117: String displayStringName) {
118: Assert.notNullParam(displayItemContainer,
119: "displayItemContainer");
120: Assert.noNullElementsParam(beanCollection, "beanCollection");
121: Assert.notNullParam(valueTransformer, "valueTransformer");
122: Assert.notEmptyParam(displayStringName, "displayStringName");
123:
124: if (beanCollection.size() == 0)
125: return;
126: BeanMapper beanMapper = new BeanMapper(beanCollection
127: .iterator().next().getClass());
128: Transformer displayTransformer = new BeanToPropertyValueTransformer(
129: beanMapper, displayStringName);
130: addItemsFromBeanCollection(displayItemContainer,
131: beanCollection, valueTransformer, displayTransformer);
132: }
133:
134: /**
135: * Creates {@link DisplayItem}s corresponding to beans in <code>beanCollection</code> and adds
136: * these to provided <code>displayItemContainer</code>.
137: *
138: * @param displayItemContainer the container for created {@link DisplayItem}s
139: * @param beanCollection <code>Collection</code> of beans, may not contain <code>null</code>.
140: * @param valueName the name of the bean field corresponding to the (submitted) value of the select item.
141: * @param valueTransformer Transformer producing value ({@link DisplayItem#getValue()}) from a bean.
142: * @param displayTransformer Transformer producing label (displayString) from a bean
143: *
144: * @since 1.1
145: */
146: public static void addItemsFromBeanCollection(
147: DisplayItemContainer displayItemContainer,
148: Collection beanCollection, Transformer valueTransformer,
149: Transformer displayTransformer) {
150: if (beanCollection == null || beanCollection.size() == 0)
151: return;
152:
153: Assert.notNullParam(displayItemContainer,
154: "displayItemContainer");
155: Assert.notNullParam(valueTransformer, "valueTransformer");
156: Assert.notNullParam(displayTransformer, "displayTransformer");
157:
158: for (Iterator i = beanCollection.iterator(); i.hasNext();) {
159: Object vo = i.next();
160: displayItemContainer.addItem(new DisplayItem(
161: (String) valueTransformer.transform(vo),
162: (String) displayTransformer.transform(vo)));
163: }
164: }
165:
166: /**
167: * Returns whether <code>value</code> is found in the select items.
168: * @param value the value that is controlled.
169: * @return whether <code>value</code> is found in the select items.
170: */
171: public static boolean isValueInItems(
172: DisplayItemContainer displayItemContainer, String value) {
173: return isValueInItems(displayItemContainer.getDisplayItems(),
174: value);
175: }
176:
177: /**
178: * Returns whether <code>value</code> is found in the select items.
179: * @param value the value that is controlled.
180: * @return whether <code>value</code> is found in the select items.
181: */
182: public static boolean isValueInItems(Collection displayItems,
183: String value) {
184: Assert.noNullElementsParam(displayItems, "displayItems");
185:
186: for (Iterator i = displayItems.iterator(); i.hasNext();) {
187: DisplayItem currentItem = (DisplayItem) i.next();
188: String currentValue = currentItem.getValue();
189: if (value == null && currentValue == null
190: && !currentItem.isDisabled())
191: return true;
192: if (value != null && value.equals(currentValue)
193: && !currentItem.isDisabled())
194: return true;
195: }
196: return false;
197: }
198:
199: /**
200: * Returns display item label by the specified value.
201: *
202: * @param displayItems display items.
203: * @param value display item value.
204: * @return display item label by the specified value.
205: */
206: public static String getLabelForValue(Collection displayItems,
207: String value) {
208: Assert.noNullElementsParam(displayItems, "displayItems");
209:
210: for (Iterator i = displayItems.iterator(); i.hasNext();) {
211: DisplayItem item = (DisplayItem) i.next();
212: String currentValue = item.getValue();
213: if (value == null && currentValue == null)
214: return item.getDisplayString();
215: if (value != null && value.equals(currentValue))
216: return item.getDisplayString();
217: }
218: return "";
219: }
220:
221: /**
222: * Returns display item index by the specified value.
223: *
224: * @param displayItems display items.
225: * @param value display item value.
226: * @return display item index by the specified value.
227: */
228: public static int getValueIndex(List displayItems, String value) {
229: Assert.noNullElementsParam(displayItems, "displayItems");
230:
231: for (ListIterator i = displayItems.listIterator(); i.hasNext();) {
232: DisplayItem item = (DisplayItem) i.next();
233: if ((value == null && item.getValue() == null)
234: || value != null && item.getValue() != null
235: && value.equals(item.getValue())) {
236: return i.previousIndex();
237: }
238: }
239:
240: return -1;
241: }
242:
243: private static class BeanToPropertyValueTransformer implements
244: Transformer, Serializable {
245: private static final long serialVersionUID = 1L;
246: private final BeanMapper bm;
247: private final String propertyName;
248:
249: public BeanToPropertyValueTransformer(
250: final BeanMapper beanMapper, final String propertyName) {
251: this .bm = beanMapper;
252: this .propertyName = propertyName;
253: }
254:
255: public Object transform(Object bean) {
256: return bm.getFieldValue(bean, propertyName).toString();
257: }
258: }
259: }
|