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

de.carne.jfx.scene.control.Controls Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2016-2020 Holger de Carne and contributors, All Rights Reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package de.carne.jfx.scene.control;

import java.util.Comparator;

import org.eclipse.jdt.annotation.Nullable;

import de.carne.jfx.util.DefaultSet;
import javafx.collections.ObservableList;
import javafx.scene.control.ComboBox;

/**
 * Utility class providing convenience functions for all kind of UI controls.
 */
public final class Controls {

	private Controls() {
		// Make sure this class is not instantiated from outside
	}

	/**
	 * Reset a {@link ComboBox}'s content and selection.
	 *
	 * @param  The {@link ComboBox}'s element type.
	 * @param control The {@link ComboBox} to reset.
	 * @param defaultSet The {@link DefaultSet} to apply (may be {@code null}).
	 * @param comparator The {@link Comparator} to use for combobox item sorting.
	 */
	public static  void resetComboBoxOptions(ComboBox control, @Nullable DefaultSet defaultSet,
			Comparator comparator) {
		ObservableList options = control.getItems();

		options.clear();
		if (defaultSet != null && !defaultSet.isEmpty()) {
			options.addAll(defaultSet);
			options.sort(comparator);
			control.setValue(defaultSet.getDefault());
			if (!control.disableProperty().isBound()) {
				control.setDisable(false);
			}
		} else {
			if (!control.disableProperty().isBound()) {
				control.setDisable(!control.isEditable());
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy