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