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.IResourceListener;
020: import org.apache.wicket.Resource;
021: import org.apache.wicket.ResourceReference;
022: import org.apache.wicket.util.value.ValueMap;
023:
024: /**
025: * A link to any ResourceReference.
026: *
027: * @author Jonathan Locke
028: */
029: public class ResourceLink extends Link implements IResourceListener {
030: private static final long serialVersionUID = 1L;
031:
032: /** The Resource reference */
033: private final ResourceReference resourceReference;
034:
035: /** The Resource */
036: private final Resource resource;
037:
038: /** The resource parameters */
039: private final ValueMap resourceParameters;
040:
041: /**
042: * Constructs an ResourceLink from an resourcereference. That resource
043: * reference will bind its resource to the current SharedResources.
044: *
045: * @param id
046: * See Component
047: * @param resourceReference
048: * The shared resource to link to
049: */
050: public ResourceLink(final String id,
051: final ResourceReference resourceReference) {
052: this (id, resourceReference, null);
053: }
054:
055: /**
056: * Constructs an ResourceLink from an resourcereference. That resource
057: * reference will bind its resource to the current SharedResources.
058: *
059: * @param id
060: * See Component
061: * @param resourceReference
062: * The shared resource to link to
063: * @param resourceParameters
064: * The resource parameters
065: */
066: public ResourceLink(final String id,
067: final ResourceReference resourceReference,
068: ValueMap resourceParameters) {
069: super (id);
070: this .resourceReference = resourceReference;
071: this .resourceParameters = resourceParameters;
072: this .resource = null;
073: }
074:
075: /**
076: * Constructs a link directly to the provided resource.
077: *
078: * @param id
079: * See Component
080: * @param resource
081: * The resource
082: */
083: public ResourceLink(final String id, final Resource resource) {
084: super (id);
085: this .resource = resource;
086: this .resourceReference = null;
087: this .resourceParameters = null;
088: }
089:
090: /**
091: * @see org.apache.wicket.markup.html.link.Link#onClick()
092: */
093: public void onClick() {
094: }
095:
096: /**
097: * @see org.apache.wicket.IResourceListener#onResourceRequested()
098: */
099: public final void onResourceRequested() {
100: resource.onResourceRequested();
101: onClick();
102: }
103:
104: /**
105: * @see org.apache.wicket.markup.html.link.Link#getURL()
106: */
107: protected final CharSequence getURL() {
108: if (resourceReference != null) {
109: // TODO post 1.2: should we have support for locale changes when the
110: // resource reference (or resource??) is set manually..
111: // We should get a new resource reference for the current locale
112: // then
113: // that points to the same resource but with another locale if it
114: // exists.
115: // something like
116: // SharedResource.getResourceReferenceForLocale(resourceReference);
117:
118: resourceReference.bind(getApplication());
119: return getRequestCycle().urlFor(resourceReference,
120: resourceParameters);
121: }
122: return urlFor(IResourceListener.INTERFACE);
123: }
124: }
|