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.ajax.AjaxRequestTarget;
020import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
021import org.apache.wicket.ajax.markup.html.IAjaxLink;
022import org.apache.wicket.markup.ComponentTag;
023import org.apache.wicket.markup.html.navigation.paging.IPageable;
024import org.apache.wicket.markup.html.navigation.paging.PagingNavigationIncrementLink;
025
026/**
027 * An incremental Ajaxian link to a page of a PageableListView. Assuming your list view navigation
028 * looks like
029 * 
030 * <pre>
031 * 
032 *                       [first / &lt;&lt; / &lt;] 1 | 2 | 3 [&gt; / &gt;&gt; /last]
033 * 
034 * </pre>
035 * 
036 * <p>
037 * and "&lt;" meaning the previous and "&lt;&lt;" goto the "current page - 5", than it is this kind
038 * of incremental page links which can easily be created.
039 * 
040 * This link will update the pageable and itself or the navigator the link is part of using Ajax
041 * techniques, or perform a full refresh when ajax is not available.
042 * 
043 * @since 1.2
044 * 
045 * @author Martijn Dashorst
046 */
047public class AjaxPagingNavigationIncrementLink extends PagingNavigationIncrementLink<Void>
048        implements
049                IAjaxLink
050{
051        private static final long serialVersionUID = 1L;
052
053        /**
054         * Constructor.
055         * 
056         * @param id
057         *            See Component
058         * @param pageable
059         *            The pageable component the page links are referring to
060         * @param increment
061         *            increment by
062         */
063        public AjaxPagingNavigationIncrementLink(final String id, final IPageable pageable,
064                final int increment)
065        {
066                super(id, pageable, increment);
067
068                setOutputMarkupId(true);
069        }
070
071        @Override
072        protected void onInitialize()
073        {
074                super.onInitialize();
075                add(newAjaxPagingNavigationBehavior(pageable, "click"));
076        }
077
078        /**
079         * @param pageable
080         *            The pageable component the page links are referring to
081         * @param event
082         *            the name of the default event on which this link will listen to
083         * @return the ajax behavior which will be executed when the user clicks the link
084         */
085        protected AjaxPagingNavigationBehavior newAjaxPagingNavigationBehavior(IPageable pageable,
086                String event)
087        {
088                return new AjaxPagingNavigationBehavior(this, pageable, event)
089                {
090                        @Override
091                        protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
092                        {
093                                super.updateAjaxAttributes(attributes);
094                                attributes.setPreventDefault(true);
095                                AjaxPagingNavigationIncrementLink.this.updateAjaxAttributes(attributes);
096                        }
097                };
098        }
099
100        protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
101        {
102        }
103
104        /**
105         * Fallback event listener, will redisplay the current page.
106         * 
107         * @see org.apache.wicket.markup.html.link.Link#onClick()
108         */
109        @Override
110        public void onClick()
111        {
112                onClick(null);
113        }
114
115        /**
116         * Performs the actual action of this component, performing a non-ajax fallback when there was
117         * no AjaxRequestTarget available.
118         * 
119         * @param target
120         *            the request target, when <code>null</code>, a full page refresh will be generated
121         */
122        @Override
123        public void onClick(AjaxRequestTarget target)
124        {
125                // Tell the PageableListView which page to print next
126                pageable.setCurrentPage(getPageNumber());
127        }
128
129        @Override
130        protected void onComponentTag(ComponentTag tag)
131        {
132                super.onComponentTag(tag);
133
134                // 'onclick' attribute would be set only if this component is attached
135                // to HTML element different than 'a'. This 'onclick' will break Ajax's
136                // event binding so here we remove it.
137                // AjaxFallback is supported only with 'a' HTML element. See WICKET-4862
138                tag.remove("onclick");
139        }
140}