2 Replies Latest reply on Jan 31, 2013 3:36 PM by jjoubert

    ADF showPrintablePageBehavior with action

    jjoubert
      Hi,

      I am successfully using the showPrintablePageBehavior ADF component inside a command button to render a printer-friendly version of an existing page. It is simple and works well:
      <af:commandButton text="Print Me" id="cb1">
          <af:showPrintablePageBehavior/>
      </af:commandButton>
      I understand from the documentation that adding this component to a commandButton for example supresses any server-side action event for the button:
      Note that the command component will not submit an action event to the server (the event is cancelled), though there will be a request to the server to get the printable page.
      I have a specific use case where I would like to invoke a server-side action in addition to rendering the printable page. Let's say for example I'd like to know when a user selected the print function (to audit, or update some server-side state to indicate that the page was in fact printed).
      I'm battling to implement this scenario unfortunately.
      I have tried:
      1. The normal "action" or "actionListener" feature of the commandButton.. true to the documentation, this doesn't work - and is not expected to work.

      2. Have another button on the page to perform the server-side function. When the user clicks on this button and the server-side action has been performed, I tried to simulate a button click of a second (hidden) button on the page which contains the actual showPrintablePageBehavior component. Basically structured like this:
      <af:commandButton text="Print Me" id="cb1" action="#{backingBean.myAction}">
      </af:commandButton>
      
      <af:commandButton text="Hidden print" id="cb2" visible="false">
          <af:showPrintablePageBehavior/>
      </af:commandButton>
      From the server-side action (backingBean.myAction), I tried two ways of simulating the click of the "Hidden print" button in order to render the printable version:
      1. Queue an ActionEvent from Java (hiddenPrintButton is "binding" to the bean successfully):
       ActionEvent ae = new ActionEvent(hiddenPrintButton);
       ae.queue();
      2. Inject Javascript to invoke the click of the second button:
      FacesContext fctx = FacesContext.getCurrentInstance();  
      StringBuilder script = new StringBuilder();  
      script.append(String.format("var hiddenPrintButton = AdfPage.PAGE.findComponentByAbsoluteId('%s');\n", hiddenPrintButton.getClientId()));
      //script.append("??");  Logic for simulating a "click"
      Service.getRenderKitService(fctx,ExtendedRenderKitService.class).addScript(fctx,script.toString()); 
      In the "Logic for simulating a click", I tried various options (ActionEvent, AdfCustomEvent), but I can't seem to successfully simulate a click.

      In the Java version, the defined "action" for the hidden print button gets executed if I define one, but only that - I actually need the printing behaviour to be executed.

      Am I on the right track here..? Maybe there's a much easier way to achieve what I want? I unfortunately don't have lots of ADF experience yet.

      Thanks in advance for any comment or advice.
        • 1. Re: ADF showPrintablePageBehavior with action
          Frank Nimphius-Oracle
          Hi,

          check this sample:

          http://www.oracle.com/technetwork/developer-tools/adf/learnmore/11-client-listener-169135.pdf

          What you need to do:

          1. set an af:clientListener to the command button that launches the printable page
          2. define a af:serverListener next to this command button and wire it up to a managed bean on the server
          3. Write JavaScript to call the server listener (thus the sample reference)
          4. Don't call "cancel" on the JS function event argument

          This will allow you to send a notification to the server when the event is invoked

          Frank
          • 2. Re: ADF showPrintablePageBehavior with action
            jjoubert
            Frank,

            Thank you so much for the pointers. I applied the concept in my case and it works like a charm.

            Just to summarize (for future reference), I have the following javascript which raises a custom event:
            function printInvoked(event) {
                component = event.getSource();
                AdfCustomEvent.queue(component, "printInvoked", {}, true); 
            }
            My print button looks like this. It therefore intercepts the click, call the javascript function which in turn raises a custom event. We then react to that event by calling the server-side method successfully and the printable page is also still shown as expected!
            <af:commandButton text="Print Me" id="cb1">
                <af:clientListener method="printInvoked" type="click"/>
                <af:serverListener type="printInvoked"
                           method="#{backingBean.myAction}"/>
                <af:showPrintablePageBehavior/>
            </af:commandButton>