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.configuration.CmsDefaultUserSettings;
031import org.opencms.configuration.CmsWorkplaceConfiguration;
032import org.opencms.configuration.I_CmsXmlConfiguration;
033import org.opencms.file.CmsObject;
034import org.opencms.xml.content.CmsXmlContentProperty;
035
036import org.dom4j.Element;
037
038/**
039 * Subclass for user-defined preferences.<p>
040 */
041public class CmsUserDefinedPreference extends A_CmsPreference {
042
043    /** The configured preference data. */
044    private CmsPreferenceData m_preferenceData;
045
046    /**
047     * Creates a new instance.<p>
048     *
049     * @param name the preference name
050     * @param value the preference value
051     * @param prop the bean containing the widget configuration for the preference
052     * @param tab the tab on which to display the widget
053     */
054    public CmsUserDefinedPreference(String name, String value, CmsXmlContentProperty prop, String tab) {
055
056        m_preferenceData = new CmsPreferenceData(name, value, prop, tab);
057    }
058
059    /**
060     * Helper method used to create the configuration attributes for a CmsPreferenceData bean.<p>
061     *
062     * @param pref the preference data
063     * @param elem the element in which the attributes should be created
064     */
065    public static void fillAttributes(CmsPreferenceData pref, Element elem) {
066
067        CmsXmlContentProperty prop = pref.getPropertyDefinition();
068        for (String[] attrToSet : new String[][] {
069            {I_CmsXmlConfiguration.A_VALUE, pref.getDefaultValue()},
070            {CmsWorkplaceConfiguration.A_NICE_NAME, prop.getNiceName()},
071            {CmsWorkplaceConfiguration.A_DESCRIPTION, prop.getDescription()},
072            {CmsWorkplaceConfiguration.A_WIDGET, prop.getConfiguredWidget()},
073            {CmsWorkplaceConfiguration.A_WIDGET_CONFIG, prop.getWidgetConfiguration()},
074            {CmsWorkplaceConfiguration.A_RULE_REGEX, prop.getRuleRegex()},
075            {CmsWorkplaceConfiguration.A_ERROR, prop.getError()}}) {
076            String attrName = attrToSet[0];
077            String value = attrToSet[1];
078            if (value != null) {
079                elem.addAttribute(attrName, value);
080            }
081        }
082    }
083
084    /**
085     * @see org.opencms.configuration.preferences.I_CmsPreference#getDefaultValue()
086     */
087    public String getDefaultValue() {
088
089        return m_preferenceData.getDefaultValue();
090    }
091
092    /**
093     * @see org.opencms.configuration.preferences.I_CmsPreference#getName()
094     */
095    public String getName() {
096
097        return m_preferenceData.getName();
098    }
099
100    /**
101     * @see org.opencms.configuration.preferences.I_CmsPreference#getPropertyDefinition(org.opencms.file.CmsObject)
102     */
103    @Override
104    public CmsXmlContentProperty getPropertyDefinition(CmsObject cms) {
105
106        return getPropertyDefinition();
107    }
108
109    /**
110     * @see org.opencms.configuration.preferences.I_CmsPreference#getTab()
111     */
112    public String getTab() {
113
114        return m_preferenceData.getTab();
115    }
116
117    /**
118     * @see org.opencms.configuration.preferences.I_CmsPreference#getValue(org.opencms.configuration.CmsDefaultUserSettings)
119     */
120    public String getValue(CmsDefaultUserSettings userSettings) {
121
122        return userSettings.getAdditionalPreference(getName(), true);
123    }
124
125    /**
126     * @see org.opencms.configuration.preferences.I_CmsPreference#setValue(org.opencms.configuration.CmsDefaultUserSettings, java.lang.String)
127     */
128    public void setValue(CmsDefaultUserSettings settings, String value) {
129
130        settings.setAdditionalPreference(getName(), value);
131    }
132
133    /**
134     * @see org.opencms.configuration.preferences.A_CmsPreference#getPropertyDefinition()
135     */
136    @Override
137    protected CmsXmlContentProperty getPropertyDefinition() {
138
139        return m_preferenceData.getPropertyDefinition();
140    }
141
142}