Source Code Cross Referenced for _ComponentUtils.java in  » J2EE » myfaces-core-1.2.0 » javax » faces » component » 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 » J2EE » myfaces core 1.2.0 » javax.faces.component 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004 The Apache Software Foundation.
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:         */
016:        package javax.faces.component;
017:
018:        import java.util.Iterator;
019:
020:        import javax.el.ValueExpression;
021:        import javax.faces.application.FacesMessage;
022:        import javax.faces.context.FacesContext;
023:        import javax.faces.el.EvaluationException;
024:        import javax.faces.el.MethodBinding;
025:        import javax.faces.el.ValueBinding;
026:        import javax.faces.validator.Validator;
027:        import javax.faces.validator.ValidatorException;
028:
029:        /**
030:         * A collection of static helper methods for locating UIComponents.
031:         * 
032:         * @author Manfred Geiler (latest modification by $Author: mbr $)
033:         * @version $Revision: 518784 $ $Date: 2007-03-15 23:24:40 +0100 (Do, 15 Mrz 2007) $
034:         */
035:        class _ComponentUtils {
036:            private _ComponentUtils() {
037:            }
038:
039:            static UIComponent findParentNamingContainer(UIComponent component,
040:                    boolean returnRootIfNotFound) {
041:                UIComponent parent = component.getParent();
042:                if (returnRootIfNotFound && parent == null) {
043:                    return component;
044:                }
045:                while (parent != null) {
046:                    if (parent instanceof  NamingContainer)
047:                        return parent;
048:                    if (returnRootIfNotFound) {
049:                        UIComponent nextParent = parent.getParent();
050:                        if (nextParent == null) {
051:                            return parent; //Root
052:                        }
053:                        parent = nextParent;
054:                    } else {
055:                        parent = parent.getParent();
056:                    }
057:                }
058:                return null;
059:            }
060:
061:            static UIComponent getRootComponent(UIComponent component) {
062:                UIComponent parent;
063:                for (;;) {
064:                    parent = component.getParent();
065:                    if (parent == null)
066:                        return component;
067:                    component = parent;
068:                }
069:            }
070:
071:            /**
072:             * Find the component with the specified id starting from the specified
073:             * component.
074:             * <p>
075:             * Param id must not contain any NamingContainer.SEPARATOR_CHAR characters
076:             * (ie ":"). This method explicitly does <i>not</i> search into any
077:             * child naming container components; this is expected to be handled
078:             * by the caller of this method.
079:             * <p>
080:             * For an implementation of findComponent which does descend into
081:             * child naming components, see org.apache.myfaces.custom.util.ComponentUtils.
082:             * 
083:             * @return findBase, a descendant of findBase, or null.
084:             */
085:            static UIComponent findComponent(UIComponent findBase, String id) {
086:                if (idsAreEqual(id, findBase)) {
087:                    return findBase;
088:                }
089:
090:                for (Iterator it = findBase.getFacetsAndChildren(); it
091:                        .hasNext();) {
092:                    UIComponent childOrFacet = (UIComponent) it.next();
093:                    if (!(childOrFacet instanceof  NamingContainer)) {
094:                        UIComponent find = findComponent(childOrFacet, id);
095:                        if (find != null)
096:                            return find;
097:                    } else if (idsAreEqual(id, childOrFacet)) {
098:                        return childOrFacet;
099:                    }
100:                }
101:
102:                return null;
103:            }
104:
105:            /*
106:             * Return true if the specified component matches the provided id.
107:             * This needs some quirks to handle components whose id value gets
108:             * dynamically "tweaked", eg a UIData component whose id gets
109:             * the current row index appended to it.
110:             */
111:            private static boolean idsAreEqual(String id, UIComponent cmp) {
112:                if (id.equals(cmp.getId()))
113:                    return true;
114:
115:                if (cmp instanceof  UIData) {
116:                    UIData uiData = ((UIData) cmp);
117:
118:                    if (uiData.getRowIndex() == -1) {
119:                        return dynamicIdIsEqual(id, cmp.getId());
120:                    }
121:                    return id.equals(cmp.getId()
122:                            + NamingContainer.SEPARATOR_CHAR
123:                            + uiData.getRowIndex());
124:                }
125:
126:                return false;
127:            }
128:
129:            private static boolean dynamicIdIsEqual(String dynamicId, String id) {
130:                return dynamicId.matches(id + ":[0-9]*");
131:            }
132:
133:            static void callValidators(FacesContext context, UIInput input,
134:                    Object convertedValue) {
135:                // first invoke the list of validator components
136:                Validator[] validators = input.getValidators();
137:                for (int i = 0; i < validators.length; i++) {
138:                    Validator validator = validators[i];
139:                    try {
140:                        validator.validate(context, input, convertedValue);
141:                    } catch (ValidatorException e) {
142:                        input.setValid(false);
143:
144:                        String validatorMessage = input.getValidatorMessage();
145:                        if (validatorMessage != null) {
146:                            context
147:                                    .addMessage(
148:                                            input.getClientId(context),
149:                                            new FacesMessage(
150:                                                    FacesMessage.SEVERITY_ERROR,
151:                                                    validatorMessage,
152:                                                    validatorMessage));
153:                        } else {
154:                            FacesMessage facesMessage = e.getFacesMessage();
155:                            if (facesMessage != null) {
156:                                facesMessage
157:                                        .setSeverity(FacesMessage.SEVERITY_ERROR);
158:                                context.addMessage(input.getClientId(context),
159:                                        facesMessage);
160:                            }
161:                        }
162:                    }
163:                }
164:
165:                // now invoke the validator method defined as a method-binding attribute
166:                // on the component
167:                MethodBinding validatorBinding = input.getValidator();
168:                if (validatorBinding != null) {
169:                    try {
170:                        validatorBinding.invoke(context, new Object[] {
171:                                context, input, convertedValue });
172:                    } catch (EvaluationException e) {
173:                        input.setValid(false);
174:                        Throwable cause = e.getCause();
175:                        if (cause instanceof  ValidatorException) {
176:                            String validatorMessage = input
177:                                    .getValidatorMessage();
178:                            if (validatorMessage != null) {
179:                                context.addMessage(input.getClientId(context),
180:                                        new FacesMessage(
181:                                                FacesMessage.SEVERITY_ERROR,
182:                                                validatorMessage,
183:                                                validatorMessage));
184:                            } else {
185:                                FacesMessage facesMessage = ((ValidatorException) cause)
186:                                        .getFacesMessage();
187:                                if (facesMessage != null) {
188:                                    facesMessage
189:                                            .setSeverity(FacesMessage.SEVERITY_ERROR);
190:                                    context
191:                                            .addMessage(input
192:                                                    .getClientId(context),
193:                                                    facesMessage);
194:                                }
195:                            }
196:                        } else {
197:                            throw e;
198:                        }
199:                    }
200:                }
201:            }
202:
203:            static String getStringValue(FacesContext context, ValueBinding vb) {
204:                Object value = vb.getValue(context);
205:                if (value == null) {
206:                    return null;
207:                }
208:                return value.toString();
209:            }
210:
211:            static <T> T getExpressionValue(UIComponent component,
212:                    String attribute, T overrideValue, T defaultValue) {
213:                if (overrideValue != null) {
214:                    return overrideValue;
215:                }
216:                ValueExpression ve = component.getValueExpression(attribute);
217:                if (ve != null) {
218:                    return (T) ve.getValue(component.getFacesContext()
219:                            .getELContext());
220:                }
221:                return defaultValue;
222:            }
223:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.