001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.forms.layout.io.plugins;
018
019import org.nuxeo.ecm.platform.forms.layout.api.BuiltinModes;
020import org.nuxeo.ecm.platform.forms.layout.api.BuiltinWidgetModes;
021import org.nuxeo.ecm.platform.forms.layout.api.WidgetDefinition;
022import org.nuxeo.ecm.platform.forms.layout.api.WidgetSelectOption;
023import org.nuxeo.ecm.platform.forms.layout.api.converters.AbstractWidgetDefinitionConverter;
024import org.nuxeo.ecm.platform.forms.layout.api.converters.LayoutConversionContext;
025import org.nuxeo.ecm.platform.forms.layout.io.plugins.helpers.VocabularyHelper;
026
027/**
028 * Converter that adds translated select options to widgets rendering directory items
029 *
030 * @since 5.5
031 */
032public class WidgetDirectoryItemsConverter extends AbstractWidgetDefinitionConverter {
033
034    public static final String DIR_NAME_PROPERTY = "directoryName";
035
036    protected static enum SUPPORTED_DIR_TYPES {
037        selectOneDirectory, selectManyDirectory, selectOneRadioDirectory, selectManyCheckboxDirectory, suggestOneDirectory, suggestManyDirectory
038    }
039
040    /**
041     * @since 7.3
042     */
043    public static boolean isDirectoryWidget(String wType) {
044        for (SUPPORTED_DIR_TYPES item : SUPPORTED_DIR_TYPES.values()) {
045            if (item.name().equals(wType)) {
046                return true;
047            }
048        }
049        return false;
050    }
051
052    @Override
053    public WidgetDefinition getWidgetDefinition(WidgetDefinition widgetDef, LayoutConversionContext ctx) {
054        String wType = widgetDef.getType();
055        if (isDirectoryWidget(wType)) {
056            String dirName = (String) widgetDef.getProperties(BuiltinModes.ANY, BuiltinModes.ANY).get(DIR_NAME_PROPERTY);
057            if (dirName == null) {
058                dirName = (String) widgetDef.getProperties(BuiltinModes.ANY, BuiltinWidgetModes.EDIT).get(
059                        DIR_NAME_PROPERTY);
060            }
061            if (dirName == null) {
062                dirName = (String) widgetDef.getProperties(BuiltinModes.ANY, BuiltinWidgetModes.VIEW).get(
063                        DIR_NAME_PROPERTY);
064            }
065            if (dirName != null) {
066                WidgetDefinition clone = getClonedWidget(widgetDef);
067                // change select options on new widget
068                WidgetSelectOption[] selectOptions = VocabularyHelper.getVocabularySelectOptions(dirName,
069                        ctx.getLanguage()).toArray(new WidgetSelectOption[] {});
070                clone.setSelectOptions(selectOptions);
071                return clone;
072            }
073        }
074        return widgetDef;
075    }
076}