Showing posts with label panel. Show all posts
Showing posts with label panel. Show all posts

Tuesday, April 23, 2013

Google Analytics Panel: Wicket

Generates JavaScript for Google analytics tracking code using wicket panel:

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

 
public class GoogleAnalyticsPanel extends Panel {


    private IModel<String> accountNameModel;

    public GoogleAnalyticsPanel(String id) {

      super(id);

      accountNameModel=new Model<String>("your account number");

      Label label=new Label("accountName", accountNameModel);

      label.setRenderBodyOnly(true);

      label.setEnabled(true);

       add(label);

    }

    @Override

    protected void onDetach() {

        super.onDetach();

        if (accountNameModel != null) {

            accountNameModel.detach();

        }

    }
 
    /**

     * @param accountName

     */

    public void setAccountName(String accountName) {

        this.accountNameModel = new Model<String>(accountName);

    }

    /**

     * @return

     */

    public IModel<String> getAccountNameModel() {

        return accountNameModel;

    }


    /**

     * @param accountNameModel

     */

    public void setAccountNameModel(IModel<String> accountNameModel) {

        this.accountNameModel = accountNameModel;

    }


}

GoogleAnalyticsPanel.html

 

<?xml version="1.0" encoding="UTF-8"?>

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">

<body>

<wicket:panel>

       var _gaq = _gaq || [];

        _gaq.push(['_setAccount', '<wicket:container wicket:id="accountName"></wicket:container>']);

        _gaq.push(['_trackPageview']);

 

        (function() {

          var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;

          ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

        })();

</wicket:panel>

</body>

</html>

In webpage add the Google analytics panel :

                GoogleAnalyticsPanel googleAnalyticsPanel=new GoogleAnalyticsPanel("googleAnalyticsScript");

add(googleAnalyticsPanel);

In Html page add the panel in java script section :

<script type="text/javascript" wicket:id="googleAnalyticsScript">

</script>

Wednesday, April 3, 2013

Handling Session Timeout in Wicket

Define the timeout period in web.xml :
 
<session-config>

<session-timeout>60</session-timeout>

</session-config>



Create a class which extend the AjaxselfUpdatingTimerBehavior and implements IAjaxIndicatorAware :


import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;

import org.apache.wicket.ajax.IAjaxIndicatorAware;

import org.apache.wicket.util.time.Duration;

 

public class TimerBehavior extends AjaxSelfUpdatingTimerBehavior implements IAjaxIndicatorAware{

 

      public TimerBehavior(Duration updateInterval) {

            super(updateInterval);

      }


      private static final long serialVersionUID = 1L;

 

      @Override

      public String getAjaxIndicatorMarkupId() {

            return null;

      }


}
Create a panel which ill show the confirmation popup to decide whether to continue with current session or close :
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.panel.Panel;
 
public class SessionTimeOutPanel extends Panel{
      /**
       *
       */
      private static final long serialVersionUID = 1L;
     
      private boolean status;
     
      public boolean isStatus() {
            return status;
      }
 
      public void setStatus(boolean status) {
            this.status = status;
      }
     
      public SessionTimeOutPanel(String id,final ModalWindow modalWindow) {
            super(id);
            status = false;              
            AjaxLink okLink = new AjaxLink("OkButton") {
                 
                  @Override
                  public void onClick(AjaxRequestTarget target) {
                        setStatus(true);
                        modalWindow.close(target);
                  }
            };
            AjaxLink cancelLink = new AjaxLink("CancelButton") {
                 
                  @Override
                  public void onClick(AjaxRequestTarget target) {
                        setStatus(false);
                        modalWindow.close(target);
                  }
            };
            add(okLink);
           add(cancelLink);
      }
     
}
 HTML page for the above panel:
<wicket:panel>
      <span class="" style="width:99%;background: #4286c5 !important;">Session Time Out</span>
      <div class="">
            <label class="" style="width:100%">Your session is about to expire.Do you wish to continue?</label><br />
      </div><br/>
      <div id="" style="width:100%  padding:1.3em 0em;padding-top:32%
height:2.3em; ">
<a   class=""   href="#" tabindex=""
style="margin-right: 3%;margin-left:35%"  wicket:id="OkButton"> <span>Ok</span>
</a>
<a   class=""   href="#" tabindex=""
style="margin-right: 2%;" wicket:id="CancelButton"><span >Cancel</span>
</a>
      </div>
</wicket:panel>
Create a abstract web page ,all pages should implement this abstractpage:


import org.apache.wicket.Request;

import org.apache.wicket.RequestCycle;

import org.apache.wicket.ajax.AjaxRequestTarget;

import org.apache.wicket.ajax.IAjaxIndicatorAware;

import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;

import org.apache.wicket.markup.html.WebMarkupContainer;

import org.apache.wicket.markup.html.WebPage;

import org.apache.wicket.protocol.http.PageExpiredException;

import org.apache.wicket.protocol.http.WebRequest;

import org.apache.wicket.util.time.Duration;

 

public abstract class AbstractPage extends WebPage implements

IAjaxIndicatorAware {

 

     

      protected ModalWindow sessionTimeOutModalWindow;

     

      private transient HttpSession httpSession;

      private  int timeOut=0;

      private int timOutCount=0;

     

     

      public ModalWindow getSessionTimeOutModalWindow() {

            sessionTimeOutModalWindow = new ModalWindow("sessionTimeout");

        final SessionTimeOutPanel sessionTimeOutPanel = new SessionTimeOutPanel(

                  sessionTimeOutModalWindow.getContentId(),

                  sessionTimeOutModalWindow);

        sessionTimeOutPanel.setOutputMarkupId(true);

        sessionTimeOutModalWindow.setContent(sessionTimeOutPanel);

        sessionTimeOutModalWindow.setInitialHeight(135);

        sessionTimeOutModalWindow.setInitialWidth(400);

        sessionTimeOutModalWindow.setResizable(false);

       

        sessionTimeOutModalWindow.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {                        

            public void onClose(AjaxRequestTarget target) { 

          

          if (sessionTimeOutPanel.isStatus()) {

              if(httpSession!=null){

              httpSession.setMaxInactiveInterval(timeOut);

              timOutCount=timeOut;

              }else{

                   throw new PageExpiredException("PageExpiredException");

                    }

             

          }

          else

          {

            target.prependJavascript("window.opener='X';window.open('','_parent','');" +

                        "window.close();");

          }

            }});

                 

        Request request = RequestCycle.get().getRequest();

        if( request instanceof WebRequest )

        {

            WebRequest wr = (WebRequest)request;

            httpSession = wr.getHttpServletRequest().getSession();

            if( httpSession != null ) {

                timeOut=httpSession.getMaxInactiveInterval();

            }

        }

 

 

        return sessionTimeOutModalWindow;

    }

     

     

     

      /**

       * constructor

       */

      public AbstractPage() {

 

          add(getSessionTimeOutModalWindow());

          final WebMarkupContainer timer=new WebMarkupContainer("timer");

        timer.setOutputMarkupPlaceholderTag(true);

        timer.add(new TimerBehavior(Duration.ONE_SECOND){

 

                /**

                   *

                   */

                  private static final long serialVersionUID = 1L;

 

                        @Override

                protected void onPostProcessTarget(AjaxRequestTarget target) {

                    timOutCount--;

                 

                    if(timOutCount==60){

                      sessionTimeOutModalWindow.show(target);

                     // target.addComponent(sessionTimeOutModalWindow);

                    }

                }

         

        });

 

      add(timer);

 

      }

@Override

    public String getAjaxIndicatorMarkupId() {

            timOutCount=timeOut;

           

            return null;

    }

In Html Page add the below lines :

<div wicket:id="sessionTimeout"></div>


<div wicket:id="timer"></div>