001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.forms.layout.api.impl;
020
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.Map;
024
025import org.nuxeo.ecm.platform.forms.layout.api.WidgetSelectOption;
026
027/**
028 * @author Anahide Tchertchian
029 * @since 5.4.2
030 */
031public class WidgetSelectOptionImpl implements WidgetSelectOption {
032
033    private static final long serialVersionUID = 1L;
034
035    protected Serializable value;
036
037    protected String var;
038
039    protected String itemLabel;
040
041    protected Map<String, String> labels;
042
043    protected String itemValue;
044
045    protected Serializable itemDisabled;
046
047    protected Serializable itemRendered;
048
049    // needed by GWT serialization
050    protected WidgetSelectOptionImpl() {
051        super();
052    }
053
054    public WidgetSelectOptionImpl(String itemLabel, String itemValue) {
055        this(null, null, itemLabel, itemValue);
056    }
057
058    public WidgetSelectOptionImpl(Serializable value, String var, String itemLabel, String itemValue) {
059        this(value, var, itemLabel, itemValue, null, null);
060    }
061
062    public WidgetSelectOptionImpl(Serializable value, String var, String itemLabel, String itemValue,
063            Serializable itemDisabled, Serializable itemRendered) {
064        super();
065        this.value = value;
066        this.var = var;
067        this.itemLabel = itemLabel;
068        this.itemValue = itemValue;
069        this.itemDisabled = itemDisabled;
070        this.itemRendered = itemRendered;
071    }
072
073    public Serializable getValue() {
074        return value;
075    }
076
077    public String getVar() {
078        return var;
079    }
080
081    public String getItemLabel() {
082        return itemLabel;
083    }
084
085    @Override
086    public String getItemLabel(String locale) {
087        return labels.get(locale);
088    }
089
090    @Override
091    public Map<String, String> getItemLabels() {
092        return labels;
093    }
094
095    public void setItemLabels(Map<String, String> labels) {
096        this.labels = labels;
097    }
098
099    public String getItemValue() {
100        return itemValue;
101    }
102
103    public Serializable getItemDisabled() {
104        return itemDisabled;
105    }
106
107    public Serializable getItemRendered() {
108        return itemRendered;
109    }
110
111    @Override
112    public String getTagConfigId() {
113        StringBuilder builder = new StringBuilder();
114        builder.append(value).append(";");
115        builder.append(var).append(";");
116        builder.append(itemLabel).append(";");
117        if (labels != null) {
118            builder.append(labels.toString());
119        }
120        builder.append(";");
121        builder.append(itemValue).append(";");
122        if (itemDisabled != null) {
123            builder.append(itemDisabled.toString());
124        }
125        builder.append(";");
126        if (itemRendered != null) {
127            builder.append(itemRendered.toString());
128        }
129        builder.append(";");
130
131        Integer intValue = new Integer(builder.toString().hashCode());
132        return intValue.toString();
133    }
134
135    @Override
136    public WidgetSelectOption clone() {
137        WidgetSelectOptionImpl clone = new WidgetSelectOptionImpl(value, var, itemLabel, itemValue, itemDisabled,
138                itemRendered);
139        if (labels != null) {
140            clone.setItemLabels(new HashMap<String, String>(labels));
141        }
142        return clone;
143    }
144
145    /**
146     * @since 7.2
147     */
148    @Override
149    public boolean equals(Object obj) {
150        if (!(obj instanceof WidgetSelectOptionImpl)) {
151            return false;
152        }
153        if (obj == this) {
154            return true;
155        }
156        WidgetSelectOptionImpl w = (WidgetSelectOptionImpl) obj;
157        return new EqualsBuilder().append(value, w.value).append(var, w.var).append(itemLabel, w.itemLabel).append(
158                labels, w.labels).append(itemValue, w.itemValue).append(itemDisabled, w.itemDisabled).append(
159                itemRendered, w.itemRendered).isEquals();
160    }
161
162}