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.

Property.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2000-2021 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 com.vaadin.shared.annotations.DelegateToWidget;
  18. import com.vaadin.shared.annotations.NoLayout;
  19. public class Property {
  20. private final Type bean;
  21. private final String name;
  22. public Property(Type bean, String name) {
  23. this.bean = bean;
  24. this.name = name;
  25. }
  26. public Object getValue(Object bean) throws NoDataException {
  27. return TypeDataStore.getValue(this, bean);
  28. }
  29. public void setValue(Object bean, Object value) throws NoDataException {
  30. TypeDataStore.setValue(this, bean, value);
  31. }
  32. public String getDelegateToWidgetMethodName() {
  33. String value = TypeDataStore.getDelegateToWidget(this);
  34. if (value == null) {
  35. return null;
  36. } else {
  37. return DelegateToWidget.Helper.getDelegateTarget(getName(), value);
  38. }
  39. }
  40. public Type getType() throws NoDataException {
  41. return TypeDataStore.getType(this);
  42. }
  43. public Type getBeanType() {
  44. return bean;
  45. }
  46. /**
  47. * The unique signature used to identify this property. The structure of the
  48. * returned string may change without notice and should not be used for any
  49. * other purpose than identification. The signature is currently based on
  50. * the declaring type's signature and the property's name.
  51. *
  52. * @return the unique signature of this property
  53. */
  54. public String getSignature() {
  55. return bean.getSignature() + "." + name;
  56. }
  57. /**
  58. * Gets the string that is internally used when looking up generated support
  59. * code for this method. This is the same as {@link #getSignature()}, but
  60. * without any type parameters.
  61. *
  62. * @return the string to use for looking up generated support code
  63. *
  64. * @since 7.2
  65. */
  66. public String getLookupKey() {
  67. return bean.getBaseTypeName() + "." + name;
  68. }
  69. @Override
  70. public boolean equals(Object obj) {
  71. if (this == obj) {
  72. return true;
  73. } else if (obj instanceof Property) {
  74. Property other = (Property) obj;
  75. return getSignature().equals(other.getSignature());
  76. } else {
  77. return false;
  78. }
  79. }
  80. @Override
  81. public int hashCode() {
  82. return getSignature().hashCode();
  83. }
  84. public String getName() {
  85. return name;
  86. }
  87. @Override
  88. public String toString() {
  89. return getSignature();
  90. }
  91. /**
  92. * Gets the property name formatted for displaying in a user interface. This
  93. * returns a string where e.g. "camelCase" has been converted to "Camel
  94. * case".
  95. *
  96. * @return the name of this property, formatted for humans to read
  97. */
  98. public String getDisplayName() {
  99. String camelCase = getName();
  100. StringBuilder b = new StringBuilder(camelCase.length());
  101. for (int i = 0; i < camelCase.length(); i++) {
  102. char charAt = camelCase.charAt(i);
  103. if (i == 0) {
  104. // First char always upper case
  105. b.append(Character.toUpperCase(charAt));
  106. } else if (Character.isUpperCase(charAt)) {
  107. b.append(' ');
  108. b.append(Character.toLowerCase(charAt));
  109. } else {
  110. b.append(charAt);
  111. }
  112. }
  113. return b.toString();
  114. }
  115. /**
  116. * Checks whether this property is annotated with {@link NoLayout}.
  117. *
  118. * @since 7.4
  119. *
  120. * @return <code>true</code> if this property has a NoLayout annotation;
  121. * otherwise <code>false</code>
  122. */
  123. public boolean isNoLayout() {
  124. return TypeDataStore.isNoLayoutProperty(this);
  125. }
  126. }