Saturday, October 30, 2010

ADF UI - PopupUtil class to show or hide ADF popup programmatically

After ADFUtil class, we need another util class PopupUtil that contains the methods to show/hide ADF popup. Here, I'm including the source of this java file so that it can be handy when needed.

package com.mpapana.common.util.bean; import javax.faces.context.FacesContext; import org.apache.myfaces.trinidad.render.ExtendedRenderKitService; import org.apache.myfaces.trinidad.util.Service; /** * Patterns Utility class for public use. */ public class PopupUtil { /** * Shows the specified popup component and its contents * @param popupId is the clientId of the popup to be shown * clientId is derived from backing bean for the af:popup using getClientId method */ public static void invokePopup(String popupId) { invokePopup(popupId, null, null); } /** * Shows the specified popup and uses the specified hints to align the popup. * @param popupId is the clientId of the popup to be shown - clientId is derived from backing bean for the af:popup using getClientId method * @param align is a hint for the popup display. Check AdfRichPopup js javadoc for valid values. Supported value includes: "AdfRichPopup.ALIGN_START_AFTER", "AdfRichPopup.ALIGN_BEFORE_START" and "AdfRichPopup.ALIGN_END_BEFORE" * @param alignId is the clientId of the component the popup should align to - clientId is derived from backing bean for the component using getClientId method * align and alignId need to be specified together - specifying null for either of them will have no effect. */ public static void invokePopup(String popupId, String align, String alignId) { if (popupId != null) { ExtendedRenderKitService service = Service.getRenderKitService(FacesContext.getCurrentInstance(), ExtendedRenderKitService.class); StringBuffer showPopup = new StringBuffer(); showPopup.append("var hints = new Object();"); //Add hints only if specified - see javadoc for AdfRichPopup js for details on valid values and behavior if (align != null && alignId != null) { showPopup.append("hints[AdfRichPopup.HINT_ALIGN] = " + align + ";"); showPopup.append("hints[AdfRichPopup.HINT_ALIGN_ID] ='" + alignId + "';"); } showPopup.append("var popupObj=AdfPage.PAGE.findComponent('" + popupId + "'); popupObj.show(hints);"); service.addScript(FacesContext.getCurrentInstance(), showPopup.toString()); } } /** * Hides the specified popup. * @param popupId is the clientId of the popup to be hidden * clientId is derived from backing bean for the af:popup using getClientId method */ public static void hidePopup(String popupId) { if (popupId != null) { ExtendedRenderKitService service = Service.getRenderKitService(FacesContext.getCurrentInstance(), ExtendedRenderKitService.class); String hidePopup = "var popupObj=AdfPage.PAGE.findComponent('" + popupId + "'); popupObj.hide();"; service.addScript(FacesContext.getCurrentInstance(), hidePopup); } } /* public static void hideDynamicGlobalPopup(ActionEvent actionEvent) { FacesContext fc = FacesContext.getCurrentInstance(); ExpressionFactory ef = fc.getApplication().getExpressionFactory(); String swapEmptyTaskFlow = "#{bindings.swapEmptyTaskFlow.execute}"; MethodExpression me = ef.createMethodExpression(fc.getELContext(), swapEmptyTaskFlow, String.class, new Class[]{ActionEvent.class}); me.invoke(fc.getELContext(), new Object[] {actionEvent}); if (actionEvent != null) { UIComponent source = (UIComponent)actionEvent.getSource(); UIComponent popup = PatternsUtil.getParentByType(source,RichPopup.class); String popupId = popup.getClientId(fc); PatternsPublicUtil.hidePopup(popupId); } }*/ }


Sample Usages:

i. Displaying the af:popup programmatically
import com.mpapana.bean.util.PopupUtil; public class ExampleBean { private RichPopup confirmationPopup; public void sampleMethod(ActionEvent actionEvent) { PopupUtil.invokePopup(confirmationPopup.getClientId(FacesContext.getCurrentInstance())); } }

ii. Hiding the af:popup programmatically
import com.mpapana.bean.util.PopupUtil; public class ExampleBean { private RichPopup confirmationPopup; public void sampleMethod(ActionEvent actionEvent) { PopupUtil.hidePopup(confirmationPopup.getClientId(FacesContext.getCurrentInstance())); } }


In the above code, the popup 'confirmationPopup' is a binding for af:popup in jsff page (popup code jsff page below).
<af:popup id="p1" binding="#{pageFlowScope.ExampleBean.confirmationPopup}"> <af:dialog id="d1" type="ok" title="Confirmation" inlineStyle="width:340px; height:125.0px;" dialogListener="#{pageFlowScope.ExampleBean.confirmationDialog}"> <af:panelGroupLayout id="pgl2"> <af:outputText value="#{bundle.THE_DETAILS_HAVE_BEEN_SAVED}" id="ot1"/> </af:panelGroupLayout> </af:dialog> </af:popup>


On calling invokePopup() or hidePopup() methods, this popup will be shown or hidden respectively.

3 comments:

  1. Thanks, it was helpful!

    ReplyDelete
  2. Fakhri,

    thanks for the sample, but how to controle the choice did by the user, OK or Cancel

    ReplyDelete
  3. PopUpUtil.showerrorMessage("hai"+"hello");

    where hello should be in the next line in JDEVELOPER

    ReplyDelete

Related Posts with Thumbnails