javax.faces.component.UIForm Java Examples

The following examples show how to use javax.faces.component.UIForm. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AJAXRenderer.java    From BootsFaces-OSP with Apache License 2.0 6 votes vote down vote up
private static void encodeFormSubmit(UIComponent component, ResponseWriter rw, boolean evenWithoutParameters)
		throws IOException {
	String parameterList = "";
	List<UIComponent> children = component.getChildren();
	for (UIComponent parameter : children) {
		if (parameter instanceof UIParameter) {
			String value = String.valueOf(((UIParameter) parameter).getValue());
			String name = ((UIParameter) parameter).getName();
			if (null != value) {
				parameterList += ",'" + name + "':'" + value + "'";
			}
		}
	}
	if (evenWithoutParameters || parameterList.length() > 0) {
		UIForm currentForm = getSurroundingForm((UIComponent) component, false);
		parameterList = "'" + currentForm.getClientId() + "',{'" + component.getClientId() + "':'"
				+ component.getClientId() + "'" + parameterList + "}";
		rw.writeAttribute("onclick",
				encodeClick((UIComponent) component) + "BsF.submitForm(" + parameterList + ");return false;", null);
	}
}
 
Example #2
Source File: UICommandButton.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void decode(FacesContext context) {
    Map paramMap = context.getExternalContext().getRequestParameterMap();

    UIForm parentForm = JsfRenderUtils.findForm(this);
    String parentFormClientId = parentForm.getClientId(context);
    String eventSenderHiddenFieldId = JsfRenderUtils.getEventSenderHiddenFieldId(parentFormClientId);

    String eventSenderId = (String) paramMap.get(eventSenderHiddenFieldId);
    boolean isReset = false;

    if (this.getType() != null && this.getType().equalsIgnoreCase(INPUT_RESET_TYPE)) {
        isReset = true;
    }

    if (!isReset && (eventSenderId != null) && eventSenderId.equals(this.getClientId(context))) {
        this.queueEvent(new ActionEvent(this));
    }

}
 
Example #3
Source File: JsfRenderUtils.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 
 * @param context
 * @param component
 * @param uiParameterList
 * @return
 */
public static String getSubmitJavaScriptWithParameters(FacesContext context, UIComponent component,
        List<UIParameter> uiParameterList) {
    UIForm parentForm = findForm(component);
    String parentFormClientId = parentForm.getClientId(context);
    String componentClientId = component.getClientId(context);
    String eventSenderHiddenFieldId = getEventSenderHiddenFieldId(parentFormClientId);

    StringBuilder setParametersJavaScript = new StringBuilder();

    for (int i = 0; i < uiParameterList.size(); i++) {
        UIParameter parameter = uiParameterList.get(i);
        setParametersJavaScript.append("document.forms['").append(parentFormClientId).append("'].elements['")
                .append(parameter.getName()).append("'].value='").append(parameter.getValue()).append("';");

    }

    StringBuilder onClickEvent = new StringBuilder();
    onClickEvent.append("document.forms['").append(parentFormClientId).append("'].elements['");
    onClickEvent.append(eventSenderHiddenFieldId).append("'].value='").append(componentClientId).append("';");
    onClickEvent.append(setParametersJavaScript.toString());
    onClickEvent.append("document.forms['").append(parentFormClientId).append("'].submit();");

    return onClickEvent.toString();
}
 
Example #4
Source File: JsfRenderUtils.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static String getSubmitJavaScriptWithArgument(FacesContext context, UIComponent component, String eventArgument) {
    UIForm parentForm = findForm(component);
    String parentFormClientId = parentForm.getClientId(context);
    String componentClientId = component.getClientId(context);
    String eventSenderHiddenFieldId = getEventSenderHiddenFieldId(parentFormClientId);
    String eventArgumentHiddenFieldId = getEventArgumentHiddenFieldId(parentFormClientId);

    StringBuilder onClickEvent = new StringBuilder();
    onClickEvent.append("document.forms['").append(parentFormClientId).append("'].elements['");
    onClickEvent.append(eventSenderHiddenFieldId).append("'].value='").append(componentClientId).append("';");

    onClickEvent.append("document.forms['").append(parentFormClientId).append("'].elements['");
    onClickEvent.append(eventArgumentHiddenFieldId).append("'].value='").append(eventArgument).append("';");

    onClickEvent.append("document.forms['").append(parentFormClientId).append("'].submit();");

    return onClickEvent.toString();
}
 
Example #5
Source File: JsfRenderUtils.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void addEventHandlingHiddenFieldsIfNotExists(FacesContext context, UIComponent component) throws IOException {

        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        UIForm parentForm = findForm(component);
        String parentFormClientId = parentForm.getClientId(context);
        String eventSenderHiddenFieldFinalId = getEventSenderHiddenFieldId(parentFormClientId);
        String eventSenderHiddenFieldRenderedAttribute = getEventSenderHiddenFieldRenderedAttributeName(parentFormClientId);
        String eventArgumentHiddenFieldFinalId = getEventArgumentHiddenFieldId(parentFormClientId);
        String eventArgumentHiddenFieldRenderedAttribute = getEventArgumentHiddenFieldRenderedAttributeName(parentFormClientId);
        ResponseWriter writer = context.getResponseWriter();

        if (request.getAttribute(eventSenderHiddenFieldRenderedAttribute) == null) {
            renderEmptyHiddenField(eventSenderHiddenFieldFinalId, writer);
            request.setAttribute(eventSenderHiddenFieldRenderedAttribute, true);
        }

        if (request.getAttribute(eventArgumentHiddenFieldRenderedAttribute) == null) {
            renderEmptyHiddenField(eventArgumentHiddenFieldFinalId, writer);
            request.setAttribute(eventArgumentHiddenFieldRenderedAttribute, true);
        }
    }
 
Example #6
Source File: CoreRenderer.java    From BootsFaces-OSP with Apache License 2.0 6 votes vote down vote up
public static void assertComponentIsInsideForm(UIComponent component, String msg, boolean throwException) {
	if (!FacesContext.getCurrentInstance().isProjectStage(ProjectStage.Production)) {
		UIComponent c = component;
		while ((c != null) && (!(c instanceof UIForm))) {
			c = c.getParent();
		}
		if (!(c instanceof UIForm)) {
			System.out.println("Warning: The BootsFaces component " + component.getClass()
					+ " works better if put inside a form. These capabilities get lost if not put in a form:");
			if (throwException) {
				throw new FacesException(msg);
			} else {
				System.out.println(msg);
			}
		}
	}
}
 
Example #7
Source File: CoreRenderer.java    From BootsFaces-OSP with Apache License 2.0 6 votes vote down vote up
/**
 * @deprecated Use
 *             {@link CoreRenderer#generateErrorAndRequiredClass(javax.faces.component.UIInput, javax.faces.context.ResponseWriter, java.lang.String, java.lang.String, java.lang.String, java.lang.String) }
 *             instead
 * 
 *             Renders the CSS pseudo classes for required fields and for the
 *             error levels.
 *
 * @param input
 * @param rw
 * @param clientId
 * @throws IOException
 */
@Deprecated
public void generateErrorAndRequiredClassForLabels(UIInput input, ResponseWriter rw, String clientId,
		String additionalClass) throws IOException {
	String styleClass = getErrorAndRequiredClass(input, clientId);
	if (null != additionalClass) {
		additionalClass = additionalClass.trim();
		if (additionalClass.trim().length() > 0) {
			styleClass += " " + additionalClass;
		}
	}
	UIForm currentForm = AJAXRenderer.getSurroundingForm((UIComponent) input, true);
	if (currentForm instanceof Form) {
		if (((Form) currentForm).isHorizontal()) {
			styleClass += " control-label";
		}
	}
	if (input instanceof IResponsiveLabel) {
		String responsiveLabelClass = Responsive.getResponsiveLabelClass((IResponsiveLabel) input);
		if (null != responsiveLabelClass) {
			styleClass += " " + responsiveLabelClass;
		}
	}

	rw.writeAttribute("class", styleClass, "class");
}
 
Example #8
Source File: FormExpressionResolver.java    From BootsFaces-OSP with Apache License 2.0 6 votes vote down vote up
public List<UIComponent> resolve(UIComponent component, List<UIComponent> parentComponents, String currentId,
			String originalExpression, String[] parameters) {

		List<UIComponent> result = new ArrayList<UIComponent>();
//		for (UIComponent parent : parentComponents) {
			UIComponent c = component;

			while (c != null && c.getClass() != UIViewRoot.class) {
				if (UIForm.class.isAssignableFrom(c.getClass())) {
					result.add(c);
					break;
				}
				c = c.getParent();
			}
//		}
		if (result.size() > 0) {
			return result;
		}
		throw new FacesException("Invalid search expression - the component isn't inside a form " + originalExpression);

	}
 
Example #9
Source File: AbstractPager.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
public UIComponent findSharedDataPagerParent() {
    String dataId = getFor();
    if (dataId != null) {
        UIComponent data = findComponent(dataId);
        if (data instanceof FacesDataIterator) {
            UIComponent dataParent = data;
            while (dataParent != null) {
                UIComponent pagerParent = getParent();
                while (pagerParent != null) {
                    if (pagerParent == dataParent) {
                        // Need to check that the shared parent has a user set Id as components
                        // with autogenerated Id's are not guaranteed to generate output
                        // that is capable of being partially refreshed.
                        while (dataParent!=null) {
                        	if(HtmlUtil.isUserId(dataParent.getId())) {
                        		break;
                        	}
                        	if(dataParent instanceof UIForm) {
                        		// Form always generate an ID, even when it is auto-generated.
                        		break;
                        	}
                            dataParent = dataParent.getParent();
                        }
                        return dataParent;
                    }
                    pagerParent = pagerParent.getParent();
                }
                dataParent = dataParent.getParent();
            }
        }
    }
    return null;
}
 
Example #10
Source File: HelpSetDefaultActionComponent.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * get form
 * @param component
 * @return ui form
 */
private UIForm getForm(UIComponent component)
{
  while (component != null)
  {
    if (component instanceof UIForm)
    {
      break;
    }
    component = component.getParent();
  }
  return (UIForm) component;
}
 
Example #11
Source File: JsfRenderUtils.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void addHiddenFieldsForParametersIfNotExists(FacesContext context, UIComponent parentComponent,
        List<UIParameter> uiParameters) throws IOException {

    UIForm parentForm = findForm(parentComponent);
    String parentFormClientId = parentForm.getClientId(context);
    ResponseWriter writer = context.getResponseWriter();
    HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

    for (int i = 0; i < uiParameters.size(); i++) {
        UIParameter parameter = uiParameters.get(i);
        addHiddenFieldForParameterIfNotExists(parentFormClientId, request, writer, parameter.getName());
    }
}
 
Example #12
Source File: RendererUtil.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/** Return the form ID of the form containing the given component */
public static String getFormId(FacesContext context, UIComponent component)
{
    while (component != null && !(component instanceof UIForm))
    {
        component = component.getParent();
    }
    if (component != null)
        return ((UIForm) component).getId();
    return null;
}
 
Example #13
Source File: JsfRenderUtils.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static String getSubmitJavaScript(FacesContext context, UIComponent component) {
    UIForm parentForm = findForm(component);
    String parentFormClientId = parentForm.getClientId(context);
    String componentClientId = component.getClientId(context);
    String eventSenderHiddenFieldId = getEventSenderHiddenFieldId(parentFormClientId);

    StringBuilder onClickEvent = new StringBuilder();
    onClickEvent.append("document.forms['").append(parentFormClientId).append("'].elements['");
    onClickEvent.append(eventSenderHiddenFieldId).append("'].value='").append(componentClientId);

    onClickEvent.append("';document.forms['").append(parentFormClientId).append("'].submit();");

    return onClickEvent.toString();
}
 
Example #14
Source File: JsfRenderUtils.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static UIForm findForm(UIComponent component) {
    UIComponent formCandidate = component.getParent();

    while (formCandidate != null && !(formCandidate instanceof UIForm)) {
        formCandidate = formCandidate.getParent();
    }

    return (UIForm) formCandidate;
}
 
Example #15
Source File: UICommandLink.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void decode(FacesContext context) {
    Map paramMap = context.getExternalContext().getRequestParameterMap();

    UIForm parentForm = JsfRenderUtils.findForm(this);
    String parentFormClientId = parentForm.getClientId(context);
    String eventSenderHiddenFieldId = JsfRenderUtils.getEventSenderHiddenFieldId(parentFormClientId);

    String eventSenderId = (String) paramMap.get(eventSenderHiddenFieldId);

    if (eventSenderId != null && eventSenderId.equals(this.getClientId(context))) {
        this.queueEvent(new ActionEvent(this));
    }
}
 
Example #16
Source File: RendererUtil.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/** Return the form ID of the form containing the given component */
public static String getFormId(FacesContext context, UIComponent component)
{
    while (component != null && !(component instanceof UIForm))
    {
        component = component.getParent();
    }
    if (component instanceof UIForm)
        return ((UIForm) component).getId();
    return null;
}
 
Example #17
Source File: RendererUtil.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/** Return the form ID of the form containing the given component */
public static String getFormId(FacesContext context, UIComponent component)
{
    while (component != null && !(component instanceof UIForm))
    {
        component = component.getParent();
    }
    if (component != null)
        return ((UIForm) component).getId();
    return null;
}
 
Example #18
Source File: RendererUtil.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/** Return the form ID of the form containing the given component */
public static String getFormId(FacesContext context, UIComponent component)
{
    while (component != null && !(component instanceof UIForm))
    {
        component = component.getParent();
    }
    if (component != null)
        return ((UIForm) component).getId();
    return null;
}
 
Example #19
Source File: HelpSetDefaultActionComponent.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * get form
 * @param component
 * @return ui form
 */
private UIForm getForm(UIComponent component)
{
  while (component != null)
  {
    if (component instanceof UIForm)
    {
      break;
    }
    component = component.getParent();
  }
  return (UIForm) component;
}
 
Example #20
Source File: HelpSetDefaultActionComponent.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/** 
 * @see javax.faces.component.UIComponent#encodeEnd(javax.faces.context.FacesContext)
 */
public void encodeEnd(FacesContext context) throws IOException
{
  ResponseWriter writer = context.getResponseWriter();
  UIComponent actionComponent = super.getParent();
  String acionElement = actionComponent.getClientId(context);
  UIForm form = getForm(actionComponent);
  if (form != null)
  {

    writer.startElement("script", null);
    writer.writeAttribute("type", "text/javascript", null);

    String functionCode = "if (document.layers) \n"
        + "document.captureEvents(Event.KEYDOWN); \n"
        + "document.onkeydown =" + "function (evt) \n {"
        + " var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : "
        + "event.keyCode; \n"
        + "var eventTarget = evt ? evt.target : event.srcElement;  \n"
        + "var textField = eventTarget.type == 'text';  \n"
        + "if (keyCode == 13 && textField) { \n "
        + "document.getElementById('" + acionElement
        + "').click();return false; }  \n" + "else  return true; }";

    writer.write(functionCode);

    writer.endElement("script");
  }
}
 
Example #21
Source File: DefaultCommand.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
@Override
public void encodeEnd(FacesContext context) throws IOException {
	if (!isRendered()) {
		return;
	}
	Map<String, Object> attrs = getAttributes();

	final UIForm form = BsfUtils.getClosestForm(this);
	if (form == null) {
		throw new FacesException("The default command component must be inside a form", null);
	} else {
		String target = (String)attrs.get("target");
		if(BsfUtils.isStringValued(target)) {
			ResponseWriter rw = context.getResponseWriter();
			String formId = form.getClientId();
			String actionCommandId = ExpressionResolver.getComponentIDs(context, this, target);

			rw.startElement("script", this);

			rw.writeText("" + "$(function() { " + "    $('form#"
					+ BsfUtils.escapeJQuerySpecialCharsInSelector(formId) + " input').keypress(function (e) { "
					+ "    if ((e.which && e.which === 13) || (e.keyCode && e.keyCode === 13)) { "
					+ "        document.getElementsByName('" + actionCommandId + "')[0].click();return false; "
					+ "    } else { return true; "
					+ "    } " + "    }); " + "});", null);
			
			rw.writeText("" + "$(function() { " + "    $('form#"
					+ BsfUtils.escapeJQuerySpecialCharsInSelector(formId) + " textarea').keypress(function (e) { "
					+ "    if ((e.ctrlKey && e.which && e.which === 13) || (e.ctrlKey && e.keyCode && e.keyCode === 13)) { "
					+ "        document.getElementsByName('" + actionCommandId + "')[0].click();return false; "
					+ "    } else { return true; "
					+ "    } " + "    }); " + "});", null);
			
			rw.endElement("script");
		} else {
			throw new FacesException("The default command component needs a defined target ID", null);
		}
	}
}
 
Example #22
Source File: HelpSetDefaultActionComponent.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/** 
 * @see javax.faces.component.UIComponent#encodeEnd(javax.faces.context.FacesContext)
 */
public void encodeEnd(FacesContext context) throws IOException
{
  ResponseWriter writer = context.getResponseWriter();
  UIComponent actionComponent = super.getParent();
  String acionElement = actionComponent.getClientId(context);
  UIForm form = getForm(actionComponent);
  if (form != null)
  {

    writer.startElement("script", null);
    writer.writeAttribute("type", "text/javascript", null);

    String functionCode = "if (document.layers) \n"
        + "document.captureEvents(Event.KEYDOWN); \n"
        + "document.onkeydown =" + "function (evt) \n {"
        + " var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : "
        + "event.keyCode; \n"
        + "var eventTarget = evt ? evt.target : event.srcElement;  \n"
        + "var textField = eventTarget.type == 'text';  \n"
        + "if (keyCode == 13 && textField) { \n "
        + "document.getElementById('" + acionElement
        + "').click();return false; }  \n" + "else  return true; }";

    writer.write(functionCode);

    writer.endElement("script");
  }
}
 
Example #23
Source File: BsfUtils.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
/**
 * Get the related form
 * @param component
 * @return
 */
public static UIForm getClosestForm(UIComponent component) {
	while (component != null) {
		if (component instanceof UIForm) {
			return (UIForm) component;
		}
		component = component.getParent();
	}
	return null;
}
 
Example #24
Source File: TreeRenderer.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
@Override
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
    if (!component.isRendered()) {
        return;
    }

    Tree tree = (Tree) component;
    ResponseWriter rw = context.getResponseWriter();
    String clientId = tree.getClientId();

    // check is inside form (MyFaces requires to be)
    final UIForm form = BsfUtils.getClosestForm(tree);
    if (form == null) {
        throw new FacesException("The tree component must be inside a form", null);
    }

    rw.startElement("div", tree);
    rw.writeAttribute("id", "tree_" + clientId, "id");
    renderPassThruAttributes(context, component, new String[] { "placeholder", "tabindex", "lang", "accesskey"}, true);
    String clazz = Responsive.getResponsiveStyleClass(tree, false);
    String styleClass = tree.getStyleClass();
    if (null != styleClass) {
        clazz = styleClass + clazz;
    }
    clazz = clazz.trim();
    if (clazz.length() > 0) {
        rw.writeAttribute("class", clazz, "class");
    }
    writeAttribute(rw, "style", tree.getStyle());
    rw.endElement("div");
}
 
Example #25
Source File: R.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the Form Id of a component inside a form.
 * 
 * @param fc
 *            FacesContext instance
 * @param c
 *            UIComponent instance
 * @return
 */
public static String findComponentFormId(FacesContext fc, UIComponent c) {
	UIComponent parent = c.getParent();

	while (parent != null) {
		if (parent instanceof UIForm) {
			return parent.getClientId(fc);
		}
		parent = parent.getParent();
	}
	return null;
}
 
Example #26
Source File: RendererUtil.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/** Return the form ID of the form containing the given component */
public static String getFormId(FacesContext context, UIComponent component)
{
    while (component != null && !(component instanceof UIForm))
    {
        component = component.getParent();
    }
    if (component instanceof UIForm)
        return ((UIForm) component).getId();
    return null;
}
 
Example #27
Source File: RendererUtil.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/** Return the form ID of the form containing the given component */
public static String getFormId(FacesContext context, UIComponent component)
{
    while (component != null && !(component instanceof UIForm))
    {
        component = component.getParent();
    }
    if (component != null)
        return ((UIForm) component).getId();
    return null;
}
 
Example #28
Source File: RadiobuttonRenderer.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
@Override
public void decode(FacesContext context, UIComponent component) {
	Radiobutton radioButton = (Radiobutton) component;

	if (radioButton.isDisabled() || radioButton.isReadonly()) {
		return;
	}

	decodeBehaviors(context, radioButton);

	String clientId = radioButton.getClientId(context);
	String name = radioButton.getName();
	if (null == name) {
		name = "input_" + clientId;
	}

	UIForm form = findSurroundingForm(component);
	if (null == form) {
		throw new FacesException("Radiobuttons must be enclosed in a form. Client-ID of the radio button: " + clientId);
	}
	
	// AJAX fires decode to all radio buttons. Change value only for the first element
	List<UIComponent> radioButtonGroup = findComponentsByName(form, radioButton.getName());
	if (radioButtonGroup.get(0) == component) {
		List<String> legalValues = collectLegalValues(context, radioButtonGroup);
		super.decode(context, component, legalValues, "input_" + clientId);
	}
}
 
Example #29
Source File: Responsive.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
/**
 * Temporal and ugly hack to prevent responsive classes to be applied to inputs inside inline forms.
 *
 * This should be removed and the logic placed somewhere else.
 * 
 * @return whether the component should render responsive classes
 */
private static boolean shouldRenderResponsiveClasses(Object r) {
    // This method only checks inputs.
    if(r instanceof UIComponent && r instanceof IResponsiveLabel) {
        UIForm form = AJAXRenderer.getSurroundingForm((UIComponent) r, true);
        if(form instanceof Form) {
            if(((Form)form).isInline()) {
                // If the form is inline, no responsive classes should be applied
                return false; 
            }
        }
    }
    
    return true;
}
 
Example #30
Source File: CoreRenderer.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
public static boolean isHorizontalForm(UIComponent component) {
	UIForm c = getSurroundingForm(component, true);
	if (null != c && c instanceof Form) {
		return ((Form) c).isHorizontal();
	}
	return false;
}