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

net.sf.mgp.javafx.property.NestedJavaBeanObjectProperty Maven / Gradle / Ivy

package net.sf.mgp.javafx.property;

import javafx.beans.property.ObjectPropertyBase;
import javafx.beans.property.adapter.JavaBeanObjectPropertyBuilder;
import javafx.beans.property.adapter.JavaBeanProperty;
import javafx.beans.property.adapter.ReadOnlyJavaBeanProperty;

class NestedJavaBeanObjectProperty extends ObjectPropertyBase implements JavaBeanProperty {
	private Object bean;
	private final String property;

	private final ReadOnlyJavaBeanProperty parent;
	private final ParentListener listener;

	public NestedJavaBeanObjectProperty(ReadOnlyJavaBeanProperty parent, String property) {
		this.parent = parent;
		this.property = property;
		listener = new ParentListener();
		parent.addListener(listener);
		bean = parent.getValue();
		if (parent.getValue() != null) {
			listener.bindTo(parent.getValue());
		}
	}

	@Override
	public void fireValueChangedEvent() {
		parent.fireValueChangedEvent();
		super.fireValueChangedEvent();
		listener.fireValueChangedEvent();
	}

	@Override
	public void dispose() {
		listener.dispose();
		parent.removeListener(listener);
		parent.dispose();
		bean = null;
		super.set(null);
	}

	@Override
	public void set(T newValue) {
		listener.updateValue(newValue);
	}

	private void internalSet(T newValue) {
		super.set(newValue);
	}
	@Override
	public Object getBean() {
		return bean;
	}

	private void setBean(Object bean) {
		this.bean = bean;
	}
	@Override
	public String getName() {
		return property;
	}

	private class ParentListener extends AbstractParentListener> {
		public ParentListener() {
			super(NestedJavaBeanObjectProperty.this::setBean, NestedJavaBeanObjectProperty.this::internalSet);
		}

		@Override
		protected JavaBeanProperty createProperty(Object bean) {
			try {
				return JavaBeanObjectPropertyBuilder.create().bean(bean).name(property).build();
			} catch (NoSuchMethodException e) {
				throw new IllegalArgumentException("Illegal property " + property, e);
			}
		}

		public void updateValue(T newValue) {
			if (valueProperty != null) {
				valueProperty.setValue(newValue);
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy