001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php 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 */ 016package org.kuali.rice.krad.uif.layout; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.rice.krad.uif.CssConstants; 020import org.kuali.rice.krad.uif.CssConstants.Padding; 021import org.kuali.rice.krad.uif.UifConstants.Orientation; 022import org.kuali.rice.krad.uif.container.Container; 023import org.kuali.rice.krad.uif.field.InputField; 024import org.kuali.rice.krad.uif.view.View; 025import org.kuali.rice.krad.uif.component.Component; 026 027import java.util.ArrayList; 028import java.util.List; 029 030/** 031 * Layout manager that organizes components in a single row (horizontal) or 032 * column (vertical) 033 * 034 * <p> 035 * Although a table based template could be used, setup is done to also support 036 * a CSS based template. The items in the <code>Container</code> instance are 037 * rendered sequentially wrapping each one with a span element. The padding 038 * property can be configured to space the elements as needed. To achieve a 039 * vertical orientation, the span style is set to block. Additional styling can 040 * be set for the items by using the itemSpanStyle property. 041 * </p> 042 * 043 * @author Kuali Rice Team (rice.collab@kuali.org) 044 */ 045public class BoxLayoutManager extends LayoutManagerBase { 046 private static final long serialVersionUID = 4467342272983290044L; 047 048 private Orientation orientation; 049 private String padding; 050 051 private String itemStyle; 052 private List<String> itemStyleClasses; 053 054 private boolean layoutFieldErrors; 055 056 public BoxLayoutManager() { 057 super(); 058 059 orientation = Orientation.HORIZONTAL; 060 itemStyleClasses = new ArrayList<String>(); 061 } 062 063 /** 064 * The following initialization is performed: 065 * 066 * <ul> 067 * <li>Set the itemSpanStyle</li> 068 * </ul> 069 * 070 * @see org.kuali.rice.krad.uif.component.ComponentBase#performInitialization(org.kuali.rice.krad.uif.view.View, 071 * java.lang.Object,org.kuali.rice.krad.uif.container.Container) 072 */ 073 @Override 074 public void performInitialization(View view, Object model, Container container) { 075 super.performInitialization(view, model, container); 076 077 if(StringUtils.isBlank(itemStyle)){ 078 itemStyle = ""; 079 } 080 081 if(StringUtils.isNotEmpty(padding)) { 082 if (orientation.equals(Orientation.VERTICAL)) { 083 // set item to block which will cause a line break and margin 084 // bottom for padding 085 itemStyle += CssConstants.getCssStyle(Padding.PADDING_BOTTOM, padding); 086 } 087 else { 088 // set margin right for padding 089 itemStyle += CssConstants.getCssStyle(Padding.PADDING_RIGHT, padding); 090 } 091 } 092 093 //classes to identify this layout in jQuery and to clear the float correctly in all browsers 094 this.addStyleClass("fieldLine"); 095 this.addStyleClass("clearfix"); 096 097 for (Component c : container.getItems()) { 098 if (c != null) { 099 if (orientation.equals(Orientation.HORIZONTAL)) { 100 // in a horizontal box layout errors are placed in a div next to all fields, 101 // set the errorsField to know that we are using an alternate container for them 102 if (c instanceof InputField) { 103 ((InputField) c).getErrorsField().setAlternateContainer(true); 104 layoutFieldErrors = true; 105 } 106 } 107 108 if (container.isFieldContainer()) { 109 if (c instanceof InputField) { 110 ((InputField) c).getErrorsField().setAlternateContainer(true); 111 layoutFieldErrors = true; 112 } 113 } 114 } 115 } 116 } 117 118 /** 119 * @see org.kuali.rice.krad.uif.layout.LayoutManagerBase#performFinalize(org.kuali.rice.krad.uif.view.View, 120 * java.lang.Object, org.kuali.rice.krad.uif.container.Container) 121 */ 122 @Override 123 public void performFinalize(View view, Object model, Container container) { 124 super.performFinalize(view, model, container); 125 } 126 127 /** 128 * Indicates whether the components should be rendered in a horizontal or 129 * vertical column 130 * 131 * @return Orientation orientation configured for layout 132 */ 133 public Orientation getOrientation() { 134 return this.orientation; 135 } 136 137 /** 138 * Setter for the orientation for layout 139 * 140 * @param orientation 141 */ 142 public void setOrientation(Orientation orientation) { 143 this.orientation = orientation; 144 } 145 146 /** 147 * Amount of separation between each item 148 * 149 * <p> 150 * For horizontal orientation, this will be the right padding for each item. 151 * For vertical, it will be the bottom padding for each item. The value can 152 * be a fixed length (like px) or percentage 153 * </p> 154 * 155 * @return 156 */ 157 public String getPadding() { 158 return this.padding; 159 } 160 161 /** 162 * Setter for the item padding 163 * 164 * @param padding 165 */ 166 public void setPadding(String padding) { 167 this.padding = padding; 168 } 169 170 /** 171 * Used by the render to set the style on the span element that wraps the 172 * item. By using a wrapping span the items can be aligned based on the 173 * orientation and given the correct padding 174 * 175 * @return String css style string 176 */ 177 public String getItemStyle() { 178 return this.itemStyle; 179 } 180 181 /** 182 * Setter for the span style 183 * 184 * @param itemStyle 185 */ 186 public void setItemStyle(String itemStyle) { 187 this.itemStyle = itemStyle; 188 } 189 190 /** 191 * List of style classes that should be applied to each span that wraps the item in the layout 192 * 193 * @return List<String> 194 */ 195 public List<String> getItemStyleClasses() { 196 return itemStyleClasses; 197 } 198 199 /** 200 * Setter for the list of style classes that should apply to each item span 201 * 202 * @param itemStyleClasses 203 */ 204 public void setItemStyleClasses(List<String> itemStyleClasses) { 205 this.itemStyleClasses = itemStyleClasses; 206 } 207 208 /** 209 * Builds the HTML class attribute string by combining the item styleClasses list 210 * with a space delimiter 211 * 212 * @return String class attribute string 213 */ 214 public String getItemStyleClassesAsString() { 215 if (itemStyleClasses != null) { 216 return StringUtils.join(itemStyleClasses, " "); 217 } 218 219 return ""; 220 } 221 222 /** 223 * @return the layoutFieldErrors 224 */ 225 public boolean isLayoutFieldErrors() { 226 return this.layoutFieldErrors; 227 } 228 229 /** 230 * @param layoutFieldErrors the layoutFieldErrors to set 231 */ 232 public void setLayoutFieldErrors(boolean layoutFieldErrors) { 233 this.layoutFieldErrors = layoutFieldErrors; 234 } 235 236}