package com.netopyr.reduxfx.vscenegraph.impl.patcher.property;

import com.netopyr.reduxfx.vscenegraph.property.VProperty;
import javafx.beans.property.ReadOnlyIntegerProperty;
import javafx.scene.control.TabPane;

import java.util.function.Consumer;

public class TabPaneSelectionModelAccessor extends ListenerHandlingAccessor {

	@SuppressWarnings("unchecked")
	@Override
	public void set(Consumer<Object> dispatcher, Object node, String name, VProperty vProperty) {
		if(! (node instanceof TabPane)) {
			throw new IllegalStateException("Trying to set selectionModel of node " + node);
		}

		final TabPane tabPane = (TabPane) node;

		final ReadOnlyIntegerProperty selectedIndexProperty = tabPane.getSelectionModel().selectedIndexProperty();

		clearListeners(node, selectedIndexProperty);

		final Object value = vProperty.isValueDefined() ? vProperty.getValue() : null;
		tabPane.getSelectionModel().select((int) value);

		if(vProperty.getChangeListener().isDefined()) {
			setChangeListener(dispatcher, node, selectedIndexProperty, vProperty.getChangeListener().get());
		}

		if(vProperty.getInvalidationListener().isDefined()) {
			setInvalidationListener(dispatcher, node, selectedIndexProperty, vProperty.getInvalidationListener().get());
		}
	}

	@Override
	protected Object fxToV(Object value) {
		return value;
	}

	@Override
	protected Object vToFX(Object value) {
		return value;
	}
}

@@ -794,29 +794,12 @@
 %0A%0A%09%09
-final Object value = 
+if (
 vPro
@@ -824,40 +824,13 @@
 ed()
- ? vProperty.getValue() : null;%0A
+) %7B%0A%09
 %09%09ta
@@ -873,14 +873,33 @@
 t) v
-alue);
+Property.getValue());%0A%09%09%7D
 %0A%0A%09%09

Fix for potential NPE