001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.forms.layout.api.impl;
018
019import java.io.Serializable;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.nuxeo.ecm.platform.forms.layout.api.WidgetSelectOptions;
024
025/**
026 * @author Anahide Tchertchian
027 * @since 5.4.2
028 */
029public class WidgetSelectOptionsImpl implements WidgetSelectOptions {
030
031    private static final long serialVersionUID = 1L;
032
033    protected Serializable value;
034
035    protected String var;
036
037    protected String itemLabel;
038
039    protected Map<String, String> labels;
040
041    protected String itemValue;
042
043    protected Serializable itemDisabled;
044
045    protected Serializable itemRendered;
046
047    protected String ordering;
048
049    protected Boolean caseSensitive;
050
051    // needed by GWT serialization
052    protected WidgetSelectOptionsImpl() {
053        super();
054    }
055
056    public WidgetSelectOptionsImpl(Serializable value, String var, String itemLabel, String itemValue) {
057        this(value, var, itemLabel, itemValue, null, null);
058    }
059
060    public WidgetSelectOptionsImpl(Serializable value, String var, String itemLabel, String itemValue,
061            Serializable itemDisabled, Serializable itemRendered) {
062        this(value, var, itemLabel, itemValue, itemDisabled, itemRendered, null, null);
063    }
064
065    public WidgetSelectOptionsImpl(Serializable value, String var, String itemLabel, String itemValue,
066            Serializable itemDisabled, Serializable itemRendered, String ordering, Boolean caseSensitive) {
067        super();
068        this.value = value;
069        this.var = var;
070        this.itemLabel = itemLabel;
071        this.itemValue = itemValue;
072        this.itemDisabled = itemDisabled;
073        this.itemRendered = itemRendered;
074        this.ordering = ordering;
075        this.caseSensitive = caseSensitive;
076    }
077
078    public Serializable getValue() {
079        return value;
080    }
081
082    public String getVar() {
083        return var;
084    }
085
086    public String getItemLabel() {
087        return itemLabel;
088    }
089
090    @Override
091    public String getItemLabel(String locale) {
092        return labels.get(locale);
093    }
094
095    @Override
096    public Map<String, String> getItemLabels() {
097        return labels;
098    }
099
100    public void setItemLabels(Map<String, String> labels) {
101        this.labels = labels;
102    }
103
104    public String getItemValue() {
105        return itemValue;
106    }
107
108    public Serializable getItemDisabled() {
109        return itemDisabled;
110    }
111
112    public Serializable getItemRendered() {
113        return itemRendered;
114    }
115
116    public String getOrdering() {
117        return ordering;
118    }
119
120    public Boolean getCaseSensitive() {
121        return caseSensitive;
122    }
123
124    @Override
125    public String getTagConfigId() {
126        StringBuilder builder = new StringBuilder();
127        builder.append(value).append(";");
128        builder.append(var).append(";");
129        builder.append(itemLabel).append(";");
130        if (labels != null) {
131            builder.append(labels.toString());
132        }
133        builder.append(";");
134        builder.append(itemValue).append(";");
135        if (itemDisabled != null) {
136            builder.append(itemDisabled.toString());
137        }
138        builder.append(";");
139        if (itemRendered != null) {
140            builder.append(itemRendered.toString());
141        }
142        builder.append(";");
143        builder.append(ordering).append(";");
144        builder.append(caseSensitive).append(";");
145
146        Integer intValue = new Integer(builder.toString().hashCode());
147        return intValue.toString();
148    }
149
150    @Override
151    public WidgetSelectOptions clone() {
152        WidgetSelectOptionsImpl clone = new WidgetSelectOptionsImpl(value, var, itemLabel, itemValue, itemDisabled,
153                itemRendered, ordering, caseSensitive);
154        if (labels != null) {
155            clone.setItemLabels(new HashMap<String, String>(labels));
156        }
157        return clone;
158    }
159
160    /**
161     * @since 7.2
162     */
163    @Override
164    public boolean equals(Object obj) {
165        if (!(obj instanceof WidgetSelectOptionsImpl)) {
166            return false;
167        }
168        if (obj == this) {
169            return true;
170        }
171        WidgetSelectOptionsImpl w = (WidgetSelectOptionsImpl) obj;
172        return new EqualsBuilder().append(value, w.value).append(var, w.var).append(itemLabel, w.itemLabel).append(
173                labels, w.labels).append(itemValue, w.itemValue).append(itemDisabled, w.itemDisabled).append(
174                itemRendered, w.itemRendered).append(ordering, w.ordering).isEquals();
175    }
176}