How to implement a link in Wicket Framework which opens a desired pdf document in popup window

Here I am going to discuss how to implement a link in wicket which opens a desired pdf document in separate popup widow.

To implent this link we will go through following steps:-

1. First create Wicket “Resource”(This is a class in Wicket) object and override the “getResourceStream()” method to return a “FileResourceStream” object containing the path of desired pdf document.

2. Create “PopupSettings” (This is a class in Wicket) object by passing some parameters in the constructor to set the width & height of popup window or you can also set some other properties like your popup window should be resizable or not and scrollable or not etc..

3. Create a “ResourceLink” component by passing the above Resource object in the constructor of “ResourceLink” class.

4. Now set the popup settings for your “ResourcrLink” by calling method “setPopupSettings(PopupSettings popupSettings)”.

5. Finally add this Resource link to any Wicket Container like Form,Panel etc..

Now I am going to explain the above steps by a simple example code. In this code I have a panel named “ExamplePanel” which contains our “ResourceLink” component. By clicking this link it opens a pdf named “abcd.pdf” in a popup window.

Code

“ExamplePanel.java”

public class ExamplePanel extends Panel {

   /*

   * constructor here

   */

   public ExamplePanel(String id,Date fromDate,Date toDate) {

      super(id);

      /*

       * created Resource for a pdf document named "abcd.pdf"

       */

      Resource pdfResource = new WebResource() {

      @overrides

      public IResourceStream getResourceStream() {

         /*

          * created File object named "pdfFile" by passing the full path of the pdf document           * in the constructor. ( Here my "abcd.pdf" is in C drive.)

          */

         File pdfFile = new File("C:\\abcd.pdf");

         // created FileResourceStream object by passing the above File object name             "pdfFile".

         IResourceStream stream = new FileResourceStream(pdfFile);

         //finally returns the stream

         return stream;

      }

   };

   /*

    * Created PopupSettings object named "popupSettings" by passing some parameters in the     * constructor and also setted the width and height for popup window.

    */

   PopupSettings popupSettings = new PopupSettings(PopupSettings.RESIZABLE |    PopupSettings.SCROLLBARS).setHeight(500).setWidth(700);

   /*

    * Created ResourceLink object named "resourceLink" by passing above Resource object          * named "pdfResource" as second parameters.The first parameter is "wicket:id" by which      * markup identifies the component and renders it on web page.

    */

   ResourceLink resourceLink = (ResourceLink) new ResourceLink("resourceLink",pdfResource);

   //Setted the popupSettings properties of "resourceLink".

   resourceLink.setPopupSettings(popupSettings);

   //added resourceLink to panel finally

   add(resourceLink);

}

}

Now its time to write matkup for above java file:-

One important thing to remember that “wicket:id” should be same for “ResourceLink” component in java file and html file.

“ExamplePanel.html”


   Click here to open the pdf

Now this is done. In this way you can open any document not only pdf. I used this approach to open pdf in my project so I took the example for pdf here. Hope will be helpful who is new in Wicket or will get a chance to play around this Wicket framework in future.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!