You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageIcon.java 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui;
  17. import com.google.gwt.core.client.Scheduler;
  18. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  19. import com.google.gwt.user.client.DOM;
  20. import com.google.gwt.user.client.Event;
  21. import com.vaadin.client.BrowserInfo;
  22. /**
  23. * A image based implementation of {@link Icon}.
  24. * <p>
  25. * The image is loaded from the given URL ( {@link #setUri(String)}) and
  26. * displayed in full.
  27. * </p>
  28. *
  29. * @author Vaadin Ltd
  30. */
  31. public class ImageIcon extends Icon {
  32. public static final String CLASSNAME = "v-icon";
  33. public ImageIcon() {
  34. setElement(DOM.createImg());
  35. setStyleName(CLASSNAME);
  36. }
  37. @Override
  38. public void setUri(final String imageUrl) {
  39. /*
  40. * Start sinking onload events, widgets responsibility to react. We must
  41. * do this BEFORE we set src as IE fires the event immediately if the
  42. * image is found in cache (#2592).
  43. */
  44. sinkEvents(Event.ONLOAD);
  45. if (BrowserInfo.get().isIE()) {
  46. // apply src later for IE, to ensure onload is fired
  47. Scheduler.get().scheduleDeferred(new ScheduledCommand() {
  48. @Override
  49. public void execute() {
  50. DOM.setElementProperty(getElement(), "src", imageUrl);
  51. }
  52. });
  53. }
  54. DOM.setElementProperty(getElement(), "src", imageUrl);
  55. }
  56. @Override
  57. public void setAlternateText(String alternateText) {
  58. getElement().setAttribute("alt",
  59. alternateText == null ? "" : alternateText);
  60. }
  61. @Override
  62. public String getUri() {
  63. return DOM.getElementProperty(getElement(), "src");
  64. }
  65. }