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: LayoutImpl.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.Layout;
029import org.nuxeo.ecm.platform.forms.layout.api.LayoutDefinition;
030import org.nuxeo.ecm.platform.forms.layout.api.LayoutRow;
031import org.nuxeo.ecm.platform.forms.layout.api.Widget;
032
033/**
034 * Implementation for layouts.
035 *
036 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
037 */
038public class LayoutImpl implements Layout {
039
040    private static final long serialVersionUID = -8975637002024432963L;
041
042    protected String id;
043
044    protected String name;
045
046    protected String type;
047
048    protected String typeCategory;
049
050    protected String mode;
051
052    protected String template;
053
054    protected String devTemplate;
055
056    protected LayoutRow[] rows;
057
058    protected Map<String, Widget> widgetMap;
059
060    protected int columns = 0;
061
062    protected Map<String, Serializable> properties;
063
064    protected String definitionId;
065
066    protected String valueName;
067
068    protected boolean dynamic = false;
069
070    protected LayoutDefinition definition;
071
072    // needed by GWT serialization
073    protected LayoutImpl() {
074        super();
075    }
076
077    /**
078     * @deprecated since 5.5: use {@link #LayoutImpl(String, String, String, List, int, Map, String)}
079     */
080    @Deprecated
081    public LayoutImpl(String name, String mode, String template, List<LayoutRow> rows, int columns) {
082        this(name, mode, template, rows, columns, null);
083    }
084
085    /**
086     * @deprecated since 5.5: use {@link #LayoutImpl(String, String, String, List, int, Map, String)}
087     */
088    @Deprecated
089    public LayoutImpl(String name, String mode, String template, List<LayoutRow> rows, int columns,
090            Map<String, Serializable> properties) {
091        this(name, mode, template, rows, columns, properties, null);
092    }
093
094    /**
095     * @since 5.5
096     */
097    public LayoutImpl(String name, String mode, String template, List<LayoutRow> rows, int columns,
098            Map<String, Serializable> properties, String definitionId) {
099        this.name = name;
100        this.mode = mode;
101        this.template = template;
102        this.rows = rows.toArray(new LayoutRow[0]);
103        this.columns = columns;
104        this.properties = properties;
105        this.widgetMap = new HashMap<String, Widget>();
106        computeWidgetMap();
107        this.definitionId = definitionId;
108    }
109
110    protected void computeWidgetMap() {
111        if (rows == null || rows.length == 0) {
112            return;
113        }
114        for (LayoutRow row : rows) {
115            Widget[] widgets = row.getWidgets();
116            if (widgets == null || widgets.length == 0) {
117                continue;
118            }
119            for (Widget widget : widgets) {
120                if (widget != null) {
121                    widgetMap.put(widget.getName(), widget);
122                }
123            }
124        }
125    }
126
127    public String getId() {
128        return id;
129    }
130
131    @Override
132    public String getTagConfigId() {
133        StringBuilder builder = new StringBuilder();
134        builder.append(definitionId).append(";");
135        builder.append(mode).append(";");
136
137        Integer intValue = new Integer(builder.toString().hashCode());
138        return intValue.toString();
139    }
140
141    public void setId(String id) {
142        this.id = id;
143    }
144
145    public String getName() {
146        return name;
147    }
148
149    public String getMode() {
150        return mode;
151    }
152
153    public String getTemplate() {
154        return template;
155    }
156
157    public LayoutRow[] getRows() {
158        return rows;
159    }
160
161    public int getColumns() {
162        return columns;
163    }
164
165    public Widget getWidget(String name) {
166        return widgetMap.get(name);
167    }
168
169    public Map<String, Widget> getWidgetMap() {
170        return Collections.unmodifiableMap(widgetMap);
171    }
172
173    public Map<String, Serializable> getProperties() {
174        if (properties == null) {
175            return Collections.emptyMap();
176        }
177        return Collections.unmodifiableMap(properties);
178    }
179
180    public Serializable getProperty(String name) {
181        if (properties != null) {
182            return properties.get(name);
183        }
184        return null;
185    }
186
187    public void setProperty(String name, Serializable value) {
188        if (properties == null) {
189            properties = new HashMap<String, Serializable>();
190        }
191        properties.put(name, value);
192    }
193
194    @Override
195    public String getValueName() {
196        return valueName;
197    }
198
199    @Override
200    public void setValueName(String valueName) {
201        this.valueName = valueName;
202        // set it on all widgets too
203        if (rows == null || rows.length == 0) {
204            return;
205        }
206        for (LayoutRow row : rows) {
207            Widget[] widgets = row.getWidgets();
208            if (widgets == null || widgets.length == 0) {
209                continue;
210            }
211            for (Widget widget : widgets) {
212                if (widget != null) {
213                    widget.setValueName(valueName);
214                }
215            }
216        }
217    }
218
219    /**
220     * @since 6.0
221     */
222    public String getType() {
223        return type;
224    }
225
226    /**
227     * @since 6.0
228     */
229    public void setType(String type) {
230        this.type = type;
231    }
232
233    /**
234     * @since 6.0
235     */
236    public String getTypeCategory() {
237        return typeCategory;
238    }
239
240    /**
241     * @since 6.0
242     */
243    public void setTypeCategory(String typeCategory) {
244        this.typeCategory = typeCategory;
245    }
246
247    @Override
248    public String toString() {
249        final StringBuilder buf = new StringBuilder();
250
251        buf.append("LayoutImpl");
252        buf.append(" {");
253        buf.append(" name=");
254        buf.append(name);
255        buf.append(", id=");
256        buf.append(id);
257        buf.append(", mode=");
258        buf.append(mode);
259        buf.append(", template=");
260        buf.append(template);
261        buf.append(", properties=");
262        buf.append(properties);
263        buf.append('}');
264
265        return buf.toString();
266    }
267
268    public boolean isDynamic() {
269        return dynamic;
270    }
271
272    public void setDynamic(boolean dynamic) {
273        this.dynamic = dynamic;
274    }
275
276    public String getDevTemplate() {
277        return devTemplate;
278    }
279
280    public void setDevTemplate(String devTemplate) {
281        this.devTemplate = devTemplate;
282    }
283
284    public LayoutDefinition getDefinition() {
285        return definition;
286    }
287
288    public void setDefinition(LayoutDefinition definition) {
289        this.definition = definition;
290    }
291
292}