Search in sources :

Example 71 with MultipleSelectionElement

use of org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement in project OpenOLAT by OpenOLAT.

the class ProfileFormController method initForm.

@Override
protected void initForm(FormItemContainer formLayout, Controller listener, UserRequest ureq) {
    User user = identityToModify.getUser();
    // show a form element for each property handler
    boolean first = true;
    for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
        if (userPropertyHandler == null) {
            continue;
        }
        // add spacer if necessary (i.e. when group name changes)
        String group = userPropertyHandler.getGroup();
        String formId = "group." + group;
        FormLayoutContainer groupContainer = (FormLayoutContainer) formLayout.getFormComponent(formId);
        if (groupContainer == null) {
            groupContainer = FormLayoutContainer.createDefaultFormLayout(formId, getTranslator());
            groupContainer.setFormTitle(translate("form.group." + group));
            if (first) {
                groupContainer.setFormContextHelp("Configuration");
                first = false;
            }
            formItems.put(formId, groupContainer);
            formLayout.add(groupContainer);
        }
        // add input field to container
        FormItem formItem = userPropertyHandler.addFormItem(getLocale(), user, usageIdentifier, isAdministrativeUser, groupContainer);
        String propertyName = userPropertyHandler.getName();
        formItems.put(propertyName, formItem);
        if (formItem instanceof TextElement) {
            // it's a text field, so get the value of this property into the text field
            TextElement textElement = (TextElement) formItem;
            textElement.setValue(user.getProperty(propertyName, getLocale()));
        } else if (formItem instanceof MultipleSelectionElement) {
            // it's a checkbox, so set the box to checked if the corresponding property is set to "true"
            MultipleSelectionElement checkbox = (MultipleSelectionElement) formItem;
            String value = user.getProperty(propertyName, getLocale());
            if (value != null) {
                checkbox.select(propertyName, value.equals("true"));
            } else {
                // assume "false" if the property is not present
                checkbox.select(propertyName, false);
            }
        }
        // special case for email field
        if (userPropertyHandler.getName().equals("email")) {
            String key = user.getProperty("emchangeKey", null);
            TemporaryKey tempKey = rm.loadTemporaryKeyByRegistrationKey(key);
            if (tempKey != null) {
                XStream xml = XStreamHelper.createXStreamInstance();
                @SuppressWarnings("unchecked") HashMap<String, String> mails = (HashMap<String, String>) xml.fromXML(tempKey.getEmailAddress());
                formItem.setExampleKey("email.change.form.info", new String[] { mails.get("changedEMail") });
            }
            if (!userModule.isEmailMandatory()) {
                formItem.setMandatory(false);
            }
        }
    }
    // add the "about me" text field.
    FormLayoutContainer groupContainer = FormLayoutContainer.createDefaultFormLayout("group.about", getTranslator());
    groupContainer.setFormTitle(translate("form.group.about"));
    groupContainer.setElementCssClass("o_user_aboutme");
    formLayout.add(groupContainer);
    HomePageConfig conf = hpcm.loadConfigFor(identityToModify.getName());
    textAboutMe = uifactory.addRichTextElementForStringData("form.text", "form.text", conf.getTextAboutMe(), 10, -1, false, null, null, groupContainer, ureq.getUserSession(), getWindowControl());
    textAboutMe.setMaxLength(10000);
    // upload image
    groupContainer = FormLayoutContainer.createDefaultFormLayout("portraitupload", getTranslator());
    groupContainer.setFormTitle(translate("ul.header"));
    formLayout.add(groupContainer);
    File portraitFile = dps.getLargestPortrait(identityToModify.getName());
    // Init upload controller
    Set<String> mimeTypes = new HashSet<>();
    mimeTypes.add("image/gif");
    mimeTypes.add("image/jpg");
    mimeTypes.add("image/jpeg");
    mimeTypes.add("image/png");
    portraitUpload = uifactory.addFileElement(getWindowControl(), "ul.select", "ul.select", groupContainer);
    portraitUpload.setMaxUploadSizeKB(10000, null, null);
    portraitUpload.setPreview(ureq.getUserSession(), true);
    portraitUpload.addActionListener(FormEvent.ONCHANGE);
    portraitUpload.setHelpTextKey("ul.select.fhelp", null);
    portraitUpload.setDeleteEnabled(true);
    if (portraitFile != null) {
        portraitUpload.setInitialFile(portraitFile);
    }
    portraitUpload.limitToMimeType(mimeTypes, null, null);
    if (logoEnabled) {
        // upload image
        groupContainer = FormLayoutContainer.createDefaultFormLayout("logoupload", getTranslator());
        groupContainer.setFormTitle(translate("logo.header"));
        formLayout.add(groupContainer);
        File logoFile = dps.getLargestLogo(identityToModify.getName());
        logoUpload = uifactory.addFileElement(getWindowControl(), "logo.select", "logo.select", groupContainer);
        logoUpload.setMaxUploadSizeKB(10000, null, null);
        logoUpload.setPreview(ureq.getUserSession(), true);
        logoUpload.addActionListener(FormEvent.ONCHANGE);
        logoUpload.setHelpTextKey("ul.select.fhelp", null);
        logoUpload.setDeleteEnabled(true);
        if (logoFile != null) {
            logoUpload.setInitialFile(logoFile);
        }
        logoUpload.limitToMimeType(mimeTypes, null, null);
    }
    // Create submit and cancel buttons
    FormLayoutContainer buttonLayoutWrappper = FormLayoutContainer.createDefaultFormLayout("buttonLayoutWrappper", getTranslator());
    formLayout.add(buttonLayoutWrappper);
    FormLayoutContainer buttonLayout = FormLayoutContainer.createButtonLayout("buttonLayoutInner", getTranslator());
    buttonLayoutWrappper.add(buttonLayout);
    uifactory.addFormSubmitButton("save", buttonLayout);
    uifactory.addFormCancelButton("cancel", buttonLayout, ureq, getWindowControl());
}
Also used : User(org.olat.core.id.User) HashMap(java.util.HashMap) FormItem(org.olat.core.gui.components.form.flexible.FormItem) TemporaryKey(org.olat.registration.TemporaryKey) XStream(com.thoughtworks.xstream.XStream) FormLayoutContainer(org.olat.core.gui.components.form.flexible.impl.FormLayoutContainer) RichTextElement(org.olat.core.gui.components.form.flexible.elements.RichTextElement) TextElement(org.olat.core.gui.components.form.flexible.elements.TextElement) MultipleSelectionElement(org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement) File(java.io.File) UserPropertyHandler(org.olat.user.propertyhandlers.UserPropertyHandler) HashSet(java.util.HashSet)

Example 72 with MultipleSelectionElement

use of org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement in project OpenOLAT by OpenOLAT.

the class GenericSelectionPropertyHandler method getStringValue.

@Override
public String getStringValue(FormItem formItem) {
    if (isMultiselect) {
        Collection<String> selectedKeys = ((MultipleSelectionElement) formItem).getSelectedKeys();
        StringBuilder sb = new StringBuilder();
        for (String sKey : selectedKeys) {
            sb.append(sKey).append(KEY_DELIMITER);
        }
        return sb.toString();
    }
    if (formItem instanceof SingleSelection) {
        SingleSelection sel = (SingleSelection) formItem;
        if (sel.isOneSelected()) {
            return sel.getSelectedKey();
        } else {
            return null;
        }
    }
    return null;
}
Also used : MultipleSelectionElement(org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement) SingleSelection(org.olat.core.gui.components.form.flexible.elements.SingleSelection)

Example 73 with MultipleSelectionElement

use of org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement in project OpenOLAT by OpenOLAT.

the class GenericSelectionPropertyHandler method addFormItem.

/**
 * @see org.olat.user.propertyhandlers.UserPropertyHandler#addFormItem(java.util.Locale,
 *      org.olat.core.id.User, java.lang.String, boolean,
 *      org.olat.core.gui.components.form.flexible.FormItemContainer)
 */
@Override
public FormItem addFormItem(Locale locale, User user, String usageIdentifyer, boolean isAdministrativeUser, FormItemContainer formItemContainer) {
    FormItem newItem = null;
    if (isMultiselect) {
        MultipleSelectionElement mse = FormUIFactory.getInstance().addCheckboxesHorizontal(getName(), i18nFormElementLabelKey(), formItemContainer, selectionKeys, getTranslatedValues(selectionKeys, locale));
        for (String sKey : getSelectedKeys(user)) {
            mse.select(sKey, true);
        }
        newItem = mse;
    } else {
        String[] allKeys = new String[selectionKeys.length + 1];
        System.arraycopy(selectionKeys, 0, allKeys, 1, selectionKeys.length);
        allKeys[0] = NO_SEL_KEY;
        SingleSelection sse = FormUIFactory.getInstance().addDropdownSingleselect(getName(), i18nFormElementLabelKey(), formItemContainer, allKeys, getTranslatedValues(allKeys, locale), null);
        // make pre-selection of the formItem
        String internalValue = getInternalValue(user);
        if (isValidValue(user, internalValue, null, null) && internalValue != null)
            sse.select(internalValue, true);
        newItem = sse;
    }
    // enable/disable according to settings
    UserManager um = UserManager.getInstance();
    if (um.isUserViewReadOnly(usageIdentifyer, this) && !isAdministrativeUser) {
        newItem.setEnabled(false);
    }
    if (um.isMandatoryUserProperty(usageIdentifyer, this)) {
        newItem.setMandatory(true);
    }
    return newItem;
}
Also used : MultipleSelectionElement(org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement) SingleSelection(org.olat.core.gui.components.form.flexible.elements.SingleSelection) FormItem(org.olat.core.gui.components.form.flexible.FormItem) UserManager(org.olat.user.UserManager)

Example 74 with MultipleSelectionElement

use of org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement in project OpenOLAT by OpenOLAT.

the class UserInterestsController method setSelectedInterests.

/**
 * Selects all checkboxes whose i18n keys are contained in the space-delimited <code>selectedInterestsKeys</code>.
 *
 * @param selectedInterestsIDs
 */
public void setSelectedInterests(String selectedInterestsIDs) {
    if (selectedInterestsIDs != null) {
        Set<String> selectedInterestsIDsAsSet = new HashSet<String>(Arrays.asList(selectedInterestsIDs.trim().split(":")));
        // for each ID we get, ...
        for (String selectedInterestID : selectedInterestsIDsAsSet) {
            if (!selectedInterestID.equals("")) {
                // ... we have to loop over all checkbox groups since we don't know which one the ID belongs to.
                for (MultipleSelectionElement checkboxGroup : this.checkboxGroups) {
                    String key = UserInterestsPropertyHandler.SUBCATEGORY_I18N_PREFIX + selectedInterestID;
                    MultipleSelectionElementImpl checkboxGroupImpl = (MultipleSelectionElementImpl) checkboxGroup;
                    if (checkboxGroupImpl.getKeys().contains(key)) {
                        checkboxGroup.select(key, true);
                    }
                }
            }
        }
    }
}
Also used : MultipleSelectionElement(org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement) MultipleSelectionElementImpl(org.olat.core.gui.components.form.flexible.impl.elements.MultipleSelectionElementImpl) HashSet(java.util.HashSet)

Example 75 with MultipleSelectionElement

use of org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement in project OpenOLAT by OpenOLAT.

the class UserInterestsController method formInnerEvent.

/**
 * @see org.olat.core.gui.components.form.flexible.impl.FormBasicController#formInnerEvent(org.olat.core.gui.UserRequest, org.olat.core.gui.components.form.flexible.FormItem, org.olat.core.gui.components.form.flexible.impl.FormEvent)
 */
@Override
protected void formInnerEvent(UserRequest ureq, FormItem source, FormEvent event) {
    super.formInnerEvent(ureq, source, event);
    // did the user click on a check box?
    if (this.checkboxGroups.contains(source)) {
        // find out which checkboxes are selected at the moment.
        Set<String> currentSelection = getCurrentSelection();
        // if the user has not selected the maximum number of boxes...
        if (getSelectionCount() < maxNumOfInterests) {
            // ...enable all checkboxes
            for (MultipleSelectionElement checkboxGroup : this.checkboxGroups) {
                MultipleSelectionElementImpl multipleSelectionElementImpl = (MultipleSelectionElementImpl) checkboxGroup;
                multipleSelectionElementImpl.setEnabled(multipleSelectionElementImpl.getKeys(), true);
            }
        } else {
            // ... otherwise, disable all checkboxes except the selected ones.
            for (MultipleSelectionElement checkboxGroup : this.checkboxGroups) {
                MultipleSelectionElementImpl multipleSelectionElementImpl = (MultipleSelectionElementImpl) checkboxGroup;
                Set<String> allUncheckedCheckboxes = new HashSet<String>(multipleSelectionElementImpl.getKeys());
                allUncheckedCheckboxes.removeAll(currentSelection);
                multipleSelectionElementImpl.setEnabled(allUncheckedCheckboxes, false);
            }
            showWarning("form.warning.maxNumber");
        }
    }
}
Also used : MultipleSelectionElement(org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement) MultipleSelectionElementImpl(org.olat.core.gui.components.form.flexible.impl.elements.MultipleSelectionElementImpl) HashSet(java.util.HashSet)

Aggregations

MultipleSelectionElement (org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement)136 FormLayoutContainer (org.olat.core.gui.components.form.flexible.impl.FormLayoutContainer)22 ArrayList (java.util.ArrayList)20 TextElement (org.olat.core.gui.components.form.flexible.elements.TextElement)16 MultipleSelectionElementImpl (org.olat.core.gui.components.form.flexible.impl.elements.MultipleSelectionElementImpl)16 HashMap (java.util.HashMap)12 HashSet (java.util.HashSet)12 FormLink (org.olat.core.gui.components.form.flexible.elements.FormLink)12 SingleSelection (org.olat.core.gui.components.form.flexible.elements.SingleSelection)10 FormItem (org.olat.core.gui.components.form.flexible.FormItem)8 Identity (org.olat.core.id.Identity)8 UserPropertyHandler (org.olat.user.propertyhandlers.UserPropertyHandler)8 SelectionEvent (org.olat.core.gui.components.form.flexible.impl.elements.table.SelectionEvent)6 ICourse (org.olat.course.ICourse)6 Checkbox (org.olat.course.nodes.cl.model.Checkbox)6 VFSContainer (org.olat.core.util.vfs.VFSContainer)5 VFSItem (org.olat.core.util.vfs.VFSItem)5 Section (org.olat.modules.portfolio.Section)5 File (java.io.File)4 UserShortDescription (org.olat.admin.user.UserShortDescription)4