View Javadoc
1   /*
2    * Copyright 2008 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.ole.sys.document.web.renderers;
17  
18  import java.io.IOException;
19  import java.util.List;
20  
21  import javax.servlet.jsp.JspException;
22  import javax.servlet.jsp.JspWriter;
23  import javax.servlet.jsp.PageContext;
24  import javax.servlet.jsp.tagext.Tag;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.ole.sys.OLEConstants;
28  import org.kuali.ole.sys.document.web.AccountingLineViewAction;
29  import org.kuali.rice.kns.web.taglib.html.KNSImageTag;
30  
31  /**
32   * Renders an action for the accounting line view.
33   */
34  public class ActionsRenderer implements Renderer {
35      private List<AccountingLineViewAction> actions;
36      private KNSImageTag actionButton = new KNSImageTag();
37      private int tabIndex;
38      private String postButtonSpacing;
39      private String tagBeginning;
40      private String tagEnding;
41      
42      /**
43       * Constructs a ActionsRenderer, which sets values on the actionButton tag that never change
44       */
45      public ActionsRenderer() {
46          actionButton.setStyleClass("tinybutton"); // this never changes
47      }
48  
49      /**
50       * 
51       * @see org.kuali.ole.sys.document.web.renderers.Renderer#clear()
52       */
53      public void clear() {
54          actions = null;
55          
56          resetButton();
57          actionButton.setPageContext(null);
58          actionButton.setParent(null);
59      }
60      
61      /**
62       * Clears out changing values the action button tag
63       */
64      protected void resetButton() {
65          actionButton.setProperty(null);
66          actionButton.setSrc(null);
67          actionButton.setTitle(null);
68          actionButton.setAlt(null);
69          actionButton.setTabindex(null);
70      }
71  
72      /**
73       * 
74       * @see org.kuali.ole.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)
75       */
76      public void render(PageContext pageContext, Tag parentTag) throws JspException {
77          actionButton.setPageContext(pageContext);
78          actionButton.setParent(parentTag);
79          JspWriter out = pageContext.getOut();
80          
81          try {
82              if (actions != null && actions.size() > 0) {
83                  out.write(this.getTagBeginning());
84                  for (AccountingLineViewAction action : actions) {
85                      renderAction(action);
86                      out.write(this.getPostButtonSpacing());
87                  }
88                  out.write(this.getTagEnding());
89              } 
90              else {
91                  out.write(buildNonBreakingSpace());
92              }
93          }
94          catch (IOException ioe) {
95              throw new JspException("Difficulty rendering actions block", ioe);
96          }
97      }
98      
99      /**
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(OLEConstants.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 }