001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.ajax.markup.html.navigation.paging;
018
019import org.apache.wicket.Component;
020import org.apache.wicket.ajax.AjaxEventBehavior;
021import org.apache.wicket.ajax.AjaxRequestTarget;
022import org.apache.wicket.ajax.markup.html.IAjaxLink;
023import org.apache.wicket.markup.ComponentTag;
024import org.apache.wicket.markup.html.navigation.paging.IPageable;
025
026/**
027 * Ajax behavior for the paging navigation links. This behavior can only have one parent: the link
028 * it is attached to.
029 * 
030 * @since 1.2
031 * 
032 * @author Martijn Dashorst
033 */
034public class AjaxPagingNavigationBehavior extends AjaxEventBehavior
035{
036        /** For serialization. */
037        private static final long serialVersionUID = 1L;
038
039        /**
040         * The ajaxian link that should receive the event.
041         */
042        private final IAjaxLink owner;
043
044        /**
045         * Attaches the navigation behavior to the owner link and drives the pageable component. The
046         * behavior is attached to the markup event.
047         * 
048         * @param owner
049         *            the owner ajax link
050         * @param pageable
051         *            the pageable to update
052         * @param event
053         *            the javascript event to bind to (e.g. onclick)
054         */
055        public AjaxPagingNavigationBehavior(IAjaxLink owner, IPageable pageable, String event)
056        {
057                super(event);
058                this.owner = owner;
059        }
060
061        /**
062         * The ajax event handler. This will execute the event, and update the following components,
063         * when present: the navigator the owner link is part of, or when the link is a stand alone
064         * component, the link itself. Also the pageable's parent markup container is updated, so its
065         * contents can be replaced with the newly generated pageable.
066         * 
067         * @see org.apache.wicket.ajax.AjaxEventBehavior#onEvent(org.apache.wicket.ajax.AjaxRequestTarget)
068         */
069        @Override
070        protected void onEvent(AjaxRequestTarget target)
071        {
072                // handle the event
073                owner.onClick(target);
074
075                // find the PagingNavigator parent of this link
076                AjaxPagingNavigator navigator = ((Component)owner).findParent(AjaxPagingNavigator.class);
077
078                // if this is embedded inside a navigator
079                if (navigator != null)
080                {
081                        // tell the PagingNavigator to update the IPageable
082                        navigator.onAjaxEvent(target);
083                }
084        }
085
086        /**
087         * @see org.apache.wicket.ajax.AjaxEventBehavior#onComponentTag(org.apache.wicket.markup.ComponentTag)
088         */
089        @Override
090        protected void onComponentTag(ComponentTag tag)
091        {
092                if (getComponent().isEnabledInHierarchy())
093                {
094                        super.onComponentTag(tag);
095                }
096        }
097}