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.

ComponentStateUtil.java 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.shared.ui;
  17. import java.io.Serializable;
  18. import java.util.HashSet;
  19. import com.vaadin.shared.AbstractComponentState;
  20. import com.vaadin.shared.Registration;
  21. import com.vaadin.shared.communication.SharedState;
  22. public final class ComponentStateUtil implements Serializable {
  23. private ComponentStateUtil() {
  24. // Util class is not instantiable
  25. }
  26. public static final boolean isUndefinedWidth(AbstractComponentState state) {
  27. return state.width == null || state.width.isEmpty();
  28. }
  29. public static final boolean isUndefinedHeight(
  30. AbstractComponentState state) {
  31. return state.height == null || state.height.isEmpty();
  32. }
  33. public static final boolean hasDescription(AbstractComponentState state) {
  34. return state.description != null && !state.description.isEmpty();
  35. }
  36. public static final boolean hasStyles(AbstractComponentState state) {
  37. return state.styles != null && !state.styles.isEmpty();
  38. }
  39. public static final boolean isRelativeWidth(AbstractComponentState state) {
  40. return state.width != null && state.width.endsWith("%");
  41. }
  42. public static final boolean isRelativeHeight(AbstractComponentState state) {
  43. return state.height != null && state.height.endsWith("%");
  44. }
  45. /**
  46. * Removes an event listener id.
  47. *
  48. * @param state
  49. * shared state
  50. * @param eventIdentifier
  51. * The event identifier to remove
  52. * @deprecated Use a {@link Registration} object returned by
  53. * {@link #addRegisteredEventListener(SharedState, String)} to
  54. * remove a listener
  55. */
  56. @Deprecated
  57. public static final void removeRegisteredEventListener(SharedState state,
  58. String eventIdentifier) {
  59. if (state.registeredEventListeners == null) {
  60. return;
  61. }
  62. state.registeredEventListeners.remove(eventIdentifier);
  63. if (state.registeredEventListeners.size() == 0) {
  64. state.registeredEventListeners = null;
  65. }
  66. }
  67. /**
  68. * Adds an event listener id.
  69. *
  70. * @param eventListenerId
  71. * The event identifier to add
  72. * @return a registration object for removing the listener
  73. * @since 8.0
  74. */
  75. public static final Registration addRegisteredEventListener(
  76. SharedState state, String eventListenerId) {
  77. if (state.registeredEventListeners == null) {
  78. state.registeredEventListeners = new HashSet<>();
  79. }
  80. state.registeredEventListeners.add(eventListenerId);
  81. return () -> removeRegisteredEventListener(state, eventListenerId);
  82. }
  83. }