All Downloads are FREE. Search and download functionalities are using the official Maven repository.

be.personify.iam.frontend.wicket.panel.common.FormPanel Maven / Gradle / Ivy

There is a newer version: 1.5.2.RELEASE
Show newest version
package be.personify.iam.frontend.wicket.panel.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.datetime.PatternDateConverter;
import org.apache.wicket.datetime.markup.html.form.DateTextField;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.springframework.hateoas.EntityModel;

import be.personify.iam.frontend.wicket.model.common.ResourcesModel;
import be.personify.iam.frontend.wicket.util.wicket.ChoiceRendererUtil;
import be.personify.util.BooleanUtil;
import be.personify.util.StringUtils;
import be.personify.util.ui.CustomRenderer;

/**
 * Generic form panel
 * @author vanderw
 *
 * @param  the generic type
 */
public class FormPanel extends Panel {
	
	private static final Logger logger = LogManager.getLogger(FormPanel.class);

	private static final long serialVersionUID = -2017066326676226313L;
	
	/**
	 * Constructor
	 * @param id the id of the panel
	 */
	public FormPanel(String id) {
		super(id);
	}
	
	/**
	 * Add a textfield row
	 * @param form The related form
	 * @param resource the resource
	 * @param fieldName the fieldname
	 * @param required is it required?
	 */
	public void addTextFieldRow(Form form, T resource,  String fieldName, boolean required ) {
		addTextFieldRow(form, resource,  fieldName,  required, StringUtils.EMPTY_STRING );
	}
	
	
	public void addCheckboxRow(Form form, T resource,  String fieldName, boolean required, String prefix ) {
		MarkupContainer textFieldRow = new WebMarkupContainer(fieldName + PanelConstants.ROW);
		CheckBox tf = new CheckBox(fieldName, new PropertyModel(resource,prefix +fieldName));
		tf.setRequired(required);
		textFieldRow.add(tf);
		form.add( textFieldRow );
	}
	
	
	public void addTextFieldRow(Form form, T resource,  String fieldName, boolean required, String prefix ) {
		MarkupContainer textFieldRow = new WebMarkupContainer(fieldName + PanelConstants.ROW);
		TextField tf = new TextField(fieldName, new PropertyModel(resource,prefix +fieldName));
		tf.setRequired(required);
		textFieldRow.add(tf);
		
		form.add( textFieldRow );
	}
	
	public void addStringListRow(Form form, T resource,  String fieldName, boolean required, String prefix ) {
		MarkupContainer stringListRow = new WebMarkupContainer(fieldName + PanelConstants.ROW);
		ListCreatePanel> listCreatePanel  = new ListCreatePanel>(fieldName, new PropertyModel>(resource,prefix +fieldName));
		stringListRow.add(listCreatePanel);
		form.add( stringListRow );
	}
	
	
	
	public void addCustomRendererFieldRow(Form form, T resource,  String fieldName, boolean required, String prefix, String customRenderer ) {
		MarkupContainer textFieldRow = new WebMarkupContainer(fieldName + PanelConstants.ROW);
		
		if ( customRenderer.startsWith(CustomRenderer.text_area.name())) {
			String defaultRows = "10";
			if ( customRenderer.contains("|")) {
				String[] attrs = customRenderer.split("\\|");
				for ( int i = 1; i < attrs.length; i++ ) {
					String[] a = attrs[i].split(StringUtils.COLON);
					if ( a[0].equalsIgnoreCase("rows")) {
						defaultRows = a[1];
					}
				}
			}
			TextArea textArea = new TextArea(fieldName, new PropertyModel(resource,prefix +fieldName));
			textArea.setRequired(required);
			textArea.add(new AttributeModifier("rows", new Model(defaultRows)));
			textFieldRow.add(textArea);
			form.add( textFieldRow );
		}
		else if ( customRenderer.startsWith(CustomRenderer.integer_dropdown.name())) {
			//String defaultRows = "10";
			Integer def = 1;
			List choices  = new ArrayList();
			if ( customRenderer.contains("|")) {
				String[] attrs = customRenderer.split("\\|");
				for ( int i = 1; i < attrs.length; i++ ) {
					String[] a = attrs[i].split(":");
					if ( a[0].equalsIgnoreCase("values")) {
						if ( a[1].contains(StringUtils.COMMA)) {
							String[] ss = a[1].split(StringUtils.COMMA);
							for ( int j = 0; j < ss.length; j++) {
								choices.add(Integer.parseInt(ss[j]));
							}
						}
						else if ( a[1].contains(StringUtils.DASH)) {
							String[] ss = a[1].split(StringUtils.DASH);
							if ( ss.length == 2) {
								for ( int j = Integer.parseInt(ss[0]); j <= Integer.parseInt(ss[1]); j++ ) {
									choices.add(j);
								}
							}
							else {
								throw new RuntimeException("incorrect customrenderer arguments, has to be two " + customRenderer);
							}
						}
					}
					else if ( a[0].equalsIgnoreCase("default")) {
						def = Integer.parseInt(a[1]);
					}
				}
			}
			MarkupContainer integerDropDownRow = new WebMarkupContainer(fieldName + PanelConstants.ROW);
			PropertyModel p = new PropertyModel(resource, prefix + fieldName);
			//p.setObject(def);
			integerDropDownRow.add(new DropDownChoice(fieldName, p,choices));
			form.add( integerDropDownRow );
			
			
		}
		else {
			throw new RuntimeException("custom renderer of type " + customRenderer + " is not supported");
		}
		
		
	}
	
	
	
	/**
	 * Adds a boolean dropdown
	 * @param form the related form
	 * @param resource the resource
	 * @param fieldName the fieldname
	 */
	public void addBooleanDropDownRow(Form form, T resource,  String fieldName) {
		addBooleanDropDownRow(form, resource, fieldName, StringUtils.EMPTY_STRING);
	} 
	
	
	
	public void addBooleanDropDownRow(Form form, T resource,  String fieldName, String prefix) {
		MarkupContainer booleanDropDownRow = new WebMarkupContainer(fieldName + PanelConstants.ROW);
		booleanDropDownRow.add(new DropDownChoice(fieldName, new PropertyModel(resource, prefix + fieldName),BooleanUtil.getBooleansAsList()));
		form.add( booleanDropDownRow );
	} 
	
	
	 
	/**
	 * Adds a datetextfield row
	 * @param form the related form
	 * @param resource the reosurce
	 * @param fieldName the fieldname
	 * @param required is it required?
	 */
	public void addDateTextFieldRow(Form form, T resource,  String fieldName, boolean required) {
		addDateTextFieldRow(form, resource, fieldName, required, StringUtils.EMPTY_STRING);
	}
	
	
	public void addDateTextFieldRow(Form form, T resource,  String fieldName, boolean required, String prefix ) {
		MarkupContainer lastChangedRow = new WebMarkupContainer(fieldName + PanelConstants.ROW);
		DateTextField dateTextField= new DateTextField(fieldName,new PropertyModel(resource, prefix + fieldName), new PatternDateConverter("dd/MM/yyyy",true)); //new PatternDateConverter("dd.MM.yyyy",true)
		//DatePicker picker = new DatePicker();
		//dateTextField.add(picker);
		dateTextField.setRequired(required);
		lastChangedRow.add(dateTextField);
		form.add( lastChangedRow );
	}
	
	
	/**
	 * Add a dropdown row
	 * @param form the related form
	 * @param resource the resource
	 * @param fieldName the fieldname
	 * @param choice the choice
	 */
	public void addDropdownRow(Form form, T resource, String fieldName, DropDownChoice choice) {
		MarkupContainer textFieldRow = new WebMarkupContainer(fieldName + PanelConstants.ROW);
		textFieldRow.add(choice);
		form.add( textFieldRow ); 
	}
	
	
	/**
	 * Add a collection dropdown
	 * @param form the related form
	 * @param fieldName the name of the field
	 * @param chainingModel the chaining model
	 * @param resourcesModel the resources model
	 * @param visible is it visible?d
	 * @return a dropdownchoice
	 */
	public DropDownChoice> addCollectionDropdownRow(Form form, String fieldName, IModel chainingModel, ResourcesModel resourcesModel, boolean visible) {
		MarkupContainer row = new WebMarkupContainer(fieldName + PanelConstants.ROW);
		row.setOutputMarkupId(true);
		row.setOutputMarkupPlaceholderTag(true);
		logger.debug("creating new dropdownchoice for {}", fieldName);
		DropDownChoice> dropdown = new DropDownChoice>(fieldName, 
					Model.of(chainingModel), 
					resourcesModel,
					ChoiceRendererUtil.getDefaultchoiceRenderer()
					);
		dropdown.setOutputMarkupId(true);
		dropdown.setOutputMarkupPlaceholderTag(true);
		row.add(dropdown);
		row.setVisible(visible);
		form.add(row);
		return dropdown;
		
	}
	
	
	
	
	public DropDownChoice> addCollectionDropdownRow(Form form, String fieldName, ResourcesModel resourcesModel, boolean visible) {
		MarkupContainer row = new WebMarkupContainer(fieldName + PanelConstants.ROW);
		row.setOutputMarkupId(true);
		row.setOutputMarkupPlaceholderTag(true);
		logger.debug("creating new dropdownchoice for {}", fieldName);
		DropDownChoice> dropdown = new DropDownChoice>(fieldName,
					resourcesModel,
					ChoiceRendererUtil.getDefaultchoiceRenderer()
					);
		dropdown.setOutputMarkupId(true);
		dropdown.setOutputMarkupPlaceholderTag(true);
		row.add(dropdown);
		row.setVisible(visible);
		form.add(row);
		return dropdown;
		
	}
	
	
	
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy