001/* 002 * (C) Copyright 2010-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 * 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 @SuppressWarnings("unused") 054 protected LayoutRowDefinitionImpl() { 055 super(); 056 } 057 058 /** 059 * Instantiates a new {@code LayoutRowDefinitionImpl} with a given widget name and category. 060 * 061 * @param name the row name 062 * @param widget the widget name 063 * @param category the category 064 * @since 5.6 065 */ 066 public LayoutRowDefinitionImpl(String name, String widget, String category) { 067 this.name = name; 068 this.properties = null; 069 if (widget == null) { 070 this.widgets = new WidgetReferenceImpl[0]; 071 } else { 072 WidgetReferenceImpl widgetRef = new WidgetReferenceImpl(category, widget); 073 this.widgets = new WidgetReferenceImpl[] { widgetRef }; 074 } 075 this.alwaysSelected = false; 076 this.selectedByDefault = true; 077 } 078 079 public LayoutRowDefinitionImpl(String name, String widget) { 080 this(name, widget, null); 081 } 082 083 public LayoutRowDefinitionImpl(String name, Map<String, Map<String, Serializable>> properties, 084 List<WidgetReference> widgets, boolean alwaysSelected, boolean selectedByDefault) { 085 super(); 086 this.name = name; 087 this.properties = properties; 088 if (widgets == null) { 089 this.widgets = new WidgetReferenceImpl[0]; 090 } else { 091 this.widgets = widgets.toArray(new WidgetReference[0]); 092 } 093 this.alwaysSelected = alwaysSelected; 094 this.selectedByDefault = selectedByDefault; 095 } 096 097 public LayoutRowDefinitionImpl(String name, Map<String, Map<String, Serializable>> properties, 098 WidgetReference[] widgets, boolean alwaysSelected, boolean selectedByDefault) { 099 super(); 100 this.name = name; 101 this.properties = properties; 102 this.widgets = widgets; 103 this.alwaysSelected = alwaysSelected; 104 this.selectedByDefault = selectedByDefault; 105 } 106 107 @Override 108 public String getName() { 109 return name; 110 } 111 112 @Override 113 public String getDefaultName(int index) { 114 return "layout_row_" + index; 115 } 116 117 @Override 118 public Map<String, Serializable> getProperties(String layoutMode) { 119 return WidgetDefinitionImpl.getProperties(properties, layoutMode); 120 } 121 122 @Override 123 public Map<String, Map<String, Serializable>> getProperties() { 124 return properties; 125 } 126 127 @Override 128 public int getSize() { 129 return widgets.length; 130 } 131 132 @Override 133 public WidgetReference[] getWidgetReferences() { 134 return widgets; 135 } 136 137 @Override 138 public boolean isAlwaysSelected() { 139 return alwaysSelected; 140 } 141 142 @Override 143 public boolean isSelectedByDefault() { 144 return selectedByDefault; 145 } 146 147 @Override 148 public LayoutRowDefinition clone() { 149 Map<String, Map<String, Serializable>> cprops = null; 150 if (properties != null) { 151 cprops = new HashMap<>(); 152 for (Map.Entry<String, Map<String, Serializable>> entry : properties.entrySet()) { 153 Map<String, Serializable> subProps = entry.getValue(); 154 Map<String, Serializable> csubProps = null; 155 if (subProps != null) { 156 csubProps = new HashMap<>(); 157 csubProps.putAll(subProps); 158 } 159 cprops.put(entry.getKey(), csubProps); 160 } 161 } 162 WidgetReference[] cwidgets = null; 163 if (widgets != null) { 164 cwidgets = new WidgetReference[widgets.length]; 165 for (int i = 0; i < widgets.length; i++) { 166 cwidgets[i] = widgets[i].clone(); 167 } 168 } 169 return new LayoutRowDefinitionImpl(name, cprops, cwidgets, alwaysSelected, selectedByDefault); 170 } 171 172 /** 173 * @since 7.2 174 */ 175 @Override 176 public boolean equals(Object obj) { 177 if (!(obj instanceof LayoutRowDefinitionImpl)) { 178 return false; 179 } 180 if (obj == this) { 181 return true; 182 } 183 LayoutRowDefinitionImpl ld = (LayoutRowDefinitionImpl) obj; 184 return new EqualsBuilder().append(name, ld.name) 185 .append(properties, ld.properties) 186 .append(widgets, ld.widgets) 187 .append(alwaysSelected, ld.alwaysSelected) 188 .append(selectedByDefault, ld.selectedByDefault) 189 .isEquals(); 190 } 191 192}