001/*
002 * (C) Copyright 2006-2016 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 */
021package org.nuxeo.ecm.platform.ui.web.htmleditor.service;
022
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
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<>();
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.isRemove() && 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<>(pluginsDescriptors.keySet());
077    }
078
079    @Override
080    public String getFormattedPluginsNames() {
081        return String.join(",", getPluginsName());
082    }
083
084    public List<String> getToolbarsButtonsNames() {
085        return new ArrayList<>(pluginsDescriptors.keySet());
086    }
087
088    @Override
089    public String getFormattedToolbarsButtonsNames() {
090        return String.join(",", getToolbarsButtonsNames());
091    }
092
093    @Override
094    public Map<String, String> getToolbarsButtons() {
095        final Map<String, String> result = new HashMap<>();
096        final Map<String, List<String>> temp = new HashMap<>();
097
098        for (final HtmlEditorPluginDescriptor descriptor : pluginsDescriptors.values()) {
099            temp.computeIfAbsent(descriptor.getToolbarName(), key -> new ArrayList<>())
100                .add(descriptor.getPluginButtonName());
101        }
102
103        for (final Map.Entry<String, List<String>> entry : temp.entrySet()) {
104            result.put(entry.getKey(), String.join(",", entry.getValue()));
105        }
106        return result;
107    }
108
109}