Revision: 6327 Author: [email protected] Date: Thu Oct 8 12:27:08 2009 Log: Makes Image aware of ImageResource
Reviewed by: jlabanca http://gwt-code-reviews.appspot.com/78804 http://code.google.com/p/google-web-toolkit/source/detail?r=6327 Added: /trunk/user/test/com/google/gwt/user/client/ui/prettyPiccy.png Modified: /trunk/user/src/com/google/gwt/user/client/ui/Image.java /trunk/user/test/com/google/gwt/user/client/ui/ImageTest.java ======================================= --- /dev/null +++ /trunk/user/test/com/google/gwt/user/client/ui/prettyPiccy.png Thu Oct 8 12:27:08 2009 Binary file, no diff available. ======================================= --- /trunk/user/src/com/google/gwt/user/client/ui/Image.java Wed May 13 08:08:49 2009 +++ /trunk/user/src/com/google/gwt/user/client/ui/Image.java Thu Oct 8 12:27:08 2009 @@ -1,12 +1,12 @@ /* * Copyright 2008 Google Inc. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -42,6 +42,7 @@ import com.google.gwt.event.dom.client.MouseWheelEvent; import com.google.gwt.event.dom.client.MouseWheelHandler; import com.google.gwt.event.shared.HandlerRegistration; +import com.google.gwt.resources.client.ImageResource; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.ui.impl.ClippedImageImpl; @@ -56,16 +57,16 @@ * image is constructed, and how it is transformed after construction. Methods * will operate differently depending on the mode that the image is in. These * differences are detailed in the documentation for each method. - * + * * <p> * If an image transitions between clipped mode and unclipped mode, any * {...@link Element}-specific attributes added by the user (including style * attributes, style names, and style modifiers), except for event listeners, * will be lost. * </p> - * + * * <h3>CSS Style Rules</h3> <ul class="css"> <li>.gwt-Image { }</li> </ul> - * + * * Tranformations between clipped and unclipped state will result in a loss of * any style names that were set/added; the only style names that are preserved * are those that are mentioned in the static CSS style rules. Due to @@ -74,7 +75,7 @@ * expected when an image is in clipped mode. These limitations can usually be * easily worked around by encapsulating the image in a container widget that * can itself be styled. - * + * * <p> * <h3>Example</h3> * {...@example com.google.gwt.examples.ImageExample} @@ -304,7 +305,7 @@ /** * Causes the browser to pre-fetch the image at a given URL. - * + * * @param url the URL of the image to be prefetched */ public static void prefetch(String url) { @@ -315,11 +316,11 @@ /** * Creates a Image widget that wraps an existing <img> element. - * + * * This element must already be attached to the document. If the element is * removed from the document, you must call * {...@link RootPanel#detachNow(Widget)}. - * + * * @param element the element to be wrapped */ public static Image wrap(Element element) { @@ -345,11 +346,21 @@ changeState(new UnclippedState(this)); setStyleName("gwt-Image"); } + + /** + * Creates an image whose size and content are defined by an ImageResource. + * + * @param resource the ImageResource to be displayed + */ + public Image(ImageResource resource) { + this(resource.getURL(), resource.getLeft(), + resource.getTop(), resource.getWidth(), resource.getHeight()); + } /** * Creates an image with a specified URL. The load event will be fired once * the image at the given URL has been retrieved by the browser. - * + * * @param url the URL of the image to be displayed */ public Image(String url) { @@ -366,7 +377,7 @@ * the width and height are specified explicitly by the user, this behavior * will not cause problems with retrieving the width and height of a clipped * image in a load event handler. - * + * * @param url the URL of the image to be displayed * @param left the horizontal co-ordinate of the upper-left vertex of the * visibility rectangle @@ -383,7 +394,7 @@ /** * This constructor may be used by subclasses to explicitly use an existing * element. This element must be an <img> element. - * + * * @param element the element to be used */ protected Image(Element element) { @@ -466,7 +477,7 @@ * Gets the height of the image. When the image is in the unclipped state, the * height of the image is not known until the image has been loaded (i.e. load * event has been fired for the image). - * + * * @return the height of the image, or 0 if the height is unknown */ public int getHeight() { @@ -478,7 +489,7 @@ * visibility rectangle. If the image is in the unclipped state, then the * visibility rectangle is assumed to be the rectangle which encompasses the * entire image, which has an upper-left vertex of (0,0). - * + * * @return the horizontal co-ordinate of the upper-left vertex of the image's * visibility rectangle */ @@ -491,7 +502,7 @@ * visibility rectangle. If the image is in the unclipped state, then the * visibility rectangle is assumed to be the rectangle which encompasses the * entire image, which has an upper-left vertex of (0,0). - * + * * @return the vertical co-ordinate of the upper-left vertex of the image's * visibility rectangle */ @@ -503,7 +514,7 @@ * Gets the URL of the image. The URL that is returned is not necessarily the * URL that was passed in by the user. It may have been transformed to an * absolute URL. - * + * * @return the image URL */ public String getUrl() { @@ -514,7 +525,7 @@ * Gets the width of the image. When the image is in the unclipped state, the * width of the image is not known until the image has been loaded (i.e. load * event has been fired for the image). - * + * * @return the width of the image, or 0 if the width is unknown */ public int getWidth() { @@ -522,7 +533,7 @@ } /** - * @deprecated Use the {...@link HandlerRegistration#removeHandler} method on + * @deprecated Use the {...@link HandlerRegistration#removeHandler} method on * the object returned by {...@link #addClickHandler} instead */ @Deprecated @@ -557,13 +568,28 @@ public void removeMouseWheelListener(MouseWheelListener listener) { ListenerWrapper.WrappedMouseWheelListener.remove(this, listener); } + + /** + * Sets the url and the visibility rectangle for the image at the same time, + * based on an ImageResource instance. A single load event will be fired if + * either the incoming url or visiblity rectangle co-ordinates differ from the + * image's current url or current visibility rectangle co-ordinates. If the + * image is currently in the unclipped state, a call to this method will cause + * a transition to the clipped state. + * + * @param resource the ImageResource to display + */ + public void setResource(ImageResource resource) { + setUrlAndVisibleRect(resource.getURL(), resource.getLeft(), + resource.getTop(), resource.getWidth(), resource.getHeight()); + } /** * Sets the URL of the image to be displayed. If the image is in the clipped * state, a call to this method will cause a transition of the image to the * unclipped state. Regardless of whether or not the image is in the clipped * or unclipped state, a load event will be fired. - * + * * @param url the image URL */ public void setUrl(String url) { @@ -577,7 +603,7 @@ * visibility rectangle co-ordinates. If the image is currently in the * unclipped state, a call to this method will cause a transition to the * clipped state. - * + * * @param url the image URL * @param left the horizontal coordinate of the upper-left vertex of the * visibility rectangle @@ -600,7 +626,7 @@ * is in the unclipped state, a call to this method will cause a transition of * the image to the clipped state. This transition will cause a load event to * fire. - * + * * @param left the horizontal coordinate of the upper-left vertex of the * visibility rectangle * @param top the vertical coordinate of the upper-left vertex of the ======================================= --- /trunk/user/test/com/google/gwt/user/client/ui/ImageTest.java Wed Sep 30 16:46:38 2009 +++ /trunk/user/test/com/google/gwt/user/client/ui/ImageTest.java Thu Oct 8 12:27:08 2009 @@ -1,12 +1,12 @@ /* * Copyright 2008 Google Inc. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -15,6 +15,7 @@ */ package com.google.gwt.user.client.ui; +import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.DivElement; import com.google.gwt.dom.client.Document; import com.google.gwt.event.dom.client.ErrorEvent; @@ -24,6 +25,8 @@ import com.google.gwt.junit.DoNotRunWith; import com.google.gwt.junit.Platform; import com.google.gwt.junit.client.GWTTestCase; +import com.google.gwt.resources.client.ClientBundle; +import com.google.gwt.resources.client.ImageResource; /** * Tests for the Image widget. Images in both clipped mode and unclipped mode @@ -31,6 +34,10 @@ */ @SuppressWarnings("deprecation") public class ImageTest extends GWTTestCase { + interface Bundle extends ClientBundle { + ImageResource prettyPiccy(); + } + private static class TestErrorHandler implements ErrorHandler { private Image image; @@ -75,7 +82,7 @@ * Helper method that allows us to 'peek' at the private <code>state</code> * field in the Image object, and call the <code>state.getStateName()</code> * method. - * + * * @param image The image instance * @return "unclipped" if image is in the unclipped state, or "clipped" if the * image is in the clipped state @@ -91,7 +98,7 @@ /** * Tests the transition from the clipped state to the unclipped state. - * + * * Disabled because of issue #863 & #864. It fails intermittently in linux * hosted mode tests. */ @@ -127,7 +134,7 @@ /** * Tests the transition from the unclipped state to the clipped state. - * + * * Disabled because of issue #863. */ public void disabledTestChangeImageToClipped() { @@ -163,7 +170,7 @@ /** * Tests the creation of an image in unclipped mode. - * + * * Disabled because of issue #863 & #864. */ public void disabledTestCreateImage() { @@ -196,7 +203,7 @@ * Tests the firing of onload events when * {...@link com.google.gwt.user.client.ui.Image#setUrl(String)} is called on an * unclipped image. - * + * * Disabled because of issue #863 */ public void disabledTestSetUrlAndLoadEventsOnUnclippedImage() { @@ -227,7 +234,7 @@ * Tests the behavior of * <code>setUrlAndVisibleRect(String, int, int, int, int)</code> method on * an unclipped image, which causes a state transition to the clipped state. - * + * * Disabled because of issue #863. */ public void disabledTestSetUrlAndVisibleRectOnUnclippedImage() { @@ -335,6 +342,19 @@ assertEquals(1, firedLoad); assertEquals(1, firedError); } + + public void testResourceConstructor() { + Bundle b = GWT.create(Bundle.class); + Image image = new Image(b.prettyPiccy()); + assertResourceWorked(image, b.prettyPiccy()); + } + + public void testSetResource() { + Bundle b = GWT.create(Bundle.class); + Image image = new Image(); + image.setResource(b.prettyPiccy()); + assertResourceWorked(image, b.prettyPiccy()); + } /** * Tests the behavior of @@ -455,4 +475,12 @@ assertEquals(16, image.getHeight()); assertEquals("clipped", getCurrentImageStateName(image)); } -} + + private void assertResourceWorked(Image image, ImageResource prettyPiccy) { + assertEquals(prettyPiccy.getURL(), image.getUrl()); + assertEquals(prettyPiccy.getTop(), image.getOriginTop()); + assertEquals(prettyPiccy.getHeight(), image.getHeight()); + assertEquals(prettyPiccy.getLeft(), image.getOriginLeft()); + assertEquals(prettyPiccy.getWidth(), image.getWidth()); + } +} --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---
