001: /*
002: * $Id: PageLink.java 457783 2005-10-02 10:06:33Z jcompagner $ $Revision:
003: * 1.13 $ $Date: 2005-10-02 12:06:33 +0200 (Sun, 02 Oct 2005) $
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.Page;
021:
022: /**
023: * Links to a given page via an object implementing the IPageLink delayed
024: * linking interface. PageLinks can be constructed directly with an IPageLink
025: * interface or with a Page Class object. In the latter case, an IPageLink
026: * implementation is provided which constructs a Page of the given class when
027: * the link is clicked. A default no-args constructor must be available in this
028: * case or a WicketRuntimeException will be thrown when Wicket fails to
029: * instantiate the class.
030: *
031: * @see IPageLink
032: * @author Jonathan Locke
033: */
034: public class PageLink extends Link {
035: private static final long serialVersionUID = 1L;
036:
037: /** The delayed linking Page source. */
038: private final IPageLink pageLink;
039:
040: /**
041: * Constructs a link that instantiates the given Page class when the link is
042: * clicked. The instantiated Page is used to render a response to the user.
043: *
044: * @param id
045: * See Component
046: * @param c
047: * Page class
048: */
049: public PageLink(final String id, final Class c) {
050: super (id);
051:
052: // Ensure that c is a subclass of Page
053: if (!Page.class.isAssignableFrom(c)) {
054: throw new IllegalArgumentException("Class " + c
055: + " is not a subclass of Page");
056: }
057:
058: this .pageLink = new IPageLink() {
059: private static final long serialVersionUID = 1L;
060:
061: public Page getPage() {
062: // Create page using page factory
063: return PageLink.this .getPage().getPageFactory()
064: .newPage(c);
065: }
066:
067: public Class getPageIdentity() {
068: return c;
069: }
070: };
071: }
072:
073: /**
074: * This constructor is ideal if a Page object was passed in from a previous
075: * Page. Construct a link to the Page.
076: *
077: * @param id See component
078: * @param page The page
079: */
080: public PageLink(final String id, final Page page) {
081: super (id);
082:
083: this .pageLink = new IPageLink() {
084: private static final long serialVersionUID = 1L;
085:
086: public Page getPage() {
087: // Create page using page factory
088: return page;
089: }
090:
091: public Class getPageIdentity() {
092: return page.getClass();
093: }
094: };
095: }
096:
097: /**
098: * This constructor is ideal for constructing pages lazily.
099: *
100: * Constructs a link which invokes the getPage() method of the IPageLink
101: * interface when the link is clicked. Whatever Page objects is returned by
102: * this method will be rendered back to the user.
103: *
104: * @param id
105: * See Component
106: * @param pageLink
107: * An implementation of IPageLink which will create the page
108: * linked to if and when this hyperlink is clicked at a later
109: * time.
110: */
111: public PageLink(final String id, final IPageLink pageLink) {
112: super (id);
113: this .pageLink = pageLink;
114: }
115:
116: /**
117: * Returns true if the given page is of the same class as the (delayed)
118: * destination of this page link.
119: *
120: * @see wicket.markup.html.link.Link#linksTo(wicket.Page)
121: */
122: public boolean linksTo(final Page page) {
123: return page.getClass() == pageLink.getPageIdentity();
124: }
125:
126: /**
127: * Handles a link click by asking for a concrete Page instance through the
128: * IPageLink.getPage() delayed linking interface. This call will normally
129: * cause the destination page to be created.
130: *
131: * @see wicket.markup.html.link.Link#onClick()
132: */
133: public void onClick() {
134: // Set page source's page as response page
135: setResponsePage(pageLink.getPage());
136: }
137: }
|