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.workplace.editors;
029
030import org.opencms.util.CmsStringUtil;
031
032/**
033 * Contains the setup information about a single dialog element.<p>
034 *
035 * @since 6.0.0
036 */
037public class CmsDialogElement implements Comparable<CmsDialogElement> {
038
039    /** Indicates if the element is existing on the page or not. */
040    private boolean m_existing;
041
042    /** Indicates if the element is mandantory or not. */
043    private boolean m_mandantory;
044
045    /** The (system) name of the element. */
046    private String m_name;
047
048    /** The nice "display" name of the element. */
049    private String m_niceName;
050
051    /** Indicates if the element is declared as template-element or not. */
052    private boolean m_templateElement;
053
054    /**
055     * Creates a new dialog element.<p>
056     *
057     * @param name the (system) name of the element
058     * @param niceName the nice "display" name of the element
059     * @param mandantory indicates if the element is mandatory
060     * @param templateElement indicates if the element is defined as template-element
061     * @param existing indicates if the element is existing on the xmlPage or not
062     */
063    public CmsDialogElement(
064        String name,
065        String niceName,
066        boolean mandantory,
067        boolean templateElement,
068        boolean existing) {
069
070        m_name = name;
071        m_niceName = niceName;
072        m_mandantory = mandantory;
073        m_templateElement = templateElement;
074        m_existing = existing;
075    }
076
077    /**
078     * @see java.lang.Comparable#compareTo(java.lang.Object)
079     */
080    public int compareTo(CmsDialogElement obj) {
081
082        if (obj == this) {
083            return 0;
084        }
085        CmsDialogElement element = obj;
086        if (m_name == null) {
087            return (element.m_name == null) ? 0 : -1;
088        } else {
089            return m_name.compareToIgnoreCase(element.m_name);
090        }
091    }
092
093    /**
094     * @see java.lang.Object#equals(java.lang.Object)
095     */
096    @Override
097    public boolean equals(Object obj) {
098
099        if (obj == this) {
100            return true;
101        }
102        if (!(obj instanceof CmsDialogElement)) {
103            return false;
104        }
105        CmsDialogElement other = (CmsDialogElement)obj;
106        if (m_name == null) {
107            return other.m_name == null;
108        } else {
109            if (other.m_name == null) {
110                return false;
111            }
112            String name1 = m_name;
113            String name2 = other.m_name;
114            if (name1.endsWith("[0]")) {
115                name1 = name1.substring(0, name1.length() - 3);
116            }
117            if (name2.endsWith("[0]")) {
118                name2 = name2.substring(0, name2.length() - 3);
119            }
120            return name1.equals(name2);
121        }
122    }
123
124    /**
125     * Returns the name.<p>
126     *
127     * @return the name
128     */
129    public String getName() {
130
131        return m_name;
132    }
133
134    /**
135     * Returns the niceName.<p>
136     *
137     * @return the niceName
138     */
139    public String getNiceName() {
140
141        if (CmsStringUtil.isEmpty(m_niceName)) {
142            // if the nice name is empty use the system name for display
143
144            if (isExisting() && !isTemplateElement()) {
145                // this element was not defined with the "template-elements" property
146                return "* " + getName();
147            } else {
148                return getName();
149            }
150        }
151
152        return m_niceName;
153    }
154
155    /**
156     * @see java.lang.Object#hashCode()
157     */
158    @Override
159    public int hashCode() {
160
161        if (m_name == null) {
162            return 0;
163        } else {
164            return m_name.hashCode();
165        }
166    }
167
168    /**
169     * Returns the existing.<p>
170     *
171     * @return the existing
172     */
173    public boolean isExisting() {
174
175        return m_existing;
176    }
177
178    /**
179     * Returns the mandatory.<p>
180     *
181     * @return the mandatory
182     */
183    public boolean isMandantory() {
184
185        return m_mandantory;
186    }
187
188    /**
189     * Returns true if the element is defined by the template,
190     * false if the element is just contained in the xml page code.<p>
191     *
192     * @return true if the element is defined by the template
193     */
194    public boolean isTemplateElement() {
195
196        return m_templateElement;
197    }
198
199    /**
200     * Sets the existing.<p>
201     *
202     * @param existing the existing to set
203     */
204    public void setExisting(boolean existing) {
205
206        m_existing = existing;
207    }
208}