01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/help/tags/sakai_2-4-1/help-tool/src/java/org/sakaiproject/jsf/help/HelpSetDefaultActionComponent.java $
03: * $Id: HelpSetDefaultActionComponent.java 28753 2007-04-12 02:02:54Z ajpoland@iupui.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2003, 2004 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.sakaiproject.jsf.help;
21:
22: import java.io.IOException;
23:
24: import javax.faces.component.UIComponent;
25: import javax.faces.component.UIForm;
26: import javax.faces.component.UIOutput;
27: import javax.faces.context.FacesContext;
28: import javax.faces.context.ResponseWriter;
29:
30: /**
31: * help set default action component
32: * @version $Id: HelpSetDefaultActionComponent.java 28753 2007-04-12 02:02:54Z ajpoland@iupui.edu $
33: */
34: public class HelpSetDefaultActionComponent extends UIOutput {
35:
36: /**
37: * @see javax.faces.component.UIComponent#encodeBegin(javax.faces.context.FacesContext)
38: */
39: public void encodeBegin(FacesContext context) throws IOException {
40: return;
41: }
42:
43: public void decode(FacesContext context) {
44: return;
45: }
46:
47: /**
48: * @see javax.faces.component.UIComponent#encodeEnd(javax.faces.context.FacesContext)
49: */
50: public void encodeEnd(FacesContext context) throws IOException {
51: ResponseWriter writer = context.getResponseWriter();
52: UIComponent actionComponent = super .getParent();
53: String acionElement = actionComponent.getClientId(context);
54: UIForm form = getForm(actionComponent);
55: if (form != null) {
56:
57: String formId = form.getClientId(context);
58:
59: writer.startElement("script", null);
60: writer.writeAttribute("type", "text/javascript", null);
61:
62: String functionCode = "if (document.layers) \n"
63: + "document.captureEvents(Event.KEYDOWN); \n"
64: + "document.onkeydown ="
65: + "function (evt) \n {"
66: + " var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : "
67: + "event.keyCode; \n"
68: + "var eventTarget = evt ? evt.target : event.srcElement; \n"
69: + "var textField = eventTarget.type == 'text'; \n"
70: + "if (keyCode == 13 && textField) { \n "
71: + "document.getElementById('" + acionElement
72: + "').click();return false; } \n"
73: + "else return true; }";
74:
75: writer.write(functionCode);
76:
77: writer.endElement("script");
78: }
79: }
80:
81: /**
82: * get form
83: * @param component
84: * @return ui form
85: */
86: private UIForm getForm(UIComponent component) {
87: while (component != null) {
88: if (component instanceof UIForm) {
89: break;
90: }
91: component = component.getParent();
92: }
93: return (UIForm) component;
94: }
95:
96: }
|