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.CmsWorkplaceConfiguration;
031import org.opencms.configuration.I_CmsXmlConfiguration;
032import org.opencms.file.CmsObject;
033import org.opencms.xml.content.CmsXmlContentProperty;
034
035import org.dom4j.DocumentHelper;
036import org.dom4j.Element;
037
038/**
039 * Abstract superclass for preferences.<p>
040 */
041public abstract class A_CmsPreference implements I_CmsPreference {
042
043    /**
044     * @see org.opencms.configuration.preferences.I_CmsPreference#createConfigurationItem()
045     */
046    public Element createConfigurationItem() {
047
048        CmsXmlContentProperty prop = getPropertyDefinition();
049        Element elem = DocumentHelper.createElement(CmsWorkplaceConfiguration.N_PREFERENCE);
050        for (String[] attrToSet : new String[][] {
051            {I_CmsXmlConfiguration.A_NAME, getName()},
052            {I_CmsXmlConfiguration.A_VALUE, getDefaultValue()},
053            {CmsWorkplaceConfiguration.A_NICE_NAME, prop.getNiceName()},
054            {CmsWorkplaceConfiguration.A_DESCRIPTION, prop.getDescription()},
055            {CmsWorkplaceConfiguration.A_WIDGET, prop.getConfiguredWidget()},
056            {CmsWorkplaceConfiguration.A_WIDGET_CONFIG, prop.getWidgetConfiguration()},
057            {CmsWorkplaceConfiguration.A_RULE_REGEX, prop.getRuleRegex()},
058            {CmsWorkplaceConfiguration.A_ERROR, prop.getError()}}) {
059            String attrName = attrToSet[0];
060            String value = attrToSet[1];
061            if (value != null) {
062                elem.addAttribute(attrName, value);
063            }
064        }
065        return elem;
066    }
067
068    /**
069     * @see org.opencms.configuration.preferences.I_CmsPreference#getPropertyDefinition(org.opencms.file.CmsObject)
070     */
071    public CmsXmlContentProperty getPropertyDefinition(CmsObject cms) {
072
073        return getPropertyDefinition();
074    }
075
076    /**
077     * @see org.opencms.configuration.preferences.I_CmsPreference#isDisabled(CmsObject)
078     */
079    public boolean isDisabled(CmsObject cms) {
080
081        return false;
082    }
083
084    /**
085     * Gets the user-independent property configuration.<p>
086     *
087     * This is what is used to write the preference back to the workplace configuration.
088     *
089     * @return the property configuration
090     */
091    protected abstract CmsXmlContentProperty getPropertyDefinition();
092
093}