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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright 2000-2018 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.user.client.DOM;
  19. import com.google.gwt.user.client.Event;
  20. import com.vaadin.client.BrowserInfo;
  21. /**
  22. * A image based implementation of {@link Icon}.
  23. * <p>
  24. * The image is loaded from the given URL ( {@link #setUri(String)}) and
  25. * displayed in full.
  26. * </p>
  27. *
  28. * @author Vaadin Ltd
  29. */
  30. public class ImageIcon extends Icon {
  31. public static final String CLASSNAME = "v-icon";
  32. public ImageIcon() {
  33. setElement(DOM.createImg());
  34. setStyleName(CLASSNAME);
  35. }
  36. @Override
  37. public void setUri(final String imageUrl) {
  38. /*
  39. * Start sinking onload events, widgets responsibility to react. We must
  40. * do this BEFORE we set src as IE fires the event immediately if the
  41. * image is found in cache (#2592).
  42. */
  43. sinkEvents(Event.ONLOAD);
  44. if (BrowserInfo.get().isIE()) {
  45. // apply src later for IE, to ensure onload is fired
  46. Scheduler.get().scheduleDeferred(() -> DOM
  47. .setElementProperty(getElement(), "src", imageUrl));
  48. }
  49. DOM.setElementProperty(getElement(), "src", imageUrl);
  50. }
  51. @Override
  52. public void setAlternateText(String alternateText) {
  53. getElement().setAttribute("alt",
  54. alternateText == null ? "" : alternateText);
  55. }
  56. @Override
  57. public String getUri() {
  58. return DOM.getElementProperty(getElement(), "src");
  59. }
  60. }