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