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