Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label Page. Show all posts
Showing posts with label Page. Show all posts

Monday 17 April 2017

Show saved file (pdf/text/html/xml/image) content on page from database (BLOB) in ADF Application


Hello All

Previously I have posted about uploading and saving files in database blob column now this post is about showing saved file content on page using ADF Faces inline frame component

In this post I am extending same previous application, Now see how we can implement this
There are few more steps to go , See the additional steps to show file content on page-


Friday 20 May 2016

Navigate to another page on value change event of ADF input component

Recently I have seen a thread on OTN forum in that user want to navigate from one page to another on valueChangeListener of input component.

First Method-
This is a simple piece of code to perform navigation to specific page , Use it in ValueChangeListener directly

 FacesContext facesContext = FacesContext.getCurrentInstance();
 facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, "controlFlowName");

Second Method-
ValueChangeListener fires when value of input component changes but it is not meant for navigation, For navigation we use buttons and links with Action property
But for this requirement we have to be a bit tricky, We can queue button Action on value change listener of inputText to navigate to another page
Now first value change listener will be executed and then it'll queue button action to execute and as final outcome user will be able to navigate

Tuesday 7 July 2015

Create shortcut of page on a button click in Oracle ADF using JShortcut library (For windows)

This post is about a small trick to create shortcut file from ADF application
Actually this type of requirement doesn't come in picture when you are working in web application , for web application bookmarks replaces desktop shortcut
Still if you want to do this then you can follow approach mentioned in this post

Creating shortcut programmatically requires access of operating system but you need not to worry about that
There is a java library to do this for you - JShortcut
Download jar files from here..

1. Now first step is to create a fusion web application and add this jar to viewController project


2. Create a page (independent runnable like jspx or jsf not fragments) and drop a button in that
3. Create a managed bean to handle button's action
    Here we will make use of JShellLink class of this library

See what docs says about this -

Provide access to shortcuts (shell links) from Java. The native library (jshortcut.dll) is loaded when JShellLink is first loaded. By default, JShellLink first looks for the native library in the PATH, using System.loadLibrary. If the native library is not found in the PATH, JShellLink then looks through each directory in the CLASSPATH (as determined by the value of the system property java.class.path). If an entry in the CLASSPATH is a jar file, then JShellLink looks for the native library in the directory containing that jar file. The application can override this behavior and force JShellLink to look for the native library in a specific directory by setting the system property JSHORTCUT_HOME to point to that directory. This property must be set before the JShellLink class is loaded. This makes it possible to use this library from a self-extracting jar file. 



4. Get the url of current page
 How to get url of current page in ADF Application?
 (See button action listener code for this )
5. Check code written in managed bean-


import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import javax.servlet.http.HttpServletRequest;

import net.jimmc.jshortcut.JShellLink;

import oracle.adf.controller.ControllerContext;

    JShellLink link;
    String filePath;

    /**Button action listener to create shortcurt of current page on desktop
     * @param actionEvent
     */
    public void createSortcutonDpAction(ActionEvent actionEvent) {
        try {
            //Get url of current ViewPort
            String viewId = ControllerContext.getInstance().getCurrentViewPort().getViewId();
            String viewUrl = ControllerContext.getInstance().getGlobalViewActivityURL(viewId);

            //Get Server Name and Port
            FacesContext fctx = FacesContext.getCurrentInstance();
            HttpServletRequest hsrequest = (HttpServletRequest) fctx.getExternalContext().getRequest();

            String serverName = hsrequest.getServerName();
            int serverPort = hsrequest.getServerPort();

            String runnableUrl = "http://" + serverName + ":" + serverPort + viewUrl;

            //Create Object of shortcurt link
            link = new JShellLink();
            filePath = JShellLink.getDirectory("") + runnableUrl;
            //Set Where you want to create shortcut
            link.setFolder(JShellLink.getDirectory("desktop"));
            //Set shortcut file name
            link.setName("Programmatically Created Shortcut");
            //Use ico file to use as shortcut icon
            link.setIconLocation("C://Users//Admin//Desktop//Jdev_Logo.ico");
            link.setPath(filePath);
            link.save();

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

6.  All done :) , run and check application
Click on button and check on desktop



Same code can be used to create shortcut from pure java application
Cheers :) Happy Learning

Sunday 15 March 2015

Disable(Override) browser back button functionality in ADF Application using JavaScript

This is another JavaScript trick to disable browser back button functionality
In this example i am using two jspx pages - Page1 and Page2



Added two buttons in both pages to navigate between pages



Source of Page1-

<af:outputText value="Page 1" id="ot1" inlineStyle="font-size:medium;font-weight:bold;"/>
                <af:commandButton text="Go To Second Page" id="cb1" action="forward"/>


Source of Page2-


 <af:outputText value="Page 2" id="ot1" inlineStyle="font-size:medium;font-weight:bold;"/>
                <af:commandButton text="Go To First Page" id="cb1" action="backward"/>


Now see JavaScript function to disable back button functionality, In actual JavaScript doesn't disable back button but override it's default functionality
This function forces browser to navigate forward instead of going back so on click of back button first browser executes it's default back operation but due to this function again navigates to forward and show same page


function preventBackButton() {
                  window.history.forward();
              }

Add this javascript to Page 1 and invoke using af:clientListener tag on pageLoad


<af:clientListener method="preventBackButton" type="load"/>

Now run application and check
Go to second page and the press back button - it returns to page one for a fraction of second but again navigates to Page 2
It means it is working , Cheers :) Happy Learning

Thursday 22 January 2015

Show uploaded file (pdf/text/html/xml/image) content on page -Oracle ADF

Hello all
In previous post
Uploading and downloading files from absolute server path in Oracle ADF (12.1.3)
we saw that how can we upload any file to absolute server path and download same from there.
Uploading part is done using simple java code to write file to specified folder and downloading part is done using <af:fileDownloadActionListener>

Now in this post i am going to show that how can we display a file on page directly without downloading it.
So for this i am using same sample application that i have used in previous post , you can download it from here



See the additional steps to show file content on page-
  • Create a servlet to parse file into output stream , this output stream will be used to show file content further.
    To create servlet right click on viewController project select New--->From Gallery-->Web Tier-->Servlet




  • See servlet source code to parse file into outputStream

  • import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class PreviewFileServlet extends HttpServlet {
        private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
    
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
        }
    
        /**
         * @param request
         * @param response
         * @throws ServletException
         * @throws IOException
         */
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String path = (request.getParameter("path"));
    
            OutputStream os = response.getOutputStream();
            //If path is null
            if (path.equalsIgnoreCase("No")) {
                path = "D:\\Document\\ItemImage\\Item.jpg";
            }
            if (request.getParameter("path") == "") {
                path = "D:\\Document\\ItemImage\\Item.jpg";
            }
            InputStream inputStream = null;
    
            try {
                File outputFile = new File(path);
                inputStream = new FileInputStream(outputFile);
                BufferedInputStream in = new BufferedInputStream(inputStream);
                int b;
                byte[] buffer = new byte[10240];
                while ((b = in.read(buffer, 0, 10240)) != -1) {
                    os.write(buffer, 0, b);
                }
    
            } catch (Exception e) {
    
                System.out.println(e);
            } finally {
                if (os != null) {
                    os.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
    
            }
        }
    }
    

  • Now open page in editor and drop an af:inlineFrame from component palette . inlineFrame is used to create a frame to show any external page or document just like as HTML iframe , it is loaded from source attribute
    So here i am using servlet as source attribute for this inlineFrame

  • <af:inlineFrame id="if2"
                                                        source="/previewfileservlet?path=#{bindings.Path.inputValue == null ? 'No' : bindings.Path.inputValue}"
                                                        inlineStyle="height:350px;width:800px;" sizing="preferred"/>
    

    here path of document is added to pageDef bindings that is passed to servelt as parameter

  • Now run this application and check it

    View Text File-
          View PDF File-


Thanks, Happy Learning :)

Post Related to file handling in Oracle ADF- 

Show saved file (pdf/text/html/xml/image) content on page from database (BLOB) in ADF Application
Uploading and showing image file from absolute server path -Orace ADF
Exporting viewObject data in text file using af:fileDownloadActionListener in Oracle ADF (12.1.3)
Creating pdf file using Apache PDFBox API in ADF Faces and opening it in new window -Oracle ADF
Download file from url using Oracle ADF & Java- Download Manager
Reading html source of a webpage (url) using Oracle ADF (af:richTextEditor) & Java

Wednesday 24 December 2014

Update page component in between of event processing in ADF Faces using JavaScript

This post is not about any specific topic of framework, recently i was working in an application, requirement was like this
There is a button on page that uploads a large file to server and it takes some time . So as soon as user press this button it's text should be changed to 'Processing..' and after upload is complete it should be 'Done'




In this post i am sharing same - How to update page component while an event is processing ?
First i have tried it using java means in same button action listener but in this case button text was changed only after event processing is complete
So for this purpose i have used some javascript function, this is quite simple (see the implementation)

  • Drop a button on page and create a ActionListener , it will change button text to 'Done' after event processing (used Thread.sleep() for some long processing)

  •     private RichButton buttonBind;
    
        /**Method to process Action Event
         * @param actionEvent
         */
        public void processValueAction(ActionEvent actionEvent) {
            // Sleep for some time (Event Processing time)
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //After processing set button text to 'Done'
            buttonBind.setText("Done");
            AdfFacesContext.getCurrentInstance().addPartialTarget(buttonBind);
        }
    
        public void setButtonBind(RichButton buttonBind) {
            this.buttonBind = buttonBind;
        }
    
        public RichButton getButtonBind() {
            return buttonBind;
        }
    

  • Now question is how to change button text to 'Processing..' immediately after clicking button. So for this purpose i am using javascript

  • function changeButtonText(event) {
                      var comp = event.getSource();
                      comp.setText('Processing..');
                  }
    

  • Calling this javascript function using af:clientListener in button like this (See XML source of page)

  •  <af:document title="UpdatePageComponent.jspx" id="d1">
                <af:resource type="javascript">
                  function changeButtonText(event) {
                      var comp = event.getSource();
                      comp.setText('Processing..');
                  }
                </af:resource>
                <af:form id="f1">
                    <af:spacer width="10" height="10" id="s1"/>
                    <af:panelGroupLayout id="pgl1" layout="horizontal" halign="center">
                        <af:panelBox id="pb1" showDisclosure="false">
                            <f:facet name="toolbar"/>
                            <af:button text="Click to process" id="b1"
                                       actionListener="#{viewScope.UpdatePageComponent.processValueAction}"
                                       clientComponent="true" binding="#{viewScope.UpdatePageComponent.buttonBind}"
                                       inlineStyle="width:250px;text-align:center;padding:5px;font-weight:bold;">
                                <af:clientListener method="changeButtonText" type="action"/>
                            </af:button>
                        </af:panelBox>
                    </af:panelGroupLayout>
                </af:form>
            </af:document>
    

  • As soon as button is clicked this javascript function will be invoked and set new text in button and after event processing button text is again set by managed bean actionListener.


  • After Clicking button (Event is processing)-

    Processing complete-

Thanks, Happy Learning :)

Monday 25 August 2014

Using HashMap in EL expression to set component properties in Oracle ADF

Basic requirement while using EL on pages in ADF, we use bean variables, binding attributes, iterators in expression to check conditions , to set properties for components as Enable-Disable, Visible, inline style etc.
in the same way we can use collections (Array, HashMap) in expression, i know it is pretty simple but have seen many thread in OTN asking how to use HashMap in expression

So here i am taking a very simple example



I have 5 input text on page and i have to enable-disable some fields based on it's id value, there is two buttons on page


as you can see on click of first button , input field 1,3 and 5 should be disabled and on click of second button af:inputText 2 and 4 should be disabled
so for this purpose i have used a HashMap in managed bean


    private HashMap fieldVal = new HashMap();

    public void setFieldVal(HashMap fieldVal) {
        this.fieldVal = fieldVal;
    }

    public HashMap getFieldVal() {
        return fieldVal;
    }

values in HashMap are populated using id value of inputTexts on page as key, this id will be passed as parameter in HashMap in expression
see how expression used in disabled property of inputText-


in same way expression for all inputText is set


<af:panelFormLayout id="pfl1" partialTriggers="b1 b2">
                    <af:inputText label="Label 1" id="it1" disabled="#{EnableDisableBean.fieldVal['it1']}"/>
                    <af:inputText label="Label 2" id="it2"
                                  disabled="#{requestScope.EnableDisableBean.fieldVal['it2']}"/>
                    <af:inputText label="Label 3" id="it3"
                                  disabled="#{requestScope.EnableDisableBean.fieldVal['it3']}"/>
                    <af:inputText label="Label 4" id="it4"
                                  disabled="#{requestScope.EnableDisableBean.fieldVal['it4']}"/>
                    <af:inputText label="Label 5" id="it5" autoSubmit="true"
                                  disabled="#{requestScope.EnableDisableBean.fieldVal['it5']}"/>
                </af:panelFormLayout>

now see code on buttons, simply setting values against key


    /**Method to disable inputText (1,3,5) on basis of id value
     * @param actionEvent
     */
    public void disable135Action(ActionEvent actionEvent) {
        fieldVal.put("it1", true);
        fieldVal.put("it2", false);
        fieldVal.put("it3", true);
        fieldVal.put("it4", false);
        fieldVal.put("it5", true);

    }

    /**Method to disable inputText (2,4) on basis of id value
     * @param actionEvent
     */
    public void disable24Action(ActionEvent actionEvent) {
        fieldVal.put("it1", false);
        fieldVal.put("it2", true);
        fieldVal.put("it3", false);
        fieldVal.put("it4", true);
        fieldVal.put("it5", false);
    }

Run Application and see what is on page
On Click of First Button

On Click of Second Button

Thanks
Happy learning :)

Saturday 8 March 2014

Launching browser print dialog using simple javascript function in ADF

Hello All ,
This posts talks about a requirement of printing a simple page (not much component as, form,tree,etc & not much data)
you can use a simple one liner javascript function to invoke browser's print dialog.

  • there is a page with Departments table on it, and a button to print this page

  • Called this simple javascript function on button click to open print dialog

  • window.print();
    

  • to execute javascript through managed bean use this method 



  •     /**Method to execute Javascript
         * @param javascriptCode
         */
        public static void runJavaScriptCode(String javascriptCode) {
            FacesContext facesCtx = FacesContext.getCurrentInstance();
            ExtendedRenderKitService service = Service.getRenderKitService(facesCtx, ExtendedRenderKitService.class);
            service.addScript(facesCtx, javascriptCode);
        }
    

  • click on print button- In Google Chrome

In Mozilla-




after printing, the page look like this
Sample ADF Application- Download
Cheers :-)

Friday 6 December 2013

Show current Date and Time on Page in Oracle ADF (refresh Date/time Programmatically)

Hello All,
in this tutorial i am going to explain that how to show current date and time on your ADF application page
Follow Steps-
  • Create a fusion web application and a page in it (i have used .jspx page)
  • Now drag an output text from component palette and drop it on page

  • Now select the output text and go to its property inspector then select value from Expression Builder as shown in image
  • Now in Expression Builder ,create a Managed Bean of type java.util.Date and assign its value to output text




  •  Now run your page and see current date is there
  • Now to format Date and Time , drag and drop af:convertDateTime  under output text from component palette



  •  Select convertDateTime and go to property inspector and change its pattern and run your page


  •  Now you have done basic configuration for Date/Time, if you want to refresh time (second and minute part) on page periodically then drop a poll component in page and create a poll listener in managed bean
  • Now write this simple code in your managed bean to invoke poll listener

  •     /**Binding of Output text*/
        private RichOutputText dateBind;
    
        public void setDateBind(RichOutputText dateBind) {
            this.dateBind = dateBind;
        }
    
        public RichOutputText getDateBind() {
            return dateBind;
        }
    
    
        /**Poll Listener Method ,handles Poll Event
         * @param pollEvent
         */
        public void refreshTime(PollEvent pollEvent) {
            AdfFacesContext.getCurrentInstance().addPartialTarget(dateBind);
    
        }
    

    And don't forget to set clientComponent property of outputText to true
  • Now Run your application , see in this video clip how your page get refreshed... Cheers ..!

Cheers :) Happy Learning

Monday 22 October 2012

Refresh Page in Oracle ADF by Java Code, Set partial trigger programmatically

Some times we need to refresh whole page , then we can use this managed bean code to refresh whole page in ADF.


    
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

//Method to reload page
    protected void refreshPage() {

        FacesContext fctx = FacesContext.getCurrentInstance();
        String page = fctx.getViewRoot().getViewId();
        ViewHandler ViewH = fctx.getApplication().getViewHandler();
        UIViewRoot UIV = ViewH.createView(fctx, page);
        UIV.setViewId(page);
        fctx.setViewRoot(UIV);

    }

Partially refresh any UIComponent, Set partial trigger programmatically-




import oracle.adf.view.rich.context.AdfFacesContext;

AdfFacesContext.getCurrentInstance().addPartialTarget(UIComponentBinding);