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: */
017: package org.apache.wicket.markup.html.link;
018:
019: import org.apache.wicket.AttributeModifier;
020: import org.apache.wicket.IPageMap;
021: import org.apache.wicket.Page;
022: import org.apache.wicket.PageMap;
023: import org.apache.wicket.PageParameters;
024: import org.apache.wicket.model.Model;
025: import org.apache.wicket.util.lang.Classes;
026:
027: /**
028: * Renders a stable link which can be cached in a web browser and used at a
029: * later time.
030: *
031: * @author Jonathan Locke
032: */
033: public class BookmarkablePageLink extends Link {
034: private static final long serialVersionUID = 1L;
035:
036: /** The page class that this link links to. */
037: private final String pageClassName;
038:
039: /** Any page map for this link */
040: private String pageMapName = null;
041:
042: /** The parameters to pass to the class constructor when instantiated. */
043: protected final PageParameters parameters;
044:
045: /**
046: * Constructor.
047: *
048: * @param id
049: * The name of this component
050: * @param pageClass
051: * The class of page to link to
052: */
053: public BookmarkablePageLink(final String id, final Class pageClass) {
054: this (id, pageClass, new PageParameters());
055: }
056:
057: /**
058: * Constructor.
059: *
060: * @param id
061: * See Component
062: * @param pageClass
063: * The class of page to link to
064: * @param parameters
065: * The parameters to pass to the new page when the link is
066: * clicked
067: */
068: public BookmarkablePageLink(final String id, final Class pageClass,
069: final PageParameters parameters) {
070: super (id);
071: if (pageClass == null) {
072: throw new IllegalArgumentException(
073: "Page class for bookmarkable link cannot be null");
074: } else if (!Page.class.isAssignableFrom(pageClass)) {
075: throw new IllegalArgumentException(
076: "Page class must be derived from "
077: + Page.class.getName());
078: }
079: this .pageClassName = pageClass.getName();
080: this .parameters = parameters;
081: }
082:
083: /**
084: * Get tge page class registered with the link
085: *
086: * @return Page class
087: */
088: public final Class getPageClass() {
089: return Classes.resolveClass(this .pageClassName);
090: }
091:
092: /**
093: * @return Page map for this link
094: */
095: public final IPageMap getPageMap() {
096: if (pageMapName != null) {
097: return PageMap.forName(pageMapName);
098: } else {
099: return getPage().getPageMap();
100: }
101: }
102:
103: /**
104: * Whether this link refers to the given page.
105: *
106: * @param page
107: * the page
108: * @see org.apache.wicket.markup.html.link.Link#linksTo(org.apache.wicket.Page)
109: */
110: public boolean linksTo(final Page page) {
111: return page.getClass() == getPageClass();
112: }
113:
114: protected boolean getStatelessHint() {
115: return true;
116: }
117:
118: /**
119: * THIS METHOD IS NOT USED! Bookmarkable links do not have a click handler.
120: * It is here to satisfy the interface only, as bookmarkable links will be
121: * dispatched by the handling servlet.
122: *
123: * @see org.apache.wicket.markup.html.link.Link#onClick()
124: */
125: public final void onClick() {
126: // Bookmarkable links do not have a click handler.
127: // Instead they are dispatched by the request handling servlet.
128: }
129:
130: /**
131: * @param pageMap
132: * The pagemap for this link's destination
133: * @return This
134: */
135: public final BookmarkablePageLink setPageMap(final IPageMap pageMap) {
136: if (pageMap != null) {
137: this .pageMapName = pageMap.getName();
138: add(new AttributeModifier("target", true, new Model(
139: pageMapName)));
140: }
141: return this ;
142: }
143:
144: /**
145: * Adds a given page property value to this link.
146: *
147: * @param property
148: * The property
149: * @param value
150: * The value
151: * @return This
152: */
153: public BookmarkablePageLink setParameter(final String property,
154: final int value) {
155: parameters.put(property, Integer.toString(value));
156: return this ;
157: }
158:
159: /**
160: * Adds a given page property value to this link.
161: *
162: * @param property
163: * The property
164: * @param value
165: * The value
166: * @return This
167: */
168: public BookmarkablePageLink setParameter(final String property,
169: final long value) {
170: parameters.put(property, Long.toString(value));
171: return this ;
172: }
173:
174: /**
175: * Adds a given page property value to this link.
176: *
177: * @param property
178: * The property
179: * @param value
180: * The value
181: * @return This
182: */
183: public BookmarkablePageLink setParameter(final String property,
184: final String value) {
185: parameters.put(property, value);
186: return this ;
187: }
188:
189: /**
190: * Gets the url to use for this link.
191: *
192: * @return The URL that this link links to
193: * @see org.apache.wicket.markup.html.link.Link#getURL()
194: */
195: protected CharSequence getURL() {
196: if (pageMapName != null && getPopupSettings() != null) {
197: throw new IllegalStateException(
198: "You cannot specify popup settings and a page map");
199: }
200:
201: if (getPopupSettings() != null) {
202: return urlFor(getPopupSettings().getPageMap(this),
203: getPageClass(), parameters);
204: } else {
205: return urlFor(getPageMap(), getPageClass(), parameters);
206: }
207: }
208: }
|