001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     troger
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.ui.web.htmleditor.service;
023
024import java.util.ArrayList;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import org.nuxeo.common.utils.StringUtils;
030import org.nuxeo.ecm.platform.ui.web.htmleditor.api.HtmlEditorPluginService;
031import org.nuxeo.runtime.model.ComponentContext;
032import org.nuxeo.runtime.model.ComponentInstance;
033import org.nuxeo.runtime.model.DefaultComponent;
034
035/**
036 * Service used to register plugins for TinyMCE.
037 *
038 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
039 */
040public class HtmlEditorPluginServiceImpl extends DefaultComponent implements HtmlEditorPluginService {
041
042    public static final String PLUGINS_EXTENSION_POINT = "plugins";
043
044    private Map<String, HtmlEditorPluginDescriptor> pluginsDescriptors;
045
046    @Override
047    public void activate(ComponentContext context) {
048        pluginsDescriptors = new HashMap<String, HtmlEditorPluginDescriptor>();
049    }
050
051    @Override
052    public void deactivate(ComponentContext context) {
053        pluginsDescriptors = null;
054    }
055
056    @Override
057    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
058        if (PLUGINS_EXTENSION_POINT.equals(extensionPoint)) {
059            final HtmlEditorPluginDescriptor descriptor = (HtmlEditorPluginDescriptor) contribution;
060            if (descriptor.getRemove() && pluginsDescriptors.containsKey(descriptor.getPluginName())) {
061                pluginsDescriptors.remove(descriptor.getPluginName());
062            } else {
063                pluginsDescriptors.put(descriptor.getPluginName(), descriptor);
064            }
065        }
066    }
067
068    @Override
069    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
070        if (PLUGINS_EXTENSION_POINT.equals(extensionPoint)) {
071            final HtmlEditorPluginDescriptor descriptor = (HtmlEditorPluginDescriptor) contribution;
072            pluginsDescriptors.remove(descriptor.getPluginName());
073        }
074    }
075
076    @Override
077    public List<String> getPluginsName() {
078        return new ArrayList<String>(pluginsDescriptors.keySet());
079    }
080
081    @Override
082    public String getFormattedPluginsNames() {
083        return StringUtils.join(getPluginsName(), ',');
084    }
085
086    public List<String> getToolbarsButtonsNames() {
087        return new ArrayList<String>(pluginsDescriptors.keySet());
088    }
089
090    @Override
091    public String getFormattedToolbarsButtonsNames() {
092        return StringUtils.join(getToolbarsButtonsNames(), ',');
093    }
094
095    @Override
096    public Map<String, String> getToolbarsButtons() {
097        final Map<String, String> result = new HashMap<String, String>();
098        final Map<String, List<String>> temp = new HashMap<String, List<String>>();
099
100        for (final HtmlEditorPluginDescriptor descriptor : pluginsDescriptors.values()) {
101            List<String> buttonsList = temp.get(descriptor.getToolbarName());
102            if (buttonsList == null) {
103                buttonsList = new ArrayList<String>();
104            }
105            buttonsList.add(descriptor.getPluginButtonName());
106            temp.put(descriptor.getToolbarName(), buttonsList);
107        }
108
109        for (final Map.Entry<String, List<String>> entry : temp.entrySet()) {
110            result.put(entry.getKey(), StringUtils.join(entry.getValue(), ','));
111        }
112        return result;
113    }
114
115}