From 40a2c7cfd7c8b81c6fb8d335e181bf08e1248fd2 Mon Sep 17 00:00:00 2001 From: strentini <none@none> Date: Mon, 30 Jan 2012 12:23:48 +0100 Subject: [PATCH] OO-98 : new genericYesNoPropertyHandler, fix componentID-bug in uiFactory --- .../form/flexible/FormUIFactory.java | 8 +- .../GenericYesNoPropertyHandler.java | 129 ++++++++++++++++++ 2 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/olat/user/propertyhandlers/GenericYesNoPropertyHandler.java diff --git a/src/main/java/org/olat/core/gui/components/form/flexible/FormUIFactory.java b/src/main/java/org/olat/core/gui/components/form/flexible/FormUIFactory.java index 881b2233080..f8edc4f984a 100644 --- a/src/main/java/org/olat/core/gui/components/form/flexible/FormUIFactory.java +++ b/src/main/java/org/olat/core/gui/components/form/flexible/FormUIFactory.java @@ -336,7 +336,7 @@ public class FormUIFactory { * @return */ public SingleSelection addRadiosHorizontal(final String name, final String i18nLabel, FormItemContainer formLayout, final String[] theKeys, final String[] theValues) { - SingleSelection ss = new SingleSelectionImpl(null, name, SingleSelectionImpl.createHorizontalLayout(null, name)){ + SingleSelection ss = new SingleSelectionImpl(name, name, SingleSelectionImpl.createHorizontalLayout(null, name)){ { this.keys = theKeys; this.values = theValues; @@ -375,7 +375,7 @@ public class FormUIFactory { * @return */ public SingleSelection addRadiosVertical(final String name, final String i18nLabel, FormItemContainer formLayout, final String[] theKeys, final String[] theValues) { - SingleSelection ss = new SingleSelectionImpl(null, name, SingleSelectionImpl.createVerticalLayout(null, name)){ + SingleSelection ss = new SingleSelectionImpl(name, name, SingleSelectionImpl.createVerticalLayout(null, name)){ { this.keys = theKeys; this.values = theValues; @@ -400,7 +400,7 @@ public class FormUIFactory { * @return */ public SingleSelection addDropdownSingleselect(final String name, FormItemContainer formLayout, final String[] theKeys, final String[] theValues, final String[] theCssClasses) { - return addDropdownSingleselect(null, name, name, formLayout, theKeys, theValues, theCssClasses); + return addDropdownSingleselect(name, name, name, formLayout, theKeys, theValues, theCssClasses); } /** @@ -414,7 +414,7 @@ public class FormUIFactory { * @return */ public SingleSelection addDropdownSingleselect(final String name, final String i18nLabel, FormItemContainer formLayout, final String[] theKeys, final String[] theValues, final String[] theCssClasses) { - return addDropdownSingleselect(null, name, i18nLabel, formLayout, theKeys, theValues, theCssClasses); + return addDropdownSingleselect(name, name, i18nLabel, formLayout, theKeys, theValues, theCssClasses); } /** diff --git a/src/main/java/org/olat/user/propertyhandlers/GenericYesNoPropertyHandler.java b/src/main/java/org/olat/user/propertyhandlers/GenericYesNoPropertyHandler.java new file mode 100644 index 00000000000..c789524a45a --- /dev/null +++ b/src/main/java/org/olat/user/propertyhandlers/GenericYesNoPropertyHandler.java @@ -0,0 +1,129 @@ +/** + * OLAT - Online Learning and Training<br> + * http://www.olat.org + * <p> + * Licensed under the Apache License, Version 2.0 (the "License"); <br> + * you may not use this file except in compliance with the License.<br> + * You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing,<br> + * software distributed under the License is distributed on an "AS IS" BASIS, <br> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br> + * See the License for the specific language governing permissions and <br> + * limitations under the License. + * <p> + * frentix GmbH, Switzerland, http://www.frentix.com + * <p> + */ +package org.olat.user.propertyhandlers; + +import java.util.Locale; +import java.util.Map; + +import org.olat.core.gui.components.form.ValidationError; +import org.olat.core.gui.components.form.flexible.FormItem; +import org.olat.core.gui.components.form.flexible.FormItemContainer; +import org.olat.core.gui.components.form.flexible.FormUIFactory; +import org.olat.core.gui.components.form.flexible.elements.SelectionElement; +import org.olat.core.gui.components.form.flexible.elements.SingleSelection; +import org.olat.core.gui.translator.Translator; +import org.olat.core.id.User; +import org.olat.core.util.Util; +import org.olat.user.AbstractUserPropertyHandler; +import org.olat.user.UserManager; + +/** + * + * A User Property Handler for a Yes/No Property.<br /> + * (originated from http://jira.openolat.org/browse/OO-98) + * + * This renders two radio-buttons with "yes" "no" options. (which brings a clear + * distinction from the homeprofile (vcard) checkbox in the homeprofileform) + * + * @author strentini, sergio.trentini@frentix.com, http://www.frentix.com + * @date 30.01.2012 + * + */ +public class GenericYesNoPropertyHandler extends AbstractUserPropertyHandler { + + private static final String KEY_YES = "gchpr.y"; + private static final String KEY_NO = "gchpr.n"; + + private static final String VAL_YES = "true"; + private static final String VAL_NO = "false"; + + @Override + public FormItem addFormItem(Locale locale, User user, String usageIdentifyer, boolean isAdministrativeUser, FormItemContainer formItemContainer) { + SelectionElement sElem = null; + Translator trans = Util.createPackageTranslator(this.getClass(), locale); + sElem = FormUIFactory.getInstance().addRadiosHorizontal(getName(), i18nFormElementLabelKey(), formItemContainer, + new String[] { KEY_YES, KEY_NO }, new String[] { trans.translate("yes"), trans.translate("no") }); + + // pre-select yes/no + String internalValue = getInternalValue(user); + if (isValidValue(internalValue, null, null)) { + if (VAL_YES.equals(internalValue)) + sElem.select(KEY_YES, true); + if (VAL_NO.equals(internalValue)) + sElem.select(KEY_NO, true); + } + + UserManager um = UserManager.getInstance(); + if (um.isUserViewReadOnly(usageIdentifyer, this) && !isAdministrativeUser) { + sElem.setEnabled(false); + } + if (um.isMandatoryUserProperty(usageIdentifyer, this)) { + sElem.setMandatory(true); + } + return sElem; + } + + @Override + public void updateUserFromFormItem(User user, FormItem formItem) { + String internalValue = getStringValue(formItem); + setInternalValue(user, internalValue); + } + + @Override + public boolean isValid(FormItem formItem, Map formContext) { + // the formItem is always valid. if no radio-button is selected, the + // value is just "NO" + // this is also ok if item is mandatory... + return true; + } + + @Override + public String getUserProperty(User user, Locale locale) { + Translator trans = Util.createPackageTranslator(this.getClass(), locale); + String internalValue = getInternalValue(user); + if (VAL_YES.equals(internalValue)) + return trans.translate("yes"); + if (VAL_NO.equals(internalValue)) + return trans.translate("no"); + return "-"; + } + + @Override + public boolean isValidValue(String value, ValidationError validationError, Locale locale) { + // a value is valid if it is either val_no or val_yes + return (VAL_NO.equals(value) || VAL_YES.equals(value)); + } + + @Override + public String getStringValue(FormItem formItem) { + if (formItem instanceof SingleSelection) { + SingleSelection sItem = (SingleSelection) formItem; + if (sItem.isSelected(0)) + return VAL_YES; + } + return VAL_NO; + } + + @Override + public String getStringValue(String displayValue, Locale locale) { + return displayValue; + } + +} -- GitLab