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.workplace.editors.directedit;
029
030import org.opencms.util.CmsStringUtil;
031
032/**
033 * Constants to indicate which mode to use for placement of the HTML that generates
034 * the direct edit buttons.<p>
035 *
036 * There are 3 basic options for the direct edit mode:
037 * <ul>
038 * <li>{@link #FALSE}: Direct edit is disabled.
039 * <li>{@link #AUTO}: Direct edit button HTML is inserted automatically.
040 * <li>{@link #MANUAL}: Direct edit button HTML is inserted manually by using &ltcms: editable mode="manual" /&gt tags.
041 * </ul>
042 *
043 * There is one global option set for the page / template.
044 * The default is {@link #AUTO}.<p>
045 *
046 * There is an additional constant {@link #TRUE} that means "use the default mode of the page / template".<p>
047 *
048 * It is possible to switch modes for an individual content loop.
049 * This is intended to use with XmlContents that require special placement of the direct edit HTML
050 * because the default placement does not give good results.<p>
051 *
052 * @since 6.2.3
053 */
054public final class CmsDirectEditMode {
055
056    /** Indicates automatic placement of direct edit HTML. */
057    public static final CmsDirectEditMode AUTO = new CmsDirectEditMode(2);
058
059    /** Indicates direct edit is disabled. */
060    public static final CmsDirectEditMode FALSE = new CmsDirectEditMode(0);
061
062    /** Indicates manual placement of direct edit HTML. */
063    public static final CmsDirectEditMode MANUAL = new CmsDirectEditMode(3);
064
065    /** Indicates direct edit HTML is to be generated according to the default setting of the current page. */
066    public static final CmsDirectEditMode TRUE = new CmsDirectEditMode(1);
067
068    /** Array of mode constants. */
069    private static final CmsDirectEditMode[] MODES = {FALSE, TRUE, AUTO, MANUAL};
070
071    /** Constant to indicate editable mode is "auto", which is equivalent to "true". */
072    private static final String VALUE_AUTO = "auto";
073
074    /** Constant to indicate editable mode is "false", which means direct edit is turned off. */
075    private static final String VALUE_FALSE = CmsStringUtil.FALSE;
076
077    /** Constant to indicate editable mode is "manual". */
078    private static final String VALUE_MANUAL = "manual";
079
080    /** Constant to indicate editable mode is "true", which means use the default from the page. */
081    private static final String VALUE_TRUE = CmsStringUtil.TRUE;
082
083    /** The direct edit mode to use. */
084    private int m_mode;
085
086    /**
087     * Hides the public constructor.<p>
088     *
089     * @param mode the mode to initialize
090     */
091    private CmsDirectEditMode(int mode) {
092
093        m_mode = mode;
094    }
095
096    /**
097     * Returns {@link #TRUE} in case the given value is <code>true</code>, {@link #FALSE} otherwise.<p>
098     *
099     * @param value the direct edit mode to get the constant for
100     *
101     * @return {@link #TRUE} in case the given value is <code>true</code>, {@link #FALSE} otherwise
102     */
103    public static CmsDirectEditMode valueOf(boolean value) {
104
105        return value ? TRUE : FALSE;
106    }
107
108    /**
109     * Returns the mode constant for the selected direct edit int mode.<p>
110     *
111     * The possible value are:
112     * <ul>
113     * <li>0: Mode is {@link #FALSE}.
114     * <li>1: Mode is {@link #TRUE}.
115     * <li>2: Mode is {@link #AUTO}.
116     * <li>3: Mode is {@link #MANUAL}.
117     * </ul>
118     *
119     * @param mode the direct edit int mode to get the constant for
120     *
121     * @return the mode constant for the selected direct edit int mode
122     */
123    public static CmsDirectEditMode valueOf(int mode) {
124
125        if ((mode > 0) && (mode < MODES.length)) {
126            return MODES[mode];
127        }
128        return FALSE;
129    }
130
131    /**
132     * Returns the mode constant for the selected direct edit String mode description.<p>
133     *
134     * For a mode instance <code>A</code>, {@link #toString()} returns the String mode description.<p>
135     *
136     * @param value the direct edit String mode description to get the constant for
137     *
138     * @return the mode constant for the selected direct edit String mode description
139     */
140    public static CmsDirectEditMode valueOf(String value) {
141
142        CmsDirectEditMode result = FALSE;
143        if (CmsStringUtil.isNotEmpty(value)) {
144            value = value.trim().toLowerCase();
145            if (Boolean.valueOf(value).booleanValue()) {
146                result = TRUE;
147            } else if (VALUE_AUTO.equals(value)) {
148                result = AUTO;
149            } else if (VALUE_MANUAL.equals(value)) {
150                result = MANUAL;
151            }
152        }
153        return result;
154    }
155
156    /**
157     * Returns this modes int value.<p>
158     *
159     * @return this modes int value
160     *
161     * @see #valueOf(int)
162     */
163    public int getMode() {
164
165        return m_mode;
166    }
167
168    /**
169     * Returns <code>true</code> in case this mode indicates direct edit is enabled.<p>
170     *
171     * Direct edit is enabled if this mode is not {@link #FALSE}, which is
172     * identical to <code>{@link #getMode()} > 0</code>.
173     *
174     * @return <code>true</code> in case this mode indicates direct edit is enabled
175     */
176    public boolean isEnabled() {
177
178        return m_mode > 0;
179    }
180
181    /**
182     * @see java.lang.Object#toString()
183     * @see #valueOf(String)
184     */
185    @Override
186    public String toString() {
187
188        switch (m_mode) {
189            case 1:
190                return VALUE_TRUE;
191            case 2:
192                return VALUE_AUTO;
193            case 3:
194                return VALUE_MANUAL;
195            default:
196                return VALUE_FALSE;
197        }
198    }
199}