View Javadoc

1   /**
2    * Copyright 2005-2013 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.datadictionary.parse.BeanTags;
22  import org.kuali.rice.krad.uif.CssConstants;
23  import org.kuali.rice.krad.uif.CssConstants.Padding;
24  import org.kuali.rice.krad.uif.UifConstants.Orientation;
25  import org.kuali.rice.krad.uif.component.Component;
26  import org.kuali.rice.krad.uif.container.Container;
27  import org.kuali.rice.krad.uif.view.View;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  /**
33   * Layout manager that organizes components in a single row (horizontal) or
34   * column (vertical)
35   *
36   * <p>
37   * Although a table based template could be used, setup is done to also support
38   * a CSS based template. The items in the <code>Container</code> instance are
39   * rendered sequentially wrapping each one with a span element. The padding
40   * property can be configured to space the elements as needed. To achieve a
41   * vertical orientation, the span style is set to block. Additional styling can
42   * be set for the items by using the itemSpanStyle property.
43   * </p>
44   *
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   */
47  @BeanTags({@BeanTag(name = "boxLayout-bean", parent = "Uif-BoxLayoutBase"),
48          @BeanTag(name = "horizontalBoxLayout-bean", parent = "Uif-HorizontalBoxLayout"),
49          @BeanTag(name = "verticalBoxLayout-bean", parent = "Uif-VerticalBoxLayout")})
50  public class BoxLayoutManager extends LayoutManagerBase {
51      private static final long serialVersionUID = 4467342272983290044L;
52  
53      private Orientation orientation;
54      private String padding;
55  
56      private String itemStyle;
57      private List<String> itemStyleClasses;
58  
59      public BoxLayoutManager() {
60          super();
61  
62          itemStyle = "";
63          orientation = Orientation.HORIZONTAL;
64          itemStyleClasses = new ArrayList<String>();
65      }
66  
67      /**
68       * Sets the item span style
69       *
70       * @see org.kuali.rice.krad.uif.layout.LayoutManagerBase#performFinalize(org.kuali.rice.krad.uif.view.View,
71       *      java.lang.Object, org.kuali.rice.krad.uif.container.Container)
72       */
73      @Override
74      public void performFinalize(View view, Object model, Container container) {
75          super.performFinalize(view, model, container);
76  
77          if (StringUtils.isBlank(itemStyle)) {
78              itemStyle = "";
79          }
80  
81          if (StringUtils.isNotEmpty(padding)) {
82              if (orientation.equals(Orientation.VERTICAL)) {
83                  // set item to block which will cause a line break and margin
84                  // bottom for padding
85                  itemStyle += CssConstants.getCssStyle(Padding.PADDING_BOTTOM, padding);
86              } else {
87                  // set margin right for padding
88                  itemStyle += CssConstants.getCssStyle(Padding.PADDING_RIGHT, padding);
89              }
90          }
91  
92          // classes to identify this layout in jQuery and to clear the float correctly in all browsers
93          this.addStyleClass("clearfix");
94  
95          for (Component c : container.getItems()) {
96              if (c != null) {
97                  // add item styles to the the item
98                  List<String> styleClasses = c.getCssClasses();
99                  if (styleClasses == null) {
100                     styleClasses = new ArrayList<String>();
101                 }
102 
103                 if (orientation.equals(Orientation.HORIZONTAL)) {
104                     styleClasses.add("uif-boxLayoutHorizontalItem");
105                     styleClasses.addAll(this.getItemStyleClasses());
106                 } else {
107                     styleClasses.add("uif-boxLayoutVerticalItem");
108                     styleClasses.addAll(this.getItemStyleClasses());
109                     styleClasses.add("clearfix");
110                 }
111                 c.setCssClasses(styleClasses);
112 
113                 if (c.getStyle() != null && !c.getStyle().endsWith(";")) {
114                     c.appendToStyle(";" + this.getItemStyle());
115                 } else {
116                     c.appendToStyle(this.getItemStyle());
117                 }
118             }
119         }
120     }
121 
122     /**
123      * Indicates whether the components should be rendered in a horizontal or
124      * vertical column
125      *
126      * @return orientation configured for layout
127      */
128     @BeanTagAttribute(name = "orientation", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
129     public Orientation getOrientation() {
130         return this.orientation;
131     }
132 
133     /**
134      * Setter for the orientation for layout
135      *
136      * @param orientation
137      */
138     public void setOrientation(Orientation orientation) {
139         this.orientation = orientation;
140     }
141 
142     /**
143      * Amount of separation between each item
144      *
145      * <p>
146      * For horizontal orientation, this will be the right padding for each item.
147      * For vertical, it will be the bottom padding for each item. The value can
148      * be a fixed length (like px) or percentage
149      * </p>
150      *
151      * @return String
152      */
153     @BeanTagAttribute(name = "padding")
154     public String getPadding() {
155         return this.padding;
156     }
157 
158     /**
159      * Setter for the item padding
160      *
161      * @param padding
162      */
163     public void setPadding(String padding) {
164         this.padding = padding;
165     }
166 
167     /**
168      * Used by the render to set the style on the span element that wraps the
169      * item. By using a wrapping span the items can be aligned based on the
170      * orientation and given the correct padding
171      *
172      * @return css style string
173      */
174     @BeanTagAttribute(name = "itemStyle")
175     public String getItemStyle() {
176         return this.itemStyle;
177     }
178 
179     /**
180      * Setter for the span style
181      *
182      * @param itemStyle
183      */
184     public void setItemStyle(String itemStyle) {
185         this.itemStyle = itemStyle;
186     }
187 
188     /**
189      * List of style classes that should be applied to each span that wraps the item in the layout
190      *
191      * @return List<String>
192      */
193     @BeanTagAttribute(name = "itemStyleClasses", type = BeanTagAttribute.AttributeType.LISTVALUE)
194     public List<String> getItemStyleClasses() {
195         return itemStyleClasses;
196     }
197 
198     /**
199      * Setter for the list of style classes that should apply to each item span
200      *
201      * @param itemStyleClasses
202      */
203     public void setItemStyleClasses(List<String> itemStyleClasses) {
204         this.itemStyleClasses = itemStyleClasses;
205     }
206 
207     /**
208      * Builds the HTML class attribute string by combining the item styleClasses list
209      * with a space delimiter
210      *
211      * @return class attribute string
212      */
213     public String getItemStyleClassesAsString() {
214         if (itemStyleClasses != null) {
215             return StringUtils.join(itemStyleClasses, " ");
216         }
217 
218         return "";
219     }
220 
221     /**
222      * @see org.kuali.rice.krad.uif.layout.LayoutManagerBase#copy()
223      */
224     @Override
225     protected <T> void copyProperties(T layout) {
226         super.copyProperties(layout);
227         BoxLayoutManager boxLayoutManagerCopy = (BoxLayoutManager) layout;
228         boxLayoutManagerCopy.setPadding(this.padding);
229         boxLayoutManagerCopy.setItemStyle(this.itemStyle);
230         boxLayoutManagerCopy.setOrientation(this.orientation);
231 
232         if (itemStyleClasses != null) {
233             boxLayoutManagerCopy.setItemStyleClasses(new ArrayList<String>(itemStyleClasses));
234         }
235     }
236 }