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.main.CmsLog;
032import org.opencms.xml.content.CmsXmlContentProperty;
033
034import java.util.List;
035
036import org.apache.commons.beanutils.BeanUtils;
037import org.apache.commons.logging.Log;
038
039/**
040 * Preference subclass for built-in preferences accessed with a getter/setter pair via reflection.<p>
041 */
042public class CmsBuiltinPreference extends A_CmsPreference {
043
044    /**
045     * A bean representing a set of select options.<p>
046     */
047    public static class SelectOptions {
048
049        /** The list of user-readable option labels. */
050        private List<String> m_options;
051
052        /** The currently selected index. */
053        private int m_selectedIndex;
054
055        /** The list of option values. */
056        private List<String> m_values;
057
058        /**
059         * Creates a new instance.<p>
060         *
061         * @param options the option labels
062         * @param values the option values
063         * @param selectedIndex the currently selected index
064         */
065        public SelectOptions(List<String> options, List<String> values, int selectedIndex) {
066
067            m_options = options;
068            m_values = values;
069            m_selectedIndex = selectedIndex;
070        }
071
072        /**
073         * Gets the select option labels.<p>
074         *
075         * @return the select option labels
076         */
077        public List<String> getOptions() {
078
079            return m_options;
080        }
081
082        /**
083         * Gets the selected index.<p>
084         *
085         * @return the selected index
086         */
087        public int getSelectedIndex() {
088
089            return m_selectedIndex;
090        }
091
092        /**
093         * Gets the select widget values.<p>
094         *
095         * @return the select widget values
096         */
097        public List<String> getValues() {
098
099            return m_values;
100        }
101
102        /**
103         * Creates a configuration string for client-side select widgets from the options.<p>
104         *
105         * @return the widget configuration string
106         */
107        public String toClientSelectWidgetConfiguration() {
108
109            StringBuffer resultBuffer = new StringBuffer();
110            for (int i = 0; i < m_values.size(); i++) {
111                String value = m_values.get(i);
112                String option = i < m_options.size() ? m_options.get(i) : value;
113                if (i != 0) {
114                    resultBuffer.append("|");
115                }
116                resultBuffer.append(value);
117                resultBuffer.append(":");
118                resultBuffer.append(option);
119            }
120            return resultBuffer.toString();
121        }
122    }
123
124    /** The logger instance for this class. */
125    private static final Log LOG = CmsLog.getLog(CmsBuiltinPreference.class);
126
127    /** True if this is a basic preference. */
128    protected boolean m_basic;
129
130    /** True if this is a hidden preference. */
131    protected boolean m_hidden;
132
133    /** The name of the bean property used to access this preference. */
134    private String m_propName;
135
136    /**
137     * Creates a new instance.<p>
138     *
139     * @param propName the name of the bean property used to access this preference
140     */
141    public CmsBuiltinPreference(String propName) {
142
143        m_propName = propName;
144    }
145
146    /**
147     * @see org.opencms.configuration.preferences.I_CmsPreference#getDefaultValue()
148     */
149    public String getDefaultValue() {
150
151        CmsUserSettingsStringPropertyWrapper wrapper = new CmsUserSettingsStringPropertyWrapper(
152            CmsDefaultUserSettings.CURRENT_DEFAULT_SETTINGS);
153        try {
154            return (BeanUtils.getProperty(wrapper, m_propName));
155        } catch (Exception e) {
156            LOG.error(e.getLocalizedMessage(), e);
157            return null;
158        }
159    }
160
161    /**
162     * @see org.opencms.configuration.preferences.I_CmsPreference#getName()
163     */
164    public String getName() {
165
166        return m_propName;
167    }
168
169    /**
170     * @see org.opencms.configuration.preferences.A_CmsPreference#getPropertyDefinition()
171     */
172    @Override
173    public CmsXmlContentProperty getPropertyDefinition() {
174
175        CmsXmlContentProperty prop = new CmsXmlContentProperty(
176            m_propName, // name
177            "string", // type
178            null, //widget
179            null, //widgetconfig
180            null, //regex
181            null, //ruletype
182            getDefaultValue(), //default
183            null, //nicename
184            null, //description
185            null, //error
186            null //preferfolder
187        );
188        return prop;
189    }
190
191    /**
192     * @see org.opencms.configuration.preferences.I_CmsPreference#getTab()
193     */
194    public String getTab() {
195
196        return m_hidden ? "hidden" : (m_basic ? "basic" : "extended");
197    }
198
199    /**
200     * @see org.opencms.configuration.preferences.I_CmsPreference#getValue(org.opencms.configuration.CmsDefaultUserSettings)
201     */
202    public String getValue(CmsDefaultUserSettings userSettings) {
203
204        CmsUserSettingsStringPropertyWrapper wrapper = new CmsUserSettingsStringPropertyWrapper(userSettings);
205        try {
206            return (BeanUtils.getProperty(wrapper, m_propName));
207        } catch (Exception e) {
208            LOG.error(e.getLocalizedMessage(), e);
209            return null;
210        }
211    }
212
213    /**
214     * @see org.opencms.configuration.preferences.I_CmsPreference#setValue(org.opencms.configuration.CmsDefaultUserSettings, java.lang.String)
215     */
216    public void setValue(CmsDefaultUserSettings settings, String value) {
217
218        CmsUserSettingsStringPropertyWrapper wrapper = new CmsUserSettingsStringPropertyWrapper(settings);
219        try {
220            BeanUtils.setProperty(wrapper, m_propName, value);
221        } catch (Exception e) {
222            LOG.error(e.getLocalizedMessage(), e);
223        }
224    }
225
226}