content: /*
   * This library is part of OpenCms -
   * the Open Source Content Management System
   *
   * Copyright (C) Alkacon Software (https://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: https://www.alkacon.com
   *
   * For further information about OpenCms, please see the
   * project website: https://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.<p>
   *
   * 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<String, String> 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.<p>
       *
       * @param cms the current CMS context
       *
       * @return the list of URLs of scripts needed by the widget
       */
      public abstract List<String> getScripts(CmsObject cms);
  
      /**
       * Gets the list of URLs of stylesheets which are required by the widget.<p>
       *
       * @param cms the current CMS context
       *
       * @return the list of URLs of stylesheets needed by the widget
       */
      public abstract List<String> getStyleSheets(CmsObject cms);
  
      /**
       * @see org.opencms.widgets.I_CmsComplexWidget#getWidgetData(org.opencms.file.CmsObject)
       */
      public final CmsComplexWidgetData getWidgetData(CmsObject cms) {
  
          List<String> scripts = new ArrayList<String>(getScripts(cms));
          List<String> styles = new ArrayList<String>(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.<p>
       *
       * This should be called by subclasses of this class.<p>
       *
       * @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<String, Object>(m_configurationMap));
      }
  
  }
  