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.gwt.shared;
029
030import org.opencms.util.CmsDefaultSet;
031import org.opencms.xml.content.CmsXmlContentProperty;
032
033import java.util.LinkedHashMap;
034import java.util.Map;
035
036import com.google.gwt.user.client.rpc.IsSerializable;
037
038/**
039 * Client-compatible bean with information about the current template context.<p>
040 */
041public class CmsTemplateContextInfo implements IsSerializable {
042
043    /** Dummy element marker class. */
044    public static final String DUMMY_ELEMENT_MARKER = "cmsTemplateContextDummyMarker";
045
046    /** The constant used for empty setting. */
047    public static final String EMPTY_VALUE = "none";
048
049    /** The setting name used for storing the compatible template contexts. */
050    public static final String SETTING = "templateContexts";
051
052    /** The map of allowed contexts for each type. */
053    private Map<String, CmsDefaultSet<String>> m_allowedContextMap;
054
055    /** Client variant information. */
056    private Map<String, Map<String, CmsClientVariantInfo>> m_clientVariantInfo = new LinkedHashMap<String, Map<String, CmsClientVariantInfo>>();
057
058    /** A map from the names of all available template contexts to their localized names. */
059    private Map<String, String> m_contextLabels = new LinkedHashMap<String, String>();
060
061    /** The context provider class. */
062    private String m_contextProvider;
063
064    /** The custom label for the default template option. */
065    private String m_defaultLabel;
066
067    /** The name of the cookie used for overriding the template context. */
068    private String m_cookieName;
069
070    /** The key of the currently active context. */
071    private String m_currentContext;
072
073    /** Custom label for the context menu entry. */
074    private String m_menuLabel;
075
076    /** The name of the selected context (using the cookie) .*/
077    private String m_selectedContext;
078
079    /** The setting definition for the templateContexts setting. */
080    private CmsXmlContentProperty m_settingDefinition;
081
082    /** If false, hide template context select options in the settings dialog. */
083    private boolean m_shouldShowElementTemplateContextSelection;
084
085    /**
086     * Default constructor.<p>
087     */
088    public CmsTemplateContextInfo() {
089
090        // default constructor
091    }
092
093    /**
094     * Gets the map of forbidden contexts for resource types.<p>
095     *
096     * @return the map of forbidden contexts for resource types
097     */
098    public Map<String, CmsDefaultSet<String>> getAllowedContexts() {
099
100        return m_allowedContextMap;
101
102    }
103
104    /**
105     * Gets the client variant information for a specific context.<p>
106     *
107     * @param context the context name
108     *
109     * @return the client variant information for that context
110     */
111    public Map<String, CmsClientVariantInfo> getClientVariants(String context) {
112
113        return m_clientVariantInfo.get(context);
114    }
115
116    /**
117     * Gets the map of labels for the different template contexts.<p>
118     *
119     * @return the map of template context labels
120     */
121    public Map<String, String> getContextLabels() {
122
123        return m_contextLabels;
124    }
125
126    /**
127     * Gets the name of the context provider class.<p>
128     *
129     * @return the name of the context provider class
130     */
131    public String getContextProvider() {
132
133        return m_contextProvider;
134    }
135
136    /**
137     * Gets the name of the cookie used for overriding the template context.<p>
138     *
139     * @return the cookie name
140     */
141    public String getCookieName() {
142
143        return m_cookieName;
144    }
145
146    /**
147     * Gets the key of the currently active template context.<p>
148     *
149     * @return the key of the currently active template context
150     */
151    public String getCurrentContext() {
152
153        return m_currentContext;
154    }
155
156    /**
157     * Gets the default label.
158     *
159     * @return the default label
160     */
161    public String getDefaultLabel() {
162
163        return m_defaultLabel;
164    }
165
166    /**
167     * Gets the custom context menu entry label (if null, the default is used).
168     *
169     * @return the custom context menu entry label
170     */
171    public String getMenuLabel() {
172
173        return m_menuLabel;
174    }
175
176    /**
177     * Gets the key of the currently selected template context, using the cookie.<p>
178     *
179     * @return the name of the currently selected template context
180     */
181    public String getSelectedContext() {
182
183        return m_selectedContext;
184    }
185
186    /**
187     * Gets the property definition for the templateContexts setting.<p>
188     *
189     * @return the property definition for the templateContexts setting
190     */
191    public CmsXmlContentProperty getSettingDefinition() {
192
193        return m_settingDefinition;
194    }
195
196    /**
197     * Checks if client variants for the given context are present.<p>
198     *
199     * @param context a context name
200     * @return true if there are client variants for the context
201     */
202    public boolean hasClientVariants(String context) {
203
204        return m_clientVariantInfo.containsKey(context);
205    }
206
207    /**
208     * Sets the allowed contexts.<p>
209     *
210     * @param allowedContextMap the map of allowed contexts
211     */
212    public void setAllowedContexts(Map<String, CmsDefaultSet<String>> allowedContextMap) {
213
214        m_allowedContextMap = allowedContextMap;
215    }
216
217    /**
218     * Adds a client variant.<p>
219     *
220     * @param context a context name
221     * @param variant the variant name
222     * @param info the bean with the variant information
223     */
224    public void setClientVariant(String context, String variant, CmsClientVariantInfo info) {
225
226        if (!m_clientVariantInfo.containsKey(context)) {
227            Map<String, CmsClientVariantInfo> variants = new LinkedHashMap<String, CmsClientVariantInfo>();
228            m_clientVariantInfo.put(context, variants);
229        }
230        m_clientVariantInfo.get(context).put(variant, info);
231    }
232
233    /**
234     * Sets the map of labels for the contexts.<p>
235     *
236     * @param contextLabels the map of context labels
237     */
238    public void setContextLabels(Map<String, String> contextLabels) {
239
240        m_contextLabels = contextLabels;
241    }
242
243    /**
244     * Sets the context provider class name.<p>
245     *
246     * @param contextProvider the context provider class name
247     */
248    public void setContextProvider(String contextProvider) {
249
250        m_contextProvider = contextProvider;
251    }
252
253    /**
254     * Sets the name of the cookie used for overriding the template context.<p>
255     *
256     * @param cookieName the name of the cookie used for overriding the template context
257     */
258    public void setCookieName(String cookieName) {
259
260        m_cookieName = cookieName;
261    }
262
263    /**
264     * Sets the active context.<p>
265     *
266     * @param context the active context
267     */
268    public void setCurrentContext(String context) {
269
270        m_currentContext = context;
271    }
272
273    /**
274     * Sets the default label.
275     *
276     * @param defaultLabel the default label
277     */
278    public void setDefaultLabel(String defaultLabel) {
279
280        m_defaultLabel = defaultLabel;
281    }
282
283    /**
284     * Sets the custom context menu entry label.
285     *
286     * @param label the custom context menu entry label
287     */
288    public void setMenuLabel(String label) {
289
290        m_menuLabel = label;
291    }
292
293    /**
294     * Sets the selected context.<p>
295     *
296     * @param selectedContext the selected context
297     */
298    public void setSelectedContext(String selectedContext) {
299
300        m_selectedContext = selectedContext;
301    }
302
303    /**
304     * Sets the property definition for the templateContexts setting.<p>
305     *
306     * @param definition the property definition
307     */
308    public void setSettingDefinition(CmsXmlContentProperty definition) {
309
310        m_settingDefinition = definition;
311    }
312
313    /**
314     * Enables / disables display of the template context select options in the settings dialog.
315     *
316     * @param newValue if true, show the select options
317     */
318    public void setShouldShowElementTemplateContextSelection(boolean newValue) {
319
320        m_shouldShowElementTemplateContextSelection = newValue;
321    }
322
323    /**
324     * Returns true if the template context selection should be shown for container elements.<p>
325     *
326     * @return true if the template context selection for elements should be shown
327     */
328    public boolean shouldShowElementTemplateContextSelection() {
329
330        return m_shouldShowElementTemplateContextSelection && hasMoreThanOneOption();
331    }
332
333    /**
334     * Returns true if the template context selection context menu entry should be shown.<p>
335     *
336     * @return true if the template context selection context menu entry should be shown
337     */
338    public boolean shouldShowTemplateContextContextMenuEntry() {
339
340        return hasMoreThanOneOption();
341
342    }
343
344    /**
345     * Returns true if there is more than one template context to choose from.<p>
346     *
347     * @return true if there is more than one template context
348     */
349    private boolean hasMoreThanOneOption() {
350
351        return (m_currentContext != null);
352    }
353}