use of org.apache.wicket.request.resource.ResourceReference 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);
}
use of org.apache.wicket.request.resource.ResourceReference in project wicket by apache.
the class SharedResourceUrlTest method resourceReferenceUrl.
/**
* @throws Exception
*/
@Test
public void resourceReferenceUrl() throws Exception {
ResourceReference rr = new SharedResourceReference("test");
CharSequence url = tester.getRequestCycle().mapUrlFor(rr, null).toString();
assertEquals("wicket/resource/org.apache.wicket.Application/test", url);
rr = new PackageResourceReference(SharedResourceUrlTest.class, "test");
url = tester.getRequestCycle().mapUrlFor(rr, null).toString();
assertEquals("wicket/resource/org.apache.wicket.SharedResourceUrlTest/test", url);
}
use of org.apache.wicket.request.resource.ResourceReference in project wicket by apache.
the class SharedResourceUrlTest method resourceReferenceWithParamsUrl.
/**
* @throws Exception
*/
@Test
public void resourceReferenceWithParamsUrl() throws Exception {
ResourceReference rr = new SharedResourceReference("test");
CharSequence url = tester.getRequestCycle().mapUrlFor(rr, new PageParameters().set("param", "value")).toString();
assertEquals("wicket/resource/org.apache.wicket.Application/test?param=value", url);
rr = new PackageResourceReference(SharedResourceUrlTest.class, "test");
url = tester.getRequestCycle().mapUrlFor(rr, new PageParameters().set("param", "value")).toString();
assertEquals("wicket/resource/org.apache.wicket.SharedResourceUrlTest/test?param=value", url);
}
use of org.apache.wicket.request.resource.ResourceReference in project wicket by apache.
the class ResourceUtilTest method encodeResourceReferenceAttributesWithResource.
@Test
public void encodeResourceReferenceAttributesWithResource() throws Exception {
ResourceReference resourceReference = Mockito.mock(ResourceReference.class);
// test with all null attributes
UrlAttributes attributes = new UrlAttributes(null, null, null);
String urlString = "www.funny.url";
Url url = Url.parse(urlString);
Mockito.when(resourceReference.getUrlAttributes()).thenReturn(attributes);
ResourceUtil.encodeResourceReferenceAttributes(url, resourceReference);
assertEquals(urlString, url.toString());
Mockito.reset(resourceReference);
// test with locale, style and variation
attributes = new UrlAttributes(Locale.CANADA_FRENCH, "style", "variation");
Mockito.when(resourceReference.getUrlAttributes()).thenReturn(attributes);
ResourceUtil.encodeResourceReferenceAttributes(url, resourceReference);
assertEquals(urlString + "?fr_CA-style-variation", url.toString());
Mockito.reset(resourceReference);
// test with just variation
attributes = new UrlAttributes(null, null, "variation");
url = Url.parse(urlString);
Mockito.when(resourceReference.getUrlAttributes()).thenReturn(attributes);
ResourceUtil.encodeResourceReferenceAttributes(url, resourceReference);
assertEquals(urlString + "?--variation", url.toString());
}
use of org.apache.wicket.request.resource.ResourceReference in project wicket by apache.
the class ResourceAggregatorTest method assertItems.
private void assertItems(ResourceReference... references) {
List<HeaderItem> items = new ArrayList<HeaderItem>();
for (ResourceReference curReference : references) items.add(forReference(curReference));
assertItems(items);
}
Aggregations