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.markup.html.link; 018 019import org.apache.wicket.Page; 020import org.apache.wicket.request.mapper.parameter.PageParameters; 021import org.apache.wicket.core.util.lang.WicketObjects; 022 023/** 024 * Renders a stable link which can be cached in a web browser and used at a later time. 025 * 026 * @author Jonathan Locke 027 * @param <T> 028 * type of model object, if any 029 */ 030public class BookmarkablePageLink<T> extends Link<T> 031{ 032 private static final long serialVersionUID = 1L; 033 034 /** The page class that this link links to. */ 035 private final String pageClassName; 036 037 /** The parameters to pass to the class constructor when instantiated. */ 038 protected PageParameters parameters; 039 040 /** 041 * Constructor. 042 * 043 * @param <C> 044 * type of page 045 * 046 * @param id 047 * The name of this component 048 * @param pageClass 049 * The class of page to link to 050 */ 051 public <C extends Page> BookmarkablePageLink(final String id, final Class<C> pageClass) 052 { 053 this(id, pageClass, null); 054 } 055 056 /** 057 * @return page parameters 058 */ 059 public PageParameters getPageParameters() 060 { 061 if (parameters == null) 062 { 063 parameters = new PageParameters(); 064 } 065 return parameters; 066 } 067 068 069 /** 070 * Constructor. 071 * 072 * @param <C> 073 * 074 * @param id 075 * See Component 076 * @param pageClass 077 * The class of page to link to 078 * @param parameters 079 * The parameters to pass to the new page when the link is clicked 080 */ 081 public <C extends Page> BookmarkablePageLink(final String id, final Class<C> pageClass, 082 final PageParameters parameters) 083 { 084 super(id); 085 086 this.parameters = parameters; 087 088 if (pageClass == null) 089 { 090 throw new IllegalArgumentException("Page class for bookmarkable link cannot be null"); 091 } 092 else if (!Page.class.isAssignableFrom(pageClass)) 093 { 094 throw new IllegalArgumentException("Page class must be derived from " + 095 Page.class.getName()); 096 } 097 pageClassName = pageClass.getName(); 098 } 099 100 /** 101 * Get the page class registered with the link 102 * 103 * @return Page class 104 */ 105 public final Class<? extends Page> getPageClass() 106 { 107 return WicketObjects.resolveClass(pageClassName); 108 } 109 110 /** 111 * Whether this link refers to the given page. 112 * 113 * @param page 114 * the page 115 * @see org.apache.wicket.markup.html.link.Link#linksTo(org.apache.wicket.Page) 116 */ 117 @Override 118 public boolean linksTo(final Page page) 119 { 120 return page.getClass() == getPageClass(); 121 } 122 123 @Override 124 protected boolean getStatelessHint() 125 { 126 return true; 127 } 128 129 /** 130 * THIS METHOD IS NOT USED! Bookmarkable links do not have a click handler. It is here to 131 * satisfy the interface only, as bookmarkable links will be dispatched by the handling servlet. 132 * 133 * @see org.apache.wicket.markup.html.link.Link#onClick() 134 */ 135 @Override 136 public final void onClick() 137 { 138 // Bookmarkable links do not have a click handler. 139 // Instead they are dispatched by the request handling servlet. 140 } 141 142 /** 143 * Gets the url to use for this link. 144 * 145 * @return The URL that this link links to 146 * @see org.apache.wicket.markup.html.link.Link#getURL() 147 */ 148 @Override 149 protected CharSequence getURL() 150 { 151 PageParameters parameters = getPageParameters(); 152 153 return urlFor(getPageClass(), parameters); 154 } 155}