001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt.client.ui.input.form;
029
030import org.opencms.gwt.client.ui.input.CmsTextBox;
031import org.opencms.gwt.client.ui.input.I_CmsFormWidget;
032
033import java.util.HashMap;
034import java.util.Map;
035
036import com.google.common.base.Optional;
037
038/**
039 * This singleton class is used for registering all widget factories.<p>
040 *
041 *  @since 8.0.0
042 *
043 */
044public final class CmsWidgetFactoryRegistry implements I_CmsFormWidgetMultiFactory {
045
046    /** The singleton instance. */
047    private static CmsWidgetFactoryRegistry instance;
048
049    /** The widget factories for each widget type string. */
050    private Map<String, I_CmsFormWidgetFactory> m_widgetFactories = new HashMap<String, I_CmsFormWidgetFactory>();
051
052    /**
053     * The hidden default constructor.<p>
054     */
055    private CmsWidgetFactoryRegistry() {
056
057        // do nothing
058    }
059
060    /**
061     * Returns the singleton instance of this class.<p>
062     *
063     * @return the singleton instance of this class
064     */
065    public static CmsWidgetFactoryRegistry instance() {
066
067        if (instance == null) {
068            instance = new CmsWidgetFactoryRegistry();
069        }
070        return instance;
071    }
072
073    /**
074     * Creates a new widget based on a type string and widget parameters.<p>
075     *
076     * @param key the type string
077     * @param widgetParams the widget configuration parameters
078     *
079     * @return the newly created widget
080     */
081    public I_CmsFormWidget createFormWidget(
082        String key,
083        Map<String, String> widgetParams,
084        Optional<String> defaultValue) {
085
086        if (key == null) {
087            key = "";
088        }
089        I_CmsFormWidgetFactory factory = m_widgetFactories.get(key);
090        if (factory == null) {
091            // if the given widget type is not found, fall back to text box widget
092            factory = m_widgetFactories.get(CmsTextBox.WIDGET_TYPE);
093        }
094        return factory.createWidget(widgetParams, defaultValue);
095
096    }
097
098    /**
099     * Registers a new widget factory for a given widget type key.<p>
100     *
101     * @param key the widget type key
102     *
103     * @param factory the new factory for the key
104     */
105    public void registerFactory(String key, I_CmsFormWidgetFactory factory) {
106
107        m_widgetFactories.put(key, factory);
108    }
109
110}