001: /*
002: * $Id: BookmarkablePageLink.java 462394 2006-09-28 16:22:10Z ehillenius $
003: * $Revision: 462394 $ $Date: 2006-09-28 18:22:10 +0200 (Thu, 28 Sep 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.markup.html.link;
019:
020: import wicket.AttributeModifier;
021: import wicket.Page;
022: import wicket.PageMap;
023: import wicket.PageParameters;
024: import wicket.model.Model;
025:
026: /**
027: * Renders a stable link which can be cached in a web browser and used at a
028: * later time.
029: *
030: * @author Jonathan Locke
031: */
032: public class BookmarkablePageLink extends Link {
033: private static final long serialVersionUID = 1L;
034:
035: /** The page class that this link links to. */
036: private final Class pageClass;
037:
038: /** Any page map for this link */
039: private String pageMapName = null;
040:
041: /** The parameters to pass to the class constructor when instantiated. */
042: private final PageParameters parameters;
043:
044: /**
045: * Constructor.
046: *
047: * @param id
048: * The name of this component
049: * @param pageClass
050: * The class of page to link to
051: */
052: public BookmarkablePageLink(final String id, final Class pageClass) {
053: this (id, pageClass, new PageParameters());
054: }
055:
056: /**
057: * Constructor.
058: *
059: * @param id
060: * See Component
061: * @param pageClass
062: * The class of page to link to
063: * @param parameters
064: * The parameters to pass to the new page when the link is
065: * clicked
066: */
067: public BookmarkablePageLink(final String id, final Class pageClass,
068: final PageParameters parameters) {
069: super (id);
070: if (pageClass == null) {
071: throw new IllegalArgumentException(
072: "Page class for bookmarkable link cannot be null");
073: } else if (!Page.class.isAssignableFrom(pageClass)) {
074: throw new IllegalArgumentException(
075: "Page class must be derived from "
076: + Page.class.getName());
077: }
078: this .pageClass = pageClass;
079: this .parameters = parameters;
080: }
081:
082: /**
083: * Get tge page class registered with the link
084: *
085: * @return Page class
086: */
087: public final Class getPageClass() {
088: return this .pageClass;
089: }
090:
091: /**
092: * @return Page map for this link
093: */
094: public final PageMap getPageMap() {
095: if (pageMapName != null) {
096: return PageMap.forName(pageMapName);
097: } else {
098: return getPage().getPageMap();
099: }
100: }
101:
102: /**
103: * Whether this link refers to the given page.
104: *
105: * @param page
106: * the page
107: * @see wicket.markup.html.link.Link#linksTo(wicket.Page)
108: */
109: public boolean linksTo(final Page page) {
110: return page.getClass() == pageClass;
111: }
112:
113: /**
114: * THIS METHOD IS NOT USED! Bookmarkable links do not have a click handler.
115: * It is here to satisfy the interface only, as bookmarkable links will be
116: * dispatched by the handling servlet.
117: *
118: * @see wicket.markup.html.link.Link#onClick()
119: */
120: public final void onClick() {
121: // Bookmarkable links do not have a click handler.
122: // Instead they are dispatched by the request handling servlet.
123: }
124:
125: /**
126: * @param pageMap
127: * The pagemap for this link's destination
128: * @return This
129: */
130: public final BookmarkablePageLink setPageMap(final PageMap pageMap) {
131: this .pageMapName = pageMap.getName();
132: if (pageMap != null) {
133: add(new AttributeModifier("target", false, new Model(
134: pageMapName)));
135: }
136: return this ;
137: }
138:
139: /**
140: * Adds a given page property value to this link.
141: *
142: * @param property
143: * The property
144: * @param value
145: * The value
146: * @return This
147: */
148: public BookmarkablePageLink setParameter(final String property,
149: final int value) {
150: parameters.put(property, Integer.toString(value));
151: return this ;
152: }
153:
154: /**
155: * Adds a given page property value to this link.
156: *
157: * @param property
158: * The property
159: * @param value
160: * The value
161: * @return This
162: */
163: public BookmarkablePageLink setParameter(final String property,
164: final long value) {
165: parameters.put(property, Long.toString(value));
166: return this ;
167: }
168:
169: /**
170: * Adds a given page property value to this link.
171: *
172: * @param property
173: * The property
174: * @param value
175: * The value
176: * @return This
177: */
178: public BookmarkablePageLink setParameter(final String property,
179: final String value) {
180: parameters.put(property, value);
181: return this ;
182: }
183:
184: /**
185: * Gets the url to use for this link.
186: *
187: * @return The URL that this link links to
188: * @see wicket.markup.html.link.Link#getURL()
189: */
190: protected CharSequence getURL() {
191: if (pageMapName != null && getPopupSettings() != null) {
192: throw new IllegalStateException(
193: "You cannot specify popup settings and a page map");
194: }
195:
196: if (getPopupSettings() != null) {
197: return urlFor(getPopupSettings().getPageMap(this),
198: pageClass, parameters);
199: } else {
200: return urlFor(getPageMap(), pageClass, parameters);
201: }
202: }
203: }
|