List of usage examples for org.apache.wicket.request.resource ResourceReference.UrlAttributes getVariation
public String getVariation()
From source file:name.martingeisse.wicket.component.sprite.SpriteImage.java
License:Open Source License
@Override protected void onComponentTag(ComponentTag tag) { // check for allowed tags final String originalTagName = tag.getName(); if (!originalTagName.equalsIgnoreCase("img") && !originalTagName.equalsIgnoreCase("span") && !originalTagName.equalsIgnoreCase("div")) { String msg = String.format( "Component [%s] (path = [%s]) must be applied to a tag of type IMG, SPAN or DIV, not: %s", getId(), getPath(), tag.toUserDebugString()); findMarkupStream().throwMarkupException(msg); }/*from w w w .j a va 2 s . c o m*/ // make the image generate its resource reference (e.g. from a specified path), ignoring the tag name for now try { tag.setName("img"); super.onComponentTag(tag); } finally { tag.setName(originalTagName); } // localize the resource reference (including a fallback mechanism for nonexisting resource files) ResourceReference resourceReference = getImageResourceReference(); ResourceReference.UrlAttributes urlAttributes = resourceReference.getUrlAttributes(); ResourceReference.Key spriteKey = new ResourceReference.Key(resourceReference.getScope().getName(), resourceReference.getName(), urlAttributes.getLocale(), urlAttributes.getStyle(), urlAttributes.getVariation()); // look up the sprite and fall back to default behavior if not found ApplicationSpriteSupport applicationSpriteSupport = ApplicationSpriteSupport.get(getApplication()); if (applicationSpriteSupport == null) { super.onComponentTag(tag); return; } SpriteRegistry spriteRegistry = applicationSpriteSupport.getSpriteRegistry(); SpriteReference spriteReference = spriteRegistry.lookup(spriteKey); if (spriteReference == null) { super.onComponentTag(tag); return; } // IMG tags cannot handle CSS sprites. They're inline, so we convert to a SPAN. if (tag.getName().equalsIgnoreCase("img")) { tag.setName("span"); } // remove the SRC attribute -- it doesn't hurt, but it's ugly tag.getAttributes().remove("src"); // prepare modification of the style attribute StringBuilder styleBuilder; String previousStyle = tag.getAttribute("style"); if (previousStyle == null) { styleBuilder = new StringBuilder(); } else { styleBuilder = new StringBuilder(previousStyle).append("; "); } // SPAN tags must be styled as inline-block to support width/height properly if (tag.getName().equalsIgnoreCase("span")) { styleBuilder.append("display: inline-block; "); } // modify the style attribute according to the sprite reference styleBuilder.append("background-image: url(").append(spriteReference.getAtlas().getUrl()); styleBuilder.append("); background-position: -").append(spriteReference.getX()); styleBuilder.append("px -").append(spriteReference.getY()); styleBuilder.append("px; width: ").append(spriteReference.getWidth()); styleBuilder.append("px; height: ").append(spriteReference.getHeight()); styleBuilder.append("px; "); // store modified style tag.getAttributes().put("style", styleBuilder.toString()); }
From source file:name.martingeisse.wicketbits.sprite.SpriteImage.java
License:Open Source License
@Override protected void onComponentTag(ComponentTag tag) { // check for allowed tags final String originalTagName = tag.getName(); if (!originalTagName.equalsIgnoreCase("img") && !originalTagName.equalsIgnoreCase("span") && !originalTagName.equalsIgnoreCase("div")) { String msg = String.format( "Component [%s] (path = [%s]) must be applied to a tag of type IMG, SPAN or DIV, not: %s", getId(), getPath(), tag.toUserDebugString()); findMarkupStream().throwMarkupException(msg); }//from w ww. j a va 2 s. c o m // make the image generate its resource reference (e.g. from a specified path), ignoring the tag name for now try { tag.setName("img"); super.onComponentTag(tag); } finally { tag.setName(originalTagName); } // localize the resource reference (including a fallback mechanism for nonexisting resource files) ResourceReference resourceReference = getImageResourceReference(); ResourceReference.UrlAttributes urlAttributes = resourceReference.getUrlAttributes(); ResourceReference.Key spriteKey = new ResourceReference.Key(resourceReference.getScope().getName(), resourceReference.getName(), urlAttributes.getLocale(), urlAttributes.getStyle(), urlAttributes.getVariation()); // look up the sprite and fall back to default behavior if not found SpriteRegistry spriteRegistry = ApplicationSpriteSupport.get(getApplication()).getSpriteRegistry(); SpriteReference spriteReference = spriteRegistry.lookup(spriteKey); if (spriteReference == null) { super.onComponentTag(tag); return; } // IMG tags cannot handle CSS sprites. They're inline, so we convert to a SPAN. if (tag.getName().equalsIgnoreCase("img")) { tag.setName("span"); } // remove the SRC attribute -- it doesn't hurt, but it's ugly tag.getAttributes().remove("src"); // prepare modification of the style attribute StringBuilder styleBuilder; String previousStyle = tag.getAttribute("style"); if (previousStyle == null) { styleBuilder = new StringBuilder(); } else { styleBuilder = new StringBuilder(previousStyle).append("; "); } // SPAN tags must be styled as inline-block to support width/height properly if (tag.getName().equalsIgnoreCase("span")) { styleBuilder.append("display: inline-block; "); } // modify the style attribute according to the sprite reference styleBuilder.append("background-image: url(").append(spriteReference.getAtlas().getUrl()); styleBuilder.append("); background-position: -").append(spriteReference.getX()); styleBuilder.append("px -").append(spriteReference.getY()); styleBuilder.append("px; width: ").append(spriteReference.getWidth()); styleBuilder.append("px; height: ").append(spriteReference.getHeight()); styleBuilder.append("px; "); // store modified style tag.getAttributes().put("style", styleBuilder.toString()); }