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 GmbH & Co. KG, 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.ui.contextmenu;
029
030import org.opencms.util.A_CmsModeIntEnumeration;
031
032/**
033 * The visibility modes of a context menu item in the explorer view.<p>
034 *
035 * @since 6.5.6
036 */
037public final class CmsMenuItemVisibilityMode extends A_CmsModeIntEnumeration {
038
039    /** Menu item visibility: active.  */
040    public static final CmsMenuItemVisibilityMode VISIBILITY_ACTIVE = new CmsMenuItemVisibilityMode(1);
041
042    /** Menu item visibility: inactive.  */
043    public static final CmsMenuItemVisibilityMode VISIBILITY_INACTIVE = new CmsMenuItemVisibilityMode(2);
044
045    /** Menu item visibility: invisible.  */
046    public static final CmsMenuItemVisibilityMode VISIBILITY_INVISIBLE = new CmsMenuItemVisibilityMode(3);
047
048    /** Menu item visibility: invisible, use next best menu item with same ID.  */
049    public static final CmsMenuItemVisibilityMode VISIBILITY_USE_NEXT = new CmsMenuItemVisibilityMode(4);
050
051    /** Serializable version id. */
052    private static final long serialVersionUID = 2526260041565757791L;
053
054    /** The name of the message key for the visibility mode. */
055    private String m_messageKey;
056
057    /** The prioritization flag. */
058    private boolean m_prioritized;
059
060    /**
061     * Private constructor.<p>
062     *
063     * @param mode the menu item visibility mode integer representation
064     */
065    private CmsMenuItemVisibilityMode(int mode) {
066
067        super(mode);
068    }
069
070    /**
071     * Utilitiy method that returns 'active' if the parameter is true, otherwise inactive.<p>
072     *
073     * @param active - whether return value should be 'active'
074     *
075     * @return the visibility
076     */
077    public static CmsMenuItemVisibilityMode activeInactive(boolean active) {
078
079        if (active) {
080            return VISIBILITY_ACTIVE;
081        } else {
082            return VISIBILITY_INACTIVE;
083        }
084    }
085
086    /**
087     * Utility method that returns 'active' if the parameter is true, otherwise invisible.<p>
088     *
089     * @param active - whether return value should be 'active' rather than 'invisible'
090     *
091     * @return the visibility
092     */
093    public static CmsMenuItemVisibilityMode activeInvisible(boolean active) {
094
095        if (active) {
096            return VISIBILITY_ACTIVE;
097        } else {
098            return VISIBILITY_INVISIBLE;
099        }
100    }
101
102    /**
103     * Returns the menu item visibility mode for the given mode value.<p>
104     *
105     * This is used only for serialization and should not be accessed for other purposes.<p>
106     *
107     * @param type the mode value to get the item visibility mode for
108     *
109     * @return the menu item visibility mode for the given mode value
110     */
111    public static CmsMenuItemVisibilityMode valueOf(int type) {
112
113        switch (type) {
114            case 1:
115                return VISIBILITY_ACTIVE;
116            case 2:
117                return VISIBILITY_INACTIVE;
118            case 3:
119                return VISIBILITY_INVISIBLE;
120            default:
121                return VISIBILITY_INVISIBLE;
122        }
123    }
124
125    /**
126     * Adds the name of the message key for the visibility mode.<p>
127     *
128     * @param messageKey the name of the message key for the visibility mode
129     * @return an extended visibility mode containing the message key
130     */
131    public CmsMenuItemVisibilityMode addMessageKey(String messageKey) {
132
133        CmsMenuItemVisibilityMode mode = clone();
134        mode.m_messageKey = messageKey;
135        return mode;
136    }
137
138    /**
139     * Returns the name of the message key for the visibility mode.<p>
140     *
141     * Is usually used as description for the inactive visibility modes.<p>
142     *
143     * @return the name of the message key for the visibility mode
144     */
145    public String getMessageKey() {
146
147        return m_messageKey;
148    }
149
150    /**
151     * Returns if the mode is set to {@link #VISIBILITY_ACTIVE}.<p>
152     *
153     * @return true if the mode is set to {@link #VISIBILITY_ACTIVE}, otherwise false
154     */
155    public boolean isActive() {
156
157        return getMode() == VISIBILITY_ACTIVE.getMode();
158    }
159
160    /**
161     * Returns if the mode is set to {@link #VISIBILITY_INACTIVE}.<p>
162     *
163     * @return true if the mode is set to {@link #VISIBILITY_INACTIVE}, otherwise false
164     */
165    public boolean isInActive() {
166
167        return getMode() == VISIBILITY_INACTIVE.getMode();
168    }
169
170    /**
171     * Returns if the mode is set to {@link #VISIBILITY_INVISIBLE} or {@link #VISIBILITY_USE_NEXT}.<p>
172     *
173     * @return true if the mode is set to {@link #VISIBILITY_INVISIBLE} or {@link #VISIBILITY_USE_NEXT}, otherwise false
174     */
175    public boolean isInVisible() {
176
177        return (getMode() == VISIBILITY_INVISIBLE.getMode()) || (getMode() == VISIBILITY_USE_NEXT.getMode());
178    }
179
180    /**
181     * Returns the prioritization flag.<p>
182     *
183     * @return prioritization flag
184     */
185    public boolean isPrioritized() {
186
187        return m_prioritized;
188    }
189
190    /**
191     * Returns true if this item is invisible, but the next best item with the same ID should be used instead.
192     *
193     * @return true if this item is invisible, but the next best item with the same ID should be used instead
194     */
195    public boolean isUseNext() {
196
197        return getMode() == VISIBILITY_USE_NEXT.getMode();
198    }
199
200    /**
201     * Returns a prioritized instance of the visibility mode.<p>
202     *
203     * @param prioritized <code>true</code> to prioritize
204     *
205     * @return the new visibility mode instance
206     */
207    public CmsMenuItemVisibilityMode prioritize(boolean prioritized) {
208
209        if (m_prioritized != prioritized) {
210            CmsMenuItemVisibilityMode result = clone();
211            result.m_prioritized = prioritized;
212            return result;
213        } else {
214            return this;
215        }
216    }
217
218    /**
219     * @see java.lang.Object#clone()
220     */
221    @Override
222    protected CmsMenuItemVisibilityMode clone() {
223
224        return new CmsMenuItemVisibilityMode(getMode());
225    }
226
227}