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.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configurable;
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024
025import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
026
027/**
028 * This extension point handle the existing widgets on the client side (widgets are fields for cms forms).
029 */
030public class WidgetsManager extends AbstractThreadSafeComponentExtensionPoint<ClientSideWidget> implements Configurable
031{
032    /** Avalon role */
033    public static final String ROLE = WidgetsManager.class.getName();
034    
035    /** The configuration key for normal mode */
036    public static final String MODE_CONFIG_NORMAL = "normal";
037    /** The configuration key for enumerated mode */
038    public static final String MODE_CONFIG_ENUMERATED = "enumerated";
039    
040    /** The configuration key for single sub mode */
041    public static final String MODE_CONFIG_SINGLE = "single";
042    /** The configuration key for multiple sub mode */
043    public static final String MODE_CONFIG_MULTIPLE = "multiple";
044    
045
046    /** The output key for normal mode */
047    protected static final String OUTPUT_NORMAL = "non-enumeration";
048    /** The output key for enumerated mode */
049    protected static final String OUTPUT_ENUMERATED = "enumeration";
050    
051    /** The output key for single sub mode */
052    protected static final String OUTPUT_SINGLE = "single";
053    /** The output key for multiple sub mode */
054    protected static final String OUTPUT_MULTIPLE = "multiple";
055    
056
057    /** The default widgets map. See getDefaultWidgets */
058    protected Map<String, Map<String, Map<String, String>>> _defaultWidgets;
059    
060    @Override
061    public void configure(Configuration configuration) throws ConfigurationException
062    {
063        // Preparing
064        _defaultWidgets = new HashMap<>();
065        _defaultWidgets.put(OUTPUT_NORMAL, new HashMap<String, Map<String, String>>());
066        _defaultWidgets.get(OUTPUT_NORMAL).put(OUTPUT_SINGLE, _readMap(configuration.getChild(MODE_CONFIG_NORMAL).getChild(MODE_CONFIG_SINGLE).getChildren()));
067        _defaultWidgets.get(OUTPUT_NORMAL).put(OUTPUT_MULTIPLE, _readMap(configuration.getChild(MODE_CONFIG_NORMAL).getChild(MODE_CONFIG_MULTIPLE).getChildren()));
068        _defaultWidgets.put(OUTPUT_ENUMERATED, new HashMap<String, Map<String, String>>());
069        _defaultWidgets.get(OUTPUT_ENUMERATED).put(OUTPUT_SINGLE, _readMap(configuration.getChild(MODE_CONFIG_ENUMERATED).getChild(MODE_CONFIG_SINGLE).getChildren()));
070        _defaultWidgets.get(OUTPUT_ENUMERATED).put(OUTPUT_MULTIPLE, _readMap(configuration.getChild(MODE_CONFIG_ENUMERATED).getChild(MODE_CONFIG_SINGLE).getChildren()));
071        
072    }
073    
074    private Map<String, String> _readMap(Configuration[] children)
075    {
076        Map<String, String> result = new HashMap<>();
077        
078        for (Configuration child : children)
079        {
080            result.put(child.getName(), child.getValue(""));
081        }
082        
083        return result;
084    }
085
086    /**
087     * Get the default widgets
088     * normal/enumerated &lt;-&gt; (single/multiple &lt;-&gt; ftype &lt;-&gt; xtype)
089     * @return The map of default widgets
090     */
091    public Map<String, Map<String, Map<String, String>>> getDefaultWidgets()
092    {
093        return _defaultWidgets;
094    }
095}