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.workplace.editors;
029
030import org.opencms.util.CmsStringUtil;
031
032import java.util.ArrayList;
033import java.util.HashSet;
034import java.util.List;
035import java.util.Map;
036import java.util.Set;
037
038/**
039 * This class is used to help constructing the TinyMCE toolbar configuration, both for client-side and server-side code.<p>
040 */
041public final class CmsTinyMceToolbarHelper {
042
043    /** The translation of the generic widget button names to TinyMCE specific button names. */
044    public static final String BUTTON_TRANSLATION =
045    /* Row 1*/
046    "|newdocument:newdocument|bold:bold|italic:italic|underline:underline|strikethrough:strikethrough|alignleft:alignleft"
047        + "|aligncenter:aligncenter|alignright:alignright|justify:alignjustify|style:styleselect|formatselect:formatselect"
048        + "|fontselect:fontselect|fontsizeselect:fontsizeselect"
049        /* Row 2*/
050        + "|cut:cut|copy:copy|paste:paste|pastetext:pastetext|find:searchreplace|replace:searchreplace|unorderedlist:bullist"
051        + "|orderedlist:numlist|outdent:outdent|indent:indent|blockquote:blockquote|undo:undo|redo:redo|editorlink:link|unlink:unlink"
052        + "|anchor:anchor|image:image|cleanup:cleanup|source:code|insertdate:insertdate|inserttime:inserttime|forecolor:forecolor|backcolor:backcolor"
053        /* Row 3*/
054        + "|table:table|hr:hr|removeformat:removeformat|visualaid:visualaid|subscript:subscript|superscript:superscript|specialchar:charmap"
055        + "|emotions:emoticons|spellcheck:spellchecker|media:media|print:print|ltr:ltr|rtl:rtl|fitwindow:fullscreen"
056        /* Row 4*/
057        + "|insertlayer:insertlayer|moveforward:moveforward|movebackward:movebackward|absolute:absolute|styleprops:styleprops|cite:cite"
058        + "|abbr:abbr|acronym:acronym|del:del|ins:ins|attribs:attribs|visualchars:visualchars|nonbreaking:nonbreaking|template:template"
059        + "|pagebreak:pagebreak|selectall:selectall|fullpage:fullpage|imagegallery:OcmsImageGallery|downloadgallery:OcmsDownloadGallery"
060        + "|linkgallery:OcmsLinkGallery|link:link|typography:typograf";
061
062    /** The map containing the translation of the generic widget button names to TinyMCE specific button names. */
063    public static final Map<String, String> BUTTON_TRANSLATION_MAP = CmsStringUtil.splitAsMap(
064        BUTTON_TRANSLATION,
065        "|",
066        ":");
067
068    /**
069     * Hidden constructor.<p>
070     */
071    private CmsTinyMceToolbarHelper() {
072
073    }
074
075    /**
076     * Helper method to generate a TinyMCE-specific toolbar configuration string from a list of generic toolbar button names.<p>
077     *
078     * @param barItems the generic toolbar items
079     *
080     * @return the TinyMCE toolbar configuration string
081     */
082    public static String createTinyMceToolbarStringFromGenericToolbarItems(List<String> barItems) {
083
084        List<List<String>> blocks = new ArrayList<List<String>>();
085        blocks.add(new ArrayList<String>());
086        String lastItem = null;
087        List<String> processedItems = new ArrayList<String>();
088
089        // translate buttons and eliminate adjacent separators
090        for (String barItem : barItems) {
091            String translated = CmsTinyMceToolbarHelper.translateButton(barItem);
092            if (translated != null) {
093                barItem = translated;
094            }
095            if (barItem.equals("[") || barItem.equals("]") || barItem.equals("-")) {
096                barItem = "|";
097                if ("|".equals(lastItem)) {
098                    continue;
099                }
100            }
101            if (barItem.indexOf(",") > -1) {
102                for (String subItem : barItem.split(",")) {
103                    processedItems.add(subItem);
104                }
105            } else {
106                processedItems.add(barItem);
107            }
108            lastItem = barItem;
109        }
110
111        // remove leading or trailing '|'
112        if ((processedItems.size() > 0) && processedItems.get(0).equals("|")) {
113            processedItems.remove(0);
114        }
115
116        if ((processedItems.size() > 0) && processedItems.get(processedItems.size() - 1).equals("|")) {
117            processedItems.remove(processedItems.size() - 1);
118        }
119        Set<String> writtenItems = new HashSet<String>();
120
121        // transform flat list into list of groups
122        for (String processedItem : processedItems) {
123            if (!writtenItems.contains(processedItem)) {
124                blocks.get(blocks.size() - 1).add(processedItem);
125            }
126            if ("|".equals(processedItem)) {
127                blocks.add(new ArrayList<String>());
128            } else {
129                writtenItems.add(processedItem);
130            }
131        }
132
133        // produce the TinyMCE toolbar options from the groups
134        // we use TinyMCE's button rows as groups instead of rows and fix the layout using CSS.
135        // This is because we want the button bars to wrap automatically when there is not enough space.
136        // Using this method, the wraps can only occur between different blocks/rows.
137        String toolbar = "";
138        for (List<String> block : blocks) {
139            toolbar += CmsStringUtil.listAsString(block, " ") + " ";
140        }
141        return toolbar;
142    }
143
144    /**
145     * Returns the context menu entries according to the configured tool-bar items.<p>
146     *
147     * @param barItems the tool-bar items
148     *
149     * @return the context menu entries
150     */
151    public static String getContextMenuEntries(List<String> barItems) {
152
153        String result = "";
154        if (barItems.contains("link")) {
155            result += translateButton("link");
156        }
157        if (barItems.contains("downloadgallery")) {
158            result += " " + translateButton("downloadgallery");
159        }
160        if (barItems.contains("imagegallery")) {
161            result += " " + translateButton("imagegallery");
162        }
163        if (barItems.contains("table")) {
164            result += " inserttable | cell row column deletetable";
165        }
166        return result.trim();
167    }
168
169    /**
170     * Translates a generic button name to a TinyMCE-specific button name (or a comma-separated list of button names).<p>
171     *
172     * @param cmsButtonName the generic button name
173     * @return the TinyMCE button name(s)
174     */
175    public static String translateButton(String cmsButtonName) {
176
177        return BUTTON_TRANSLATION_MAP.get(cmsButtonName);
178
179    }
180
181}