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    public String getName() {
069        return name;
070    }
071
072    @Override
073    public String getTagConfigId() {
074        // XXX check if widget instances should be taken into account.
075        return definitionId;
076    }
077
078    public boolean isAlwaysSelected() {
079        return alwaysSelected;
080    }
081
082    public boolean isSelectedByDefault() {
083        return selectedByDefault;
084    }
085
086    public Widget[] getWidgets() {
087        return widgets;
088    }
089
090    public int getSize() {
091        if (widgets != null) {
092            return widgets.length;
093        }
094        return 0;
095    }
096
097    public Map<String, Serializable> getProperties() {
098        if (properties == null) {
099            return Collections.emptyMap();
100        }
101        return Collections.unmodifiableMap(properties);
102    }
103
104    public Serializable getProperty(String name) {
105        if (properties != null) {
106            return properties.get(name);
107        }
108        return null;
109    }
110
111    @Override
112    public String toString() {
113        final StringBuilder buf = new StringBuilder();
114
115        buf.append("LayoutRowImpl");
116        buf.append(" {");
117        buf.append(" name=");
118        buf.append(name);
119        buf.append(", properties=");
120        buf.append(properties);
121        buf.append('}');
122
123        return buf.toString();
124    }
125
126}