001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     troger
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.ui.web.htmleditor.service;
021
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.common.utils.StringUtils;
028import org.nuxeo.ecm.platform.ui.web.htmleditor.api.HtmlEditorPluginService;
029import org.nuxeo.runtime.model.ComponentContext;
030import org.nuxeo.runtime.model.ComponentInstance;
031import org.nuxeo.runtime.model.DefaultComponent;
032
033/**
034 * Service used to register plugins for TinyMCE.
035 *
036 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
037 */
038public class HtmlEditorPluginServiceImpl extends DefaultComponent implements HtmlEditorPluginService {
039
040    public static final String PLUGINS_EXTENSION_POINT = "plugins";
041
042    private Map<String, HtmlEditorPluginDescriptor> pluginsDescriptors;
043
044    @Override
045    public void activate(ComponentContext context) {
046        pluginsDescriptors = new HashMap<String, HtmlEditorPluginDescriptor>();
047    }
048
049    @Override
050    public void deactivate(ComponentContext context) {
051        pluginsDescriptors = null;
052    }
053
054    @Override
055    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
056        if (PLUGINS_EXTENSION_POINT.equals(extensionPoint)) {
057            final HtmlEditorPluginDescriptor descriptor = (HtmlEditorPluginDescriptor) contribution;
058            if (descriptor.getRemove() && pluginsDescriptors.containsKey(descriptor.getPluginName())) {
059                pluginsDescriptors.remove(descriptor.getPluginName());
060            } else {
061                pluginsDescriptors.put(descriptor.getPluginName(), descriptor);
062            }
063        }
064    }
065
066    @Override
067    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
068        if (PLUGINS_EXTENSION_POINT.equals(extensionPoint)) {
069            final HtmlEditorPluginDescriptor descriptor = (HtmlEditorPluginDescriptor) contribution;
070            pluginsDescriptors.remove(descriptor.getPluginName());
071        }
072    }
073
074    @Override
075    public List<String> getPluginsName() {
076        return new ArrayList<String>(pluginsDescriptors.keySet());
077    }
078
079    @Override
080    public String getFormattedPluginsNames() {
081        return StringUtils.join(getPluginsName(), ',');
082    }
083
084    public List<String> getToolbarsButtonsNames() {
085        return new ArrayList<String>(pluginsDescriptors.keySet());
086    }
087
088    @Override
089    public String getFormattedToolbarsButtonsNames() {
090        return StringUtils.join(getToolbarsButtonsNames(), ',');
091    }
092
093    @Override
094    public Map<String, String> getToolbarsButtons() {
095        final Map<String, String> result = new HashMap<String, String>();
096        final Map<String, List<String>> temp = new HashMap<String, List<String>>();
097
098        for (final HtmlEditorPluginDescriptor descriptor : pluginsDescriptors.values()) {
099            List<String> buttonsList = temp.get(descriptor.getToolbarName());
100            if (buttonsList == null) {
101                buttonsList = new ArrayList<String>();
102            }
103            buttonsList.add(descriptor.getPluginButtonName());
104            temp.put(descriptor.getToolbarName(), buttonsList);
105        }
106
107        for (final Map.Entry<String, List<String>> entry : temp.entrySet()) {
108            result.put(entry.getKey(), StringUtils.join(entry.getValue(), ','));
109        }
110        return result;
111    }
112
113}