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