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