Source Code Cross Referenced for DisplayItemUtil.java in  » Web-Framework » aranea-mvc-1.1.1 » org » araneaframework » uilib » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » aranea mvc 1.1.1 » org.araneaframework.uilib.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.