org.opencms.widgets.A_CmsNativeComplexWidget Maven / Gradle / Ivy
Show all versions of opencms-test Show documentation
/*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (C) Alkacon Software (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.widgets;
import org.opencms.acacia.shared.CmsContentDefinition;
import org.opencms.ade.contenteditor.shared.CmsComplexWidgetData;
import org.opencms.ade.contenteditor.shared.CmsExternalWidgetConfiguration;
import org.opencms.file.CmsObject;
import org.opencms.json.JSONObject;
import org.opencms.util.CmsStringUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* This is an abstract class which you can inherit from to relatively easily implement complex widgets for rendering nested contents in Javascript.
*
* Subclasses of this class only need to implement the methods getScripts(), getStyleSheets(), getName(), and configure().
*
* The name of the initialization function is generated by prepending the string "init_" to the widget name returned by getName().
*/
public abstract class A_CmsNativeComplexWidget implements I_CmsComplexWidget {
/** The prefix which is prepended to the widget name to get the name of the initialization function. */
public static final String INIT_FUNCTION_PREFIX = "init_";
/** The configuration string. */
protected String m_configuration;
/** The configuration map created from the configuration string. */
protected Map m_configurationMap;
/** The configuration map in JSON format. */
protected JSONObject m_jsonConfig;
/**
* @see org.opencms.widgets.I_CmsComplexWidget#getName()
*
* The result of this method will be used to determine the initialization function name for the widget by prepending
* the prefix "init_". For example, if "foo" is returned by this method, "init_foo" will be used as the initialization function
* name.
*/
public abstract String getName();
/**
* Gets the list of URLs of scripts which the widget reuires.
*
* @param cms the current CMS context
*
* @return the list of URLs of scripts needed by the widget
*/
public abstract List getScripts(CmsObject cms);
/**
* Gets the list of URLs of stylesheets which are required by the widget.
*
* @param cms the current CMS context
*
* @return the list of URLs of stylesheets needed by the widget
*/
public abstract List getStyleSheets(CmsObject cms);
/**
* @see org.opencms.widgets.I_CmsComplexWidget#getWidgetData(org.opencms.file.CmsObject)
*/
public final CmsComplexWidgetData getWidgetData(CmsObject cms) {
List scripts = new ArrayList(getScripts(cms));
List styles = new ArrayList(getStyleSheets(cms));
String init = m_configurationMap.get(CmsContentDefinition.PARAM_INIT_CALL);
CmsExternalWidgetConfiguration config = new CmsExternalWidgetConfiguration(getName(), init, scripts, styles);
return new CmsComplexWidgetData(CmsContentDefinition.NATIVE_RENDERER, m_jsonConfig.toString(), config);
}
/**
* Initializes the configuration date from the given configuration string.
*
* This should be called by subclasses of this class.
*
* @param config the widget configuration string
*/
public final void initConfiguration(String config) {
m_configuration = config;
m_configurationMap = CmsStringUtil.splitAsMap(config, "|", ":");
m_configurationMap.put(CmsContentDefinition.PARAM_INIT_CALL, INIT_FUNCTION_PREFIX + getName());
m_jsonConfig = new JSONObject(new HashMap(m_configurationMap));
}
}