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.

DefaultFieldFactory.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.v7.ui;
  17. import java.util.Date;
  18. import com.vaadin.shared.util.SharedUtil;
  19. import com.vaadin.ui.Component;
  20. import com.vaadin.v7.data.Container;
  21. import com.vaadin.v7.data.Item;
  22. import com.vaadin.v7.data.Property;
  23. /**
  24. * This class contains a basic implementation for both {@link FormFieldFactory}
  25. * and {@link TableFieldFactory}. The class is singleton, use {@link #get()}
  26. * method to get reference to the instance.
  27. *
  28. * <p>
  29. * There are also some static helper methods available for custom built field
  30. * factories.
  31. *
  32. * @deprecated Removed feature in 8.0
  33. */
  34. @Deprecated
  35. public class DefaultFieldFactory
  36. implements FormFieldFactory, TableFieldFactory {
  37. private static final DefaultFieldFactory INSTANCE = new DefaultFieldFactory();
  38. /**
  39. * Singleton method to get an instance of DefaultFieldFactory.
  40. *
  41. * @return an instance of DefaultFieldFactory
  42. */
  43. public static DefaultFieldFactory get() {
  44. return INSTANCE;
  45. }
  46. protected DefaultFieldFactory() {
  47. }
  48. @Override
  49. public Field<?> createField(Item item, Object propertyId,
  50. Component uiContext) {
  51. Class<?> type = item.getItemProperty(propertyId).getType();
  52. Field<?> field = createFieldByPropertyType(type);
  53. field.setCaption(createCaptionByPropertyId(propertyId));
  54. return field;
  55. }
  56. @Override
  57. public Field createField(Container container, Object itemId,
  58. Object propertyId, Component uiContext) {
  59. Property containerProperty = container.getContainerProperty(itemId,
  60. propertyId);
  61. Class<?> type = containerProperty.getType();
  62. Field<?> field = createFieldByPropertyType(type);
  63. field.setCaption(createCaptionByPropertyId(propertyId));
  64. return field;
  65. }
  66. /**
  67. * If name follows method naming conventions, convert the name to spaced
  68. * upper case text. For example, convert "firstName" to "First Name"
  69. *
  70. * @param propertyId
  71. * @return the formatted caption string
  72. */
  73. public static String createCaptionByPropertyId(Object propertyId) {
  74. return SharedUtil.propertyIdToHumanFriendly(propertyId);
  75. }
  76. /**
  77. * Creates fields based on the property type.
  78. * <p>
  79. * The default field type is {@link TextField}. Other field types generated
  80. * by this method:
  81. * <p>
  82. * <b>Boolean</b>: {@link CheckBox}.<br/>
  83. * <b>Date</b>: {@link DateField}(resolution: day).<br/>
  84. * <b>Item</b>: {@link Form}. <br/>
  85. * <b>default field type</b>: {@link TextField}.
  86. * <p>
  87. *
  88. * @param type
  89. * the type of the property
  90. * @return the most suitable generic {@link LegacyField} for given type
  91. */
  92. public static Field<?> createFieldByPropertyType(Class<?> type) {
  93. // Null typed properties can not be edited
  94. if (type == null) {
  95. return null;
  96. }
  97. // Item field
  98. if (Item.class.isAssignableFrom(type)) {
  99. return new Form();
  100. }
  101. // Date field
  102. if (Date.class.isAssignableFrom(type)) {
  103. final DateField df = new DateField();
  104. df.setResolution(DateField.RESOLUTION_DAY);
  105. return df;
  106. }
  107. // Boolean field
  108. if (Boolean.class.isAssignableFrom(type)) {
  109. return new CheckBox();
  110. }
  111. return new TextField();
  112. }
  113. }