Repository: wicket Updated Branches: refs/heads/master 1c1bb4d69 -> 6d0eae5f4
WICKET-5801 Responsive Images Follow Wicket code style. Java 7 diamonds Fix the demo. Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/6d0eae5f Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/6d0eae5f Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/6d0eae5f Branch: refs/heads/master Commit: 6d0eae5f4f8fcfd9e56dc2bf6115feb970d32a82 Parents: 2948a2c Author: Martin Tzvetanov Grigorov <mgrigo...@apache.org> Authored: Wed Jan 28 18:52:08 2015 +0200 Committer: Martin Tzvetanov Grigorov <mgrigo...@apache.org> Committed: Wed Jan 28 18:53:33 2015 +0200 ---------------------------------------------------------------------- .../apache/wicket/markup/html/image/Image.java | 155 ++++++++++--------- .../wicket/markup/html/image/Picture.java | 1 - .../apache/wicket/markup/html/image/Source.java | 9 +- .../image/ImageResourceReferenceTestPage.java | 2 +- .../markup/html/image/ResponsiveImageTest.java | 29 ++-- .../org/apache/wicket/examples/images/Home.java | 4 +- 6 files changed, 98 insertions(+), 102 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/6d0eae5f/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Image.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Image.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Image.java index 644e491..6c650cb 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Image.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Image.java @@ -48,17 +48,37 @@ import org.apache.wicket.request.resource.ResourceReference; * * @author Jonathan Locke * @author Tobias Soloschenko - * */ public class Image extends WebComponent implements IResourceListener { private static final long serialVersionUID = 1L; + /** + * To be used for the crossOrigin attribute + * + * @see {@link #setCrossOrigin(Cors)} + */ + public enum Cors { + ANONYMOUS("anonymous"), + USE_CREDENTIALS("user-credentials"), + NO_CORS(""); + + private final String realName; + + private Cors(String realName) { + this.realName = realName; + } + + public String getRealName() { + return realName; + } + } + /** The image resource this image component references (src attribute) */ private final LocalizedImageResource localizedImageResource = new LocalizedImageResource(this); /** The image resources this image component references (srcset attribute) */ - private final List<LocalizedImageResource> localizedImageResources = new ArrayList<LocalizedImageResource>(); + private final List<LocalizedImageResource> localizedImageResources = new ArrayList<>(); /** The x values to be used within the srcset */ private List<String> xValues = null; @@ -69,7 +89,7 @@ public class Image extends WebComponent implements IResourceListener /** * Cross origin settings */ - private Cors crossorigin = null; + private Cors crossOrigin = null; /** * This constructor can be used if you override {@link #getImageResourceReference()} or @@ -128,8 +148,8 @@ public class Image extends WebComponent implements IResourceListener PageParameters resourceParameters, final ResourceReference... resourceReferences) { super(id); - this.setImageResourceReference(resourceParameters, resourceReference); - this.setImageResourceReferences(resourceParameters, resourceReferences); + setImageResourceReference(resourceParameters, resourceReference); + setImageResourceReferences(resourceParameters, resourceReferences); } /** @@ -150,8 +170,8 @@ public class Image extends WebComponent implements IResourceListener public Image(final String id, final IResource imageResource, final IResource... imageResources) { super(id); - this.setImageResource(imageResource); - this.setImageResources(imageResources); + setImageResource(imageResource); + setImageResources(imageResources); } /** @@ -171,7 +191,7 @@ public class Image extends WebComponent implements IResourceListener */ public Image(final String id, final String string) { - this(id, new Model<String>(string)); + this(id, new Model<>(string)); } /** @@ -181,7 +201,7 @@ public class Image extends WebComponent implements IResourceListener public void onResourceRequested() { localizedImageResource.onResourceRequested(null); - for (LocalizedImageResource localizedImageResource : this.localizedImageResources) + for (LocalizedImageResource localizedImageResource : localizedImageResources) { localizedImageResource.onResourceRequested(null); } @@ -195,7 +215,7 @@ public class Image extends WebComponent implements IResourceListener { if (imageResource != null) { - this.localizedImageResource.setResource(imageResource); + localizedImageResource.setResource(imageResource); } } @@ -206,12 +226,12 @@ public class Image extends WebComponent implements IResourceListener */ public void setImageResources(final IResource... imageResources) { - this.localizedImageResources.clear(); + localizedImageResources.clear(); for (IResource imageResource : imageResources) { LocalizedImageResource localizedImageResource = new LocalizedImageResource(this); localizedImageResource.setResource(imageResource); - this.localizedImageResources.add(localizedImageResource); + localizedImageResources.add(localizedImageResource); } } @@ -226,11 +246,11 @@ public class Image extends WebComponent implements IResourceListener { if (parameters != null) { - this.localizedImageResource.setResourceReference(resourceReference, parameters); + localizedImageResource.setResourceReference(resourceReference, parameters); } else { - this.localizedImageResource.setResourceReference(resourceReference); + localizedImageResource.setResourceReference(resourceReference); } } } @@ -244,7 +264,7 @@ public class Image extends WebComponent implements IResourceListener public void setImageResourceReferences(final PageParameters parameters, final ResourceReference... resourceReferences) { - this.localizedImageResources.clear(); + localizedImageResources.clear(); for (ResourceReference resourceReference : resourceReferences) { LocalizedImageResource localizedImageResource = new LocalizedImageResource(this); @@ -256,7 +276,7 @@ public class Image extends WebComponent implements IResourceListener { localizedImageResource.setResourceReference(resourceReference); } - this.localizedImageResources.add(localizedImageResource); + localizedImageResources.add(localizedImageResource); } } @@ -266,12 +286,12 @@ public class Image extends WebComponent implements IResourceListener */ public void setXValues(String... values) { - if (this.xValues == null) + if (xValues == null) { - xValues = new ArrayList<String>(); + xValues = new ArrayList<>(); } - this.xValues.clear(); - this.xValues.addAll(Arrays.asList(values)); + xValues.clear(); + xValues.addAll(Arrays.asList(values)); } /** @@ -282,7 +302,7 @@ public class Image extends WebComponent implements IResourceListener { if (this.sizes == null) { - this.sizes = new ArrayList<String>(); + this.sizes = new ArrayList<>(); } this.sizes.clear(); this.sizes.addAll(Arrays.asList(sizes)); @@ -296,7 +316,7 @@ public class Image extends WebComponent implements IResourceListener { // Null out the image resource, so we reload it (otherwise we'll be // stuck with the old model. - for (LocalizedImageResource localizedImageResource : this.localizedImageResources) + for (LocalizedImageResource localizedImageResource : localizedImageResources) { localizedImageResource.setResourceReference(null); localizedImageResource.setResource(null); @@ -341,23 +361,24 @@ public class Image extends WebComponent implements IResourceListener protected void onComponentTag(final ComponentTag tag) { super.onComponentTag(tag); - if (tag.getName().equals("source")) + + if ("source".equals(tag.getName())) { - this.buildSrcSetAttribute(tag); + buildSrcSetAttribute(tag); tag.remove("src"); } else { - this.checkComponentTag(tag, "img"); - String srcAttribute = this.buildSrcAttribute(tag); - this.buildSrcSetAttribute(tag); + checkComponentTag(tag, "img"); + String srcAttribute = buildSrcAttribute(tag); + buildSrcSetAttribute(tag); tag.put("src", srcAttribute); - } - this.buildSizesAttribute(tag); + buildSizesAttribute(tag); - if (this.crossorigin != null) { - tag.put("crossorigin", this.crossorigin.getRealName()); + Cors crossOrigin = getCrossOrigin(); + if (crossOrigin != null && Cors.NO_CORS != crossOrigin) { + tag.put("crossOrigin", crossOrigin.getRealName()); } } @@ -370,24 +391,24 @@ public class Image extends WebComponent implements IResourceListener protected void buildSrcSetAttribute(final ComponentTag tag) { int srcSetPosition = 0; - for (LocalizedImageResource localizedImageResource : this.localizedImageResources) + for (LocalizedImageResource localizedImageResource : localizedImageResources) { localizedImageResource.setSrcAttribute(tag); - if (this.shouldAddAntiCacheParameter()) + if (shouldAddAntiCacheParameter()) { - this.addAntiCacheParameter(tag); + addAntiCacheParameter(tag); } String srcset = tag.getAttribute("srcset"); String xValue = ""; // If there are xValues set process them in the applied order to the srcset attribute. - if (this.xValues != null) + if (xValues != null) { - xValue = this.xValues.size() > srcSetPosition && - this.xValues.get(srcSetPosition) != null ? " " + - this.xValues.get(srcSetPosition) : ""; + xValue = xValues.size() > srcSetPosition && + xValues.get(srcSetPosition) != null ? " " + + xValues.get(srcSetPosition) : ""; } tag.put("srcset", (srcset != null ? srcset + ", " : "") + tag.getAttribute("src") + xValue); @@ -404,21 +425,21 @@ public class Image extends WebComponent implements IResourceListener */ protected String buildSrcAttribute(final ComponentTag tag) { - final IResource resource = this.getImageResource(); + final IResource resource = getImageResource(); if (resource != null) { - this.localizedImageResource.setResource(resource); + localizedImageResource.setResource(resource); } - final ResourceReference resourceReference = this.getImageResourceReference(); + final ResourceReference resourceReference = getImageResourceReference(); if (resourceReference != null) { - this.localizedImageResource.setResourceReference(resourceReference); + localizedImageResource.setResourceReference(resourceReference); } - this.localizedImageResource.setSrcAttribute(tag); + localizedImageResource.setSrcAttribute(tag); - if (this.shouldAddAntiCacheParameter()) + if (shouldAddAntiCacheParameter()) { - this.addAntiCacheParameter(tag); + addAntiCacheParameter(tag); } return tag.getAttribute("src"); } @@ -432,7 +453,7 @@ public class Image extends WebComponent implements IResourceListener protected void buildSizesAttribute(final ComponentTag tag) { // if no sizes have been set then don't build the attribute - if (this.sizes == null) + if (sizes == null) { return; } @@ -463,7 +484,7 @@ public class Image extends WebComponent implements IResourceListener */ protected boolean shouldAddAntiCacheParameter() { - return this.getRequestCycle().find(AjaxRequestTarget.class) != null; + return getRequestCycle().find(AjaxRequestTarget.class) != null; } /** @@ -489,7 +510,7 @@ public class Image extends WebComponent implements IResourceListener boolean stateless = (getImageResource() == null || getImageResource() == localizedImageResource.getResource()) && localizedImageResource.isStateless(); boolean statelessList = false; - for (LocalizedImageResource localizedImageResource : this.localizedImageResources) + for (LocalizedImageResource localizedImageResource : localizedImageResources) { if (localizedImageResource.isStateless()) { @@ -512,7 +533,7 @@ public class Image extends WebComponent implements IResourceListener { boolean isResource = method != null && IResourceListener.class.isAssignableFrom(method.getDeclaringClass()); - if (isResource && this.isVisibleInHierarchy()) + if (isResource && isVisibleInHierarchy()) { // when the image data is requested we do not care if this component // is enabled in @@ -528,50 +549,30 @@ public class Image extends WebComponent implements IResourceListener /** * Gets the cross origin settings * - * @see {@link #setCrossorigin(Cors)} + * @see {@link #setCrossOrigin(Cors)} * * @return the cross origins settings */ - public Cors getCrossorigin() { - return this.crossorigin; + public Cors getCrossOrigin() { + return crossOrigin; } /** * Sets the cross origin settings<br> * <br> * - * <b>anonymous</b>: Cross-origin CORS requests for the element will not have the credentials flag set.<br> + * <b>ANONYMOUS</b>: Cross-origin CORS requests for the element will not have the credentials flag set.<br> * <br> - * <b>use_credentials</b>: Cross-origin CORS requests for the element will have the credentials flag set.<br> + * <b>USE_CREDENTIALS</b>: Cross-origin CORS requests for the element will have the credentials flag set.<br> * <br> * <b>no_cores</b>: The empty string is also a valid keyword, and maps to the Anonymous state. The attribute's invalid value default is the * Anonymous state. The missing value default, used when the attribute is omitted, is the No CORS state * - * @param crossorigin + * @param crossOrigin * the cross origins settings to set */ - public void setCrossorigin(Cors crossorigin) { - this.crossorigin = crossorigin; + public void setCrossOrigin(Cors crossOrigin) { + this.crossOrigin = crossOrigin; } - /** - * To be used for the crossorigin attribute - * - * @see {@link #setCrossorigin(Cors)} - */ - public enum Cors { - anonymous("anonymous"), - use_credentials("user-credentials"), - no_cors(""); - - private String realName; - - private Cors(String realName) { - this.realName = realName; - } - - public String getRealName() { - return this.realName; - } - } } http://git-wip-us.apache.org/repos/asf/wicket/blob/6d0eae5f/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Picture.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Picture.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Picture.java index 5cf5fb5..a126840 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Picture.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Picture.java @@ -28,7 +28,6 @@ import org.apache.wicket.model.IModel; */ public class Picture extends WebMarkupContainer { - private static final long serialVersionUID = 1L; /** http://git-wip-us.apache.org/repos/asf/wicket/blob/6d0eae5f/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Source.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Source.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Source.java index b38f5fc..76ea0de 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Source.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/image/Source.java @@ -35,7 +35,6 @@ import org.apache.wicket.request.resource.ResourceReference; */ public class Source extends Image { - private static final long serialVersionUID = 1L; private String media = null; @@ -94,7 +93,7 @@ public class Source extends Image { checkComponentTag(tag, "source"); super.onComponentTag(tag); - if (this.media != null) + if (getMedia() != null) { tag.put("media", getMedia()); } @@ -125,7 +124,7 @@ public class Source extends Image * Unsupported for source tag */ @Override - public void setCrossorigin(Cors crossorigin) { + public void setCrossOrigin(Cors crossorigin) { throw new UnsupportedOperationException("It is not allowed to set the crossorigin attribute for source tag"); } @@ -133,7 +132,7 @@ public class Source extends Image * Unsupported for source tag */ @Override - public Cors getCrossorigin() { - throw new UnsupportedOperationException("It is not allowed to get the crossorigin attribute for source tag"); + public final Cors getCrossOrigin() { + return null; } } http://git-wip-us.apache.org/repos/asf/wicket/blob/6d0eae5f/wicket-core/src/test/java/org/apache/wicket/markup/html/image/ImageResourceReferenceTestPage.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/html/image/ImageResourceReferenceTestPage.java b/wicket-core/src/test/java/org/apache/wicket/markup/html/image/ImageResourceReferenceTestPage.java index 0bca071..4181155 100644 --- a/wicket-core/src/test/java/org/apache/wicket/markup/html/image/ImageResourceReferenceTestPage.java +++ b/wicket-core/src/test/java/org/apache/wicket/markup/html/image/ImageResourceReferenceTestPage.java @@ -39,7 +39,7 @@ public class ImageResourceReferenceTestPage extends WebPage return new PackageResourceReference(this.getClass(), "small.jpg"); } }; - image1.setCrossorigin(Cors.anonymous); + image1.setCrossOrigin(Cors.ANONYMOUS); this.add(image1); } } http://git-wip-us.apache.org/repos/asf/wicket/blob/6d0eae5f/wicket-core/src/test/java/org/apache/wicket/markup/html/image/ResponsiveImageTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/html/image/ResponsiveImageTest.java b/wicket-core/src/test/java/org/apache/wicket/markup/html/image/ResponsiveImageTest.java index 4c00d48..a7b0891 100644 --- a/wicket-core/src/test/java/org/apache/wicket/markup/html/image/ResponsiveImageTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/markup/html/image/ResponsiveImageTest.java @@ -16,38 +16,35 @@ */ package org.apache.wicket.markup.html.image; +import org.apache.wicket.WicketTestCase; +import org.apache.wicket.protocol.http.WebApplication; import org.apache.wicket.util.tester.TagTester; -import org.apache.wicket.util.tester.WicketTester; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; -public class ResponsiveImageTest +public class ResponsiveImageTest extends WicketTestCase { - - private WicketTester wicketTester; - - @Before - public void setup() + @Override + protected WebApplication newApplication() { - wicketTester = new WicketTester(new PubApplication()); + return new PubApplication(); } @Test public void testSrcSetIsNotAvailableOnDefaultUsage() { - wicketTester.startPage(ImageResourceReferenceTestPage.class); - String lastResponseAsString = wicketTester.getLastResponse().getDocument(); + tester.startPage(ImageResourceReferenceTestPage.class); + String lastResponseAsString = tester.getLastResponse().getDocument(); TagTester createTagByAttribute = TagTester.createTagByAttribute(lastResponseAsString, "img"); Assert.assertFalse(createTagByAttribute.hasAttribute("srcset")); - Assert.assertEquals("anonymous", createTagByAttribute.getAttribute("crossorigin")); + Assert.assertEquals(Image.Cors.ANONYMOUS.getRealName(), createTagByAttribute.getAttribute("crossorigin")); } @Test public void testPictureTagIsRenderedRight() { - wicketTester.startPage(ImagePictureTestPage.class); - String lastResponseAsString = wicketTester.getLastResponse().getDocument(); + tester.startPage(ImagePictureTestPage.class); + String lastResponseAsString = tester.getLastResponse().getDocument(); TagTester pictureTagTester = TagTester.createTagByAttribute(lastResponseAsString, "picture"); Assert.assertTrue(pictureTagTester.hasChildTag("img")); Assert.assertTrue(pictureTagTester.hasChildTag("source")); @@ -60,8 +57,8 @@ public class ResponsiveImageTest @Test public void testImageTagIsRenderedWithXValuesAndSrcSet() { - wicketTester.startPage(ImageSrcSetTestPage.class); - String lastResponseAsString = wicketTester.getLastResponse().getDocument(); + tester.startPage(ImageSrcSetTestPage.class); + String lastResponseAsString = tester.getLastResponse().getDocument(); TagTester imgTagTester = TagTester.createTagByAttribute(lastResponseAsString, "img"); Assert.assertTrue(imgTagTester.hasAttribute("src")); Assert.assertTrue(imgTagTester.hasAttribute("srcset")); http://git-wip-us.apache.org/repos/asf/wicket/blob/6d0eae5f/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.java b/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.java index 0744f6e..42a6a9e 100644 --- a/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.java +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/images/Home.java @@ -119,11 +119,11 @@ public final class Home extends WicketExamplePage picture.add(large); large.setOutputMarkupId(true); Source medium = new Source("sourcemedium", new PackageResourceReference(this.getClass(), - "image2_medium.gif")); + "Image2_medium.gif")); medium.setMedia("(min-width: 465px)"); picture.add(medium); Image image3 = new Image("image7", new PackageResourceReference(this.getClass(), - "image2_small.gif")); + "Image2_small.gif")); picture.add(image3); this.add(picture); }