Showing
5 changed files
with
300 additions
and
0 deletions
pom.xml
0 → 100644
1 | +<?xml version="1.0"?><project> | ||
2 | + <parent> | ||
3 | + <artifactId>ui</artifactId> | ||
4 | + <groupId>org.richfaces.sandbox</groupId> | ||
5 | + <version>3.3.3.Final</version> | ||
6 | + </parent> | ||
7 | + <modelVersion>4.0.0</modelVersion> | ||
8 | + <groupId>org.richfaces.sandbox.ui</groupId> | ||
9 | + <artifactId>focus</artifactId> | ||
10 | + <name>focus</name> | ||
11 | + <version>3.3.3.Final</version> | ||
12 | + <build> | ||
13 | + <plugins> | ||
14 | + <plugin> | ||
15 | + <groupId>org.richfaces.cdk</groupId> | ||
16 | + <artifactId>maven-cdk-plugin</artifactId> | ||
17 | + <version>3.3.3.Final</version> | ||
18 | + <executions> | ||
19 | + <execution> | ||
20 | + <phase>generate-sources</phase> | ||
21 | + <goals> | ||
22 | + <goal>generate</goal> | ||
23 | + </goals> | ||
24 | + </execution> | ||
25 | + </executions> | ||
26 | + <configuration> | ||
27 | + <library> | ||
28 | + <prefix>org.richfaces</prefix> | ||
29 | + <taglib> | ||
30 | + <shortName>focus</shortName> | ||
31 | + </taglib> | ||
32 | + </library> | ||
33 | + </configuration> | ||
34 | + </plugin> | ||
35 | + </plugins> | ||
36 | + </build> | ||
37 | + <dependencies> | ||
38 | + <dependency> | ||
39 | + <groupId>org.richfaces.framework</groupId> | ||
40 | + <artifactId>richfaces-impl</artifactId> | ||
41 | + <version>3.3.3.Final</version> | ||
42 | + </dependency> | ||
43 | + </dependencies> | ||
44 | +</project> |
src/main/config/component/focus.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<!DOCTYPE components PUBLIC "-//AJAX4JSF//CDK Generator config/EN" "http://labs.jboss.com/jbossrichfaces/component-config.dtd" > | ||
3 | +<components> | ||
4 | + <component> | ||
5 | + <name>org.richfaces.Focus</name> | ||
6 | + <family>org.richfaces.Focus</family> | ||
7 | + <classname>org.richfaces.component.html.HtmlFocus</classname> | ||
8 | + <superclass>org.richfaces.component.UIFocus</superclass> | ||
9 | + <description> | ||
10 | + <![CDATA[Puts focus on rendered field. In case of text inputs it's | ||
11 | + content is selected. If you use it with i.e. radio use suffix | ||
12 | + attribute. | ||
13 | + ]]> | ||
14 | + </description> | ||
15 | + <renderer generate="true" override="true"> | ||
16 | + <name>org.richfaces.FocusRenderer</name> | ||
17 | + <template>focus.jspx</template> | ||
18 | + </renderer> | ||
19 | + <tag> | ||
20 | + <name>focus</name> | ||
21 | + <classname>org.richfaces.taglib.FocusTag</classname> | ||
22 | + <superclass> | ||
23 | + org.ajax4jsf.webapp.taglib.HtmlComponentTagBase | ||
24 | + </superclass> | ||
25 | + <test> | ||
26 | + <classname>org.richfaces.taglib.HtmlFocusTagTest</classname> | ||
27 | + <superclassname>org.ajax4jsf.tests.AbstractJspTestCase</superclassname> | ||
28 | + </test> | ||
29 | + | ||
30 | + </tag> | ||
31 | + &ui_component_attributes; | ||
32 | + <property> | ||
33 | + <name>for</name> | ||
34 | + <classname>java.lang.String</classname> | ||
35 | + <description> | ||
36 | + Id of component that should be focused | ||
37 | + </description> | ||
38 | + <defaultvalue>""</defaultvalue> | ||
39 | + </property> | ||
40 | + <property> | ||
41 | + <name>priority</name> | ||
42 | + <classname>java.lang.Integer</classname> | ||
43 | + <description> | ||
44 | + If there are more components requesting focus, | ||
45 | + then component with lowest priority will be focused. | ||
46 | + </description> | ||
47 | + </property> | ||
48 | + <property> | ||
49 | + <name>suffix</name> | ||
50 | + <classname>java.lang.String</classname> | ||
51 | + <description> | ||
52 | + Suffix added to clientId. Useful for focusing radio elements. | ||
53 | + Example : suffix=":0" puts focus on first radio if target is | ||
54 | + radio. | ||
55 | + </description> | ||
56 | + <defaultvalue>""</defaultvalue> | ||
57 | + </property> | ||
58 | + </component> | ||
59 | +</components> |
1 | +package org.richfaces.component; | ||
2 | + | ||
3 | +import java.util.HashSet; | ||
4 | +import java.util.Iterator; | ||
5 | +import java.util.Set; | ||
6 | +import javax.faces.component.UIComponent; | ||
7 | +import javax.faces.component.UIComponentBase; | ||
8 | +import javax.faces.component.UIForm; | ||
9 | +import javax.faces.component.UIInput; | ||
10 | +import javax.faces.context.FacesContext; | ||
11 | + | ||
12 | +public abstract class UIFocus extends UIComponentBase { | ||
13 | + | ||
14 | + public static final String COMPONENT_TYPE = "org.richfaces.Focus"; | ||
15 | + public static final String COMPONENT_FAMILY = "org.richfaces.Focus"; | ||
16 | + | ||
17 | + public Integer getDefaultPriority() { | ||
18 | + UIComponent parentForm = getParent(); | ||
19 | + while (parentForm != null && !(parentForm instanceof UIForm)) { | ||
20 | + parentForm = parentForm.getParent(); | ||
21 | + } | ||
22 | + if (parentForm != null) { | ||
23 | + return getUIInputChildrenCount((UIForm) parentForm, getForComponentId()); | ||
24 | + } else { | ||
25 | + return Integer.MAX_VALUE; | ||
26 | + } | ||
27 | + } | ||
28 | + | ||
29 | + public String getForComponentId() { | ||
30 | + String aFor = getFor(); | ||
31 | + | ||
32 | + if (aFor != null && !"".equals(aFor)) { | ||
33 | + return aFor; | ||
34 | + } else { | ||
35 | + if (!(getParent() instanceof UIInput)) { | ||
36 | + Set<String> allowedClientIds = new HashSet<String>(); | ||
37 | + Iterator<String> clientIdsWithMessages = getFacesContext().getClientIdsWithMessages(); | ||
38 | + while (clientIdsWithMessages.hasNext()) { | ||
39 | + allowedClientIds.add(clientIdsWithMessages.next()); | ||
40 | + } | ||
41 | + UIComponent component = getFirstInput(getParentForm(this), allowedClientIds); | ||
42 | + return component == null ? null : component.getId(); | ||
43 | + } else { | ||
44 | + return getParent().getId(); | ||
45 | + } | ||
46 | + } | ||
47 | + } | ||
48 | + | ||
49 | + private int getUIInputChildrenCount(UIComponent component, String breakOnId) { | ||
50 | + int inputComponentsCount = 0; | ||
51 | + for (UIComponent child : component.getChildren()) { | ||
52 | + if (child.getId().equals(breakOnId)) { | ||
53 | + break; | ||
54 | + } | ||
55 | + if (child instanceof UIInput) { | ||
56 | + inputComponentsCount++; | ||
57 | + } else { | ||
58 | + int uIInputChildrenCount = getUIInputChildrenCount(child, breakOnId); | ||
59 | + inputComponentsCount += uIInputChildrenCount; | ||
60 | + } | ||
61 | + } | ||
62 | + return inputComponentsCount; | ||
63 | + } | ||
64 | + | ||
65 | + public abstract String getFor(); | ||
66 | + | ||
67 | + public abstract void setFor(String value); | ||
68 | + | ||
69 | + public abstract String getSuffix(); | ||
70 | + | ||
71 | + public abstract void setSuffix(String value); | ||
72 | + | ||
73 | + public abstract Integer getPriority(); | ||
74 | + | ||
75 | + public abstract void setPriority(Integer value); | ||
76 | + | ||
77 | + private UIComponent getFirstInput(UIComponent parent, Set<String> allowedClientIds) { | ||
78 | + UIComponent input = null; | ||
79 | + FacesContext facesContext = getFacesContext(); | ||
80 | + for (UIComponent child : parent.getChildren()) { | ||
81 | + if (child instanceof UIInput && (allowedClientIds.size() == 0 || allowedClientIds.contains(child.getClientId(facesContext)))) { | ||
82 | + return child; | ||
83 | + } else { | ||
84 | + input = getFirstInput(child, allowedClientIds); | ||
85 | + if (input != null) { | ||
86 | + break; | ||
87 | + } | ||
88 | + } | ||
89 | + } | ||
90 | + return input; | ||
91 | + } | ||
92 | + | ||
93 | + private UIForm getParentForm(UIComponent component) { | ||
94 | + UIComponent parent = component.getParent(); | ||
95 | + if (parent == null) { | ||
96 | + return null; | ||
97 | + } | ||
98 | + if (parent instanceof UIForm) { | ||
99 | + return (UIForm) parent; | ||
100 | + } else { | ||
101 | + return getParentForm(parent); | ||
102 | + } | ||
103 | + } | ||
104 | +} |
1 | +if (!window.Richfaces) window.Richfaces = {}; | ||
2 | + | ||
3 | +Richfaces.FocusManager = (function(){ | ||
4 | + | ||
5 | + var m_focus; | ||
6 | + var m_priority = 999999; | ||
7 | + var eventAttached = false; | ||
8 | + | ||
9 | + var attachEvent = function() { | ||
10 | + if(!eventAttached) { | ||
11 | + document.observe('dom:loaded',function(){ | ||
12 | + var element = $(m_focus); | ||
13 | + element.focus(); | ||
14 | + element.select(element); | ||
15 | + Richfaces.FocusManager.clearFocus(); | ||
16 | + }); | ||
17 | + } | ||
18 | + eventAttached = true; | ||
19 | + }; | ||
20 | + | ||
21 | + return { | ||
22 | + getFocus : function() { | ||
23 | + return m_focus; | ||
24 | + }, | ||
25 | + setFocus : function(id,priority) { | ||
26 | + if(priority == null) { | ||
27 | + priority = 99999; | ||
28 | + } | ||
29 | + if(m_focus == null || priority < m_priority) { | ||
30 | + m_focus = id; | ||
31 | + m_priority = priority == null ? 0 : priority; | ||
32 | + attachEvent(); | ||
33 | + } | ||
34 | + }, | ||
35 | + clearFocus : function() { | ||
36 | + m_focus = null; | ||
37 | + m_priority = 999999; | ||
38 | + } | ||
39 | + }; | ||
40 | + })(); |
src/main/templates/focus.jspx
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<f:root | ||
3 | + xmlns:f="http://jsf.exadel.com/template" | ||
4 | + xmlns:vcp=" http://jsf.exadel.com/vcp" | ||
5 | + xmlns:ui=" http://jsf.exadel.com/ui" | ||
6 | + xmlns:x=" http://jsf.exadel.com/vcp" | ||
7 | + xmlns:u="http://jsf.exadel.com/util" | ||
8 | + xmlns:h="http://jsf.exadel.com/header" | ||
9 | + component="org.richfaces.component.UIFocus" | ||
10 | + baseclass="org.ajax4jsf.renderkit.HeaderResourcesRendererBase" | ||
11 | + class="org.richfaces.renderkit.html.FocusRenderer"> | ||
12 | + | ||
13 | + <f:clientid var="clientId"/> | ||
14 | + <c:set var="for" value="#{component.forComponentId}"/> | ||
15 | + <c:set var="priority" value="#{component.attributes['priority']}"/> | ||
16 | + <h:scripts>new org.ajax4jsf.javascript.PrototypeScript(),/org/richfaces/renderkit/html/scripts/focus.js</h:scripts> | ||
17 | + | ||
18 | + <jsp:scriptlet> | ||
19 | +<![CDATA[ | ||
20 | + Integer priority = (Integer)variables.getVariable("priority"); | ||
21 | + if(priority == null) { | ||
22 | + priority = component.getDefaultPriority(); | ||
23 | + } | ||
24 | + | ||
25 | + variables.setVariable("priority",priority); | ||
26 | + String sid = (String) variables.getVariable("for"); | ||
27 | + String target; | ||
28 | + if (sid != null && ! "".equals(sid)) { | ||
29 | + try { | ||
30 | + UIComponent forcomp = getUtils().findComponentFor((UIComponent)component,sid); | ||
31 | + if (forcomp != null) { | ||
32 | + target = forcomp.getClientId(context); | ||
33 | + } else { | ||
34 | + target = sid; | ||
35 | + } | ||
36 | + }catch(IllegalArgumentException e) { | ||
37 | + target = sid; | ||
38 | + } | ||
39 | + String suffix = component.getSuffix(); | ||
40 | + if(suffix != null && !"".equals(suffix)) { | ||
41 | + target += suffix; | ||
42 | + } | ||
43 | + } else { | ||
44 | + target = component.getParent().getClientId(context); | ||
45 | + } | ||
46 | + variables.setVariable("for",target); | ||
47 | +]]> | ||
48 | + </jsp:scriptlet> | ||
49 | + | ||
50 | + <script type="text/javascript"> | ||
51 | + Richfaces.FocusManager.setFocus("#{for}",#{priority}); | ||
52 | + </script> | ||
53 | +</f:root> |
Please
register
or
login
to post a comment