001/*
002 *  Copyright 2014 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.core.ui.widgets;
017
018import java.util.Map;
019
020import org.apache.cocoon.util.StringUtils;
021
022import org.ametys.core.ui.StaticClientSideElement;
023
024/**
025 * This implementation creates a widget from a static configuration.
026 * Classes should have the parameters defined as constants in their class.
027 */
028public class StaticClientSideWidget extends StaticClientSideElement implements ClientSideWidget
029{
030    /** The parameter in the configuration for ftype. Comma-separated list. Defaut value is 'string'. */
031    public static final String PARAMETER_FTYPES = "ftypes";
032    /** The parameter in the configuration for supporting enumarated. Default value is false. */
033    public static final String PARAMETER_SUPPORTS_ENUMERATED = "supports-enumerated";
034    /** The parameter in the configuration for supporting non-enumarated. Default value is true. */
035    public static final String PARAMETER_SUPPORTS_NONENUMERATED = "supports-non-enumerated";
036    /** The parameter in the configuration for supporting multiple. Default value is false. */
037    public static final String PARAMETER_SUPPORTS_MULTIPLE = "supports-multiple";
038    /** The parameter in the configuration for supporting non-multiple. Default value is true. */
039    public static final String PARAMETER_SUPPORTS_NONMULTIPLE = "supports-non-multiple";
040    
041    @Override
042    public String[] getFormTypes(Map<String, Object> contextParameters)
043    {
044        Map<String, Object> initialParameters = _script.getParameters();
045        if (initialParameters.containsKey(PARAMETER_FTYPES))
046        {
047            return StringUtils.split((String) initialParameters.get(PARAMETER_FTYPES), ",");
048        }
049        else
050        {
051            return new String[] {"string"}; 
052        }
053    }
054    
055    @Override
056    public boolean supportsEnumerated(Map<String, Object> contextParameters)
057    {
058        Map<String, Object> initialParameters = _script.getParameters();
059        if (initialParameters.containsKey(PARAMETER_SUPPORTS_ENUMERATED))
060        {
061            return Boolean.parseBoolean((String) initialParameters.get(PARAMETER_SUPPORTS_ENUMERATED));
062        }
063        else
064        {
065            return false;
066        }
067    }
068    
069    @Override
070    public boolean supportsNonEnumerated(Map<String, Object> contextParameters)
071    {
072        Map<String, Object> initialParameters = _script.getParameters();
073        if (initialParameters.containsKey(PARAMETER_SUPPORTS_NONENUMERATED))
074        {
075            return Boolean.parseBoolean((String) initialParameters.get(PARAMETER_SUPPORTS_NONENUMERATED));
076        }
077        else
078        {
079            return true;
080        }
081    }
082    
083    @Override
084    public boolean supportsMultiple(Map<String, Object> contextParameters)
085    {
086        Map<String, Object> initialParameters = _script.getParameters();
087        if (initialParameters.containsKey(PARAMETER_SUPPORTS_MULTIPLE))
088        {
089            return Boolean.parseBoolean((String) initialParameters.get(PARAMETER_SUPPORTS_MULTIPLE));
090        }
091        else
092        {
093            return false;
094        }
095    }
096    
097    @Override
098    public boolean supportsNonMultiple(Map<String, Object> contextParameters)
099    {
100        Map<String, Object> initialParameters = _script.getParameters();
101        if (initialParameters.containsKey(PARAMETER_SUPPORTS_NONMULTIPLE))
102        {
103            return Boolean.parseBoolean((String) initialParameters.get(PARAMETER_SUPPORTS_NONMULTIPLE));
104        }
105        else
106        {
107            return true;
108        }
109    }
110}