<commit_before>package ch.openech.mj.edit.fields;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import org.joda.time.ReadablePartial;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import ch.openech.mj.autofill.DateGenerator;
import ch.openech.mj.autofill.DemoEnabled;
import ch.openech.mj.model.InvalidValues;
import ch.openech.mj.model.PropertyInterface;
import ch.openech.mj.model.annotation.Size;
import ch.openech.mj.toolkit.ClientToolkit;
import ch.openech.mj.toolkit.IComponent;
import ch.openech.mj.toolkit.TextField;
import ch.openech.mj.util.DateUtils;
import ch.openech.mj.util.StringUtils;


public abstract class AbstractJodaField<T> extends AbstractEditField<T> implements Enable, DemoEnabled {
	
	protected final TextField textField;
	
	public AbstractJodaField(PropertyInterface property) {
		this(property, false);
	}
	
	public AbstractJodaField(PropertyInterface property, boolean editable) {
		super(property, editable);
		if (editable) {
			textField = ClientToolkit.getToolkit().createTextField(listener(), getAllowedSize(property), getAllowedCharacters(property));
			installFocusLostListener();
		} else {
			textField = ClientToolkit.getToolkit().createReadOnlyTextField();
		}
	}

	protected abstract String getAllowedCharacters(PropertyInterface property);

	protected abstract int getAllowedSize(PropertyInterface property);

	@Override
	public IComponent getComponent() {
		return textField;
	}

	public abstract T getObject();
	
	public abstract void setObject(T value);
		
	private void installFocusLostListener() {
        textField.setFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				// Formattierung auslösen
				T value = getObject();
				if (value != null && !InvalidValues.isInvalid(value)) {
					setObject(value);
				}
			}
		});
	}
	

	public void setEnabled(boolean enabled) {
		textField.setEnabled(enabled);
		if (!enabled) {
			setObject(null);
		}
	}
	
	
	public static class JodaPartialField extends AbstractJodaField<ReadablePartial> {
		private static final DateTimeFormatter DATE_FORMATTER = ISODateTimeFormat.date();

		public JodaPartialField(PropertyInterface property, boolean editable) {
			super(property, editable);
		}
		
		@Override
		protected String getAllowedCharacters(PropertyInterface property) {
			return "01234567890.";
		} 
		
		@Override
		protected int getAllowedSize(PropertyInterface property) {
			return 10;
		}

		@Override
		public ReadablePartial getObject() {
			String text = textField.getText();
			text = DateUtils.parseCH(text, true);
			boolean fieldTextWasEmpty = text == null;
			if (fieldTextWasEmpty) return null;
			boolean fieldTextWasUnparsable = text.length() == 0;
			if (fieldTextWasUnparsable) return InvalidValues.createInvalidPartial(text);

			return DateUtils.parsePartial(text);
		}
		
		@Override
		public void setObject(ReadablePartial value) {
			if (InvalidValues.isInvalid(value)) {
				String text = InvalidValues.getInvalidValue(value);
				textField.setText(text);
			} else if (value != null) {
				String text = DateUtils.formatPartialCH(value);
				if (!StringUtils.equals(textField.getText(), text)) {
					textField.setText(text);
				}
			} else {
				textField.setText(null);
			}
		}

		@Override
		public void fillWithDemoData() {
			// TODO fillWithDemoData for Partial Date Field
		}

	}
	
	public static class JodaDateField extends AbstractJodaField<LocalDate> {<commit_msg>JodaPartialField: implemented demoFill & setEnabled(false) doesnt delete content<commit_after>package ch.openech.mj.edit.fields;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import org.joda.time.ReadablePartial;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import ch.openech.mj.autofill.DateGenerator;
import ch.openech.mj.autofill.DemoEnabled;
import ch.openech.mj.model.InvalidValues;
import ch.openech.mj.model.PropertyInterface;
import ch.openech.mj.model.annotation.Size;
import ch.openech.mj.toolkit.ClientToolkit;
import ch.openech.mj.toolkit.IComponent;
import ch.openech.mj.toolkit.TextField;
import ch.openech.mj.util.DateUtils;
import ch.openech.mj.util.StringUtils;


public abstract class AbstractJodaField<T> extends AbstractEditField<T> implements Enable, DemoEnabled {
	
	protected final TextField textField;
	
	public AbstractJodaField(PropertyInterface property) {
		this(property, false);
	}
	
	public AbstractJodaField(PropertyInterface property, boolean editable) {
		super(property, editable);
		if (editable) {
			textField = ClientToolkit.getToolkit().createTextField(listener(), getAllowedSize(property), getAllowedCharacters(property));
			installFocusLostListener();
		} else {
			textField = ClientToolkit.getToolkit().createReadOnlyTextField();
		}
	}

	protected abstract String getAllowedCharacters(PropertyInterface property);

	protected abstract int getAllowedSize(PropertyInterface property);

	@Override
	public IComponent getComponent() {
		return textField;
	}

	public abstract T getObject();
	
	public abstract void setObject(T value);
		
	private void installFocusLostListener() {
        textField.setFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				// Formattierung auslösen
				T value = getObject();
				if (value != null && !InvalidValues.isInvalid(value)) {
					setObject(value);
				}
			}
		});
	}
	

	public void setEnabled(boolean enabled) {
		textField.setEnabled(enabled);
//		if (!enabled) {
//			setObject(null);
//		}
	}
	
	
	public static class JodaPartialField extends AbstractJodaField<ReadablePartial> {

		public JodaPartialField(PropertyInterface property, boolean editable) {
			super(property, editable);
		}
		
		@Override
		protected String getAllowedCharacters(PropertyInterface property) {
			return "01234567890.";
		} 
		
		@Override
		protected int getAllowedSize(PropertyInterface property) {
			return 10;
		}

		@Override
		public ReadablePartial getObject() {
			String text = textField.getText();
			text = DateUtils.parseCH(text, true);
			boolean fieldTextWasEmpty = text == null;
			if (fieldTextWasEmpty) return null;
			boolean fieldTextWasUnparsable = text.length() == 0;
			if (fieldTextWasUnparsable) return InvalidValues.createInvalidPartial(text);

			return DateUtils.parsePartial(text);
		}
		
		@Override
		public void setObject(ReadablePartial value) {
			if (InvalidValues.isInvalid(value)) {
				String text = InvalidValues.getInvalidValue(value);
				textField.setText(text);
			} else if (value != null) {
				String text = DateUtils.formatPartialCH(value);
				if (!StringUtils.equals(textField.getText(), text)) {
					textField.setText(text);
				}
			} else {
				textField.setText(null);
			}
		}

		@Override
		public void fillWithDemoData() {
			setObject(DateGenerator.generateRandomDate());
		}

	}
	
	public static class JodaDateField extends AbstractJodaField<LocalDate> {
{'fraction_non_alphanumeric': 0.10088569056185995, 'fraction_numerical': 0.0038748962081372822, 'mean_word_length': 4.749403341288783, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '420044', 'n_tokens_mistral': 2484, 'n_tokens_neox': 2156, 'n_words': 449}