001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.ade.galleries.shared;
029
030import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryTabId;
031import org.opencms.util.CmsStringUtil;
032
033import java.util.ArrayList;
034import java.util.Collections;
035import java.util.HashMap;
036import java.util.LinkedHashMap;
037import java.util.List;
038import java.util.Map;
039
040import com.google.gwt.user.client.rpc.IsSerializable;
041
042/**
043 * A bean containing a configuration for the gallery dialog's available tabs,
044 * consisting of a list of tabs and a default tab to display first.
045 */
046public class CmsGalleryTabConfiguration implements IsSerializable {
047
048    /** The map containing the predefined tab configurations. */
049    public static final Map<String, CmsGalleryTabConfiguration> DEFAULT_CONFIGURATIONS;
050
051    /** Gallery configuration id. */
052    public static final String TC_ADE_ADD = "adeAdd";
053
054    /** Gallery configuration id. */
055    public static final String TC_FOLDERS = "folders";
056
057    /** Gallery configuration id. */
058    public static final String TC_GALLERIES = "galleries";
059
060    /** Gallery configuration id. */
061    public static final String TC_SELECT_ALL = "selectAll";
062
063    /** Gallery configuration id. */
064    public static final String TC_SELECT_DOC = "selectDoc";
065
066    /** Gallery confiugration id. */
067    public static final String TC_SELECT_ALL_NO_SITEMAP = "selectAllNoSitemap";
068
069    static {
070        Map<String, CmsGalleryTabConfiguration> defaultConfigs = new HashMap<String, CmsGalleryTabConfiguration>();
071        defaultConfigs.put(TC_SELECT_ALL, parse("*sitemap,types,galleries,categories,vfstree,search,results"));
072        defaultConfigs.put(TC_SELECT_ALL_NO_SITEMAP, parse("*types,galleries,categories,vfstree,search,results"));
073        defaultConfigs.put(TC_SELECT_DOC, parse("types,*galleries,categories,vfstree,search,results"));
074        defaultConfigs.put(TC_ADE_ADD, parse("*types,galleries,categories,vfstree,search,results"));
075        defaultConfigs.put(TC_FOLDERS, parse("*vfstree"));
076        defaultConfigs.put(TC_GALLERIES, parse("*galleries,vfstree,results"));
077        DEFAULT_CONFIGURATIONS = Collections.unmodifiableMap(defaultConfigs);
078    }
079
080    /** The id of the default tab. */
081    protected GalleryTabId m_defaultTab;
082
083    /** The list of tab ids. */
084    private List<GalleryTabId> m_tabs;
085
086    /**
087     * Creates  a new gallery tab configuration based on a list of tabs.<p>
088     *
089     * @param tabsList the list of tabs
090     */
091    public CmsGalleryTabConfiguration(List<GalleryTabId> tabsList) {
092
093        m_tabs = tabsList;
094    }
095
096    /**
097     * Default constructor for serialization.<p>
098     */
099    protected CmsGalleryTabConfiguration() {
100
101        // only used for serialization
102    }
103
104    /**
105     * Gets the default tab configuration.<p>
106     *
107     * @return the default tab configuration
108     */
109    public static CmsGalleryTabConfiguration getDefault() {
110
111        return resolve(TC_SELECT_ALL);
112    }
113
114    /**
115     * Creates a gallery tab configuration from a configuration string.<p>
116     *
117     * The string should consist of a comma-separated list of tab ids, omitting the cms_tab_ prefix of the corresponding enum values.
118     * The tab which should be used as a default tab should be prefixed with an asterisk '*'.
119     *
120     * @param configStr the configuration string
121     *
122     * @return the parsed tab configuration
123     */
124    public static CmsGalleryTabConfiguration parse(String configStr) {
125
126        String[] tokens = configStr.split(" *, *");
127        GalleryTabId defaultTabId = null;
128        // use LinkedHashMap to preserve both order and uniqueness of tabs
129        LinkedHashMap<GalleryTabId, Object> tabs = new LinkedHashMap<GalleryTabId, Object>();
130        for (String token : tokens) {
131            token = token.trim();
132            boolean isDefault = false;
133            if (token.startsWith("*")) {
134                token = token.substring(1).trim();
135                isDefault = true;
136            }
137            GalleryTabId tab = parseTabId(token);
138            if (tab != null) {
139                tabs.put(tab, null);
140                if (isDefault && (defaultTabId == null)) {
141                    defaultTabId = tab;
142                }
143            }
144        }
145        List<GalleryTabId> tabsList = new ArrayList<GalleryTabId>();
146        for (GalleryTabId tab : tabs.keySet()) {
147            tabsList.add(tab);
148        }
149        if (defaultTabId == null) {
150            defaultTabId = tabsList.get(0);
151        }
152        return new CmsGalleryTabConfiguration(tabsList).withDefault(defaultTabId);
153
154    }
155
156    /**
157     * Parses a tab id from a gallery configuration string.<p>
158     *
159     * @param tabId the tab id to parse
160     *
161     * @return the gallery tab id enum value
162     */
163    public static GalleryTabId parseTabId(String tabId) {
164
165        try {
166            return GalleryTabId.valueOf("cms_tab_" + tabId);
167        } catch (Throwable e) {
168            return null;
169        }
170
171    }
172
173    /**
174     * Given a string which is either the name of a predefined tab configuration or a configuration string, returns
175     * the corresponding tab configuration.
176     *
177     * @param configStr a configuration string or predefined configuration name
178     *
179     * @return the gallery tab configuration
180     */
181    public static CmsGalleryTabConfiguration resolve(String configStr) {
182
183        CmsGalleryTabConfiguration tabConfig;
184        if (CmsStringUtil.isEmptyOrWhitespaceOnly(configStr)) {
185            configStr = "*sitemap,types,galleries,categories,vfstree,search,results";
186        }
187        if (DEFAULT_CONFIGURATIONS != null) {
188            tabConfig = DEFAULT_CONFIGURATIONS.get(configStr);
189            if (tabConfig != null) {
190                return tabConfig;
191            }
192
193        }
194        return parse(configStr);
195    }
196
197    /**
198     * Gets the default tab.<p>
199     *
200     * @return the default tab
201     */
202    public GalleryTabId getDefaultTab() {
203
204        return m_defaultTab;
205    }
206
207    /**
208     * Gets the list of tabs.<p>
209     *
210     * @return the list of tabs
211     */
212    public List<GalleryTabId> getTabs() {
213
214        return Collections.unmodifiableList(m_tabs);
215
216    }
217
218    /**
219     * Creates a new tab configuration based on this one, but changes its default tab.<P>
220     *
221     * @param defaultTab the new default tab
222     * @return the copy with the changed default tab
223     */
224    public CmsGalleryTabConfiguration withDefault(GalleryTabId defaultTab) {
225
226        CmsGalleryTabConfiguration result = new CmsGalleryTabConfiguration(m_tabs);
227        result.m_defaultTab = defaultTab;
228        return result;
229    }
230
231}