net.rrm.ehour.ui.common.component.KeepAliveTextArea.java Source code

Java tutorial

Introduction

Here is the source code for net.rrm.ehour.ui.common.component.KeepAliveTextArea.java

Source

/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package net.rrm.ehour.ui.common.component;

import org.apache.wicket.ajax.AbstractAjaxTimerBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.time.Duration;

/**
 * KeepAlive text area that pings back to the server every 10 minutes to keep the session alive.
 * http://chillenious.wordpress.com/2007/06/19/how-to-create-a-text-area-with-a-heart-beat-with-wicket/
 */

public class KeepAliveTextArea extends TextArea<String> {
    private static final long serialVersionUID = 744361204848769680L;

    public KeepAliveTextArea(String id) {
        super(id);
        add(new KeepAliveBehavior());
    }

    public KeepAliveTextArea(String id, IModel<String> model) {
        super(id, model);
        add(new KeepAliveBehavior());
    }

    @SuppressWarnings("serial")
    private static class KeepAliveBehavior extends AbstractAjaxTimerBehavior {
        public KeepAliveBehavior() {
            super(Duration.minutes(10));
        }

        @Override
        protected void onTimer(AjaxRequestTarget target) {
            // prevent wicket changing focus
            target.focusComponent(null);
        }
    }
}