de.espirit.firstspirit.access.editor.ComboboxEditorValueExample


package de.espirit.firstspirit.access.editor;

import de.espirit.firstspirit.access.Language;
import de.espirit.firstspirit.access.editor.value.InvalidValueException;
import de.espirit.firstspirit.access.editor.value.Option;
import de.espirit.firstspirit.access.editor.value.OptionModel;

/**
 * Example how to get the value of a ComboboxEditorValue and how to store a value into a ComboboxEditorValue.
 *
 * @since 4.2.34
 */
public class ComboboxEditorValueExample {

	/**
	 * Gets the first option found in option model and stores it into the ComboboxEditorValue for the given language.
	 * Note: The selected Option has to be defined in the editors option model.
	 *
	 * @since 4.2.34
	 */
	public void setValueForLanguage(final ComboboxEditorValue editor, final Language language) throws InvalidValueException {

		// 1. check if language is provided if editor is language dependent ("useLanguages='yes'" in gom syntax)
		if (editor.isLanguageDependent() && language == null) {
			throw new NullPointerException("Language is missing!");
		}

		// 2. get the option model of the editor value
		final OptionModel optionModel = editor.getOptionModel(language);

		// 3. get the first option defined in the option model if its not empty
		if (optionModel.getSize() > 0) {
			final Option option = optionModel.iterator().next();

			// 4. store the selected value into the editor value
			editor.set(language, option);
		}
	}


	/**
	 * Returns the selected Option of the ComboboxEditorValue for the given language.
	 *
	 * @since 4.2.34
	 */
	public Option getValueForLanguage(final ComboboxEditorValue editor, final Language language) {

		// 1. get the stored instance in the editor value
		final Option value = editor.get(language);

		// 2. and return the value for further processing
		return value;
	}
}