001/*
002 *  Copyright 2022 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.forms.question.types;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.ametys.cms.data.type.ModelItemTypeConstants;
023import org.ametys.plugins.forms.helper.FormElementDefinitionHelper;
024import org.ametys.plugins.forms.repository.FormQuestion;
025import org.ametys.runtime.i18n.I18nizableText;
026import org.ametys.runtime.model.ElementDefinition;
027import org.ametys.runtime.model.ModelItem;
028import org.ametys.runtime.model.SimpleViewItemGroup;
029import org.ametys.runtime.model.ViewElement;
030import org.ametys.runtime.model.ViewItemGroup;
031
032/**
033 * Class for creating rich text to insert in forms
034 */
035public class RichTextQuestionType extends AbstractStaticFormQuestionType
036{
037    /** Constant for rich text attribute. */
038    public static final String ATTRIBUTE_RICH_TEXT = "rich-text";
039    
040    /** Constant for default title */
041    public static final String DEFAULT_TITLE = "PLUGIN_FORMS_QUESTION_DEFAULT_TITLE_RICH_TEXT";
042    
043    @Override
044    protected List<ModelItem> _getModelItems()
045    {
046        List<ModelItem> modelItems = super._getModelItems();
047        
048        ElementDefinition textValue = FormElementDefinitionHelper.getElementDefinition(ATTRIBUTE_RICH_TEXT, ModelItemTypeConstants.RICH_TEXT_ELEMENT_TYPE_ID, "PLUGINS_FORMS_QUESTIONS_DIALOG_RICH_TEXT", "PLUGINS_FORMS_QUESTIONS_DIALOG_RICH_TEXT_DESC", null);
049        Map<String, I18nizableText> widgetParameters  = new HashMap<>();
050        widgetParameters.put("resizable", new I18nizableText("false"));
051        textValue.setWidgetParameters(widgetParameters);
052        modelItems.add(textValue);
053        
054        return modelItems;
055    }
056    
057    @Override
058    protected List<ViewItemGroup> _getTabs()
059    {
060        return List.of(_getMainTab(), _getRulesTab());
061    }
062    
063    /**
064     * Define the content of the main tab
065     * @return the main tab definition
066     */
067    protected SimpleViewItemGroup _getMainTab()
068    {
069        SimpleViewItemGroup fieldset =  super._createMainTab();
070        
071        ViewElement title = new ViewElement();
072        title.setDefinition((ElementDefinition< ? >) getModel().getModelItem(FormQuestion.ATTRIBUTE_TITLE));
073        fieldset.addViewItem(title);
074        
075        ViewElement text = new ViewElement();
076        text.setDefinition((ElementDefinition< ? >) getModel().getModelItem(ATTRIBUTE_RICH_TEXT));
077        fieldset.addViewItem(text);
078        
079        return fieldset;
080    }
081    
082    public String getStorageType(FormQuestion question)
083    {
084        throw new UnsupportedOperationException("Method getStorageType doesn't be called for richText question type");
085    }
086    
087    @Override
088    public boolean onlyForDisplay(FormQuestion question)
089    {
090        return true;
091    }
092    
093    @Override
094    public boolean canBeAnsweredByUser(FormQuestion question)
095    {
096        return false;
097    }
098    
099    public I18nizableText getDefaultTitle()
100    {
101        return new I18nizableText("plugin.forms", DEFAULT_TITLE);
102    }
103}