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;
17  
18  import java.util.HashSet;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import org.kuali.ole.sys.businessobject.AccountingLine;
24  import org.kuali.ole.sys.document.datadictionary.AccountingLineViewLinesDefinition;
25  import org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation;
26  
27  /**
28   * Represents the rendering for a bunch of elements within the accounting line view
29   */
30  public class AccountingLineViewLines implements TableJoining, ReadOnlyable {
31      private List<AccountingLineViewLineFillingElement> elements;
32      private AccountingLineViewLinesDefinition definition;
33      
34      /**
35       * Gets the definition attribute. 
36       * @return Returns the definition.
37       */
38      public AccountingLineViewLinesDefinition getDefinition() {
39          return definition;
40      }
41      /**
42       * Sets the definition attribute value.
43       * @param definition The definition to set.
44       */
45      public void setDefinition(AccountingLineViewLinesDefinition definition) {
46          this.definition = definition;
47      }
48      
49      /**
50       * @see org.kuali.ole.sys.document.web.AccountingLineViewRenderableElementContainer#getName()
51       */
52      public String getName() {
53          return definition.getElementName();
54      }
55      /**
56       * Gets the elements attribute. 
57       * @return Returns the elements.
58       */
59      public List<AccountingLineViewLineFillingElement> getElements() {
60          return elements;
61      }
62      /**
63       * Sets the elements attribute value.
64       * @param elements The elements to set.
65       */
66      public void setElements(List<AccountingLineViewLineFillingElement> lines) {
67          this.elements = lines;
68      }
69      
70      /**
71       * The interesting implementation...how many does it need?  Let's see here...one for each child row...
72       * yes...that's right, one table row for each child row
73       * @see org.kuali.ole.sys.document.web.AccountingLineViewRenderableElement#getRequestedRowCount()
74       */
75      public int getRequestedRowCount() {
76          int sum = 0;
77          for (AccountingLineViewLineFillingElement line : elements) {
78              sum += line.getRequestedRowCount();
79          }
80          return sum;
81      }
82      
83      /**
84       * Throws an exception - lines should never be asked to join rows
85       * @see org.kuali.ole.sys.document.web.TableJoining#joinRow(org.kuali.ole.sys.document.web.AccountingLineTableRow)
86       */
87      public void joinRow(AccountingLineTableRow headerRow, AccountingLineTableRow row) {
88          throw new IllegalStateException("Error in line rendering algorithm - lines cannot join a single row.");
89      }
90      
91      /**
92       * Attempts to have each child line join the rows that have been given
93       * @see org.kuali.ole.sys.document.web.TableJoining#joinTable(java.util.List)
94       */
95      public void joinTable(List<AccountingLineTableRow> rows) {
96          final int maxExpectedLineWidth = getMaxExpectedLineWidth();
97          
98          int count = 0;
99          for (AccountingLineViewLineFillingElement line : elements) {
100             AccountingLineTableRow headerRow = rows.get(count);
101             
102             if (line.getRequestedRowCount() > 1) {
103                 line.joinRow(headerRow, rows.get(count+1));
104                 padOutOrStretchCells(line, maxExpectedLineWidth, headerRow, rows.get(count+1));
105                 
106                 count += 2;
107             } else {
108                 line.joinRow(headerRow, null);
109                 padOutOrStretchCells(line, maxExpectedLineWidth, headerRow, null);
110                 
111                 count += 1;
112             }
113         }
114     }
115     
116     /**
117      * Either pads out out the given table rows with an empty cell or stretches the cell to fill the whole line 
118      * @param line the line joining the table
119      * @param maxExpectedLineWidth the expected width, in cell count, of the line
120      * @param headerRow the first row to add padding out to
121      * @param row the second row to add padding out to - if we're only filling one row, this will be null
122      */
123     protected void padOutOrStretchCells(AccountingLineViewLineFillingElement line, int maxExpectedLineWidth, AccountingLineTableRow headerRow, AccountingLineTableRow row) {
124         final int shorterThanMax = maxExpectedLineWidth - line.getDisplayingFieldWidth();
125         if (shorterThanMax > 0) {
126             if (line.shouldStretchToFillLine() && headerRow.getChildCellCount() == 1) {
127                 headerRow.getCells().get(0).setColSpan(maxExpectedLineWidth);
128                 if (row != null) {
129                     row.getCells().get(0).setColSpan(maxExpectedLineWidth);
130                 }
131             } else {
132                 PlaceHoldingLayoutElement placeHolder = new PlaceHoldingLayoutElement(shorterThanMax);
133                 placeHolder.joinRow(headerRow, row);
134             }
135         }
136     }
137     
138     /**
139      * @see org.kuali.ole.sys.document.web.ReadOnlyable#readOnlyize()
140      */
141     public void readOnlyize() {
142         for (AccountingLineViewLineFillingElement line : elements) {
143             line.readOnlyize();
144         }
145     }
146     
147     /**
148      * @see org.kuali.ole.sys.document.web.ReadOnlyable#isReadOnly()
149      */
150     public boolean isReadOnly() {
151         for (AccountingLineViewLineFillingElement line : elements) {
152             if (!line.isReadOnly()) {
153                 return false;
154             }
155         }
156         return true;
157     }
158     
159     /**
160      * @see org.kuali.ole.sys.document.web.TableJoining#removeAllActionBlocks()
161      */
162     public void removeAllActionBlocks() {
163         for (AccountingLineViewLineFillingElement line : elements) {
164             line.removeAllActionBlocks();
165         }
166     }
167     
168     /**
169      * @see org.kuali.ole.sys.document.web.TableJoining#removeUnviewableBlocks(java.util.Set)
170      */
171     public void removeUnviewableBlocks(Set<String> unviewableBlocks) {
172         Set<AccountingLineViewLineFillingElement> linesToRemove = new HashSet<AccountingLineViewLineFillingElement>();
173         for (AccountingLineViewLineFillingElement line : elements) {
174             if (unviewableBlocks.contains(line.getName())) {
175                 linesToRemove.add(line);
176             } else {
177                 line.removeUnviewableBlocks(unviewableBlocks);
178             }
179         }
180         elements.removeAll(linesToRemove);
181     }
182     
183     /**
184      * @see org.kuali.ole.sys.document.web.TableJoining#performFieldTransformation(org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation, org.kuali.ole.sys.businessobject.AccountingLine, java.util.Map, java.util.Map)
185      */
186     public void performFieldTransformations(List<AccountingLineFieldRenderingTransformation> fieldTransformations, AccountingLine accountingLine, Map unconvertedValues) {
187         for (AccountingLineViewLineFillingElement line : elements) {
188             line.performFieldTransformations(fieldTransformations, accountingLine, unconvertedValues);
189         }
190     }
191     
192     /**
193      * @return the maximum expected width of any of the child line elements in cells
194      */
195     public int getMaxExpectedLineWidth() {
196         int maxWidth = 0;
197         for (AccountingLineViewLineFillingElement line: elements) {
198             int width = line.getDisplayingFieldWidth();
199             if (width > maxWidth) {
200                 maxWidth = width;
201             }
202         }
203         return maxWidth;
204     }
205     
206     /**
207      * Shuffles the responsibility to the child lines
208      * @see org.kuali.ole.sys.document.web.TableJoining#readOnlyizeReadOnlyBlocks(java.util.Set)
209      */
210     public void readOnlyizeReadOnlyBlocks(Set<String> readOnlyBlocks) {
211         for (AccountingLineViewLineFillingElement line : elements) {
212             line.readOnlyizeReadOnlyBlocks(readOnlyBlocks);
213         }
214     }
215     
216     /**
217      * @see org.kuali.ole.sys.document.web.TableJoining#setEditableBlocks(java.util.Set)
218      */
219     public void setEditableBlocks(Set<String> editableBlocks) {
220         for (AccountingLineViewLineFillingElement line : elements) {
221             line.setEditableBlocks(editableBlocks);
222         }
223     }
224     
225     /**
226      * @see org.kuali.ole.sys.document.web.ReadOnlyable#setEditable()
227      */
228     public void setEditable() {
229         for (AccountingLineViewLineFillingElement line : elements) {
230             line.setEditable();
231         }       
232     }
233 }