001/*
002 * (C) Copyright 2006-2007 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 *
019 * $Id: LayoutRowDescriptor.java 26053 2007-10-16 01:45:43Z atchertchian $
020 */
021
022package org.nuxeo.ecm.platform.forms.layout.descriptors;
023
024import java.io.Serializable;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.nuxeo.common.xmap.annotation.XNode;
029import org.nuxeo.common.xmap.annotation.XNodeList;
030import org.nuxeo.common.xmap.annotation.XNodeMap;
031import org.nuxeo.common.xmap.annotation.XObject;
032import org.nuxeo.ecm.platform.forms.layout.api.LayoutRowDefinition;
033import org.nuxeo.ecm.platform.forms.layout.api.WidgetReference;
034import org.nuxeo.ecm.platform.forms.layout.api.impl.LayoutRowDefinitionImpl;
035
036/**
037 * Layout row descriptor.
038 *
039 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
040 */
041@XObject("row")
042public class LayoutRowDescriptor {
043
044    @XNode("@name")
045    String name;
046
047    @XNode("@selectedByDefault")
048    boolean selectedByDefault = true;
049
050    @XNode("@alwaysSelected")
051    boolean alwaysSelected = false;
052
053    @XNodeList(value = "widget", type = WidgetReferenceDescriptor[].class, componentType = WidgetReferenceDescriptor.class)
054    WidgetReferenceDescriptor[] widgets = new WidgetReferenceDescriptor[0];
055
056    @XNodeMap(value = "properties", key = "@mode", type = HashMap.class, componentType = PropertiesDescriptor.class)
057    Map<String, PropertiesDescriptor> properties = new HashMap<String, PropertiesDescriptor>();
058
059    public String getName() {
060        return name;
061    }
062
063    public boolean isSelectedByDefault() {
064        return selectedByDefault;
065    }
066
067    public boolean isAlwaysSelected() {
068        return alwaysSelected;
069    }
070
071    public int getSize() {
072        return widgets.length;
073    }
074
075    public String[] getWidgets() {
076        String[] names = new String[widgets.length];
077        for (int i = 0; i < widgets.length; i++) {
078            names[i] = widgets[i].getName();
079        }
080        return names;
081    }
082
083    public Map<String, Serializable> getProperties(String layoutMode) {
084        return WidgetDescriptor.getProperties(properties, layoutMode);
085    }
086
087    public Map<String, Map<String, Serializable>> getProperties() {
088        return WidgetDescriptor.getProperties(properties);
089    }
090
091    public LayoutRowDefinition getLayoutRowDefinition() {
092        WidgetReference[] cwidgets = null;
093        if (widgets != null) {
094            cwidgets = new WidgetReference[widgets.length];
095            for (int i = 0; i < widgets.length; i++) {
096                cwidgets[i] = widgets[i].getWidgetReference();
097            }
098        }
099        LayoutRowDefinition clone = new LayoutRowDefinitionImpl(name, getProperties(), cwidgets, alwaysSelected,
100                selectedByDefault);
101        return clone;
102    }
103}