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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.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. */
  33. public class DefaultFieldFactory
  34. implements FormFieldFactory, TableFieldFactory {
  35. private static final DefaultFieldFactory instance = new DefaultFieldFactory();
  36. /**
  37. * Singleton method to get an instance of DefaultFieldFactory.
  38. *
  39. * @return an instance of DefaultFieldFactory
  40. */
  41. public static DefaultFieldFactory get() {
  42. return instance;
  43. }
  44. protected DefaultFieldFactory() {
  45. }
  46. @Override
  47. public Field<?> createField(Item item, Object propertyId,
  48. Component uiContext) {
  49. Class<?> type = item.getItemProperty(propertyId).getType();
  50. Field<?> field = createFieldByPropertyType(type);
  51. field.setCaption(createCaptionByPropertyId(propertyId));
  52. return field;
  53. }
  54. @Override
  55. public Field createField(Container container, Object itemId,
  56. Object propertyId, Component uiContext) {
  57. Property containerProperty = container.getContainerProperty(itemId,
  58. propertyId);
  59. Class<?> type = containerProperty.getType();
  60. Field<?> field = createFieldByPropertyType(type);
  61. field.setCaption(createCaptionByPropertyId(propertyId));
  62. return field;
  63. }
  64. /**
  65. * If name follows method naming conventions, convert the name to spaced
  66. * upper case text. For example, convert "firstName" to "First Name"
  67. *
  68. * @param propertyId
  69. * @return the formatted caption string
  70. */
  71. public static String createCaptionByPropertyId(Object propertyId) {
  72. return SharedUtil.propertyIdToHumanFriendly(propertyId);
  73. }
  74. /**
  75. * Creates fields based on the property type.
  76. * <p>
  77. * The default field type is {@link LegacyTextField}. Other field types
  78. * generated by this method:
  79. * <p>
  80. * <b>Boolean</b>: {@link CheckBox}.<br/>
  81. * <b>Date</b>: {@link LegacyDateField}(resolution: day).<br/>
  82. * <b>Item</b>: {@link Form}. <br/>
  83. * <b>default field type</b>: {@link LegacyTextField}.
  84. * <p>
  85. *
  86. * @param type
  87. * the type of the property
  88. * @return the most suitable generic {@link LegacyField} for given type
  89. */
  90. public static Field<?> createFieldByPropertyType(Class<?> type) {
  91. // Null typed properties can not be edited
  92. if (type == null) {
  93. return null;
  94. }
  95. // Item field
  96. if (com.vaadin.v7.data.Item.class.isAssignableFrom(type)) {
  97. return new Form();
  98. }
  99. // Date field
  100. if (Date.class.isAssignableFrom(type)) {
  101. final DateField df = new DateField();
  102. df.setResolution(DateField.RESOLUTION_DAY);
  103. return df;
  104. }
  105. // Boolean field
  106. if (Boolean.class.isAssignableFrom(type)) {
  107. return new CheckBox();
  108. }
  109. return new TextField();
  110. }
  111. }