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.loader;
029
030import org.opencms.i18n.CmsMessageContainer;
031import org.opencms.i18n.I_CmsMessageContainer;
032
033import java.util.ArrayList;
034import java.util.Collection;
035import java.util.LinkedHashMap;
036import java.util.Locale;
037import java.util.Map;
038
039/**
040 * A template context is basically a named path to a template JSP, which
041 * has both an internal name used as a key, and a user-readable, localizable name. It
042 * also has a reference to the template context provider which produced it.<p>
043 */
044public class CmsTemplateContext {
045
046    /** Map of client variants, with the variant names used as keys. */
047    private Map<String, CmsClientVariant> m_clientVariants;
048
049    /** A flag which indicates whether this template context has been manually selected rather than automatically determined. */
050    private boolean m_forced;
051
052    /** The key used for identifying the template context. */
053    private String m_key;
054
055    /** The message container for the user-readable name. */
056    private I_CmsMessageContainer m_messageContainer;
057
058    /** The template context provider which created this context. */
059    private I_CmsTemplateContextProvider m_provider;
060
061    /** The path to the template. */
062    private String m_templatePath;
063
064    /**
065     * Constructor.<p>
066     *
067     * @param key the internal name
068     * @param path the template path
069     * @param container the message container for the name
070     * @param provider the template context provider
071     */
072    public CmsTemplateContext(
073        String key,
074        String path,
075        CmsMessageContainer container,
076        I_CmsTemplateContextProvider provider) {
077
078        this(key, path, container, provider, new ArrayList<CmsClientVariant>(), false);
079    }
080
081    /**
082     * Constructor.<p>
083     *
084     * @param key the internal name
085     * @param path the template path
086     * @param container the message container for the name
087     * @param provider the template context provider
088     * @param clientVariants the client variants
089     * @param forced true if the template context is forced to a specific value instead of automatically determined
090     *
091     */
092    public CmsTemplateContext(
093        String key,
094        String path,
095        I_CmsMessageContainer container,
096        I_CmsTemplateContextProvider provider,
097        Collection<CmsClientVariant> clientVariants,
098        boolean forced) {
099
100        m_key = key;
101        m_templatePath = path;
102        m_messageContainer = container;
103        m_provider = provider;
104        m_forced = forced;
105        m_clientVariants = new LinkedHashMap<String, CmsClientVariant>();
106        for (CmsClientVariant variant : clientVariants) {
107            m_clientVariants.put(variant.getName(), variant);
108        }
109    }
110
111    /**
112     * Gets the map of client variants.<p>
113     *
114     * Client variants are specialized variants of a template context which are only used by the container page editor
115     * for preview purposes.
116     *
117     * @return the client variants
118     */
119    public Map<String, CmsClientVariant> getClientVariants() {
120
121        return m_clientVariants;
122    }
123
124    /**
125     * Gets the internal name used as a key.<p>
126     *
127     * @return the internal name
128     */
129    public String getKey() {
130
131        return m_key;
132    }
133
134    /**
135     * Gets the localized name for a given locale.<p>
136     *
137     * @param locale the locale for which we want the name
138     *
139     * @return the localized name
140     */
141    public String getLocalizedName(Locale locale) {
142
143        if (m_messageContainer != null) {
144            return m_messageContainer.key(locale);
145        }
146        return m_key;
147    }
148
149    /**
150     * Gets the message container for the user-readable name.<p>
151     *
152     * @return the message container
153     */
154    public I_CmsMessageContainer getMessageContainer() {
155
156        return m_messageContainer;
157    }
158
159    /**
160     * Gets the template context provider which produced this template context.<p>
161     *
162     * @return the template context provider
163     */
164    public I_CmsTemplateContextProvider getProvider() {
165
166        return m_provider;
167    }
168
169    /**
170     * Gets the path to the template.<p>
171     *
172     * @return the path to the template
173     */
174    public String getTemplatePath() {
175
176        return m_templatePath;
177    }
178
179    /**
180     * Return true if the template context was not automatically determined.<p>
181     *
182     * @return true if the template context was not automatically determined
183     */
184    public boolean isForced() {
185
186        return m_forced;
187    }
188
189    /**
190     * @see java.lang.Object#toString()
191     */
192    @Override
193    public String toString() {
194
195        return getKey();
196    }
197}