Search in sources :

Example 36 with ResourceReference

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);
}
Also used : Locale(java.util.Locale) PackageResourceReference(org.apache.wicket.request.resource.PackageResourceReference) ResourceReference(org.apache.wicket.request.resource.ResourceReference) IResource(org.apache.wicket.request.resource.IResource)

Example 37 with ResourceReference

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);
}
Also used : SharedResourceReference(org.apache.wicket.request.resource.SharedResourceReference) PackageResourceReference(org.apache.wicket.request.resource.PackageResourceReference) PackageResourceReference(org.apache.wicket.request.resource.PackageResourceReference) SharedResourceReference(org.apache.wicket.request.resource.SharedResourceReference) ResourceReference(org.apache.wicket.request.resource.ResourceReference) Test(org.junit.Test)

Example 38 with ResourceReference

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);
}
Also used : SharedResourceReference(org.apache.wicket.request.resource.SharedResourceReference) PackageResourceReference(org.apache.wicket.request.resource.PackageResourceReference) PackageResourceReference(org.apache.wicket.request.resource.PackageResourceReference) SharedResourceReference(org.apache.wicket.request.resource.SharedResourceReference) ResourceReference(org.apache.wicket.request.resource.ResourceReference) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) Test(org.junit.Test)

Example 39 with ResourceReference

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());
}
Also used : UrlAttributes(org.apache.wicket.request.resource.ResourceReference.UrlAttributes) ResourceReference(org.apache.wicket.request.resource.ResourceReference) Url(org.apache.wicket.request.Url) Test(org.junit.Test)

Example 40 with ResourceReference

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);
}
Also used : ArrayList(java.util.ArrayList) HeaderItem(org.apache.wicket.markup.head.HeaderItem) PriorityHeaderItem(org.apache.wicket.markup.head.PriorityHeaderItem) ResourceReference(org.apache.wicket.request.resource.ResourceReference)

Aggregations

ResourceReference (org.apache.wicket.request.resource.ResourceReference)46 IResource (org.apache.wicket.request.resource.IResource)10 PackageResourceReference (org.apache.wicket.request.resource.PackageResourceReference)8 SharedResourceReference (org.apache.wicket.request.resource.SharedResourceReference)7 Test (org.junit.Test)7 ResourceReferenceRequestHandler (org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler)6 HeaderItem (org.apache.wicket.markup.head.HeaderItem)5 Url (org.apache.wicket.request.Url)5 JavaScriptResourceReference (org.apache.wicket.request.resource.JavaScriptResourceReference)5 JavaScriptLibrarySettings (org.apache.wicket.settings.JavaScriptLibrarySettings)5 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)4 Page (org.apache.wicket.Page)3 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)3 JavaScriptHeaderItem (org.apache.wicket.markup.head.JavaScriptHeaderItem)3 Image (org.apache.wicket.markup.html.image.Image)3 Properties (java.util.Properties)2 Application (org.apache.wicket.Application)2 StringResourceModel (org.apache.wicket.model.StringResourceModel)2 ByteArrayResource (org.apache.wicket.request.resource.ByteArrayResource)2 CssResourceReference (org.apache.wicket.request.resource.CssResourceReference)2