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; import java.util.Collections; import java.util.HashSet; import java.util.Set; /** * Example how to get the value of a CheckboxEditorValue and how to store a value into a CheckboxEditorValue. * * @since 4.2.34 */ public class CheckboxEditorValueExample { /** * Gets the first option found in option model and stores it into the CheckboxEditorValue for the given language. * Note: All the selected Option's has to be defined in the editors option model. * * @since 4.2.34 */ public void setValueForLanguage(final CheckboxEditorValue 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 Set<Option> options = Collections.singleton(optionModel.iterator().next()); // 4. store the selected value into the editor value editor.set(language, options); } } /** * Returns the value of the CheckboxEditorValue for the given language. * * @since 4.2.34 */ public Set<Option> getValueForLanguage(final CheckboxEditorValue editor, final Language language) { // 1. get the stored instance in the editor value final Set<Option> options = editor.get(language); // 2. and return the value for further processing return options; } /** * Adds the value to the CheckboxEditorValue for the given language. * The selected Option has to be defined in the editors option model. * * @since 4.2.34 */ public void addValueForLanguage(final CheckboxEditorValue 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. check the options model not to be empty if (optionModel.getSize() > 0) { // 4. get the stored instance in the editor value Set<Option> options = editor.get(language); // 5. ensure the value is not null if (options == null) { options = new HashSet<Option>(); } // 6. add the selected entry to list of values options.add(optionModel.iterator().next()); // 7. store the selected options into the editor value editor.set(language, options); } } /** * Removes the value out of the CheckboxEditorValue for the given language. * * @since 4.2.34 */ public void removeValueForLanguage(final CheckboxEditorValue 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 stored instance in the editor value final Set<Option> options = editor.get(language); // 3. if the value is null or the collection is empty there is no need to do anything if (options == null || options.isEmpty()) { return; } // 4. remove the first entry from list of values options.remove(options.iterator().next()); // 5. store value into the editor value editor.set(language, options); } }