001    /**
002     * Copyright 2005-2012 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     */
016    package org.kuali.rice.krad.uif.layout;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.krad.datadictionary.parse.BeanTag;
020    import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
021    import org.kuali.rice.krad.uif.CssConstants;
022    import org.kuali.rice.krad.uif.CssConstants.Padding;
023    import org.kuali.rice.krad.uif.UifConstants.Orientation;
024    import org.kuali.rice.krad.uif.container.Container;
025    import org.kuali.rice.krad.uif.view.View;
026    import org.kuali.rice.krad.uif.component.Component;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    /**
032     * Layout manager that organizes components in a single row (horizontal) or
033     * column (vertical)
034     *
035     * <p>
036     * Although a table based template could be used, setup is done to also support
037     * a CSS based template. The items in the <code>Container</code> instance are
038     * rendered sequentially wrapping each one with a span element. The padding
039     * property can be configured to space the elements as needed. To achieve a
040     * vertical orientation, the span style is set to block. Additional styling can
041     * be set for the items by using the itemSpanStyle property.
042     * </p>
043     *
044     * @author Kuali Rice Team (rice.collab@kuali.org)
045     */
046    @BeanTag(name="boxLayoutManager")
047    public class BoxLayoutManager extends LayoutManagerBase {
048            private static final long serialVersionUID = 4467342272983290044L;
049    
050            private Orientation orientation;
051            private String padding;
052    
053            private String itemStyle;
054        private List<String> itemStyleClasses;
055    
056            public BoxLayoutManager() {
057                    super();
058    
059            itemStyle = "";
060                    orientation = Orientation.HORIZONTAL;
061            itemStyleClasses = new ArrayList<String>();
062            }
063    
064            /**
065         * Sets the item span style
066         *
067             * @see org.kuali.rice.krad.uif.layout.LayoutManagerBase#performFinalize(org.kuali.rice.krad.uif.view.View,
068             *      java.lang.Object, org.kuali.rice.krad.uif.container.Container)
069             */
070            @Override
071            public void performFinalize(View view, Object model, Container container) {
072                    super.performFinalize(view, model, container);
073    
074            if (StringUtils.isBlank(itemStyle)) {
075                itemStyle = "";
076            }
077    
078            if (StringUtils.isNotEmpty(padding)) {
079                if (orientation.equals(Orientation.VERTICAL)) {
080                    // set item to block which will cause a line break and margin
081                    // bottom for padding
082                    itemStyle += CssConstants.getCssStyle(Padding.PADDING_BOTTOM, padding);
083                } else {
084                    // set margin right for padding
085                    itemStyle += CssConstants.getCssStyle(Padding.PADDING_RIGHT, padding);
086                }
087            }
088    
089            // classes to identify this layout in jQuery and to clear the float correctly in all browsers
090            this.addStyleClass("clearfix");
091    
092            for (Component c : container.getItems()) {
093                if (c != null) {
094                    // add item styles to the the item
095                    List<String> styleClasses = c.getCssClasses();
096                    if (orientation.equals(Orientation.HORIZONTAL)) {
097                        styleClasses.add("uif-boxLayoutHorizontalItem");
098                        styleClasses.addAll(this.getItemStyleClasses());
099                    } 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    }