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.

OnStateChangeMethod.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2000-2013 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.metadata;
  17. import java.util.Arrays;
  18. import java.util.Collections;
  19. import java.util.List;
  20. import com.vaadin.client.ServerConnector;
  21. import com.vaadin.client.annotations.OnStateChange;
  22. import com.vaadin.client.communication.StateChangeEvent;
  23. /**
  24. * Encapsulates the data that the widgetset compiler generates for supporting a
  25. * connector method annotated with {@link OnStateChange}
  26. *
  27. * @since 7.2
  28. * @author Vaadin Ltd
  29. */
  30. public class OnStateChangeMethod {
  31. private final String methodName;
  32. private final List<String> properties;
  33. private final Class<?> declaringClass;
  34. /**
  35. * Creates a new instance based on a method name, a list of parameters names
  36. * and a list of properties to listen for.
  37. *
  38. * @param methodName
  39. * the name of the method to call
  40. * @param properties
  41. * an array of state property names to listen to
  42. */
  43. public OnStateChangeMethod(String methodName, String[] properties) {
  44. this(null, methodName, properties);
  45. }
  46. /**
  47. * Creates a new instance based on declaring class, a method name, a list of
  48. * parameters names and a list of properties to listen for.
  49. * <p>
  50. * If the declaring class is <code>null</code>, the method is found based on
  51. * the type of the connector that fired the state change event.
  52. *
  53. * @param declaringClass
  54. * the class in which the target method is declared, or
  55. * <code>null</code> to use the class of the connector firing the
  56. * event
  57. * @param methodName
  58. * the name of the method to call
  59. * @param properties
  60. * an array of state property names to listen to
  61. */
  62. public OnStateChangeMethod(Class<?> declaringClass, String methodName,
  63. String[] properties) {
  64. this.methodName = methodName;
  65. this.properties = Collections.unmodifiableList(Arrays
  66. .asList(properties));
  67. this.declaringClass = declaringClass;
  68. }
  69. /**
  70. * Invokes the listener method for a state change.
  71. *
  72. * @param stateChangeEvent
  73. * the state change event
  74. */
  75. public void invoke(StateChangeEvent stateChangeEvent) {
  76. ServerConnector connector = (ServerConnector) stateChangeEvent
  77. .getSource();
  78. Class<?> declaringClass = this.declaringClass;
  79. if (declaringClass == null) {
  80. declaringClass = connector.getClass();
  81. }
  82. Type declaringType = TypeDataStore.getType(declaringClass);
  83. try {
  84. declaringType.getMethod(methodName).invoke(connector);
  85. } catch (NoDataException e) {
  86. throw new RuntimeException("Couldn't invoke @OnStateChange method "
  87. + declaringType.getSignature() + "." + methodName, e);
  88. }
  89. }
  90. /**
  91. * Gets the list of state property names to listen for.
  92. *
  93. * @return the list of state property names to listen for
  94. */
  95. public List<String> getProperties() {
  96. return properties;
  97. }
  98. }