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

swingtree.ArrayPropertyComboModel Maven / Gradle / Ivy

package swingtree;

import org.jspecify.annotations.Nullable;
import sprouts.From;
import sprouts.Val;
import sprouts.Var;

final class ArrayPropertyComboModel extends AbstractComboModel
{
	private final Val _items;
	private final boolean _mutable;

	ArrayPropertyComboModel( Var selection, Val items ) {
		super(selection);
		_items = items;
		_selectedIndex = _indexOf(_selectedItem.orElseNull());
		_mutable = false;
	}

	ArrayPropertyComboModel( Var selection, Var items ) {
		super(selection);
		_items = items;
		_selectedIndex = _indexOf(_selectedItem.orElseNull());
		_mutable = true;
	}

	@Override
	public AbstractComboModel withVar( Var newVar ) {
		return new ArrayPropertyComboModel<>(newVar, _items);
	}

	@Override protected void setAt(int index, @Nullable E element) {
		if ( _mutable )
			_items.ifPresent(i -> {
				i[index] = element;
				if ( _items instanceof Var ) ((Var) _items).fireChange(From.VIEW);
			});
	}
	@Override public int getSize() { return _items.mapTo(Integer.class, i -> i.length ).orElse(0); }
	@Override public @Nullable E getElementAt( int index ) {
		return (E) _items.mapTo(Object.class, i -> i[index] ).orElseNull();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy