use of org.apache.wicket.request.resource.IResource in project wicket by apache.
the class Image method setImageResources.
/**
* @param imageResources
* the new ImageResource to set.
*/
public void setImageResources(final IResource... imageResources) {
localizedImageResources.clear();
for (IResource imageResource : imageResources) {
LocalizedImageResource localizedImageResource = new LocalizedImageResource(this);
localizedImageResource.setResource(imageResource);
localizedImageResources.add(localizedImageResource);
}
}
use of org.apache.wicket.request.resource.IResource in project wicket by apache.
the class Image method buildSrcAttribute.
/**
* Builds the src attribute
*
* @param tag
* the component tag
* @return the value of the src attribute
*/
protected String buildSrcAttribute(final ComponentTag tag) {
final IResource resource = getImageResource();
if (resource != null) {
localizedImageResource.setResource(resource);
}
final ResourceReference resourceReference = getImageResourceReference();
if (resourceReference != null) {
localizedImageResource.setResourceReference(resourceReference);
}
localizedImageResource.setSrcAttribute(tag);
if (shouldAddAntiCacheParameter()) {
addAntiCacheParameter(tag);
}
return tag.getAttribute("src");
}
use of org.apache.wicket.request.resource.IResource in project wicket by apache.
the class BasicResourceReferenceMapper method mapHandler.
@Override
public Url mapHandler(IRequestHandler requestHandler) {
if (requestHandler instanceof ResourceReferenceRequestHandler) {
ResourceReferenceRequestHandler referenceRequestHandler = (ResourceReferenceRequestHandler) requestHandler;
ResourceReference reference = referenceRequestHandler.getResourceReference();
Url url;
while (reference instanceof ResourceBundleReference) {
// unwrap the bundle to render the url for the actual reference
reference = ((ResourceBundleReference) reference).getBundleReference();
}
if (reference instanceof MetaInfStaticResourceReference) {
url = ((MetaInfStaticResourceReference) reference).mapHandler(referenceRequestHandler);
// if running on Servlet 3.0 engine url is not null
if (url != null) {
return url;
}
// otherwise it has to be served by the standard wicket way
}
if (reference.canBeRegistered()) {
ResourceReferenceRegistry resourceReferenceRegistry = getContext().getResourceReferenceRegistry();
resourceReferenceRegistry.registerResourceReference(reference);
}
url = new Url();
List<String> segments = url.getSegments();
segments.add(getContext().getNamespace());
segments.add(getContext().getResourceIdentifier());
segments.add(getClassName(reference.getScope()));
// setup resource parameters
PageParameters parameters = new PageParameters(referenceRequestHandler.getPageParameters());
// need to remove indexed parameters otherwise the URL won't be able to decode
parameters.clearIndexed();
ResourceUtil.encodeResourceReferenceAttributes(url, reference);
StringTokenizer tokens = new StringTokenizer(reference.getName(), "/");
while (tokens.hasMoreTokens()) {
String token = tokens.nextToken();
// on the last component of the resource path
if (tokens.hasMoreTokens() == false && Strings.isEmpty(token) == false) {
final IResource resource = reference.getResource();
// is resource supposed to be cached?
if (resource instanceof IStaticCacheableResource) {
final IStaticCacheableResource cacheable = (IStaticCacheableResource) resource;
// is caching enabled?
if (cacheable.isCachingEnabled()) {
// apply caching scheme to resource url
final ResourceUrl resourceUrl = new ResourceUrl(token, parameters);
getCachingStrategy().decorateUrl(resourceUrl, cacheable);
token = resourceUrl.getFileName();
Checks.notEmpty(token, "Caching strategy returned empty name for '%s'", resource);
}
}
}
segments.add(token);
}
if (parameters.isEmpty() == false) {
url = encodePageParameters(url, parameters, pageParametersEncoder);
}
return url;
}
return null;
}
use of org.apache.wicket.request.resource.IResource in project wicket by apache.
the class LocalizedImageResource method newImage.
/**
* Generates an image resource based on the attribute values on tag
*
* @param value
* The value to parse
*/
private void newImage(final CharSequence value) {
// Parse value
final ImageValueParser valueParser = new ImageValueParser(value);
// Does value match parser?
if (valueParser.matches()) {
final String imageReferenceName = valueParser.getImageReferenceName();
final String specification = Strings.replaceHtmlEscapeNumber(valueParser.getSpecification());
final String factoryName = valueParser.getFactoryName();
final Application application = component.getApplication();
// Do we have a reference?
if (!Strings.isEmpty(imageReferenceName)) {
// Is resource already available via the application?
if (application.getResourceReferenceRegistry().getResourceReference(Application.class, imageReferenceName, locale, style, variation, true, false) == null) {
// Resource not available yet, so create it with factory and
// share via Application
final IResource imageResource = getResourceFactory(application, factoryName).newResource(specification, locale, style, variation);
ResourceReference ref = new SimpleStaticResourceReference(Application.class, imageReferenceName, locale, style, variation, imageResource);
application.getResourceReferenceRegistry().registerResourceReference(ref);
}
// Create resource reference
resourceReference = new PackageResourceReference(Application.class, imageReferenceName, locale, style, variation);
} else {
resource = getResourceFactory(application, factoryName).newResource(specification, locale, style, variation);
}
} else {
throw new WicketRuntimeException("Could not generate image for value attribute '" + value + "'. Was expecting a value attribute of the form \"[resourceFactoryName]:[resourceReferenceName]?:[factorySpecification]\".");
}
}
use of org.apache.wicket.request.resource.IResource in project wicket by apache.
the class LocalizedImageResource method setSrcAttribute.
/**
* @param tag
* The tag to inspect for an optional src attribute that might reference an image.
* @throws WicketRuntimeException
* Thrown if an image is required by the caller, but none can be found.
*/
public final void setSrcAttribute(final ComponentTag tag) {
// If locale has changed from the initial locale used to attach image
// resource, then we need to reload the resource in the new locale
Locale l = component.getLocale();
String s = component.getStyle();
String v = component.getVariation();
if (resourceKind == null && (!Objects.equal(locale, l) || !Objects.equal(style, s) || !Objects.equal(variation, v))) {
// Get new component locale and style
locale = l;
style = s;
variation = v;
// Invalidate current resource so it will be reloaded/recomputed
resourceReference = null;
resource = null;
} else {
// TODO post 1.2: should we have support for locale changes when the
// resource reference (or resource??) is set manually..
// We should get a new resource reference for the current locale
// then that points to the same resource but with another locale if
// it exists. Something like
// SharedResource.getResourceReferenceForLocale(resourceReference);
}
// check if the model contains a resource, if so, load the resource from
// the model.
Object modelObject = component.getDefaultModelObject();
if (modelObject instanceof ResourceReference) {
resourceReference = (ResourceReference) modelObject;
} else if (modelObject instanceof IResource) {
resource = (IResource) modelObject;
}
// Need to load image resource for this component?
if (resource == null && resourceReference == null) {
// Get SRC attribute of tag
final CharSequence src = tag.getAttribute("src");
if (src != null) {
// Try to load static image
loadStaticImage(src.toString());
} else {
// Get VALUE attribute of tag
final CharSequence value = tag.getAttribute("value");
if (value != null) {
// Try to generate an image using an image factory
newImage(value);
} else {
// Load static image using model object as the path
loadStaticImage(component.getDefaultModelObjectAsString());
}
}
}
// Get URL for resource
final CharSequence url;
if (resourceReference != null) {
// Create URL to resource
url = RequestCycle.get().urlFor(resourceReference, resourceParameters);
} else {
// Create URL to component
url = component.urlForListener(resourceParameters);
}
// Set the SRC attribute to point to the component or shared resource
tag.put("src", url);
}
Aggregations