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.

ImageRenderer.java 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright 2000-2016 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.ui.renderers;
  17. import com.vaadin.server.ExternalResource;
  18. import com.vaadin.server.Resource;
  19. import com.vaadin.server.ResourceReference;
  20. import com.vaadin.server.ThemeResource;
  21. import com.vaadin.shared.communication.URLReference;
  22. import elemental.json.JsonValue;
  23. /**
  24. * A renderer for presenting images.
  25. * <p>
  26. * The image for each rendered cell is read from a Resource-typed property in
  27. * the data source. Only {@link ExternalResource}s and {@link ThemeResource}s
  28. * are currently supported.
  29. *
  30. * @since 7.4
  31. * @author Vaadin Ltd
  32. */
  33. public class ImageRenderer extends ClickableRenderer<Resource> {
  34. /**
  35. * Creates a new image renderer.
  36. */
  37. public ImageRenderer() {
  38. super(Resource.class, null);
  39. }
  40. /**
  41. * Creates a new image renderer and adds the given click listener to it.
  42. *
  43. * @param listener
  44. * the click listener to register
  45. */
  46. public ImageRenderer(RendererClickListener listener) {
  47. this();
  48. addClickListener(listener);
  49. }
  50. @Override
  51. public JsonValue encode(Resource resource) {
  52. if (!(resource == null || resource instanceof ExternalResource
  53. || resource instanceof ThemeResource)) {
  54. throw new IllegalArgumentException(
  55. "ImageRenderer only supports ExternalResource and ThemeResource ("
  56. + resource.getClass().getSimpleName() + " given)");
  57. }
  58. return encode(ResourceReference.create(resource, this, null),
  59. URLReference.class);
  60. }
  61. }