001/*
002 * (C) Copyright 2006-2016 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 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
018 */
019package org.nuxeo.ecm.platform.forms.layout.api.impl;
020
021import java.io.Serializable;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.ecm.platform.forms.layout.api.FieldDefinition;
028import org.nuxeo.ecm.platform.forms.layout.api.RenderingInfo;
029import org.nuxeo.ecm.platform.forms.layout.api.Widget;
030import org.nuxeo.ecm.platform.forms.layout.api.WidgetDefinition;
031import org.nuxeo.ecm.platform.forms.layout.api.WidgetSelectOption;
032
033/**
034 * Implementation for widgets.
035 *
036 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
037 */
038public class WidgetImpl implements Widget {
039
040    private static final long serialVersionUID = 1L;
041
042    protected String id;
043
044    protected String layoutName;
045
046    protected String name;
047
048    protected String mode;
049
050    protected String type;
051
052    protected String typeCategory;
053
054    protected FieldDefinition[] fields;
055
056    protected String helpLabel;
057
058    protected Widget[] subWidgets;
059
060    protected Map<String, Serializable> properties;
061
062    protected Map<String, Serializable> controls;
063
064    protected boolean required = false;
065
066    protected String valueName;
067
068    protected String label;
069
070    protected boolean translated = false;
071
072    /**
073     * @deprecated since 5.7: use {@link #controls} instead
074     */
075    @Deprecated
076    protected boolean handlingLabels = false;
077
078    protected int level = 0;
079
080    protected WidgetSelectOption[] selectOptions;
081
082    protected List<RenderingInfo> renderingInfos;
083
084    protected String definitionId;
085
086    protected boolean dynamic = false;
087
088    protected boolean global = false;
089
090    protected WidgetDefinition definition;
091
092    // needed by GWT serialization
093    protected WidgetImpl() {
094        super();
095    }
096
097    // BBB
098    public WidgetImpl(String layoutName, String name, String mode, String type, String valueName,
099            FieldDefinition[] fields, String label, String helpLabel, boolean translated,
100            Map<String, Serializable> properties, boolean required, Widget[] subWidgets, int level,
101            WidgetSelectOption[] selectOptions, String definitionId) {
102        this(layoutName, name, mode, type, valueName, fields, label, helpLabel, translated, properties, required,
103                subWidgets, level, selectOptions, definitionId, null);
104    }
105
106    /**
107     * @since 5.5
108     */
109    // BBB
110    public WidgetImpl(String layoutName, String name, String mode, String type, String valueName,
111            FieldDefinition[] fields, String label, String helpLabel, boolean translated,
112            Map<String, Serializable> properties, boolean required, Widget[] subWidgets, int level,
113            WidgetSelectOption[] selectOptions, String definitionId, List<RenderingInfo> renderingInfos) {
114        this(layoutName, name, mode, type, valueName, fields, label, helpLabel, translated, false, properties,
115                required, subWidgets, level, selectOptions, definitionId, renderingInfos);
116    }
117
118    /**
119     * @since 5.6
120     */
121    public WidgetImpl(String layoutName, String name, String mode, String type, String valueName,
122            FieldDefinition[] fields, String label, String helpLabel, boolean translated, boolean handlingLabels,
123            Map<String, Serializable> properties, boolean required, Widget[] subWidgets, int level,
124            WidgetSelectOption[] selectOptions, String definitionId, List<RenderingInfo> renderingInfos) {
125        this.layoutName = layoutName;
126        this.name = name;
127        this.mode = mode;
128        this.type = type;
129        this.valueName = valueName;
130        this.fields = fields;
131        this.label = label;
132        this.helpLabel = helpLabel;
133        this.translated = translated;
134        this.handlingLabels = handlingLabels;
135        this.properties = properties;
136        this.required = required;
137        this.subWidgets = subWidgets;
138        this.level = level;
139        this.selectOptions = selectOptions;
140        this.definitionId = definitionId;
141        this.renderingInfos = renderingInfos;
142    }
143
144    @Override
145    public String getId() {
146        return id;
147    }
148
149    @Override
150    public String getTagConfigId() {
151        StringBuilder builder = new StringBuilder();
152        builder.append(definitionId).append(";");
153        builder.append(layoutName).append(";");
154        builder.append(mode).append(";");
155        builder.append(level).append(";");
156
157        Integer intValue = Integer.valueOf(builder.toString().hashCode());
158        return intValue.toString();
159    }
160
161    @Override
162    public void setId(String id) {
163        this.id = id;
164    }
165
166    @Override
167    public String getLayoutName() {
168        return layoutName;
169    }
170
171    @Override
172    public String getName() {
173        return name;
174    }
175
176    @Override
177    public String getMode() {
178        return mode;
179    }
180
181    @Override
182    public String getType() {
183        return type;
184    }
185
186    @Override
187    public String getTypeCategory() {
188        return typeCategory;
189    }
190
191    public void setTypeCategory(String typeCategory) {
192        this.typeCategory = typeCategory;
193    }
194
195    @Override
196    public String getLabel() {
197        if (label == null) {
198            // compute default label name
199            label = "label.widget." + layoutName + "." + name;
200        }
201        return label;
202    }
203
204    @Override
205    public String getHelpLabel() {
206        return helpLabel;
207    }
208
209    @Override
210    public boolean isTranslated() {
211        return translated;
212    }
213
214    @Override
215    public boolean isHandlingLabels() {
216        Map<String, Serializable> controls = getControls();
217        if (controls != null && controls.containsKey("handleLabels")) {
218            Serializable handling = controls.get("handleLabels");
219            if (handling != null) {
220                return Boolean.parseBoolean(handling.toString());
221            }
222        }
223        // BBB
224        return handlingLabels;
225    }
226
227    @Override
228    public Map<String, Serializable> getProperties() {
229        if (properties == null) {
230            return Collections.emptyMap();
231        }
232        return Collections.unmodifiableMap(properties);
233    }
234
235    @Override
236    public Serializable getProperty(String name) {
237        if (properties != null) {
238            return properties.get(name);
239        }
240        return null;
241    }
242
243    @Override
244    public void setProperty(String name, Serializable value) {
245        if (properties == null) {
246            properties = new HashMap<>();
247        }
248        properties.put(name, value);
249    }
250
251    @Override
252    public Map<String, Serializable> getControls() {
253        if (controls == null) {
254            return Collections.emptyMap();
255        }
256        return Collections.unmodifiableMap(controls);
257    }
258
259    @Override
260    public Serializable getControl(String name) {
261        if (controls != null) {
262            return controls.get(name);
263        }
264        return null;
265    }
266
267    @Override
268    public void setControl(String name, Serializable value) {
269        if (controls == null) {
270            controls = new HashMap<>();
271        }
272        controls.put(name, value);
273    }
274
275    /**
276     * @since 6.0
277     */
278    public void setControls(Map<String, Serializable> controls) {
279        this.controls = controls;
280    }
281
282    @Override
283    public boolean isRequired() {
284        return required;
285    }
286
287    @Override
288    public FieldDefinition[] getFieldDefinitions() {
289        return fields;
290    }
291
292    @Override
293    public Widget[] getSubWidgets() {
294        return subWidgets;
295    }
296
297    @Override
298    public String getValueName() {
299        return valueName;
300    }
301
302    @Override
303    public void setValueName(String valueName) {
304        this.valueName = valueName;
305    }
306
307    @Override
308    public int getLevel() {
309        return level;
310    }
311
312    @Override
313    public WidgetSelectOption[] getSelectOptions() {
314        return selectOptions;
315    }
316
317    @Override
318    public List<RenderingInfo> getRenderingInfos() {
319        return renderingInfos;
320    }
321
322    @Override
323    public boolean isDynamic() {
324        return dynamic;
325    }
326
327    public void setDynamic(boolean dynamic) {
328        this.dynamic = dynamic;
329    }
330
331    @Override
332    public boolean isGlobal() {
333        return global;
334    }
335
336    public void setGlobal(boolean global) {
337        this.global = global;
338    }
339
340    @Override
341    public WidgetDefinition getDefinition() {
342        return definition;
343    }
344
345    public void setDefinition(WidgetDefinition definition) {
346        this.definition = definition;
347    }
348
349    @Override
350    public String toString() {
351        final StringBuilder sb = new StringBuilder();
352
353        sb.append("WidgetImpl");
354        sb.append(" {");
355        sb.append(" name=");
356        sb.append(name);
357        sb.append(", layoutName=");
358        sb.append(layoutName);
359        sb.append(", id=");
360        sb.append(id);
361        sb.append(", mode=");
362        sb.append(mode);
363        sb.append(", type=");
364        sb.append(type);
365        sb.append(", label=");
366        sb.append(label);
367        sb.append(", helpLabel=");
368        sb.append(helpLabel);
369        sb.append(", translated=");
370        sb.append(translated);
371        sb.append(", handlingLabels=");
372        sb.append(handlingLabels);
373        sb.append(", required=");
374        sb.append(required);
375        sb.append(", properties=");
376        sb.append(properties);
377        sb.append(", controls=");
378        sb.append(controls);
379        sb.append(", valueName=");
380        sb.append(valueName);
381        sb.append(", level=");
382        sb.append(level);
383        sb.append('}');
384
385        return sb.toString();
386    }
387
388}