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.

DeclarativeIconGenerator.java 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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.ui;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import com.vaadin.server.Resource;
  20. /**
  21. * Icon generator class for declarative support.
  22. * <p>
  23. * Provides a straightforward mapping between an item and its icon.
  24. *
  25. * @param <T>
  26. * item type
  27. */
  28. class DeclarativeIconGenerator<T> implements IconGenerator<T> {
  29. private IconGenerator<T> fallback;
  30. private Map<T, Resource> captions = new HashMap<>();
  31. public DeclarativeIconGenerator(IconGenerator<T> fallback) {
  32. this.fallback = fallback;
  33. }
  34. @Override
  35. public Resource apply(T item) {
  36. return captions.containsKey(item) ? captions.get(item)
  37. : fallback.apply(item);
  38. }
  39. /**
  40. * Sets an {@code icon} for the {@code item}.
  41. *
  42. * @param item
  43. * a data item
  44. * @param icon
  45. * an icon for the {@code item}
  46. */
  47. protected void setIcon(T item, Resource icon) {
  48. captions.put(item, icon);
  49. }
  50. }