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.configuration.preferences;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.OpenCms;
032import org.opencms.util.CmsStringUtil;
033import org.opencms.xml.content.CmsXmlContentProperty;
034
035import java.util.Iterator;
036import java.util.List;
037import java.util.Locale;
038
039/**
040 * Workplace locale preference.<p>
041 */
042public class CmsLanguagePreference extends CmsBuiltinPreference {
043
044    /** The nice name. */
045    private static final String NICE_NAME = "%(key."
046        + org.opencms.workplace.commons.Messages.GUI_LABEL_LANGUAGE_0
047        + ")";
048
049    /**
050     * Creates a new instance.<p>
051     *
052     * @param name the name
053     */
054    public CmsLanguagePreference(String name) {
055
056        super(name);
057        m_basic = true;
058    }
059
060    /**
061     * @see org.opencms.configuration.preferences.CmsBuiltinPreference#getPropertyDefinition(org.opencms.file.CmsObject)
062     */
063    @Override
064    public CmsXmlContentProperty getPropertyDefinition() {
065
066        CmsXmlContentProperty prop = new CmsXmlContentProperty(
067            getName(), //name
068            "string", //type
069            null, //widget
070            null, //widgetconfig
071            null, //regex
072            null, //ruletype
073            null, //default
074            NICE_NAME, //nicename
075            null, //description
076            null, //error
077            null//preferfolder
078        );
079        return prop;
080    }
081
082    /**
083     * @see org.opencms.configuration.preferences.CmsBuiltinPreference#getPropertyDefinition(org.opencms.file.CmsObject)
084     */
085    @Override
086    public CmsXmlContentProperty getPropertyDefinition(CmsObject cms) {
087
088        Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(cms);
089        String options = getOptionsForLanguage(locale);
090        CmsXmlContentProperty prop = new CmsXmlContentProperty(
091            getName(), //name
092            "string", //type
093            "select_notnull", //widget
094            options, //widgetconfig
095            null, //regex
096            null, //ruletype
097            null, //default
098            NICE_NAME, //nicename
099            null, //description
100            null, //error
101            null//preferfolder
102        );
103        return prop;
104    }
105
106    /**
107     * Gets the options for the language selector.<p>
108     *
109     * @param setLocale the locale for the select options
110     *
111     * @return the options for the language selector
112     */
113    private String getOptionsForLanguage(Locale setLocale) {
114
115        // get available locales from the workplace manager
116        List<Locale> locales = OpenCms.getWorkplaceManager().getLocales();
117        StringBuffer resultBuffer = new StringBuffer();
118        int counter = 0;
119        Iterator<Locale> i = locales.iterator();
120        while (i.hasNext()) {
121            Locale currentLocale = i.next();
122            // add all locales to the select box
123            String language = currentLocale.getDisplayLanguage(setLocale);
124            if (CmsStringUtil.isNotEmpty(currentLocale.getCountry())) {
125                language = language + " (" + currentLocale.getDisplayCountry(setLocale) + ")";
126            }
127            if (CmsStringUtil.isNotEmpty(currentLocale.getVariant())) {
128                language = language + " (" + currentLocale.getDisplayVariant(setLocale) + ")";
129            }
130            if (counter != 0) {
131                resultBuffer.append("|");
132            }
133            resultBuffer.append(currentLocale.toString()).append(":").append(language);
134            counter++;
135        }
136        return resultBuffer.toString();
137    }
138
139}