/**
 * Copyright Notice
 * 
 * This is a work of the U.S. Government and is not subject to copyright
 * protection in the United States. Foreign copyrights may apply.
 * 
 * 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.
 */

/**
 * PreferencesPluginTextFieldProperty
 * 
 * @author <a href="mailto:joel.kniaz@gmail.com">Joel Kniaz</a>
 */
package gov.va.isaac.gui.preferences.plugins.properties;

import gov.va.isaac.gui.util.TextErrorColorHelper;
import gov.va.isaac.util.ValidBooleanBinding;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * PreferencesPluginTextFieldProperty
 * 
 * @author <a href="mailto:joel.kniaz@gmail.com">Joel Kniaz</a>
 *
 */
public abstract class PreferencesPluginTextFieldProperty extends PreferencesPluginProperty<String, TextField> {
	private Logger logger = LoggerFactory.getLogger(PreferencesPluginTextFieldProperty.class);
	
	/**
	 * @param label
	 * @param control
	 * @param property
	 * @param validator
	 * @param stringConverter
	 * @param binder
	 * @param controlPersistedValueSetter
	 * @param guiFormattingApplicator
	 */
	public PreferencesPluginTextFieldProperty(
			Label label,
			TextField control,
			Property<String> property,
			ValidBooleanBinding validator,
			StringConverter<String> stringConverter,
			PropertyAction<String, TextField> binder,
			PropertyAction<String, TextField> controlPersistedValueSetter,
			PropertyAction<String, TextField> guiFormattingApplicator) {
		super(
				label, 
				control, 
				property, 
				validator, 
				stringConverter, 
				binder,
				controlPersistedValueSetter, 
				guiFormattingApplicator);
	}
	
	/**
	 * @param label
	 * @param control
	 * @param property
	 * @param validator
	 * @param stringConverter
	 * @param binder
	 * @param controlPersistedValueSetter
	 * @param guiFormattingApplicator
	 */
	public PreferencesPluginTextFieldProperty(
			String name,
			TextField control,
			Property<String> property,
			ValidBooleanBinding validator,
			StringConverter<String> stringConverter,
			PropertyAction<String, TextField> binder,
			PropertyAction<String, TextField> controlPersistedValueSetter,
			PropertyAction<String, TextField> guiFormattingApplicator) {
		this(
				new Label(name),
				control, 
				property, 
				validator, 
				stringConverter, 
				binder,
				controlPersistedValueSetter, 
				guiFormattingApplicator);
	}

	public PreferencesPluginTextFieldProperty(String name) {
		this(new Label(name));
	}

	public PreferencesPluginTextFieldProperty(Label label) {
		super(
				label, 
				new TextField(), 
				new SimpleStringProperty(), 
				null, // validator handled below
				new StringConverter<String>() {
					@Override
					public String convertToString(String value) {
						return value != null ? value.toString() : null;
					}
				}, 
				new PropertyAction<String, TextField>() {
					@Override
					public void apply(PreferencesPluginProperty<String, TextField> property) {
						property.getProperty().bind(property.getControl().textProperty());
					}	
				},
				new PropertyAction<String, TextField>() {
					@Override
					public void apply(PreferencesPluginProperty<String, TextField> property) {
						property.getControl().textProperty().set(property.readFromPersistedPreferences());
					}
				}, 
				new PropertyAction<String, TextField>() {
					@Override
					public void apply(PreferencesPluginProperty<String, TextField> property) {
						GridPane.setHgrow(property.getLabel(), Priority.NEVER);
						GridPane.setFillWidth(property.getControl(), true);
						GridPane.setHgrow(property.getControl(), Priority.ALWAYS);
					}
				});
		validator = new ValidBooleanBinding() {
			{
				bind(getProperty());
				setComputeOnInvalidate(true);
			}
			@Override
			protected boolean computeValue() {
				if (StringUtils.isBlank(getProperty().getValue())) {
					this.setInvalidReason("null/unset/unselected " + name);
					logger.debug(getReasonWhyInvalid().get());

					TextErrorColorHelper.setTextErrorColor(label);

					return false;
				} else {
					TextErrorColorHelper.clearTextErrorColor(label);
				}

				this.clearInvalidReason();

				return true;
			}
		};
	}
}

@@ -3239,32 +3239,33 @@
 plicator);%0D%0A%09%7D%0D%0A
+%09
 %0D%0A%09public Prefer
@@ -3304,16 +3304,44 @@
 ing name
+, boolean emptyStringAllowed
 ) %7B%0D%0A%09%09t
@@ -3359,16 +3359,36 @@
 el(name)
+, emptyStringAllowed
 );%0D%0A%09%7D%0D%0A
@@ -3432,27 +3432,153 @@
 roperty(
-Label label
+String name) %7B%0D%0A%09%09this(new Label(name), false);%0D%0A%09%7D%0D%0A%0D%0A%09public PreferencesPluginTextFieldProperty(Label label, boolean emptyStringAllowed
 ) %7B%0D%0A%09%09s
@@ -4881,16 +4881,373 @@
 %09%09%09%09if (
+getProperty().getValue() == null)%0D%0A%09%09%09%09%7B%0D%0A%09%09%09%09%09//in reality, I don't think this ever happens - it will likely be an empty string%0D%0A%09%09%09%09%09this.setInvalidReason(%22missing value for %22 + name);%0D%0A%09%09%09%09%09logger.debug(getReasonWhyInvalid().get());%0D%0A%0D%0A%09%09%09%09%09TextErrorColorHelper.setTextErrorColor(label);%0D%0A%0D%0A%09%09%09%09%09return false;%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%09else if (!emptyStringAllowed && 
 StringUt
@@ -5320,29 +5320,29 @@
 on(%22
-null/unset/unselected
+unspecified value for
  %22 +

allow fields to be empty