<commit_before>package org.rapidoid.widget;

/*
 * #%L
 * rapidoid-pages
 * %%
 * Copyright (C) 2014 - 2015 <NAME>
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.rapidoid.annotation.Optional;
import org.rapidoid.html.FieldType;
import org.rapidoid.html.FormLayout;
import org.rapidoid.html.Tag;
import org.rapidoid.html.tag.ButtonTag;
import org.rapidoid.html.tag.FormTag;
import org.rapidoid.html.tag.InputTag;
import org.rapidoid.html.tag.OptionTag;
import org.rapidoid.html.tag.SelectTag;
import org.rapidoid.html.tag.TextareaTag;
import org.rapidoid.model.Item;
import org.rapidoid.model.Property;
import org.rapidoid.security.DataPermissions;
import org.rapidoid.security.Secure;
import org.rapidoid.util.Cls;
import org.rapidoid.util.Metadata;
import org.rapidoid.util.TypeKind;
import org.rapidoid.util.U;
import org.rapidoid.var.Var;
import org.rapidoid.var.Vars;

public class FormWidget extends AbstractWidget {

	protected final DataManager dataManager;
	protected final Item item;
	protected final FormMode mode;

	protected Tag[] buttons;
	protected FormLayout layout = FormLayout.VERTICAL;
	protected String[] fieldNames;
	protected String[] fieldLabels;
	protected FieldType[] fieldTypes;
	protected Collection<?>[] fieldOptions;
	protected boolean[] fieldRequired;
	protected Var<?>[] vars;
	protected DataPermissions[] permissions;

	protected boolean hasFields = false;

	public FormWidget(DataManager dataManager, FormMode mode, Item item, String... properties) {
		this.dataManager = dataManager;
		this.mode = mode;
		this.item = item;
		init(item, properties);
	}

	public FormWidget(DataManager dataManager, FormMode mode, FormLayout layout, String[] fieldNames,
			String[] fieldLabels, FieldType[] fieldTypes, Collection<?>[] options, Var<?>[] vars, Tag[] buttons) {
		this.dataManager = dataManager;
		this.mode = mode;
		this.item = null;
		this.layout = layout;
		this.fieldNames = fieldNames;
		this.fieldLabels = U.or(fieldLabels, Arrays.copyOf(fieldNames, fieldNames.length));
		this.fieldTypes = fieldTypes;
		this.fieldOptions = options;
		this.vars = vars;
		this.buttons = buttons;
	}

	/************************** FIELD LABEL ********************************/

	public FormWidget fieldLabel(String fieldName, String fieldLabel) {
		return fieldLabel(fieldIndex(fieldName), fieldLabel);
	}

	public FormWidget fieldLabel(int fieldIndex, String fieldLabel) {
		this.fieldLabels[fieldIndex] = fieldLabel;
		return this;
	}

	public String fieldLabel(String fieldName) {
		return fieldLabel(fieldIndex(fieldName));
	}

	public String fieldLabel(int fieldIndex) {
		return this.fieldLabels[fieldIndex];
	}

	/************************** FIELD TYPE ********************************/

	public FormWidget fieldType(String fieldName, FieldType fieldType) {
		return fieldType(fieldIndex(fieldName), fieldType);
	}

	public FormWidget fieldType(int fieldIndex, FieldType fieldType) {
		this.fieldTypes[fieldIndex] = fieldType;
		return this;
	}

	public String fieldType(String fieldName) {
		return fieldLabel(fieldType(fieldName));
	}

	public FieldType fieldType(int fieldIndex) {
		return this.fieldTypes[fieldIndex];
	}

	/************************** FIELD OPTIONS ********************************/

	public FormWidget fieldOptions(String fieldName, Collection<?> fieldOptions) {
		return fieldOptions(fieldIndex(fieldName), fieldOptions);
	}

	public FormWidget fieldOptions(int fieldIndex, Collection<?> fieldOptions) {
		this.fieldOptions[fieldIndex] = fieldOptions;
		return this;
	}

	public Collection<?> fieldOptions(String fieldName) {
		return fieldOptions(fieldIndex(fieldName));
	}

	public Collection<?> fieldOptions(int fieldIndex) {
		return this.fieldOptions[fieldIndex];
	}
<commit_msg>Added support for customization of required fields and field permissions.<commit_after>package org.rapidoid.widget;

/*
 * #%L
 * rapidoid-pages
 * %%
 * Copyright (C) 2014 - 2015 <NAME>
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.rapidoid.annotation.Optional;
import org.rapidoid.html.FieldType;
import org.rapidoid.html.FormLayout;
import org.rapidoid.html.Tag;
import org.rapidoid.html.tag.ButtonTag;
import org.rapidoid.html.tag.FormTag;
import org.rapidoid.html.tag.InputTag;
import org.rapidoid.html.tag.OptionTag;
import org.rapidoid.html.tag.SelectTag;
import org.rapidoid.html.tag.TextareaTag;
import org.rapidoid.model.Item;
import org.rapidoid.model.Property;
import org.rapidoid.security.DataPermissions;
import org.rapidoid.security.Secure;
import org.rapidoid.util.Cls;
import org.rapidoid.util.Metadata;
import org.rapidoid.util.TypeKind;
import org.rapidoid.util.U;
import org.rapidoid.var.Var;
import org.rapidoid.var.Vars;

public class FormWidget extends AbstractWidget {

	protected final DataManager dataManager;
	protected final Item item;
	protected final FormMode mode;

	protected Tag[] buttons;
	protected FormLayout layout = FormLayout.VERTICAL;
	protected String[] fieldNames;
	protected String[] fieldLabels;
	protected FieldType[] fieldTypes;
	protected Collection<?>[] fieldOptions;
	protected boolean[] fieldRequired;
	protected Var<?>[] vars;
	protected DataPermissions[] permissions;

	protected boolean hasFields = false;

	public FormWidget(DataManager dataManager, FormMode mode, Item item, String... properties) {
		this.dataManager = dataManager;
		this.mode = mode;
		this.item = item;
		init(item, properties);
	}

	public FormWidget(DataManager dataManager, FormMode mode, FormLayout layout, String[] fieldNames,
			String[] fieldLabels, FieldType[] fieldTypes, Collection<?>[] options, Var<?>[] vars, Tag[] buttons) {
		this.dataManager = dataManager;
		this.mode = mode;
		this.item = null;
		this.layout = layout;
		this.fieldNames = fieldNames;
		this.fieldLabels = U.or(fieldLabels, Arrays.copyOf(fieldNames, fieldNames.length));
		this.fieldTypes = fieldTypes;
		this.fieldOptions = options;
		this.vars = vars;
		this.buttons = buttons;
	}

	/************************** FIELD LABEL ********************************/

	public FormWidget fieldLabel(String fieldName, String fieldLabel) {
		return fieldLabel(fieldIndex(fieldName), fieldLabel);
	}

	public FormWidget fieldLabel(int fieldIndex, String fieldLabel) {
		this.fieldLabels[fieldIndex] = fieldLabel;
		return this;
	}

	public String fieldLabel(String fieldName) {
		return fieldLabel(fieldIndex(fieldName));
	}

	public String fieldLabel(int fieldIndex) {
		return this.fieldLabels[fieldIndex];
	}

	/************************** FIELD TYPE ********************************/

	public FormWidget fieldType(String fieldName, FieldType fieldType) {
		return fieldType(fieldIndex(fieldName), fieldType);
	}

	public FormWidget fieldType(int fieldIndex, FieldType fieldType) {
		this.fieldTypes[fieldIndex] = fieldType;
		return this;
	}

	public String fieldType(String fieldName) {
		return fieldLabel(fieldType(fieldName));
	}

	public FieldType fieldType(int fieldIndex) {
		return this.fieldTypes[fieldIndex];
	}

	/************************** FIELD OPTIONS ********************************/

	public FormWidget fieldOptions(String fieldName, Collection<?> fieldOptions) {
		return fieldOptions(fieldIndex(fieldName), fieldOptions);
	}

	public FormWidget fieldOptions(int fieldIndex, Collection<?> fieldOptions) {
		this.fieldOptions[fieldIndex] = fieldOptions;
		return this;
	}

	public Collection<?> fieldOptions(String fieldName) {
		return fieldOptions(fieldIndex(fieldName));
	}

	public Collection<?> fieldOptions(int fieldIndex) {
		return this.fieldOptions[fieldIndex];
	}

	/************************** FIELD REQUIRED ********************************/

	public FormWidget fieldRequired(String fieldName, boolean fieldRequired) {
		return fieldRequired(fieldIndex(fieldName), fieldRequired);
	}

	public FormWidget fieldRequired(int fieldIndex, boolean fieldRequired) {
		this.fieldRequired[fieldIndex] = fieldRequired;
		return this;
	}

	public boolean fieldRequired(String fieldName) {
		return fieldRequired(fieldIndex(fieldName));
	}

	public boolean fieldRequired(int fieldIndex) {
		return this.fieldRequired[fieldIndex];
	}

	/************************** FIELD PERMISSIONS ********************************/

	public FormWidget permissions(String fieldName, DataPermissions permissions) {
		return permissions(fieldIndex(fieldName), permissions);
	}

	public FormWidget permissions(int fieldIndex, DataPermissions permissions) {
		this.permissions[fieldIndex] = permissions;
		return this;
	}

	public DataPermissions permissions(String fieldName) {
		return permissions(fieldIndex(fieldName));
	}

	public DataPermissions permissions(int fieldIndex) {
		return this.permissions[fieldIndex];
	}

{'fraction_non_alphanumeric': 0.14120115918856802, 'fraction_numerical': 0.0023983211751773758, 'mean_word_length': 5.436012861736335, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5563014', 'n_tokens_mistral': 2977, 'n_tokens_neox': 2834, 'n_words': 763}