View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.layout;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
21  import org.kuali.rice.krad.uif.CssConstants;
22  import org.kuali.rice.krad.uif.CssConstants.Padding;
23  import org.kuali.rice.krad.uif.UifConstants.Orientation;
24  import org.kuali.rice.krad.uif.container.Container;
25  import org.kuali.rice.krad.uif.view.View;
26  import org.kuali.rice.krad.uif.component.Component;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  /**
32   * Layout manager that organizes components in a single row (horizontal) or
33   * column (vertical)
34   *
35   * <p>
36   * Although a table based template could be used, setup is done to also support
37   * a CSS based template. The items in the <code>Container</code> instance are
38   * rendered sequentially wrapping each one with a span element. The padding
39   * property can be configured to space the elements as needed. To achieve a
40   * vertical orientation, the span style is set to block. Additional styling can
41   * be set for the items by using the itemSpanStyle property.
42   * </p>
43   *
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   */
46  @BeanTag(name="boxLayoutManager")
47  public class BoxLayoutManager extends LayoutManagerBase {
48  	private static final long serialVersionUID = 4467342272983290044L;
49  
50  	private Orientation orientation;
51  	private String padding;
52  
53  	private String itemStyle;
54      private List<String> itemStyleClasses;
55  
56  	public BoxLayoutManager() {
57  		super();
58  
59          itemStyle = "";
60  		orientation = Orientation.HORIZONTAL;
61          itemStyleClasses = new ArrayList<String>();
62  	}
63  
64  	/**
65       * Sets the item span style
66       *
67  	 * @see org.kuali.rice.krad.uif.layout.LayoutManagerBase#performFinalize(org.kuali.rice.krad.uif.view.View,
68  	 *      java.lang.Object, org.kuali.rice.krad.uif.container.Container)
69  	 */
70  	@Override
71  	public void performFinalize(View view, Object model, Container container) {
72  		super.performFinalize(view, model, container);
73  
74          if (StringUtils.isBlank(itemStyle)) {
75              itemStyle = "";
76          }
77  
78          if (StringUtils.isNotEmpty(padding)) {
79              if (orientation.equals(Orientation.VERTICAL)) {
80                  // set item to block which will cause a line break and margin
81                  // bottom for padding
82                  itemStyle += CssConstants.getCssStyle(Padding.PADDING_BOTTOM, padding);
83              } else {
84                  // set margin right for padding
85                  itemStyle += CssConstants.getCssStyle(Padding.PADDING_RIGHT, padding);
86              }
87          }
88  
89          // classes to identify this layout in jQuery and to clear the float correctly in all browsers
90          this.addStyleClass("clearfix");
91  
92          for (Component c : container.getItems()) {
93              if (c != null) {
94                  // add item styles to the the item
95                  List<String> styleClasses = c.getCssClasses();
96                  if (orientation.equals(Orientation.HORIZONTAL)) {
97                      styleClasses.add("uif-boxLayoutHorizontalItem");
98                      styleClasses.addAll(this.getItemStyleClasses());
99                  } else {
100                     styleClasses.add("uif-boxLayoutVerticalItem");
101                     styleClasses.addAll(this.getItemStyleClasses());
102                     styleClasses.add("clearfix");
103                 }
104                 c.setCssClasses(styleClasses);
105 
106                 if (c.getStyle() != null && !c.getStyle().endsWith(";")) {
107                     c.appendToStyle(";" + this.getItemStyle());
108                 } else {
109                     c.appendToStyle(this.getItemStyle());
110                 }
111             }
112         }
113     }
114 
115 	/**
116 	 * Indicates whether the components should be rendered in a horizontal or
117 	 * vertical column
118 	 *
119 	 * @return Orientation orientation configured for layout
120 	 */
121     @BeanTagAttribute(name="orientation", type= BeanTagAttribute.AttributeType.SINGLEBEAN)
122 	public Orientation getOrientation() {
123 		return this.orientation;
124 	}
125 
126 	/**
127 	 * Setter for the orientation for layout
128 	 *
129 	 * @param orientation
130 	 */
131 	public void setOrientation(Orientation orientation) {
132 		this.orientation = orientation;
133 	}
134 
135 	/**
136 	 * Amount of separation between each item
137 	 *
138 	 * <p>
139 	 * For horizontal orientation, this will be the right padding for each item.
140 	 * For vertical, it will be the bottom padding for each item. The value can
141 	 * be a fixed length (like px) or percentage
142 	 * </p>
143 	 *
144 	 * @return
145 	 */
146     @BeanTagAttribute(name="padding")
147 	public String getPadding() {
148 		return this.padding;
149 	}
150 
151 	/**
152 	 * Setter for the item padding
153 	 *
154 	 * @param padding
155 	 */
156 	public void setPadding(String padding) {
157 		this.padding = padding;
158 	}
159 
160 	/**
161 	 * Used by the render to set the style on the span element that wraps the
162 	 * item. By using a wrapping span the items can be aligned based on the
163 	 * orientation and given the correct padding
164 	 *
165 	 * @return String css style string
166 	 */
167     @BeanTagAttribute(name="itemStyle")
168 	public String getItemStyle() {
169 		return this.itemStyle;
170 	}
171 
172 	/**
173 	 * Setter for the span style
174 	 *
175 	 * @param itemStyle
176 	 */
177 	public void setItemStyle(String itemStyle) {
178 		this.itemStyle = itemStyle;
179 	}
180 
181     /**
182      * List of style classes that should be applied to each span that wraps the item in the layout
183      *
184      * @return List<String>
185      */
186     @BeanTagAttribute(name="itemStyleClasses",type= BeanTagAttribute.AttributeType.LISTVALUE)
187     public List<String> getItemStyleClasses() {
188         return itemStyleClasses;
189     }
190 
191     /**
192      * Setter for the list of style classes that should apply to each item span
193      *
194      * @param itemStyleClasses
195      */
196     public void setItemStyleClasses(List<String> itemStyleClasses) {
197         this.itemStyleClasses = itemStyleClasses;
198     }
199 
200     /**
201      * Builds the HTML class attribute string by combining the item styleClasses list
202      * with a space delimiter
203      *
204      * @return String class attribute string
205      */
206     public String getItemStyleClassesAsString() {
207         if (itemStyleClasses != null) {
208             return StringUtils.join(itemStyleClasses, " ");
209         }
210 
211         return "";
212     }
213 
214 }