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.List;
024import java.util.Map;
025
026import org.nuxeo.ecm.platform.forms.layout.api.LayoutRow;
027import org.nuxeo.ecm.platform.forms.layout.api.Widget;
028
029/**
030 * Implementation for layout rows.
031 *
032 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
033 */
034public class LayoutRowImpl implements LayoutRow {
035
036    private static final long serialVersionUID = 1528198770297610864L;
037
038    protected String name;
039
040    protected boolean selectedByDefault = true;
041
042    protected boolean alwaysSelected = false;
043
044    protected Widget[] widgets;
045
046    protected Map<String, Serializable> properties;
047
048    protected String definitionId;
049
050    // needed by GWT serialization
051    protected LayoutRowImpl() {
052        super();
053    }
054
055    /**
056     * @since 5.5
057     */
058    public LayoutRowImpl(String name, boolean selectedByDefault, boolean alwaysSelected, List<Widget> widgets,
059            Map<String, Serializable> properties, String definitionId) {
060        this.name = name;
061        this.selectedByDefault = selectedByDefault;
062        this.alwaysSelected = alwaysSelected;
063        this.widgets = widgets.toArray(new Widget[0]);
064        this.properties = properties;
065        this.definitionId = definitionId;
066    }
067
068    @Override
069    public String getName() {
070        return name;
071    }
072
073    @Override
074    public String getTagConfigId() {
075        // XXX check if widget instances should be taken into account.
076        return definitionId;
077    }
078
079    @Override
080    public boolean isAlwaysSelected() {
081        return alwaysSelected;
082    }
083
084    @Override
085    public boolean isSelectedByDefault() {
086        return selectedByDefault;
087    }
088
089    @Override
090    public Widget[] getWidgets() {
091        return widgets;
092    }
093
094    @Override
095    public int getSize() {
096        if (widgets != null) {
097            return widgets.length;
098        }
099        return 0;
100    }
101
102    @Override
103    public Map<String, Serializable> getProperties() {
104        if (properties == null) {
105            return Collections.emptyMap();
106        }
107        return Collections.unmodifiableMap(properties);
108    }
109
110    @Override
111    public Serializable getProperty(String name) {
112        if (properties != null) {
113            return properties.get(name);
114        }
115        return null;
116    }
117
118    @Override
119    public String toString() {
120        final StringBuilder sb = new StringBuilder();
121
122        sb.append("LayoutRowImpl");
123        sb.append(" {");
124        sb.append(" name=");
125        sb.append(name);
126        sb.append(", properties=");
127        sb.append(properties);
128        sb.append('}');
129
130        return sb.toString();
131    }
132
133}