001/* 002 * (C) Copyright 2010 Nuxeo SA (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 * Anahide Tchertchian 016 */ 017package org.nuxeo.ecm.platform.forms.layout.api.impl; 018 019import java.io.Serializable; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.nuxeo.ecm.platform.forms.layout.api.LayoutRowDefinition; 025import org.nuxeo.ecm.platform.forms.layout.api.WidgetReference; 026 027/** 028 * Default implementation for a layout row definition. 029 * <p> 030 * Useful to compute rows independently from the layout service. 031 * 032 * @author Anahide Tchertchian 033 * @author Antoine Taillefer 034 * @since 5.4 035 */ 036public class LayoutRowDefinitionImpl implements LayoutRowDefinition { 037 038 private static final long serialVersionUID = 1L; 039 040 protected String name; 041 042 protected Map<String, Map<String, Serializable>> properties; 043 044 protected WidgetReference[] widgets; 045 046 protected boolean alwaysSelected = false; 047 048 protected boolean selectedByDefault = true; 049 050 // needed by GWT serialization 051 protected LayoutRowDefinitionImpl() { 052 super(); 053 } 054 055 /** 056 * Instantiates a new {@code LayoutRowDefinitionImpl} with a given widget name and category. 057 * 058 * @param name the row name 059 * @param widget the widget name 060 * @param category the category 061 * @since 5.6 062 */ 063 public LayoutRowDefinitionImpl(String name, String widget, String category) { 064 this.name = name; 065 this.properties = null; 066 if (widget == null) { 067 this.widgets = new WidgetReferenceImpl[0]; 068 } else { 069 WidgetReferenceImpl widgetRef = new WidgetReferenceImpl(category, widget); 070 this.widgets = new WidgetReferenceImpl[] { widgetRef }; 071 } 072 this.alwaysSelected = false; 073 this.selectedByDefault = true; 074 } 075 076 public LayoutRowDefinitionImpl(String name, String widget) { 077 this(name, widget, null); 078 } 079 080 public LayoutRowDefinitionImpl(String name, Map<String, Map<String, Serializable>> properties, 081 List<WidgetReference> widgets, boolean alwaysSelected, boolean selectedByDefault) { 082 super(); 083 this.name = name; 084 this.properties = properties; 085 if (widgets == null) { 086 this.widgets = new WidgetReferenceImpl[0]; 087 } else { 088 WidgetReference[] refs = new WidgetReference[widgets.size()]; 089 for (int i = 0; i < widgets.size(); i++) { 090 refs[i] = widgets.get(i); 091 } 092 this.widgets = widgets.toArray(new WidgetReference[0]); 093 } 094 this.alwaysSelected = alwaysSelected; 095 this.selectedByDefault = selectedByDefault; 096 } 097 098 public LayoutRowDefinitionImpl(String name, Map<String, Map<String, Serializable>> properties, 099 WidgetReference[] widgets, boolean alwaysSelected, boolean selectedByDefault) { 100 super(); 101 this.name = name; 102 this.properties = properties; 103 this.widgets = widgets; 104 this.alwaysSelected = alwaysSelected; 105 this.selectedByDefault = selectedByDefault; 106 } 107 108 @Override 109 public String getName() { 110 return name; 111 } 112 113 @Override 114 public String getDefaultName(int index) { 115 return "layout_row_" + index; 116 } 117 118 @Override 119 public Map<String, Serializable> getProperties(String layoutMode) { 120 return WidgetDefinitionImpl.getProperties(properties, layoutMode); 121 } 122 123 @Override 124 public Map<String, Map<String, Serializable>> getProperties() { 125 return properties; 126 } 127 128 @Override 129 public int getSize() { 130 return widgets.length; 131 } 132 133 @Override 134 public String[] getWidgets() { 135 String[] names = new String[widgets.length]; 136 for (int i = 0; i < widgets.length; i++) { 137 names[i] = widgets[i].getName(); 138 } 139 return names; 140 } 141 142 @Override 143 public WidgetReference[] getWidgetReferences() { 144 return widgets; 145 } 146 147 @Override 148 public boolean isAlwaysSelected() { 149 return alwaysSelected; 150 } 151 152 @Override 153 public boolean isSelectedByDefault() { 154 return selectedByDefault; 155 } 156 157 @Override 158 public LayoutRowDefinition clone() { 159 Map<String, Map<String, Serializable>> cprops = null; 160 if (properties != null) { 161 cprops = new HashMap<String, Map<String, Serializable>>(); 162 for (Map.Entry<String, Map<String, Serializable>> entry : properties.entrySet()) { 163 Map<String, Serializable> subProps = entry.getValue(); 164 Map<String, Serializable> csubProps = null; 165 if (subProps != null) { 166 csubProps = new HashMap<String, Serializable>(); 167 csubProps.putAll(subProps); 168 } 169 cprops.put(entry.getKey(), csubProps); 170 } 171 } 172 WidgetReference[] cwidgets = null; 173 if (widgets != null) { 174 cwidgets = new WidgetReference[widgets.length]; 175 for (int i = 0; i < widgets.length; i++) { 176 cwidgets[i] = widgets[i].clone(); 177 } 178 } 179 LayoutRowDefinition clone = new LayoutRowDefinitionImpl(name, cprops, cwidgets, alwaysSelected, 180 selectedByDefault); 181 return clone; 182 } 183 184 /** 185 * @since 7.2 186 */ 187 @Override 188 public boolean equals(Object obj) { 189 if (!(obj instanceof LayoutRowDefinitionImpl)) { 190 return false; 191 } 192 if (obj == this) { 193 return true; 194 } 195 LayoutRowDefinitionImpl ld = (LayoutRowDefinitionImpl) obj; 196 return new EqualsBuilder().append(name, ld.name).append(properties, ld.properties).append(widgets, ld.widgets).append( 197 alwaysSelected, ld.alwaysSelected).append(selectedByDefault, ld.selectedByDefault).isEquals(); 198 } 199 200}