001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.widgets;
029
030import org.opencms.ade.configuration.CmsADEConfigData;
031import org.opencms.ade.configuration.CmsConfigurationReader;
032import org.opencms.file.CmsObject;
033import org.opencms.util.CmsUUID;
034import org.opencms.xml.containerpage.CmsFunctionFormatterBean;
035import org.opencms.xml.containerpage.CmsSchemaFormatterBeanWrapper;
036import org.opencms.xml.containerpage.I_CmsFormatterBean;
037import org.opencms.xml.content.CmsXmlContent;
038import org.opencms.xml.content.CmsXmlContentRootLocation;
039
040import java.util.Collections;
041import java.util.List;
042import java.util.Locale;
043import java.util.Map;
044import java.util.Set;
045
046import com.google.common.collect.Lists;
047
048/**
049 * Widget used to select a formatter to remove.<p>
050 *
051 * Please note that this widget assumes the resource being edited is a sitemap configuration, and will not work correctly in a different context.
052 */
053public class CmsRemoveFormatterWidget extends A_CmsFormatterWidget {
054
055    /**
056     * Default constructor.<p>
057     */
058    public CmsRemoveFormatterWidget() {
059
060        super();
061    }
062
063    /**
064     * Constructor with a configuration parameter.<p>
065     * @param config the configuration string
066     */
067    public CmsRemoveFormatterWidget(String config) {
068
069        super();
070    }
071
072    /**
073     * @see org.opencms.widgets.I_CmsADEWidget#getCssResourceLinks(org.opencms.file.CmsObject)
074     */
075    @Override
076    public List<String> getCssResourceLinks(CmsObject cms) {
077
078        return null;
079    }
080
081    /**
082     * @see org.opencms.widgets.I_CmsADEWidget#getInitCall()
083     */
084    @Override
085    public String getInitCall() {
086
087        return null;
088    }
089
090    /**
091     * @see org.opencms.widgets.I_CmsADEWidget#getJavaScriptResourceLinks(org.opencms.file.CmsObject)
092     */
093    @Override
094    public List<String> getJavaScriptResourceLinks(CmsObject cms) {
095
096        return null;
097    }
098
099    /**
100     * @see org.opencms.widgets.I_CmsADEWidget#isInternal()
101     */
102    @Override
103    public boolean isInternal() {
104
105        return true;
106    }
107
108    /**
109     * @see org.opencms.widgets.CmsSelectWidget#newInstance()
110     */
111    @Override
112    public I_CmsWidget newInstance() {
113
114        return new CmsRemoveFormatterWidget();
115    }
116
117    /**
118     * @see org.opencms.widgets.A_CmsFormatterWidget#getFormatterOptions(org.opencms.file.CmsObject, org.opencms.ade.configuration.CmsADEConfigData, java.lang.String, boolean)
119     */
120    @Override
121    protected List<CmsSelectWidgetOption> getFormatterOptions(
122        CmsObject cms,
123        CmsADEConfigData config,
124        String rootPath,
125        boolean allRemoved) {
126
127        List<CmsSelectWidgetOption> result = Lists.newArrayList();
128        if (!allRemoved) {
129            Map<CmsUUID, I_CmsFormatterBean> activeFormatters = config.getActiveFormatters();
130            List<I_CmsFormatterBean> formatters = Lists.newArrayList(activeFormatters.values());
131            Collections.sort(formatters, new A_CmsFormatterWidget.FormatterSelectComparator());
132            for (I_CmsFormatterBean formatterBean : formatters) {
133                if ((formatterBean instanceof CmsFunctionFormatterBean)
134                    || (formatterBean instanceof CmsSchemaFormatterBeanWrapper)) {
135                    continue;
136                }
137                try {
138                    CmsSelectWidgetOption option = getWidgetOptionForFormatter(cms, formatterBean);
139                    result.add(option);
140                } catch (Exception e) {
141                    LOG.error(e.getLocalizedMessage(), e);
142                }
143            }
144        }
145        return result;
146    }
147
148    /**
149     * @see org.opencms.widgets.A_CmsFormatterWidget#getSelectedInFile(org.opencms.ade.configuration.CmsConfigurationReader, org.opencms.xml.content.CmsXmlContent)
150     */
151    @Override
152    protected Set<String> getSelectedInFile(CmsConfigurationReader reader, CmsXmlContent content) {
153
154        CmsXmlContentRootLocation root = new CmsXmlContentRootLocation(content, Locale.ENGLISH);
155        Set<String> addFormatters = reader.parseRemoveFormatters(root);
156        return addFormatters;
157    }
158
159    /**
160     * @see org.opencms.widgets.A_CmsFormatterWidget#getTypeOptions(org.opencms.file.CmsObject, org.opencms.ade.configuration.CmsADEConfigData, boolean)
161     */
162    @Override
163    protected List<CmsSelectWidgetOption> getTypeOptions(
164        CmsObject cms,
165        CmsADEConfigData adeConfig,
166        boolean allRemoved) {
167
168        List<CmsSelectWidgetOption> result = Lists.newArrayList();
169        if (!allRemoved) {
170            Set<String> activeTypes = adeConfig.getTypesWithActiveSchemaFormatters();
171            for (String activeType : activeTypes) {
172                CmsSelectWidgetOption option = getWidgetOptionForType(cms, activeType);
173                result.add(option);
174            }
175        }
176        return result;
177    }
178
179}