001    /*
002     * Copyright 2011 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.kfs.sys.document.web.renderers;
017    
018    import java.io.IOException;
019    import java.util.List;
020    
021    import javax.servlet.jsp.JspException;
022    import javax.servlet.jsp.JspWriter;
023    import javax.servlet.jsp.PageContext;
024    import javax.servlet.jsp.tagext.Tag;
025    
026    import org.apache.commons.lang.StringUtils;
027    import org.kuali.kfs.sys.KFSConstants;
028    import org.kuali.kfs.sys.document.web.AccountingLineViewAction;
029    import org.kuali.rice.kns.web.taglib.html.KNSImageTag;
030    
031    /**
032     * Renders an action for the accounting line view.
033     */
034    public class ActionsRenderer implements Renderer {
035        private List<AccountingLineViewAction> actions;
036        private KNSImageTag actionButton = new KNSImageTag();
037        private int tabIndex;
038        private String postButtonSpacing;
039        private String tagBeginning;
040        private String tagEnding;
041        
042        /**
043         * Constructs a ActionsRenderer, which sets values on the actionButton tag that never change
044         */
045        public ActionsRenderer() {
046            actionButton.setStyleClass("tinybutton"); // this never changes
047        }
048    
049        /**
050         * 
051         * @see org.kuali.kfs.sys.document.web.renderers.Renderer#clear()
052         */
053        public void clear() {
054            actions = null;
055            
056            resetButton();
057            actionButton.setPageContext(null);
058            actionButton.setParent(null);
059        }
060        
061        /**
062         * Clears out changing values the action button tag
063         */
064        protected void resetButton() {
065            actionButton.setProperty(null);
066            actionButton.setSrc(null);
067            actionButton.setTitle(null);
068            actionButton.setAlt(null);
069            actionButton.setTabindex(null);
070        }
071    
072        /**
073         * 
074         * @see org.kuali.kfs.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)
075         */
076        public void render(PageContext pageContext, Tag parentTag) throws JspException {
077            actionButton.setPageContext(pageContext);
078            actionButton.setParent(parentTag);
079            JspWriter out = pageContext.getOut();
080            
081            try {
082                if (actions != null && actions.size() > 0) {
083                    out.write(this.getTagBeginning());
084                    for (AccountingLineViewAction action : actions) {
085                        renderAction(action);
086                        out.write(this.getPostButtonSpacing());
087                    }
088                    out.write(this.getTagEnding());
089                } 
090                else {
091                    out.write(buildNonBreakingSpace());
092                }
093            }
094            catch (IOException ioe) {
095                throw new JspException("Difficulty rendering actions block", ioe);
096            }
097        }
098        
099        /**
100         * Renders a single action, using the action button
101         * @param action the action to render
102         * @throws JspException thrown if the actionButton cannot uphold its duties to render the 
103         */
104        protected void renderAction(AccountingLineViewAction action) throws JspException {
105            actionButton.setProperty(KFSConstants.DISPATCH_REQUEST_PARAMETER+"."+action.getActionMethod());
106            actionButton.setSrc(action.getImageName());
107            actionButton.setTitle(action.getActionLabel());
108            actionButton.setAlt(action.getActionLabel());
109            if (!StringUtils.isBlank(getTabIndex())) {
110                actionButton.setTabindex(getTabIndex());
111            }
112            actionButton.doStartTag();
113            actionButton.doEndTag();
114            resetButton();
115        }
116        
117        /**
118         * Builds the opening of the centering div tag
119         * @return the opening of the centering div tag in HTML
120         */
121        protected String buildCenteringDivBeginning() {
122            return "<div style=\"text-align: center;\">";
123        }
124        
125        /**
126         * Builds the close of the centering div tag
127         * @return the close of the centering div tag in HTML
128         */
129        protected String buildCenteringDivEnding() {
130            return "</div>";
131        }
132        
133        /**
134         * Builds spacing for after the button is displayed
135         * @return a String of HTML that will space after the button
136         */
137        public String getPostButtonSpacing() {
138            return postButtonSpacing == null ? "<br />" : postButtonSpacing;
139        }
140        
141        /**
142         * Sets the postButtonSpacing attribute value.
143         * @param postButtonSpacing The postButtonSpacing to set.
144         */
145        public void setPostButtonSpacing(String postButtonSpacing) {
146            this.postButtonSpacing = postButtonSpacing;
147        }
148    
149        /**
150         * Gets the action attribute. 
151         * @return Returns the action.
152         */
153        public List<AccountingLineViewAction> getActions() {
154            return actions;
155        }
156    
157        /**
158         * Sets the action attribute value.
159         * @param action The action to set.
160         */
161        public void setActions(List<AccountingLineViewAction> actions) {
162            this.actions = actions;
163        }
164    
165        /**
166         * Sets the tab index for the action
167         * @param tabIndex the tab index to set
168         */
169        public void setTabIndex(int tabIndex) {
170            this.tabIndex = tabIndex;   
171        }
172        
173        /**
174         * Retrieves the set tab index as a String, or, if the tabIndex was never set, returns a null
175         * @return the tab index as a String or null
176         */
177        protected String getTabIndex() {
178            if (tabIndex > -1) return Integer.toString(tabIndex); 
179            return null;
180        }
181        
182        /**
183         * @return the HTML for a non-breaking space, so the box isn't all empty
184         */
185        protected String buildNonBreakingSpace() {
186            return "&nbsp;";
187        }
188    
189        /**
190         * Gets the tagBeginning attribute. 
191         * @return Returns the tagBeginning.
192         */
193        public String getTagBeginning() {
194            return tagBeginning == null ? this.buildCenteringDivBeginning() : tagBeginning;
195        }
196    
197        /**
198         * Sets the tagBeginning attribute value.
199         * @param tagBeginning The tagBeginning to set.
200         */
201        public void setTagBeginning(String tagBeginning) {
202            this.tagBeginning = tagBeginning;
203        }
204    
205        /**
206         * Gets the tagEnding attribute. 
207         * @return Returns the tagEnding.
208         */
209        public String getTagEnding() {
210            return tagEnding == null ? this.buildCenteringDivEnding() : tagEnding;
211        }
212    
213        /**
214         * Sets the tagEnding attribute value.
215         * @param tagEnding The tagEnding to set.
216         */
217        public void setTagEnding(String tagEnding) {
218            this.tagEnding = tagEnding;
219        }
220    }