PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/ModelibraWicket/src/org/modelibra/wicket/widget/TextFieldPanel.java

https://github.com/DarioGT/Modelibra-Family
Java | 254 lines | 152 code | 21 blank | 81 comment | 30 complexity | 1b72c106f91d7b7b8306e95784e792a7 MD5 | raw file
  1. /*
  2. * Modelibra
  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 org.modelibra.wicket.widget;
  17. import java.util.Locale;
  18. import org.apache.wicket.AttributeModifier;
  19. import org.apache.wicket.markup.html.form.TextField;
  20. import org.apache.wicket.markup.html.form.validation.FormComponentFeedbackBorder;
  21. import org.apache.wicket.markup.html.panel.Panel;
  22. import org.apache.wicket.model.CompoundPropertyModel;
  23. import org.apache.wicket.model.Model;
  24. import org.apache.wicket.util.convert.IConverter;
  25. import org.modelibra.IEntity;
  26. import org.modelibra.config.ModelConfig;
  27. import org.modelibra.config.PropertyConfig;
  28. import org.modelibra.type.PropertyClass;
  29. import org.modelibra.wicket.type.DateConverter;
  30. import org.modelibra.wicket.type.EmailConverter;
  31. import org.modelibra.wicket.type.UrlConverter;
  32. import org.modelibra.wicket.util.LocalizedText;
  33. import org.modelibra.wicket.view.View;
  34. import org.modelibra.wicket.view.ViewModel;
  35. /**
  36. * Text field panel.
  37. *
  38. * @author Dzenan Ridjanovic
  39. * @author Vedad Kirlic
  40. * @version 2007-10-26
  41. */
  42. public class TextFieldPanel extends Panel {
  43. private static final long serialVersionUID = 101910L;
  44. protected TextField textField;
  45. /**
  46. * Constructs a text field panel.
  47. *
  48. * @param viewModel
  49. * view model
  50. * @param view
  51. * view
  52. */
  53. public TextFieldPanel(final ViewModel viewModel, final View view) {
  54. super(view.getWicketId());
  55. IEntity<?> entity;
  56. String action = viewModel.getAction();
  57. if (action != null && action.equals("update")) {
  58. entity = viewModel.getUpdateEntity();
  59. } else {
  60. entity = viewModel.getEntity();
  61. }
  62. CompoundPropertyModel entityModel = new CompoundPropertyModel(
  63. entity);
  64. final PropertyConfig propertyConfig = viewModel.getPropertyConfig();
  65. String propertyCode = propertyConfig.getCode();
  66. /*if (propertyConfig.isReference()) {
  67. String neighborCode = propertyConfig.getReferenceNeighbor();
  68. NeighborConfig neighborConfig = entity.getConceptConfig()
  69. .getNeighborsConfig().getNeighborConfig(neighborCode);
  70. String neighborConceptCode = neighborConfig
  71. .getDestinationConcept();
  72. IEntities neighborEntities = viewModel.getModel().getEntry(
  73. neighborConceptCode);
  74. if (neighborEntities != null) {
  75. Long referenceOid = (Long) entity.getProperty(propertyCode);
  76. Oid neighborParentOid = new Oid(referenceOid);
  77. IEntity neighborParent = neighborEntities
  78. .retrieveByOid(neighborParentOid);
  79. if (neighborParent != null) {
  80. PropertyConfig firstEssentialPropertyConfig = neighborParent
  81. .getConceptConfig().getPropertiesConfig()
  82. .getFirstEssentialPropertyConfig();
  83. if (firstEssentialPropertyConfig != null) {
  84. String firstEssentialPropertyCode = firstEssentialPropertyConfig
  85. .getCode();
  86. CompoundPropertyModel parentModel = new CompoundPropertyModel(
  87. neighborParent);
  88. textField = new TextField("propertyValue",
  89. parentModel
  90. .bind(firstEssentialPropertyCode));
  91. }
  92. }
  93. }
  94. if (textField == null) {
  95. textField = new TextField("propertyValue", entityModel
  96. .bind(propertyCode));
  97. textField.setVisible(false);
  98. }
  99. // textField.setType(PropertyClass.getLongClass());
  100. } else*/ if (propertyConfig.getPropertyClass().equals(
  101. PropertyClass.getBoolean())) {
  102. textField = new TextField("propertyValue", entityModel
  103. .bind(propertyCode));
  104. textField.setType(PropertyClass.getBooleanClass());
  105. } else if (propertyConfig.getPropertyClass().equals(
  106. PropertyClass.getString())) {
  107. textField = new TextField("propertyValue", entityModel
  108. .bind(propertyCode)) {
  109. static final long serialVersionUID = 200915L;
  110. public IConverter getConverter(final Class type) {
  111. return new IConverter() {
  112. public Object convertToObject(String source,
  113. Locale locale) {
  114. if (propertyConfig.isWhitespaceAllowed())
  115. return source.isEmpty() ? null : source;
  116. return source.trim().isEmpty() ? null : source;
  117. }
  118. public String convertToString(Object source,
  119. Locale locale) {
  120. return source == null ? null : (String) source;
  121. }
  122. };
  123. }
  124. };
  125. textField.setType(PropertyClass.getStringClass());
  126. if (propertyConfig.isScramble()) {
  127. setAttribute("type", "password");
  128. }
  129. } else if (propertyConfig.getPropertyClass().equals(
  130. PropertyClass.getInteger())) {
  131. textField = new TextField("propertyValue", entityModel
  132. .bind(propertyCode));
  133. textField.setType(PropertyClass.getIntegerClass());
  134. } else if (propertyConfig.getPropertyClass().equals(
  135. PropertyClass.getLong())) {
  136. textField = new TextField("propertyValue", entityModel
  137. .bind(propertyCode));
  138. textField.setType(PropertyClass.getLongClass());
  139. } else if (propertyConfig.getPropertyClass().equals(
  140. PropertyClass.getFloat())) {
  141. textField = new TextField("propertyValue", entityModel
  142. .bind(propertyCode));
  143. textField.setType(PropertyClass.getFloatClass());
  144. } else if (propertyConfig.getPropertyClass().equals(
  145. PropertyClass.getDouble())) {
  146. textField = new TextField("propertyValue", entityModel
  147. .bind(propertyCode));
  148. textField.setType(PropertyClass.getDoubleClass());
  149. } else if (propertyConfig.getPropertyClass().equals(
  150. PropertyClass.getDate())) {
  151. textField = new TextField("propertyValue", entityModel
  152. .bind(propertyCode)) {
  153. static final long serialVersionUID = 200915L;
  154. String datePattern = viewModel.getModel().getModelConfig()
  155. .getDatePattern();
  156. public IConverter getConverter(final Class type) {
  157. return new DateConverter(datePattern);
  158. }
  159. };
  160. textField.setType(PropertyClass.getDateClass());
  161. } else if (propertyConfig.getPropertyClass().equals(
  162. PropertyClass.getUrl())) {
  163. textField = new TextField("propertyValue", entityModel
  164. .bind(propertyCode)) {
  165. static final long serialVersionUID = 200916L;
  166. public IConverter getConverter(final Class type) {
  167. return new UrlConverter();
  168. }
  169. };
  170. textField.setType(PropertyClass.getUrlClass());
  171. } else if (propertyConfig.getPropertyClass().equals(
  172. PropertyClass.getEmail())) {
  173. textField = new TextField("propertyValue", entityModel
  174. .bind(propertyCode)) {
  175. static final long serialVersionUID = 200917L;
  176. public IConverter getConverter(final Class type) {
  177. return new EmailConverter();
  178. }
  179. };
  180. textField.setType(PropertyClass.getEmailClass());
  181. }
  182. // Sets loacalized label for textfield
  183. textField.setLabel(new Model(LocalizedText.getPropertyName(this,
  184. entity, propertyConfig)));
  185. ModelConfig modelConfig = propertyConfig.getConceptConfig()
  186. .getModelConfig();
  187. if (modelConfig.getDomainConfig().isValidateForm()) {
  188. if (propertyConfig.isRequired() && propertyConfig.isUpdate()
  189. && !propertyConfig.isWhitespaceAllowed()) {
  190. textField.setRequired(true);
  191. }
  192. }
  193. if (propertyConfig.getDisplayLengthInt() > 0) {
  194. setAttribute("size", propertyConfig.getDisplayLength());
  195. }
  196. if (!propertyConfig.isUpdate()) {
  197. setAttribute("readonly", "readonly");
  198. }
  199. FormComponentFeedbackBorder propertyFeedback = new FormComponentFeedbackBorder(
  200. "propertyFeedback");
  201. propertyFeedback.add(textField);
  202. add(propertyFeedback);
  203. }
  204. /**
  205. * Gets inner textField component
  206. *
  207. * @return inner textField component
  208. */
  209. public TextField getTextField() {
  210. return textField;
  211. }
  212. /**
  213. * Sets an attribute.
  214. *
  215. * @param attributeName
  216. * attribute name
  217. * @param attributeValue
  218. * attribute value
  219. */
  220. public void setAttribute(String attributeName, String attributeValue) {
  221. AttributeModifier attributeModifier = new AttributeModifier(
  222. attributeName, true, new Model(attributeValue));
  223. textField.add(attributeModifier);
  224. }
  225. /**
  226. * Empties the text field.
  227. */
  228. public void emptyTextField() {
  229. String[] empty = new String[1];
  230. empty[0] = "";
  231. textField.setModelValue(empty);
  232. }
  233. }